Declaring variables in C language

C programming language allows to define and classify data so we can use it appropriately in various parts of the program in the correct form with or without changing the original value or just copying the original value. Any data that we need to use in the program need to be stored in the memory.

Variable is the name that we give to that particular temporary location in memory where we can store data. Each data or value need to classified as of a particular type eg: integer or decimal or string etc. This is accomplished by the data type.

The main data types used in C are integer, floating-point and character data types. Below are the data types used in C programs –

char Smallest addressable unit of the machine that can contain CHAR_BIT bits.
signed char Of the same size as char, but signed. [−127, +127] range
unsigned char Of the same size as char, but unsigned. [0, 255] range.
short

short int

signed short

signed short int

Short signed integer type. [−32,767, +32,767] range; 16 bits in size.
unsigned short

unsigned short int

Short unsigned integer type. [0, 65,535] range;
int

signed

signed int

Basic signed integer type. [−32,767, +32,767] range, 16 bits in size.
unsigned

unsigned int

Basic unsigned integer type. [0, 65,535] range.
long

long int

signed long

signed long int

Long signed integer type.  [−2,147,483,647, +2,147,483,647] range. 32 bits in size.
unsigned long

unsigned long int

Long unsigned integer type.  [0, 4,294,967,295] range.
long long

long long int

signed long long

signed long long int

Long long signed integer type.  [−9,223,372,036,854,775,807, +9,223,372,036,854,775,807] range.
unsigned long long unsigned long long int Long long unsigned integer type. [0, +18,446,744,073,709,551,615] range.
float referred to as a single-precision floating-point type.
double referred to as a double-precision floating-point type.
long double Real floating-point type. 80 bits, but typically 96 bits or 128 bits in memory

An example involving variables and data types showing mixed-mode calculation –

main () {

int a = 4, answer;
float b = 4.1, result;

answer = a * b;
result = a * b;

printf("%d * %f = %d \n", a, b, answer);
printf("%d * %f = %f \n", a, b, result);
printf("%d * %f = %e \n", a, b, result);
printf("%f in scientific form is %e \n", result, result);
}

 

All data types are keywords and need to be typed in lowercase. The variable name should begin with the alphabet, it shouldn’t have space, period etc, could have an underscore (_) and it should be a keyword. Most compilers will consider two variables as the same if the first 8 chars are the same eg: variable 1 and variable 2. Also, variables are case sensitive.

C language support coercion and cast operators. Below is an example usage of this –

int a = 2, b = 3;
float c = 4.24;

d = (float) a * b + (int) c; // d = 10.000003

The main arithmetic operators used in C are +, -, /, *. ‘%’ is the modulus operator. ‘=’ is the assignment operator. ‘==’, ‘!=’, compares values