C Data Types

Data types are merely used to specify variables or functions. The variable type specifies how much storage space it will take up and how the bit pattern stored will be interpreted. C defines the four basic or primary data types as follows:

Modifiers can be added before the basic data types. A type modifier modifies the meaning of the base type to better suit a specific need. The following are the modifiers:

Modifiers for the "int" base type include "signed," "short," "long," and "unsigned" values. Additionally, the "char" type can be set to "signed" or "unsigned." Additionally, "long" can be used with "double."

C Data Types with Size and Their Ranges

The following table lists all the data types with their sizes and ranges:

Type Storage Size (in bytes) Minimal Range
char 1 -127 to 127
unsigned char 1 0 to 255
signed char 1 -127 to 127
int 2 or 4 -32,767 to 32,767
unsigned int 2 or 4 0 to 65,535
signed int 2 or 4 -32,767 to 32,767
short int 2 -32,767 to 32,767
unsigned short int 2 0 to 65,535
signed short int 2 -32,767 to 32,767
long int 4 -2,147,483,647 to 2,147,483,647
long long int 8 -(263-1) to 263-1
signed long int 4 -2,147,483,647 to 2,147,483,647
unsigned long int 4 0 to 4,294,967,295
unsigned long long int 8 264-1
float 4 1E-37 to 1E+37 (six digits of precision)
double 8 1E-37 to 1E+37 (ten digits of precision)
long double 10 1E-37 to 1E+37 (ten digits of precision)

Signed integers are important for a great many algorithms, but they only have half the absolute magnitude of their unsigned relatives. For instance, here, 32,767 in binary is:

0 1 1 1 1 1 1 1   1 1 1 1 1 1 1 1

When a type modifier doesn't precede a basic type, then int is assumed. Let's look at the following table:

specifier is same as
signed signed int
unsigned unsigned int
long long int
short short int

Get the Exact Size of Any Data Type in C

Because "int" data types have different sizes on different platforms, use the "sizeof" operator to get the exact or actual size of a "int" type or "int" type variable on a specific platform. The expression "sizeof(type)" indicates the storage size of an object or type in bytes. The following example demonstrates how to obtain the precise size of an "int" type on any platform:

#include <stdio.h>
int main()
{
   printf("'int' storage size = %d\n", sizeof(int));
   return 0;
}

This C program was written in the "Code::Blocks" IDE, then built and executed. The output of the above program is shown in the following snapshot.

c data types example program

As you can see, the size of "int" in my platform is "4".

Change the name of the data type in a C program

There is a keyword named "typedef" that can be used in the C programming language to define a new name for a data type. The general form of the "typedef" keyword is as follows:

typedef type new_name;

For example:

typedef float mark;

Now "mark" will be the new name for the type "float" in the program. Consider the following program as an example:

C Code
#include <stdio.h>
int main()
{
   typedef float mark;
   mark a, b, c, tot;

   printf("Enter marks obtained in three subjects: ");
   scanf("%f %f %f", &a, &b, &c);

   tot = a+b+c;
   printf("\nTotal marks = %.2f \n", tot);

   return 0;
}
Output
Enter marks obtained in three subjects: 89
98.3
74

Total marks = 261.30

The ".2" is used to print only two digits after the decimal.

C type casting

Type casting is a technique for making an expression of a specific or required type. The general form of type casting in C is as follows.

(type) expression;

For example:

(float) i/2;

Assuming the "i" variable is of the "int" type. The preceding statement casts the expression as "float" rather than "int." As an example, consider the following program:

C Code
#include <stdio.h>
int main()
{
   int i = 11;
   float res;

   res = i/2;
   printf("%.2f \n", res);

   res = (float) i/2;
   printf("%.2f \n", res);

   return 0;
}
Output
5.00
5.50

Storage classes in C

Storage classes such as "auto," "extern," "static," and "register" can be used to define extra features of a variable or function. The table below summarizes the four storage classes available in the C programming language.

Specifier Initial value Scope
auto garbage within the block
static 0 within the block
register garbage within the block
extern 0 global
multiple files

The "register" variable is stored in the CPU register. The "auto" and "register" variables exist until the end of the block, whereas the "static" and "external" variables exist until the end of the program.

The general form to use the storage class to add an extra feature to a variable or function in the C language is as follows:

storageClass type name;

Consider the following program as an example: In this program, I will use the "static" storage specifier to keep remembering the previous value during the complete program execution.

C Code
#include <stdio.h>
void myfun(void);
int main()
{
   myfun();
   myfun();
   myfun();
   return 0;
}
void myfun(void)
{
   static int num = 1;
   printf("%d \n", num);
   num++;
}
Output
1
2
3

I called the function "myfun()" three times. Each time the value of "num" is printed on the output console, Since I used the "static" specifier before the variable "num," therefore, the variable "num" remembers its previous value. If I remove the "static" specifier from the previous program, then the output should be:

1
1
1

That is, when the variable "num" is used in the body of a function definition without the "static" specifier, it is treated as a new variable. You will learn all about the functions in C in a separate post.

C Online Test


« Previous Tutorial Next Tutorial »