Skip to main content

Data Types and Qualifiers in C



There are four data types in C language. They are,
Types
Data Types
  Basic data types   int, char, float, double
  Enumeration data type   enum
  Derived data type   pointer, array, structure, union
  Void data type   void

Here we will discuss only basic data types.

Each variable in C has an associated data type. Each data type requires different amounts of memory and has some specific operations which can be performed over it. Let us briefly describe them one by one:
 
Following are the examples of some very common primitive data types used in C:
 
char: The most basic data type in C. It stores a single character and requires a single byte of memory in almost all compilers.
int: As the name suggests, an int variable is used to store an integer.
float: It is used to store decimal numbers (numbers with floating point value) with single precision.
double: It is used to store decimal numbers (numbers with floating point value) with double precision.
void:value less

Qualifiers are used to qualify the data type.The following are the qualifiers used with primitive data type.
 
unsigned, signed(default)
short,long
const
volatile

The unsigned qualifier avoids sign bit.
 
short and long :All store integers, but consume different memory and have different ranges. For eg: short int consumes 16bits, long int consumes 32bits and long long int consumes 64bits. The maximum value of short int is 32767. So of you need to store big values you need to use long int .
 
The qualifier const can be applied to the declaration of any variable to specify that its value will not be changed directly.Value of any variable can be changed either by program or external device.
 
The volatile qualifier is applied to a variable when we declare it. It is used to tell the compiler, that the value may change at any time. These are some properties of volatile.
The volatile keyword cannot remove the memory assignment
It cannot cache the variables in register.
The value cannot change in order of assignment.


Different data types also have different ranges upto which they can store numbers. These ranges may vary from compiler to compiler. Below is list of ranges along with the memory requirement and format specifiers on 32 bit gcc compiler.


Data Type             Memory(bytes)    Range              Format Specifier
short int                   2       -32,768 to 32,767                   %hd                   
unsigned short int          2       0 to 65,535                         %hu
unsigned int                4       0 to  4,294,967,295                 %u
int                         4      -2,147,483,648 to 2,147,483,647      %d or %i
long int                    4      -2,147,483,648 to 2,147,483,647      %ld
unsigned long int           4       0 to 4,294,967,295                  %lu
long long int               8       -(2^63) to (2^63)-1                 %lld
unsigned long long int      8       0 to 18,446,744,073,709,551,615     %llu
signed char                 1       -128 to 127                         %c 
unsigned char               1       0 to 255                            %c
float                       4       3.4E-38  to 3.4E+38                 %f
double                      8       1.7E-308 to 1.7E-30                 %lf
long double                12      3.4E-4932 to 1.1E+4932               %Lf
 
 
For windows ( 32 bit)

Data type- size in byte
char -1
short int -2
int -2
long int or long -4
float -4 (6 decimal places of precision)
double -8 (15 decimal places of precision)
long double -10 (19 decimal places of precision)

32-bit UNIX and Linux applications ( Solaris)Name Length

char 1 byte
short 2 bytes
int 4 bytes
long 4 bytes
float 4 bytes
double 8 bytes
long double 16 bytes

This shows that size depends on operating system and architecture ( 32 bit or 64 bit).We can use the sizeof() operator to check the size of a variable. See the following C program for the usage of the various data types:

#include <stdio.h>
int main()
{
int a = 1;
char b ='G';
double c = 3.14;
//printing the variables defined above along with their sizes
printf("character. value is %c and size is %lu byte.\n",b,sizeof(char));
printf(" integer. value is %d and size is %lu bytes.\n", a,sizeof(int));
printf(" double value is %lf and size is %lu ytes.\n",c,sizeof(double));
return 0;
}
 
The header file limits.h defines limits of integer types and the user can use the constants defined in it.
#include <limits.h>
#include <stdio.h>
int main()
{
printf("The max value of integer=%d",INT_MAX);
printf("The min value of integer=%d",INT_MIN);
return 0;
}

The header file float.h defines macros that allow you to use these values and other details about the binary representation of real numbers in your programs. The following example prints the storage space taken by a float type and its range values

#include <stdio.h> #include <float.h>
int main()
{
printf("Storage size for float : %d \n", sizeof(float));
printf("Minimum float positive value: %E\n", FLT_MIN );
printf("Maximum float positive value: %E\n", FLT_MAX );
printf("Precision value: %d\n", FLT_DIG ); return 0;
}

 

Macro

Value

