Variables and Data types in C programming language
Variable
A variable is basically a container for storing data. Every variable has a unique name known as its identifier, which can be anything from a single letter to a word. Besides being unique, an identifier has to be valid as follows:
- must begin with an alphabetic letter or the underscore "_" character. For example,
initVelocity
,_latentHeat
anda_person
are valid identifiers. - cannot begin with a digit but a digit can be used in any other part of the identifier. For example
9patch
is invalid butp9atch
andpatch9
are valid.
Identifiers in C are case sensitive which means that, for example, gameWorld
, GameWorld
and gameworld
are different identifiers.
Variable types in C
In C programming, variables do not only have names, they also have types. The type of a variable tells the compiler what type of data will be stored in the variable. This allows the compiler to determine the amount of memory that will be given to a variable when the compiled program is run.
There are a lot of different types in C and C also gives us the ability to define our own types. Some basic data types in C are listed below:
Type | Description |
---|---|
char | stores a single ASCII character |
short | stores a short integer (usually 16-bits) |
short int | stores short integer |
int | stores a standard integer (usually 32-bits) |
long | stores a long integer |
long int | stores a long integer (usually 32-bits, but increasingly 64 bits) |
float | stores a floating point or real number (short) |
long float | stores a long floating point number |
double | stores a long floating point number |
Variable declaration and initialisation in C
In C, variables are declared by the following syntax:
typename variablename;
For example:
#include <stdio.h>
int main(){
// declare interger variables
int age;
int shoeSize;
// declare character variables
char grade;
char gender;
// declare double variables
double bankBalance;
return 0;
}
Variable initialisation is the process of giving a value to a declared variable. For example:
#include <stdio.h>
int main(){
// declare interger variables
int age;
int shoeSize;
// declare character variables
char grade;
char gender;
// declare double variables
double bankBalance;
// initialising the variables
age = 33;
shoeSize = 10;
grade = 'A';
gender = 'M';
bankBalance = 19.87;
return 0;
}
Multiple variable declarations in C
Multiple variables can be declared in a single line as follows:
#include <stdio.h>
int main(){
//declaring variables seperately
int age;
int shoeSize;
int numberOfStudents;
//declaring multiple variables in a single line
int cars, buses, numberOfPassengers;
return 0;
}
Variable declaration and initialisation in a single line
C allows us to neatly declare and initialise variables in a single line as follows:
#include <stdio.h>
int main(){
// declare and initialise variables in a single line
int age = 33;
int shoeSize = 10;
char grade = 'A';
char gender = 'M';
char bankBalance = 19.87;
return 0;
}
This is no more efficient than doing it in two stages, but it shortens the code.
char
type in C
The char
type (character type) is a variable type which can store a single ASCII character. In C, a single character is written enclosed by single quotes, e.g. char level = 'B';
.
A character is any ASCII character, printable or not printable from values -128 to 127. This includes control characters which are non printable characters but can be used to format the output. Control characters are used in programs with a preceding backslash "".
Here are some of the control characters and their meanings:
character | meaning |
---|---|
\b | backspace BS |
\f | form feed FF (also clears screen) |
\n | new line NL |
\r | carriage return CR (puts cursor to start of line) |
\t | horizontal tab HT |
\v | vertical tab |
Example:
#include <stdio.h>
int main (){
printf ("First line Second line\n");
printf ("First line \nSecond line\n");
printf ("First line \rSecond line\n");
printf ("First line \tSecond line");
}
/**** Output ***
First line Second line
First line
Second line
Second line
First line Second line
*/
int
type in C
There are different types of integers in C:
- int
- long
- long long
- short
They differ in the size of the integer which each can hold and the amount of memory allowed for each. The sizes of these int
types also depend on the operating system of the computer.
On a typical 32 bit microcomputer the size of these integers is as follows:
Type | Bits allowed | Possible value range |
---|---|---|
short | 16 | -32768 to 32767 |
unsigned short | 16 | 0 to 65535 |
int | 32 | -2147483648 to 2147483647 |
long | 32 | -2147483648 to 2147483647 |
unsigned int | 32 | 0 to 4294967295 |
long long | 64 | -9×1018 to + 8×1018 |
On your machine you can find the size of each respective int as follows:
#include <stdio.h>
int main(){
printf("Storage size of short : %d bytes\n", sizeof(short));
printf("Storage size of unsigned short : %d bytes \n", sizeof(unsigned short ));
printf("Storage size of long : %d bytes \n", sizeof(long));
printf("Storage size of long long : %d bytes\n", sizeof(long long));
printf("Storage size of int : %d bytes\n", sizeof(int));
printf("Storage size of unsigned int : %d bytes \n", sizeof(unsigned int));
return 0;
}
And the typical output will be:
Storage size of short : 2 bytes
Storage size of unsigned short : 2 bytes
Storage size of long : 4 bytes
Storage size of long long : 8 bytes
Storage size of int : 4 bytes
Storage size of unsigned int : 4 bytes
float
type in C
float
type (floating point) stores inexact representations of real numbers, both integer and non-integer (numbers with a decimal point) values. float
literals must be suffixed with F or f.
For example:
#include <stdio.h>
int main(){
float bankBalance = 2.10f;
float avogadroNumber = 6.022e+23f;
printf("My bank balance is %f", bankBalance);
printf("\nThe avogadro cosntant is %f", avogadroNumber);
return 0;
}
Expect output is:
My bank balance is 2.100000
The avogadro cosntant is 602200013124147498450944.000000
From the example above we can see that 6.022e+23 (which means 6.022 × 1023 or 602200000000000000000000) is actually represented in the output as 602200013124147498450944.000000. The reason is that floating-point numbers are not exact.
It is basically very large and very small numbers that will have noticeably less precision.
You can view the size of float
type on your machine as follows:
#include <stdio.h>
#include <float.h>
int main(){
printf("Storage size of float : %d \n", sizeof(float));
printf("Minimum float positive value: %E\n", FLT_MIN );
printf("Maximum float positive value: %E\n", FLT_MAX );
printf("Precision value: %d\n", FLT_DIG );
return 0;
}
The typical output will be:
Storage size for float : 4
Minimum float positive value: 1.175494E-38
Maximum float positive value: 3.402823E+38
Precision value: 6
double
type in C
The double
and float
types are similar except only for the fact that the
float
type stores single precision floating point numbers- while the
double
type stores double precision floating point numbers
A float
is only one machine word (4 bytes typically) in size whilst the size of a double
is typically two machine words (8 bytes typically). Therefore, float
is used when less precision than a double
provides is required.
For example:
#include <stdio.h>
int main(){
float av1 = 6.022e+23f;
double av2 = 6.022e+23;
printf("av1 = %f", av1);
printf("\n whilst\n");
printf("av2 = %f", av2);
return 0;
}
Typical output is:
av1 = 602200013124147498450944.000000
whilst
av2 = 602200000000000027262976.000000
The output shows that double
is more precise than float
.
Why float
if double
is more precise?
#include <stdio.h>
int main(){
printf("Size of float is %d bytes", sizeof(float));
printf("\nSize of double is %d bytes", sizeof(double));
return 0;
}
Typical output is:
Size of float is 4 bytes
Size of double is 8 bytes
In the early days of C, computers had very low memories and working space for the program was minimum. So the use float
instead of a double
saved some memory. However, nowadays, memory is no longer a problem it is now much better to use doubles consistently.