Data Types In C – With Examples | C Tutorials

C Data types with examples and sample programs – Here we cover the complete step by step information on what are data types in C, examples and sample programs and output for those example programs.

What are Data Types?

In C, we have 4 data-types and we can create a variable that belongs to any of these 4 types. Out of the 4, we have 2 types to work with real numbers (numbers with decimal point). The four types are

  1. int
  2. char
  3. float
  4. double

Note: some people say “void” is also a data type and arguably we can consider void also as a data type.

When using a variable, we should mention what type of variable it is. When we want to store age of a person, we create the variable as integer type.

Eg:

When we want to store temperature of a place, we mention the variable as double type.

Eg:  

The general way of creating a variable of a specific type is

   Eg1:        <datatype> variable;

                Eg2:        <datatype> variable1,variable2,….variable N;

In C, an integer variable occupies 4 bytes (2 bytes on some compilers. Eg: Turbo-C) of memory and a double variable occupy 8 bytes of memory.

When we mention the data-type, the system understands the operations allowed and memory requirements of the variable.

We can apply almost all operators on integer/character type data but cannot apply some operators (like modulus and bitwise operators) on float/double type data.

Size and Range:

On the compilers that take 4 bytes for an integer, a total of 32 bits (each byte is formed with 8 bits) are allocated and generally 1 bit is used to represent sign. So the actual number is formed with the remaining 31 bits.

When the sign bit is 0, the number is treated as positive and when the sign bit is 1, the number is treated as negative. On the compilers that take 2 bytes for integer, a total of 16 bits are allocated and in that also 1 bit is used to represent sign. So the actual number is formed with the remaining 15 bits.

These are known as “signed int” type and this is the default when we create a variable as “int” type. If we don’t want to use any sign bit then we can make use of all the 32 (or 16) bits for representing a number.

This is good when we know that we are not going to use any negative values for a particular kind of data (like age of a person). Such things are known as “unsigned int” type.

On a 4-Byte Compiler

on a 4-byte compiler
Modifier Type Size

(bytes)

max-value min-value
signed Char 1 127  (27-1) -128 (-27)
Unsigned char 1 255  (28-1) 0
signed int 4 2147483647  (231-1) -2147483648  (-231)
Unsigned int 4 4294967295  (232-1) 0
signed long 8 9223372036854775807  (263-1) -9223372036854775808  (-263)
Unsigned long 8 18446744073709551615  (264-1) 0
float 4 3.4*1038 1.4*10-45
double 8 1.7*10308 4.9*10-324

 

On a 2-Byte Compiler

on a 2-byte compiler
Modifier type Size

(bytes)

max-value min-value
signed char 1 127  (27-1) -128 (-27)
unsigned char 1 255  (28-1) 0
signed int 2 32767  (215-1) -32768  (-215)
unsigned int 2 65535  (216-1) 0
signed long 4 2147483647  (231-1) -2147483648  (-231)
unsigned long 4 4294967295  (232-1) 0
float 4 3.4*1038 1.4*10-45
double 8 1.7*10308 4.9*10-324

Note:

  • The ‘signed’ or ‘unsigned’ are not applied on float type.
  • The ‘signed’ or ‘unsigned’ are not applied on double type.
  • The min-values given for ‘float’ and ‘double’ are the minimum possible (the smallest) positive value.

 

with Range and Data Types

The compilers that we use for compiling a c-program behave differently than we expect. The reasons are many. But in terms of data types, we should understand that different compilers implement and behave differently for a same code.

  • One of the reasons is the memory allocated for a same type is different from compiler to compiler.
  • So we should check the size of each data type on that particular machine before working with it.

The typical sizes are given below. In the following table, we have given size of types on 3 different compilers. We should understand that there are other compilers with a different set of sizes also.

 

Most of the times “short int” and “int” are same in size.

 

Datatype Type1-compiler Type2-compiler Type3-compiler
short int size 2 2 4
int size 2 4 4
long int size 4 4 8

Suppose we are storing age of a person, (the age can be a maximum of 200) then we can create the corresponding variable as int (short int) type.

Similarly when we are storing distance from California to Moscow in miles (5898 mi) then also we can use an int type variable.

  • When we want to store gender of a person (like ‘M’ or ‘F’ for male and female respectively) we can go for char type of data.
  • Suppose we are working with distance from earth to moon then we may need to use long or bigger type variable.

 

Int –

This is the type we use to work with whole numbers and we can use up to a 10 digit number (on 4 byte systems) with int type.

Eg:

Long Int –

When we want to store a value bigger than int range, we can use long type. With long, we can store up to a 19 digit number.

When we using a value bigger than int range, we better to suffix it with ‘l’ or ‘L’ to notify it to be a long (long int) value.

  • Eg:          long a=131009;
  •                 long b=123456789012345;
  •                 long c=123456789012345L;
  •                 long int d=121009;
  •                 long int e=123456789012345;

Char –

In C, char type takes 1 byte of memory and it supports ASCII characters.

The 256 ASCII characters (numbered from 0 to 255) can be represented by this type. In ASCII ‘A’ is 65, ‘B’ is 66, ‘C’ is 67, ‘D’ is 68, ‘Z’ is 90, ‘ a’ is 97, ‘b’ is 98, ‘z’ is 122, ‘0’ is 48, ‘1’ is 49, ‘9’ is 57, ‘ ‘ is 32, ‘\n’ is 10, ‘\t’ is 9.

Eg:

  • char a=55;
  • char b=’A’; (is deemed to be equal to) char b=65;
  • char c=’9’; (is different from) char c=9;
  • char d=’+’;

Float –

To work with numbers with decimal points, we can use float type variables.

Generally double is preferred over float as accuracy and range of double is better than float. So, we should prefer float only for simple calculations.

Eg: 

  • float a=3.6;
  • float b=3.6f;
  • float c=4.5e12; ß is understood as (float c=4.5*1012)
  • float d=123.44e-45; ß is understood as (float d=123.44*10-45)

Double –

This is the default data-type to store real numbers.

By default any value with a fractional part is considered as double. A smaller type value can be given to bigger type variable. So the following statements are valid.

Eg:

  • double a=44.66;
  • double b=’A’;
  • double c=6545;

Logical Values –

C does not have an exclusive data type to represent logical values like ‘true’ and ‘false’.

Even to store such values, C uses int type only. Generally 1 is used to represent true and 0 is used to represent false. In fact, logically any value other than 0 is treated as true in C.

Eg: 

  • int a=10<20;
  • int b=1;
  • int c=0;
  • int d=10==20;

Prefer Int & Double –

Generally when an operation is performed on floats they are internally up casted to double before actual operation takes place. Similarly when we try to add two characters, the equivalent integer values (ASCII values) are added.

So some programmers use integer type of variables even to store character values.

But storing char type data into int variables would result in waste of memory and when working with strings this approach (using ints for chars) would give worse results.

  • So in such situations we should use char type only.

By default the system considers a fractional value as double. If we try to assign a fraction value to a float variable the fraction value (double type) is down casted to float type. So many prefer to use double variables rather than float variables.

Add a Comment

Your email address will not be published. Required fields are marked *