---Advertisement---
Learn C

Learn C Language : Data Type

By Smart Answer

Updated on:

---Advertisement---

Integer, long and short

Sometimes, we know in advance that the value stored in a given integer variable will always be positive.When it is being used to only count things, for example, In such a case we can declare the variable to be unsigned, as innn unsigned int num;

With such a declaration, the range of permissible integer values will shift from the range -32768 to 32767 to the range 0 to 65535.Thus, declaring an integer as unsigned almost double the size of the largest possible value that it can otherwise take.This so happen because on declaring the integer as unsigned, the sixteenth bit is now free and is not used to store the sign of the number.Note that unsigned integer still occupies two bytes.In fact an unsigned int is nothing but short unsigned int.
Thus all the following declarations sre same:

short unsigned int i;
unsigned int i;
unsigned i;

The way there exists a short unsigned int, there also exists a long unsigned int which has a range of 0 to 4294967295 and occupies four bytes of memory.

By default a short int is a signed short int and a long int is a signed long int.

Chars

The way there are signed and unsigned int (either short and long) similarly there are signed and unsigned char, both occupying one byte each, but having different range.A signed char is same as our ordinary char and has range from -128 to 127; whereas an unsigned char has a range from 0 to 255.

Now see a program that illustrates this range:

main()
{
char ch = 291;
printf(“%d %c”,ch,ch);
}

 

What output do you expect from this program?
Possible, 291 and the character corresponding to it.
Well,not really.Surprised?
The reason is that ch has been defined as a char and a char cannot take the value bigger than +127.Hence the value of ch exceeds +127, an appropriate value from the other sideof the range is picked up and stored in ch.This value in our case happen to be 35, hence 35 and its corresponding character , # gets printed out.

Floats and Doubles

A float occupy four bytes in memory and can range from -3.4e38 to +3.4e38.If this is sufficient then C offers a double data type which occupies 8 bytes in memory and has a range from -1.7e308 to +1.7e308.
A variable of type double can be declared as,

double num;

If the situation demands usage of real numbers which lie even beyond the range offered by double data type then there exists a long double which can range from -1.7e4932 to +1.7e4932.A long double occupies 10 bytes in memory.

In C programming one is required to use either char or int and cases where float, double or long double would be used are indeed rare.

Lets capture all the data types that we have learnt so far :

Data Types

Range

Bytes

Format

signed char

-128 to +127

1

%c

unsigned char

0 to 255

1

%c

short signed int

-32768 to +32767

2

%d

short unsigned int

0 to 65535

2

%u

long signed int

-2147483648 to +2147483647

4

%ld

long unsigned int

0 to 4294967295

4

%lu

float

-3.4e38 to +3.4e38

4

%f

double

-1.7e308 to +1.7e308

8

%lf

long double

-1.7e4932 to +1.7e4932

10

%Lf

 

Storage Class in C

To fully define a variable one needs to mention not only its ‘type’ but also its ‘storage class’.Not only all variables have a data types, they also have a ‘storage class’.
Variable name identifies some physical location within the computer where the string of bits representing the variable stored value.There are basically two kinds of locations in a computer : Memory and CPU register.It is the variable’s storage class which determines in which of these two location the value is stored.

A variable storage class tells us:
[a] Where the variable would be stored.
[b] What will be the initial value of the variable(i.e. the default initial value)
[c] What is the scope of the variable i.e. in which function the value of the variable would be available
[d] what is the life of the variable i.e. how long variable exist

There are four storage classes in C :
1. Automatic Storage Class
2. Register Storage Class
3. Static storage class
4. External storage class

lets examine one by one

Automatic Storage Class

Default initial value:

An unpredictable value, which is often called a garbage value.

Scope

Local to the block in which the variable is defined.

Life

Till the control remains within the block in which the variable is defined.

Storage

memory

A simple program to demonstrate automatic storage class:

 

/* If the variable is not initialized then it contain garbage value */
main()
{
auto int i,j;
printf(“%d %d”,i,j);
}

Output
1211 221

 

 

where, 1211 and 221 are garbage value of i and j.When you run this program you may get different values, since garbage values could be absolutely any values.So always make it a point that you initialize the automatic variables properly,otherwise you are likely to get unexpected results.
Note that the keyword for this storage class is auto and not automatic.

 

Register Storage Class

Default initial value:

An unpredictable value, which is often called a garbage value.

Scope

Local to the block in which the variable is defined.

Life

Till the control remains within the block in which the variable is defined.

Storage

CPU registers

A simple program to demonstrate register storage class:

 

main()
{
register int i;
for(i=1; 10 >= i; i++)
printf(“%d”,i);
}

 

 

A value stored in a CPU register can always be accessed faster than the one which is stored in memory.Therefore, if a variable is used at many places in a program it is better to declare its storage class as register.
A good example of frequently used variables are loop counter.

 

Static Storage Class

Default initial value:

Zero

Scope

Local to the block in which the variable is defined.

Life

Value of the variable persist between different function calls

Storage

Memory

Difference between the automatic and static storage classes:

/* Example of automatic storage class */

main()
{
increment();
increment();
increment();
}

void increment()
{
auto int i = 1;
printf(“%d”,i);
i++;
}

Output
1 1 1

 

 

/* Example of static storage class */

main()
{
increment();
increment();
increment();
}

void increment()
{
static int i = 1;
printf(“%d”,i);
i++;
}

Output
1 2 3

 

 

The program above consist of two functions main() and increment().The function increment() gets called from main() thrice.Each time it increment the value of i and print it.The only difference between the two program is that one uses an auto storage class for variable i, whereas the other uses static storage class.

 

External Storage Class

Default initial value:

Zero

Scope

Global

Life

As long as the program execution doesn’t come to an end.

Storage

Memory

 

External variables differ from those we have already discussed in that their scope is global, not local.External variable are declared outside all functions, yet are available to all functions that care to use them.

Smart Answer

---Advertisement---

Related Post

Learn C Language : String

‹ previous What Is String The way a group of integer can be stored in an integer array, similarly a group of character can be stored in a ...

Learn C Language : Array

‹ previous Next › What are Arrays For understanding the arrays properly, consider the following program: main(){     int x;      x = 5;      x ...

Learn C Language : Functions

‹ previous Next › What is a Function A function is a self-contained block of statements that perform a coherent task of some kind.Every c program can be ...

Learn C Language : Switch Case Statement (Case Control Structure)

‹ previous Next › Switch Case Statement The control statement which allows us to make a decision from the number of choices is called a switch, or more ...

Leave a Comment