Data Types in C Language
Why Data Types are used in C language?
Suppose you are writing a program in C language and you need to store an integer value for your program, but how will C know that the value you are going to store for your program is an integer value. So, data types are used. Now if you are going to store an integer value you will declare a “int” as a data type. Now look at the example given below.
Now from the above instruction you will see that a variable ‘i’ is declared and is given ‘int’ as a data type. Now what “C” will do is allocate 2 bytes of memory for ‘i’ in the storage area and you will be able to store an integer value in ‘i’.
The following are the commonly used data types used in C language:-
- int: – This data type is used to store integer values. 2 bytes of memory is allocated for the storage. The range for int data type is from -32,768 to 32,767 i.e. you cannot store a value less than -32,768 and greater than 32,767. For higher precision ‘long int’ data type is used.
- char :- This data type is used to store character values. 2 bytes of memory is allocated.
- long int :- This data type is also used to store integer values but with higher precision than int. 4 bytes of memory is allocated i the system.
- float :- This data type is used to store real numbers i.e. the fractional numbers. The memory allocate is 4 bytes.
- double :- This data type is also used to store fractional numbers but the precision is higher. The memory allocated is 8 bytes
Note: – The memory allocation is in C under DOS i.e. Disk Operating System. The memory allocation in C under Linux will be different.
Remember: – The programmer should be aware of nature and range of data before writing any program. Accordingly, you will have to request the system to allocate storage for data following up standard data types supportive by ‘C’ under DOS (Disk Operating System).
C language is case sensitive. All standard function names, variable declaration are always in lower case.
The concept of Delimiter
A character has one ASCII associated. A name consists of ‘n’ such characters where ‘n’ is variable i.e. some name may have 10 characters while other may have something else. Therefore, the computer cannot automatically understand where the name ends. Now when you want to type a name in ‘C’ it gets stored in the form of strings. Now the question arises how ‘C’ will know that the string has ended. The end of the string is has an additional character equal to ’00’. This 00 is known as Delimiter. When you are finished typing a string you press enter, after pressing enter the system automatically put the delimiter that is ’00’ which tells ‘C’ that the string came to an end. For example if a string contains ‘HELLO’ it will actually contain 5 ASCII code followed by an additional byte containing ’00’ i.e. DELIMITER. This 00 is not the part of the string.
Why format specifiers are used?
When you are giving an input and displaying the result the concept of format specifiers are used. The system must know that which value is going to be stored or which value the system is going to display. For example, if you are going to give some decimal value or display decimal value ‘%d’ is used. Now don’t get confused with data types. Data types are used to tell the system that which value the user is going to use in the program and system will allocate the memory accordingly.
For example int i; Here ‘i’ is the variable and the data type is int. Now 2 bytes of memory will be allocated in the storage and decimal value will be stored.
Scanf(“%d”, &i); Here in this instruction the system will know that the value that the user is going to give is decimal value and that value will be stored in variable ‘i’
Here are some formats specifiers
- %d :- This is for decimal numbers
- %u :- unsigned decimal numbers
- %x :- hexadecimal numbers
- %f :- float numbers
- %c :- Character equivalent
if You have Missed the Module 1 Read Here : Basics of C Language