Saturday, September 13, 2014

variable initialisation in c programming!

 what happens exactly if one typed as code below?

main()
{
  int a=10;
}

Here the compiler starts begins compiling from main() function.(Every C program begins with main() function )
when a compiler reached third step i.e., int a=10; it allocates 2 bytes of memory in main memory whose name is a and a contains the values 10. What exactly it doing is we can compare it like this-

   suppose a person needs a house.So he needs a space(here 2 bytes) and he needs a name of house(here a) and he stays in house(here 10).

what happens if one typed code as given below?

main()
{
  int a,b=5;
  float c=10.5;
 }

Here the compiler starts begins compiling from main() function and allocates two bytes of memory for a whose value is 10. It allocates four bytes of memory for  c.
But for b it allocates two bytes of memory and we have not given any value to c.
(NOTE: c contains garbage value we discuss later in storage classes about garbage value)




important points to remember:

                                    numbers are divided into two types in c 
                                     integer numbers and fraction numbers. 

           integer numbers are stored in memory using int datatype and c compiler  allocates two bytes of memory.
           fraction numbers are stored in memory as float data type and c compiler allocates four bytes of memory.

  Data types and memory sizes in C.

    datatype name                              size                                    example
     int                                                2bytes                                -123,   0,  345,
    float                                              4 bytes                               -4.5 , 0 , 3.76
   char                                                1 byte                                a, b, c        

int a=10;   /* here a is integer variable whose value is 10*/
float b=4.5;  /* here b is a float variable whose value is 45*/
 char c='a';   /* here c is character variable whose values is a*/
        (note: character variable must be enclosed in single quotes)


If any doubts query us or comment us.



(c programming, c programming variable intialisation, datatypes in c, integer data types in c, float data types in c,int,float,char,data range, decision making, looping, entry control looops, exit control loops, for,while,do while , if else , nested if , switch, functions, recursive functions, nested loops, tower of hanoi, pointers, strcutres , unions , enum, files in c, binar files, random access in files, fseek,ftell, rewind, copy of one file to other file, merging, continue, break, goto, realtional operators, bitwise operators, logical operators, shift operators procedural languages.





















No comments:

Post a Comment