Home C Language C Data Types

C Data Types


C language supports a rich set of datatypes. For storage representation and machine instruction to handle the constants we can use these data types. The data type are divide into three types.

1.built-in Data types (primary data types)

    Int, char, float, double

2.Derived Datatypes

    Arrays, pointers

3.User-Defined Datatypes

    Structures, unions

Basic data types in the c language are character, integer, floating point and double floating point. These are declared as char, int, float and double respectively. The size and range of these data types may vary based on processors and compilers.

Basic data types may have various modifiers preceding them. A type modifier alters the meaning of the base type to more precisely fit a specified need. The list of modifiers is as follows:

signed

unsigned

long

short

Using the data types we declare the variables to store and calculate the user required data. The data of a variable can be displayed on the screen using a printf() statement, but we didn’t do that directly by passing the variable. Using particular format specifiers we can do it easily. While reading data from the keyboard using scanf() statement to a variable, at that time also we required format specifiers.

Following table will show various data types, format specifiers, ranges and size of variables:

datatype size (bytes) Range Control Format
short Int 2 -32,768 to 32,767 %hd
unsigned short int 2 0 to 65,535 %hu
int 2 or 4 (It vary based on O.S) -32,768 to 32,767 (or)
-2,147,483,648 to 2,147,483,647
%d
unsigned int 2 or 4 0 to 65,535 (or)
0 to 4,294,967,295
%u
long int 4 -2,147,483,648 to 2,147,483,647 %ld
unsigned long int 4 0 to 4,294,967,295 %lu
float 4 1.2E-38 to 3.4E+38
6 decimal places
%f
double 8 2.3E-308 to 1.7E+308
15 decimal places
%lf
long double 10 3.4E-4932 to 1.1E+4932
19 decimal places
%lf
char 1 -128to127 or
0 to 255
%c
unsigned char 1 0 to 255 %c
signed char 1 -128 to 127 %c
long long int 8 -(2^63) to (2^63)-1 %lld
unsigned long long int 8 0 to (2^64)-1 %llu

In case of string variable %s.

The void Type

The void type specifies that no value is available. It is used in three kinds of situations:

  1. Function returns as void: There are various functions in C which do not return value or you can say they return void. A function with no 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. A function with no parameter can accept as a void. For example, int rand(void);
  3. Pointers to void: A pointer of type void * represents the address of an object, but not its 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.

The void type may not be understood to you at this point, so let us proceed and we will cover these concepts in the upcoming chapters.

Programs

Write a C program to compute the perimeter and area of a rectangle with a height of 7 inches and width of 5 inches.

Write a C program to display multiple variables.

Write a C Program to read and write a variable.