Jun 4, 2014

C Programming #04: Datatypes and Variable

Data types

Generally any computer program processes and generates data. Data can be of different types, C supports following Data natively
  1. Character
  2. Whole number
  3. Real number


Character

Characters consists of a to z and A to Z, apart from this there are symbols like , . + - etc. C uses ASCII representation to store characters hence it supports all ASCII characters.


Whole number

C supports both positive and negative whole numbers. In C they are referred to as integers.


Real numbers

Real numbers are supported in C, but by approximating them. In C they are called floating point numbers.


String - Not natively

String is sequence of charecters. It is not natively supported in C. It will be explained further.


Variables

Something that could change in course of time/space is called variable. In C variables act as storage locations for data in program(NOTE: this data can be character, integer, float). C makes it compulsory to declare a variable before using it. It is like giving heads up to compiler before using variable.


Variable declaration

Following is the syntax (syntax means programming rule) to declare a character variable.

char c;

char is a keyword (keyword means token with special meaning) which tells to compiler to declare a variable of character type. c is the name of variable. There are some rules which needs to be followed while naming a variable, you can find them in following link. Like for example variable name cannot be the keyword (Compiler will get confused !!). Variables will be stored in RAM, in some memory location.

Hence with each variable there is name, valuedata type and memory address associated.


Why variables have a name ?

Since data are stored in memory. Each data have memory address in RAM. we could have used this memory address directly. If we had written a program like this then it would have become unreadable, filled with addresses. It would be very hard to track which address has which data. Hence computer engineers have given address a name and called it variable. Hence, a variable is the name for a place in the computer's memory where you store some data.


Why variables have data type ?

For example to represent character, C uses ASCII .ASCII chart has 128 characters. Hence we can represent in 7 bits (2^7 = 128). While a integer number e.g. 3421 (represented as 3421 in decimal, D5D in hex, 1100 0101 1100 in binary) can be represented in 12 bits. Different type of data have different memory requirements, Hence Data type tells us how much amount of memory is required.
NOTE: size of char variable in not 7 bit in C and size of integer is not 12 bit in C.


Minimum addressable memory

Memory in general are byte addressable, means that we can address every byte in memory. Minimum granularity in addressing memory is 1 byte. That is why may be even though we can represent a character  in 7 bit we still have size of char as 8 bit (1 byte).

Some example


Data type (C keywords
used in declaration)
Explanation
Size
Format specifier
used in printf
char
Characters
1 Byte
%c
int
Integers (both +ve and -ve)
4 Byte
%i
unsigned int
Only +ve integers)
4 Byte
%u
float
Real numbers
4 Byte
%f
double
Real number with better range and precision 
8 Byte
%d
long
Integer with better range
8 Byte
%l

NOTE: size of data type can vary with CPU architecture, above number are for typical 32 bit machine. For detail list see the following link. Format specifiers are used in printf and scanf to specify the data type of variable.

Program printing variable contents


#include <stdio.h>

int main()
{
   char c = 'a';
   int  i = 32;
   float f = 2.3;

   printf("Character is %c\n", c);
   printf("Integer   is %i\n", i);
   printf("Float     is %f\n", f);
}

output of above program is:


Character is a
Integer   is 32
Float     is 2.300000

printf will replace %c with character that is stored in c, %i with integer stored in i and %f with floating value stored in f.

Address operator &

& is address operator, it will return the address of variable. In the following example the memory location of variable c,i and f are determined.


#include <stdio.h>
int main()
{
   char c = 'a';
   int  i = 32;
   float f = 2.3;

   printf("Address of Character is %p\n", &c);
   printf("Address of Integer   is %p\n", &i);
   printf("Address of Float     is %p\n", &f);
}

output of above program in 32 bit machine is as follows


Address of Character is 0xbffe7d57
Address of Integer   is 0xbffe7d58
Address of Float     is 0xbffe7d5c

Here %p is format specifier to print addresses.

If I draw the memory layout and where the variables are present


Address
Variables
0xbffe7d57
c
0xbffe7d58
0xbffe7d59
0xbffe7d5a
0xbffe7d5b
i
0xbffe7d5c
0xbffe7d5d
0xbffe7d5e
0xbffe7d5f
f

c takes 1 byte and it is present @ 0xbffe7d57.
i takes 4 bytes and it is present starting @ 0xbffe7d58.
f takes 4 bytes  and it is present starting @ 0xbffe7d5c.

Food for Thought: What should be size of each variable ? How they are stored in memory ?
This is answered by which data model is used in compiler.
You can find more information here.

Data range

Actual integers (whole) numbers in sense are infinite, we cannot represent all in limited 4 byte. Hence there is minimum and maximum number that can be represented by particular data type.

For integer data type -
int - represent both signed and unsigned, hence one bit in 4 byte is used to store whether it is signed or unsigned number. 4 byte = 32 bits. The MSB corresponds to the sign bit ( "1" meaning negative and "0" meaning positive), remaining 31 bits are used to represent number. Maximum number would be with all 1 in it which is 2,147,483,647(nothing but 2^ 31)For negative number 2'complement representation is used in which case minimum number would be -2,147,483,648

This is very nice article why -ve number use 2's complement representation.

Float and double are using IEEE floating point standard, which is too complected to be covered here.

Size of operator

C has a operator called sizeof, which can be used to find the size of different data types.


#include <stdio.h>
int main()
{
   char c = 'a';
   int  i = 32;
   float f = 2.3;

   printf("Size of Character is %d\n", sizeof(c));
   printf("Size of Integer   is %d\n", sizeof(i));
   printf("Size of Float     is %d\n", sizeof(f));
}

output of which is


Size of Character is 1
Size of Integer   is 4
Size of Float     is 4


Links

Quiz - No written yet !
Next Article -  C Programming #05: Constants
Previous Article - C Programming #03: First Program - Hello C
All Article - C Programming

1 comment :