PDF

Showing posts with label data-type. Show all posts
Showing posts with label data-type. Show all posts

Data Type Introduction

Leave a Comment

Introduction


Every programming language deals with some data. For example to print any message it requires charter or string type of data. To solve any mathematics expression it requires integral as well as real number (floating type) of data. C is very rich in data type. We can broadly divide all data type in C in three categories : 

  1. Primitive or fundamental data type 
  2. Derived data type
  3. User defined data type

Built-in data types or primitive data types :


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

int - data type


int is used to define integer numbers.

{
int count;
Count = 5;
}


float - data type


float is used to define floating point numbers.

{
float Miles;
Miles = 4.6;
}


double - data type


double is used to define BIG floating point numbers.
It reserves twice the storage for the number. On PCs this is likely to be 8 bytes;

{
double Atoms;
Atoms = 25000000;
}


char - data type 


char defines characters.

{
char Letter;
Letter = 'a';
}

Derived data types available are 


  1.  array
  2.  pointers
  3.  functions


User defined data types


  1.  structure 
  2.  union
  3.  enumerations


Derived data types and User defined data types will be explained in next sessions.

Data Type Modifiers

Leave a Comment

Modifiers 


The data types explained previously have the following modifiers : 

  1.  short 
  2.  long
  3.  signed
  4.  unsigned


The modifiers define the amount if storage allocated to variable. The amount of storage allocated is not cast in stone. ANSI has the following rules:

short int <= int <= long int

float <= double <= long double


What this means is that a 'short int' should assign less than or the same amount of storage as an 'int' and the 'int' should be less or the same bytes than a 'long int'.

What this means in the real word is:

Bytes:


2 short int
2 unsigned short int
4 unsigned int
4 int
4 long int
1 signed char
1 unsigned char
4 float
8 double
12 long double


Ranges


short int             -32,768 to +32,767

unsigned short int    0 to +65,535

unsigned int         0 to +65,535

int                  -32,768 to +32,767

long int             -2,147,483,648 to + 2,147,483,647

signed char          -128 to +127

unsigned char        0 to +255

float                3.4*10^-38 to 3.4*10^38

double               1.7*10^-308 to +1.7*10^308


Note: One simple way to remember the limits is :
Every 1 byte = 8 bits
so range is form (2^(n-1))  to  (2^(n-1))-1
where 'n' is the no. of. bits

e.g.
for int takes 2 bytes
2 bytes = 16 bits
so n = 16
so range is from (2^15) to (2^15)-1


Qualifiers 


A type qualifier is used to refine the declaration of a variable, a function, and parameters, by specifying whether:

The value of a variable can be changed. The value of variable must always be read from memory rather than from a register.

Standard C language recognizes the following two qualifiers:

1. const :


The const qualifiers is used to tell C that the variable value can not changed after initialisation.

const float pi = 3.24259;

Now pi cannot be changed at a later time within the program. Another way to define constants is with the # define preprocessor which has the advantage that it does not use any storage.

2. volatile :


The volatile qualifier declares a data type that can have its value changed in ways outside the control or detection of the compiler ( such as a variable updates by the system clock or by another program).
This prevents the compiler from optimizing code referring to the object by storing the objects value in a registered re-reading it from there, rather than from memory, where it may have changed. You will use this qualifier once you will become expert in 'C'. So for now just proceed.

Data Type Example

Leave a Comment
Note : As you know size of data types is compiler depended in C. Answer of all question is based upon that compilers whose word sixe is two byte. Just to know you, size of data type in 2 bytes compilers is as follows:

char  : 1 byte
int   : 2 byte
float : 4 byte
double: 8 byte

Please adjust the answers according to size of data types in your compiler.

1. What will be output when you will execute following C code?


#include<stdio.h>
int main()
{
printf("%d\t",sizeof(6.5));
printf("%d\t",sizeof(90000));
printf("%d",sizeof('A'));
return 0
}

Output : 

Turbo C++ 3.0 = 8 4 2
Turbo C++ 4.5 = 8 4 2
Linux GCC     = 8 4 4
Visual C ++   = 8 4 4

Explanation : 

By default data type of numeric constants is :
6.5 = double
90000 = long int
'A' = char

In C size of data type varies from compiler to compiler.
In TURBO C 3.0 (16 bit compilers) size of : 
double is 8 byte
long int is 4 byte
character constant is 2 byte (size of char data type is one byte)

In TURBO C $.5 or Linux GCC compilers (32 bit compilers ) size of :
double is 8 byte
long int is 8 byte 
character constant is 2 byte


2. What will be output when you will execute following C code ?

#include<stdio.h>
int main()
{
double num = 5.2;
int var = 5;
printf("%d\t",sizeof(!num));
printf("%d\t",sizeof(var = 15/2));
printf("%d",var);

return 0;
}


Output :

Turbo C++ 3.0 = 2 2 5
Turbo C++ 4.5 = 2 2 5
Linux GCC     = 4 4 5
Visual C ++   = 4 4 5

Explanation:
sizeof(Expr) operator always returns the integer value which represents the size of the final value of the expression expr.

Consider on the following expression: 
!num
=!5.2
=0

0 is int type integer constant and it size is 2 by in TURBO C 3.0 compiler and 4 in the TURBO C 4.5 and Linux GCC compilers.

Consider on the following expression:
var = 15/2
= var = 7
= 7

7 is int type integer constant.

Any expression which is evaluated inside the sizeof operator. So value of variable 'var' will remain 5 in the printf statement.


3. What will be output when you will execute following C code?

#include<stdio.h>
int main()
{
signed x;
unsigned y;
x = 10 +-10u + 10u +-10;
y = x;
if(x==y)
printf("%d %d",x,y);
else if(x!=y)
printf("%u %u",x,y);

return 0;
}


Output :

Turbo C++ 3.0 = 0 0
Turbo C++ 4.5 = 0 0
Linux GCC     = 0 0
Visual C ++   = 0 0

Explanation : 
Consider on the expression:
x = 10 +- 10u + 10u +- 10;

10 = it is signed integer constant
10u = it is unsigned integer constant
X = it is signed integer variable.

In nay binary operation of dissimilar data type operand always automatically type casted into the operand of higher data type before performing the operation and result will be higher data type.
As we know operators enjoy higher precedence than binary operators. So our expression is:

x = 10 + (-10u) + 10u + (-10);
  = 10 + -10 + 10 + (-10);
  =0

Note : Signed is higher data type than unsigned int. So, Corresponding signed value of unsigned 10u is +10


4. What will be output when you will execute following C code?

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


Output : Connot predict

Explanation:
We cannot predict the value of volatile vriavle because it's value can be changed by nay microprocessor interrupt.