Variables in C Programming
Language
Datatype, Variable Declaration, Variable Definition
Variable Concept
• Data container or Location of Main Memory where data can be stored
• Its value can be changed during execution.
• Each Variable has a name, datatype, size and value.
• Variable name is defined based on Identifier rules
• Variable Datatype can be set from the built-in data types
Primary Data Types
• ANSI C supports different classes of data types Datatypes
• Primitive data types can be directly used to Primitive
Derived DT
DT
declare the variable
• Integer – Keyword int used for integer data types int Arrays
• Character – Keyword char used for character data float Pointers
types
char Structures
• Floating Point – Keyword float used for single
precision floating point numbers double
• Double Floating Point – Keyword double used for
double precision floating point numbers void
Variable Syntax
• Creating a Single Variable or multiple variables (Variable Declaration)
• DATATYPE VARIABLE_NAME;
• DATATYPE VARIABLE1, VARIABLE2, VARIABLE3, …;
• int var;
• int var1,var2,var3;
• Creating a variable and initializing a value to it(Variable Initialization)
• DATATYPE VARIABLE_NAME = VALUE;
• int var1 = 100;
Assigning Value to Variable
We can assign value to already created variable in source code or via
input console at runtime
1. Assigning value via source code
variable_name = value;
2. Assigning value via input console can be achieved by scanf() function
Data Type Format Specifier
int %d
float %f
char %c
string %s
double %lf
Printing Value of Variable to Output Console
• Value of a variable can printed to output console by printf() function
printf("%d",var1);
Format Specifier
Variable Name