Description

CHAR_BIT

8

Defines the number of bits in a byte.

SCHAR_MIN

-128

Defines the minimum value for a signed char.

SCHAR_MAX

+127

Defines the maximum value for a signed char.

UCHAR_MAX

255

Defines the maximum value for an unsigned char.

CHAR_MIN

-128

Defines the minimum value for type char and its value will be equal to SCHAR_MIN if char represents negative values, otherwise zero.

CHAR_MAX

+127

Defines the value for type char and its value will be equal to SCHAR_MAX if char represents negative values, otherwise UCHAR_MAX.

 
 

MB_LEN_MAX

16

Defines the maximum number of bytes in a multi-byte character.

SHRT_MIN

-32768

Defines the minimum value for a short int.

SHRT_MAX

+32767

Defines the maximum value for a short int.

USHRT_MAX

65535

Defines the maximum value for an unsigned short int.

INT_MIN

-2147483648

Defines the minimum value for an int.

INT_MAX

+2147483647

Defines the maximum value for an int.

UINT_MAX

4294967295

Defines the maximum value for an unsigned int.

LONG_MIN

-9223372036854775808

Defines the minimum value for a long int.


 

LONG_MAX

+9223372036854775807

Defines the maximum value for a long int.

ULONG_MAX

18446744073709551615

Defines the maximum value for an unsigned long int.

 
 

LLONG_MIN

-9223372036854775808

Minimumvalue for a variable of type long long

LLONG_MAX

9223372036854775807

Maximum value for a variable of type long long

ULLONG_MAX

18446744073709551615

(0xffffffffffffffff)

Maximum value for a variable of type unsigned long long

  

FLT_MAX
DBL_MAX
LDBL_MAX

3.402823466e+38F 1.7976931348623158e+308
1.7976931348623158e+308

Maximum representable floating point number

 

FLT_MIN
DBL_MIN
LDBL_MIN

1.175494351e-38F
2.2250738585072014e-308
2.2250738585072014e-308

Minimum positive value

 
enumerated data types
The discussion can be found here https://cprogramktu.blogspot.com/2018/01/enumerated-data-type.html

arrays
The discussion can be found here https://cprogramktu.blogspot.com/2018/01/arrays.html

structure and unions
The discussions can be found here https://cprogramktu.blogspot.com/2018/01/structures-and-unions.html

pointers
The discussions can be found here https://cprogramktu.blogspot.com/2018/02/pointers.html


Summary:




Comments

Popular posts from this blog

KTU Mandatory C programs for Laboratory and Solutions

LIST OF LAB EXPERIMENTS 1. Familiarization of Hardware Components of a Computer 2. Familiarization of Linux environment – Programming in C with Linux 3. Familiarization of console I/O and operators in C     i) Display “Hello World”     ii) Read two numbers, add them and display their sum     iii) Read the radius of a circle, calculate its area and display it 4. Evaluate the arithmetic expression ((a -b / c * d + e) * (f +g))   and display its solution. Read the values of the variables from the user through console 5. Read 3 integer values, find the largest among them. 6. Read a Natural Number and check whether the number is prime or not 7. Read a Natural Number and check whether the number is Armstrong or not 8. Read n integers, store them in an array and find their sum and average 9. Read n integers, store them in an array and search for an element in the    array using an algorithm for Linear Search 10.Read n integers, store them in an array and sort the elements in t

PROGRAMMING IN C KTU EST 102 THEORY AND LAB NOTES

PROGRAMMING IN C  KTU  EST 102  THEORY AND LAB   COMMON FOR ALL BRANCHES About Me Syllabus Theory Syllabus Lab Model Question Paper EST 102 Programmin in C University Question Papers  and evaluation scheme   EST 102 Programming in C  Introduction( Lab) Introduction to C programming Linux History and GNU How to create a bootable ubuntu USB stick Installing  Linux Install Linux within  Windows Virtual Box and WSL Linux Basic Features and Architecture Basic Linux Commands Beginning C Programming Compiling C programs using gcc in Linux Debugging C program using gdb Module 1: Basics of computer hardware and software          Module-1 Reading Material Basics of Computer Architecture Hardware and Software System Software and Application Software  Programming Languages ( High level, Low level and Machine Language) and Translators ( Compiler, Interpreter, Assembler) Algorithm, Flowcharts and Pseudo code Program Development Structured Programming Basics of hardware ( video) Know about Motherboar