C Programming Lesson - Data Types in C Language
A programming language helps us process certain kinds of data by providing useful output. The task of data processing is accomplished by executing a series of commands called a program. A program usually contains different types of data, such as integer, float, character etc. A program stores the values being used along with a library function and the user defined function (UDF) to process that stored data. C language is rich in data types and library functions.
C data types can be broadly classified as :
- Primary data types
- Secondary data types
Data Types in C
Primary Data Types in C Language
Primary data types include the following:
Integer Data Type
- Integers are whole numbers with a range of values, the range limits being machine dependent.
- Denoted by the keyword int.
- Generally an integer occupies 2 bytes memory space and its value range is limited to -32768 to +32767 (that is, -215 to +215-1).
- A signed integer uses one bit for storing the sign and 15 bits for the number.
To control the range of numbers and storage space, C has three classes of integer storage:
- short int
- long int
A short int requires half the amount of storage than normal integer. The long integers are used to declare a longer range of values and it occupies 4 bytes of storage space.
All three data types have signed and unsigned forms.
- Unlike signed integers, unsigned integers are always positive and use all the bits for the magnitude of the number. Therefore the range of an unsigned integer will be from 0 to 65535.
Syntax: int <variable name>; like
short int num2;
long int num3;
Example: 5, 6, 100, 2500.
Integer Data Type Memory Allocation
Primary Data Types in C: Float
- The float data type is used to store fractional numbers (real numbers) within 6 digits of precision.
- Floating point numbers are denoted by the keyword float.
- When the accuracy of the floating point number is insufficient, we can use the specifier "double" to define the number. “Double” is the same as “float” but has double the precision and takes up twice as much memory space (8 bytes) as float.
- For even more precision we can use “long double” which occupies 10 bytes of memory space.
Syntax: float <variable name>;
long double num3;
Example: 9.125, 3.1254.
Floating Point Data Type Memory Allocation
Primary Data Types in C: Character
- The character type variable can hold a single character.
- "Char," like "int," may be singed or unsigned. Both signed and unsigned occupy 1 byte each but have different ranges. Unsigned characters have values between 0 and 255; signed characters have values from –128 to 127.
- Denoted by char.
Syntax: char <variable name>;
char ch = ‘a’;
Example: a, b, g, S, j.
Primary Data Types in C: Void
- The “void” data type returns no data; it is not a variable like the other types. It may be used with functions that need to be run without returning any values.
- For example, we can declare “main()” as a void type because it does not return any value.
User-Defined Type Declaration in C Language
C language supports a feature where the user can define the identifier that characterizes an existing data type. This user-defined data type identifier can later be used to declare variables. In short, its purpose is to redefine the name of an existing data type.
Syntax: typedef <type> <identifier>;
Example: typedef int number;
Now we can use number in lieu of int to declare integer variable. For example: if we enter int x1 or number x1 both statements declare an integer variable. We have just changed the default keyword int to declare integer variable as number.
Size and Range of Data Types in C Language
Secondary Data Types in C Language
- Arrays: collections of data of a similar type.
- Pointers: a way to refer to information by referring to the address where the information is kept.
- Structures: a way of associating data of different types.
- Unions: a way of allowing the same data to be read as data of different types.
More by this Author
Every C complier provides a library of around 200 or more predefined functions and macros which we can use in our C program. These library functions or inbuilt functions help programmers to perform common programming...
In my previous c programming tutorial I tried to explain what the function, its advantages is and how to declare a C function. And I told you that there are five types of functions and they are: Functions with no...
C allows array of two or more dimensions. More dimensions in an array means more data can be held.