Showing posts with label C Basics. Show all posts
Showing posts with label C Basics. Show all posts

Friday, February 25, 2011

Data types & Type modifiers


Data types: int,float,char,double & void
The size and range of these data types may vary among processor types and compilers.


int: Size of int depends on length of a word of execution environment.In General int is 2 bytes for 16bit compiler.Int is used to store integer values.
eg: int a = 35 , int b.
If  b value is not specified, a memory of 2 bytes will allocated containing junk values

Float & Double: Float is used to store floating point values.The range of float and double will depend upon the method used to represent the floating point-point numbers.
eg: float b=35.6

Char:Char can be better called as byte for better understanding . As we know machine understands only binary values (0's and 1's) & we cant store character as it is in memory.
Eg: char a='A'


Now you may be wondering A is stored as 65(ASCII value of A) how will we able to get character when we give printf.This is were compiler comes into picture.Compiler does this job of converting ASCII to character.

Void:The type void either explicitly declares a function as returning no value or creates generic pointers.

Table below shows all data types defined by C standard

Type Modifier: A type modifier changes meaning of basic data type to fit specific needs.
Type modifiers are: Signed ,Unsigned,Long,Short

Signed: If an integer is specified as integer than compiler assumes high-order bit of an integer to used as sign flag.is sign flag is 0, the number is positive & if its 1,the number is negative .



Negative numbers are represented using the two's complement approach, which reverses all bits in the number (except the sign flag), adds 1 to this number, and sets the sign flag to 1.
Eg:32,776 in binary
0 1 1 1 1 1 1 1  1 1 1 1 1 1 1 0

Variables




Variables: For better understanding lets remember variable as any container.
Container is a device which holds things of different sizes .Similarly variable holds data of different sizes.To be clear on bits and byte used in storage of data see fig given above.General form of variable declaration is
 type variable_list;


Variable are declared in following 3 places:
Local variables(inside functions)
Formal parameters(in the definition of function parameters)
Global variables(outside of all functions)


Local variables:
Variables that are declared inside a function are called local variables.Local variables can be used only by statements that are inside the block in which the variables are declared. Local variables are not known outside their own code block.A block of code begins with an opening curly brace and terminates with a closing curly brace Each time a function is called a place is allocated in stack for that function with a return value after function is executed.Hence variable declared inside function has local scope.
 As you can see in example a stack is allocated to function 'sum' when its called from main. Variables used in sum i.e x,y,z is placed in stack of sum.After execution , z value is stored in location allocated for return value in stack.Hence z is returned and stored in c of stack allocated for function main. This why local variables cant be accessed from other functions.


Formal parameters:

If a function has input parameters/arguments, it must declare variables that will accept the values of the arguments. These variables are called the formal parameters of that function. They behave local variables inside the function.
Eg: In example given below 'a' is formal parameter of function hello
void hello(char a)
{
   int b=0;
   while(b> 5)
     { 
        b-- ;
        printf("%c",a);
      }
}


Note:Since formal parameters are like local variables, they are also dynamic and are destroyed upon exit from the function.


Global variables:

Global variables can be used by any piece of code.They will hold their value throughout the program's execution. You create global variables by declaring them outside of any function. Any expression can access them, regardless of its position in main program.
Eg: 'b' is a global variable 
#include<stdio.h>
int b=0;
main()
{
   while(b> 5)
     { 
        b-- ;
        printf("%d",b);
      }
}

Data segment in C



Data segment in C is broadly classified in 4 sections: ".txt" , ".rodata" , ".bss" , ".data"

.rodata:The .rodata is also used for storing string constants and read only data's.
.data:The compiler places initialized and uninitialized global variables in the .data section.
.bss: A separate section called .bss is used for uninitialized variables or temporary variable in stack
.txt: Opcode i.e executable code is stored in this section.


Note:Flash content cannot be modified, it can only be erased and re-written hence code and constants are stored in flash while variables are stored in RAM.

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Powered by Blogger | Printable Coupons
Design Downloaded from Free Blogger Templates | Free Website Templates