Variables in C
Variables in C
Outline:
# Variable Declaration in C
# Variable Declaration and Initialization
# Variable Assignment
# There are some rules on choosing variable names
# C Program to Print Value of a Variable
Variable Declaration in C
Syntax:
type variable_name;
or
type variable_name, variable_name, variable_name;
Variable Assignment
void main()
{
/* c program to print value of a variable */
int age = 33;
printf("I am %d years old.\n", age);
}
Program Output:
I am 33 years old.
C Type Casting
Type Casting in C is used to convert a variable from one data type to another data type,
and after type casting compiler treats the variable as of the new data type.
Syntax:
(type_name) expression
Outline:
It's best practice to convert lower data type to higher data type to avoid data loss.
C Variable Scope
A scope is a region of the program, and the scope of variables refers to the area of the
program where the variables can be accessed after its declaration.
In C every variable defined in scope. You can define scope as the section or region of a
program where a variable has its existence; moreover, that variable cannot be used or
accessed beyond that region.
In C programming, variable declared within a function is different from a variable
declared outside of a function. The variable can be declared in three places.
These are:
Position Type
Outline:
# Local Variables
# Local Scope or Block Scope
# Global Variables
# Global Scope
# Global Variable Initialization
Local Variables
Variables that are declared within the function block and can be used only within the
function is called local variables.
int main ()
{
/* local variable definition and initialization */ int x,y,z;
return 0;
}
Global Variables
Variables that are declared outside of a function block and can be accessed inside the
function is called global variables.
Global Scope
Global variables are defined outside a function or any specific block, in most of the
case, on the top of the C program. These variables hold their values all through the end
of the program and are accessible within any of the functions defined in your program.
Any function can access variables defined within the global scope, i.e., its availability
stays for the entire program after being declared.
Example:
#include <stdio.h>
int main ()
{
/* local variable definition and initialization */ int x,y;
return 0;
}
int 0
char '\0'
float 0
double 0
pointer NULL