Lab #9
Declaration of Variable
Declaration of variable in c can be done using following syntax:
data_type variable_name;
or
data_type variable1, variable2,…,variablen;
where data_type is any valid c data type and variable_name is any
valid identifier.
For example,
int a;
float variable;
float a, b;
Initialization of Variable
C variables declared can be initialized with the help of assignment
operator ‘=’.
Syntax
data_type variable_name=constant/literal/expression;
or
variable_name=constant/literal/expression;
Example:
int a=10;
int a=b+c;
a=10;
a=b+c;
Multiple variables can be initialized in a single statement by single
value, for example, a=b=c=d=e=10;
Primary data types
Integer Types
short
int
long
Floating-point Types
float
double
Character Types
char
Datatypes of C language
Char: The most basic data type in C. It stores a single character
and requires a single byte of memory in almost all compilers.
Int: As the name suggests, an int variable is used to store an integer.
Integer data type allows a variable to store numeric values. The storage
size of int data type is 2 or 4 or 8 byte.
Float: It is used to store decimal numbers (numbers with floating
point value) with single precision. Storage size of float data type is
4byte. This also varies depend upon the processor in the CPU as “int”
data type.
Double: It is used to store decimal numbers (numbers with floating
point value) with double precision. Double data type is also same as
float data type which allows up-to 10 digits after decimal.
Long: Long is a data type used in programming languages, such
as Java, C++, and C#. A constant or variable defined as long can store a
single 64-bit signed integer.
types Storage Range example Format
or size specifier
Char 1 byte -128 to 127 ‘A’,’B’ %c
Integer 4 bytes - 2,3,10,15,10 %d
2147483648
to
2147483677
Float 4 bytes -3.4*10-38 2.0, 5.0, 1.0 %f
to 3.4*1038
Double 8 bytes -1.7*10308 1000000, %lf
to 200000
1.7*10308
Short 2 bytes -32768 to -2, 10,-3 %o
32767
Bool 1 byte T\F O, 1 No
specifier
Long 8 bytes -9.2*1017 to 12.4312 %ld
9.2*1017