- Introduction to C
- Introduction to C
- Program Structure
- Basic Syntax
- Data Types
- Storage Classes
- If statements
- Case control statements
- File I/O
- Type Casting
- Command line arguments
- Header Files
- Dynamic memory allocation
- Error Handling
C - Data Types
Data types in C
- Fundamental Data Types
- Integer types
- Floating Type
- Character types
- Derived Data Types
In the C programming language, data type refers to an broad system used for declaring different different type of variables or functions. The type of a variable determines how much storage space a variable occupies in memory and how the bit pattern stored is interpreted.
The data types in C can be classified as follows:
|S.N.||Types and Description|
They are of arithmetic types and consists of the two types: (a) integer types and (b) floating-point types.
They are also arithmetic types and they are used to define variables that can only be assigned certain discrete integer values throughout the program.
|3||The type void:|
The type void indicates that no value is available.
They include (a) Pointer types, (b) Array types, (c) Structure types, (d) Union types and (e) Function types.
The array types and structure types are referred to collectively as aggregate types. The function type specifies the return type of the function. We will see basic types in the current section, whereas, other types will be covered in the upcoming chapters.
Table below gives you details about standard integer types with its storage sizes and value ranges:
|Type||Storage size||Value range|
|char||1 byte||-128 to 127 or 0 to 255|
|unsigned char||1 byte||0 to 255|
|signed char||1 byte||-128 to 127|
|int||2 or 4 bytes||-32,768 to 32,767 or -2,147,483,648 to 2,147,483,647|
|unsigned int||2 or 4 bytes||0 to 65,535 or 0 to 4,294,967,295|
|short||2 bytes||-32,768 to 32,767|
|unsigned short||2 bytes||0 to 65,535|
|long||4 bytes||-2,147,483,648 to 2,147,483,647|
|unsigned long||4 bytes||0 to 4,294,967,295|
To get the exact size of a type or variable, you can use the sizeof operator. The expressions sizeof(type) gives the storage size of the object or type in bytes. Given the example to get the size of int type:
printf("Integer storage size : %d \n", sizeof(int));
Given table gives you details about standard floating-point types with storage sizes and value ranges and their precision:
|Type||Storage size||Value range||Precision|
|float||4 byte||1.2E-38 to 3.4E+38||6 decimal places|
|double||8 byte||2.3E-308 to 1.7E+308||15 decimal places|
|long double||10 byte||3.4E-4932 to 1.1E+4932||19 decimal places|
The header file float.h defines macros that allow you to use these values and other details about the binary representation of real numbers in your programs. Given example will print storage space taken by a float type and its range values:
printf("Minimum float positive value: %E\n", FLT_MIN );
printf("Maximum float positive value: %E\n", FLT_MAX );
printf("Float storage size : %d \n", sizeof(float));
printf("Precision value: %d\n", FLT_DIG );
char keyword is used for declaring a character type variable. Given example:
Here we declare a variable of type character (i.e. var4) which is storing a character 'h'.
The size of char is 1 byte. The character data type consists of ASCII values. Each character is given a specific ASCII value. For example:
For, 'b', value=98
For, 'A', value=65
For, '&', value=33
For, '2', value=49
ASCII Character Codes
How to use ASCII character code table
Suppose you want to find the ASCII code of capital A then, watch the corresponding bold number in left, i.e, 6 in this case and the bold number on the top of it, i.e, 5. Then, ASCII value of 'A' equals to 65
It alters the meaning of base data types to yield a new data type.
Size qualifiers alters the size of basic data type. The keywords short and long are two size qualifiers. Given example:
The size of int is 2 bytes or 4 bytes but, when using long keyword, that variable will be either 4 bytes of 8 bytes. Short keyword can be used in similar manner as long keyword.
Whether a variable can hold only positive value or both values this is specified by sign qualifiers. signed and unsigned keywords are used for sign qualifiers.
// unsigned variable can only holds zero or positive values
It is not necessary to define variable with keyword signed because, It is signed by default. Sign qualifiers can be applied to int and char data types only. A int variable of size 4 bytes it can hold data from -231 to 231-1 but, if that variable is defined unsigned, it can hold data from 0 to 232 -1.
Constant qualifiers can be declared with const keyword. An object which is declared by const keyword cannot be modified
The value of variable p cannot be changed in the program.
Variable should be declared volatile whenever it's value can be changed by some external sources outside program.For indicating volatile variable keyword volatile is used.
The void Type
The void type specifies that no value is available. void is used in three situations:
|S.N.||Types and Description|
|1||Function returns as void|
There are various functions in C which do not return value or you can say they return void. Function with no return value or a void return value has the return type as void. For example void exit (int status);
|2||Function arguments as void|
There are various functions in C which do not accept any parameter. Function with no parameter can accept as a void. Example, int rand(void);
|3||Pointers to void |
A pointer of type void * represents the address of an object only not it's type. For example a memory allocation function void *malloc( size_t size ); returns a pointer to void which can be casted to any data type.