A variable is a name given to a memory location inside our computer where we can store data.
Variable declaration
int age;
In C programming, a variable declaration is a statement that informs the compiler about the name and type of a variable without allocating any memory for it. It essentially tells the compiler that a variable with a specific name and data type will be used in the program. Variable declarations are necessary before you can use a variable in your program.
Variable Initialisation
age = 22
Variable initialisation in C refers to the process of assigning an initial value to a variable when it is declared.
We can write variable declaration and initialisation in single line: int age = 22;
Changing values in variable
#include <stdio.h>
int main(){
int age = 25;
printf("Age: %d", age);
age = 31;
printf("\nNew AGE: %d", age);
return 0;
}
%d
is format specifier. It is a special character sequence used in the printf and scanf functions (and related functions) to specify the type and format of the data that will be input or output.\n
is used for new line.
Assigning one variable to another variable
#include <stdio.h>
int main(){
int firstNumber = 33;
printf("firstNumber = %d", firstNumber);
int secondNumber = firstNumber;
printf("\nsecondNumber = %d", secondNumber);
return 0;
}
Declaring two variable in a single line
#include <stdio.h>
int main(){
int variable1, variable2=25;
return 0;
}
Variable naming convention
- Use camel case variable like
myAge
,firstName
,lightSpeed
. Cannot:- Create variable names with space in between.
int first number
- Start variable names with numbers.
int 1number
- Use keywords as variables.
int if
- Create variable names with space in between.