3.7 – Data Types putting all together

by subbu on September 5, 2013

In the previous sessions we have discoursed different primary data types available in C language and addressed some of the issues like ASCII representation, using sizeof(), overflow-underflow and default constant types etc. Now we are concluding all at a place.

Primary data types: sizes, ranges, format specifiers

Type size range format SPR
signed char 1byte -128 to +127 %c
unsigned char 1byte 0 t0 255 %c
short signed int 2bytes -32,768 to +32,767 %d or %i
short unsigned int 2bytes 0 to 65,535 %u
signed int 2bytes -32,768 to +32,767 %d or %i
unsigned int 2bytes 0 to 65,535 %u
long signed int 4bytes -2,147,483,648 to +2,147,483,647 %ld
long unsigned int 4bytes 0 to 4,294,967,295 %lu
float 4bytes -3.4e38 to +3.4e38 %f or %g
double 8bytes -1.7e308 to +1.7e308 %lf or %lg
long double 10bytes -3.4e4932 to +1.1e4932
or
-1.7e4932 to +1.7e4932
%Lf or %Lg
Note: The sizes and ranges of data types are compiler dependent, changes compiler to compiler and architecture to architecture

Note:
The size of short is 2 bytes any where (maximum size of short is 2bytes)
The size of int is 2 bytes in some compilers and 4bytes in other compilers (minimum size of int is 2bytes)
The size of long double is 10 bytes in some compilers and 12bytes in other compilers

Various forms in declaring variables:

signed short int x;
short signed int x;
short int x;
signed int x;
int x;
short x;
signed x;

All the above statements have the same effect

long signed int x;
signed long int x;
long int x;
long x;

All the above statements have the same effect

unsigned short int x;
short unsigned int x;
unsigned int x;
unsigned x;

All the above statements have the same effect

long unsigned int x;
unsigned long int x;
unsigned long x;

All the above statements have the same effect

signed char ch;
char signed ch;
char ch;

All the above statements have the same effect

unsigned char ch;
char unsigned ch;

All the above statements have the same effect

Quiz-1:
1) How many characters can a char type variable store?
2) What is the maximum value that an unsigned int can store?
3) What would be the output of following program?

#include<stdio.h>
int main()
{
unsigned int x=234;
long unsigned int y=234L;
printf("%lu\n%u",x,y);
return 0;
}

4) What would be the output of following program?

#include<stdio.h>
int main()
{
long float x=a=456.45645656;
unsigned double y=45;
printf("%lf\n%ulf",x,y);
return 0;
}

5) What would be the output of following program?

#include<stdio.h>
int main()
{
int a,b;
printf("%d\n%d",a,b);
return 0;
}

6) What would be the output of following program?

#include<stdio.h>
int main()
{
printf("%d\n%c");
return 0;
}

7) What would be the output of following program?


#include<stdio.h>
int main()
{
int a=10,b=20;
printf("%d",a,b);
return 0;
}

8) What would be the output of following program?

#include<stdio.h>
int main()
{
printf("%f");
return 0;
}

9) What would be the output of following program?

#include<stdio.h>
int main()
{
long num;
num=2;
printf("%ld",num);
return 0;
}
Show Answers
1) 1 character
2) 65,535
3) 234
   234
4) errors
   long can't be applied to float.
   floating point types have no unsigned types.
5) 4203523
   2293480
   It is the garbage value.
6) 4793392
   Φ
   Garbage value is printed when printing nothing with format specifier
    %d and %c
7) 10
8) 0.000000
   Garbage value
   Note: In some compilers, it gives to error
9) 2
   by default long means long int

Overflow and underflow:

Assigning a value which is more than its upper limit is called overflow and less than its lower limit is called underflow.
In case of integer types overflow results wrapping towards negative side and underflow results wrapping towards positive.
In case of floating point types overflow results +INF and underflow results -INF

Quiz-2:
1) What would be the output of following program?

#include<stdio.h>
int main()
{
char ch=200;
printf("%d\n%c",ch,ch);
return 0;
}

2) What would be the output of following program

#include<stdio.h>
int main()
{
char ch=300;
printf("%d\n%c",ch,ch);
return 0;
}

3) What would be the output of following program?

#include<stdio.h>
int main()
{
int x=300;
printf("%d\n%c",x,x);
return 0;
}

4) What would be the output of following program?

#include<stdio.h>
int main()
{
printf("%d",32770);
return 0;
}

5) What would be output of following program?

#include<stdio.h>
int main()
{
int a=33000;
float b=3.4e50;
printf("a=%d\nb=%f",a,b);
printf("\n%d\n%d",sizeof(a),sizeof(b));
return 0;
}
Show Answers
1)
Output:
-56

Binary equal to 200 (1100 1000) is stored in the variable
While printing with %d, it is treated as a negative number because the first bit in 8bits is 1, Reverts the 2’s (1100 0111) and 1’s (0011 1000) compliment and print the value with -ve sign (-56)
While printing with %c equal character is printed (200 is the ASCII value to a graphical character)

2)
Output:
44
,
Binary equal to 300 (1  0010  1100) is a 9bits binary.
Because char type takes only 1byte, last 8bits (0010 1100) are stored in the memory
While printing with %d, it is treated as a positive number because the first bit in 8bits is 0 and prints its decimal equal 44
While printing with %c equal character (,) is printed (44 is the ASCII value of ,).

3)
Output:
300
,
Binary equal to 300 (1 0010 1100) is stored in int type memory as (0000 0001 0010 1100)
While printing with %d, it is treated as positive number because the first bit in 16bits is 0 and prints its decimal equal 300
While printing with %c equal character to the lower 8bits is printed (44 is the ASCII value of ,)

printing an integer with %c

4)
Output:
-32766
The range of integer -32768 to +32767, assigning 32770 results overflow and wrap towards -ve side

5)
Output:
-32536
+INF
2
4
The range of integer is -32768 to +32767, assigning 33000 results overflow and wrap towards -ve side
+INF is a result of float overflow
int take 2 bytes and float takes 4 bytes.

Previous post:

Next post: