Best Industrial Training in C,C++,PHP,Dot Net,Java in Jalandhar

Friday, 21 March 2014

C Questions

Ques 1 : What is meant by lifetime and scope of variable and explain storage classes with example ?
Ans: 
The scope and lifetime of the variables define in C is not same when compared to other languages. The scope and lifetime depends on the storage class of the variable in c language the variables can be any one of the four storage classes:

1. Automatic variable
2. External variable .
3. Static variable .
4. Register variable.

The scope actually determines over which part or parts of the program the variable is available. The lifetime of the variable retains a given value. During the execution of the program. Variables can also be categorized as local or global. Local variables are the variables that are declared within that function and are accessible to all the functions in a program and they can be declared within a function or outside the function also.

Storage Classes :
Every C variable has a storage class and a scope.The storage class determines the part of memory where storage is allocated for an object and how long the storage allocation continues to exist. It also determines the scope which specifies the part of the program over which a variable name is visible, i.e. the variable is accessible by name.

Syntax :
storage_class data_type variable_name;

There are 4 types of storage class:
1. automatic 2. external 3. static 4. register.
Automatic storage class
Keyword for automatic variable auto.

Variables declared inside the function body are automatic by default. These variable are also known as local variables as they are local to the function and doesn't have meaning outside that function Since, variable inside a function is automatic by default, keyword auto are rarely used.Keyword for automatic variable auto Variables declared inside the function body are automatic by default. These variable are also known  as local variables as they are local to the function and doesn't have meaning outside that function Since, variable inside a function is automatic by default, keyword auto are rarely used.

 
 
For example : 
#include<stdio.h>
void main( )
{
auto int a;
printf("%d",a);
getch( );
}

OUTPUT:
1476
 
External storage class  :
External variable can be accessed by any function. They are also known as global variables. Variables declared outside every function are externalvariables. In case of large program, containing more than one file, if the global variable is declared in file 1 and that variable is used in file 2 then, compiler will show error. To solve this problem, keyword extern is used in file 2 to indicate that, the variable specified is global variable and declared in another file.
For example :

#include<stdio.h>
int a;
void main()
{
extern int b;
 printf(“%d %d”,a,b);
 }
int b=10;
 
 Output:
 0 10
 
 
 
Register Storage Class :
Keyword to declare register variable register.
 
Register variables are similar to automatic variable and exists inside that particular function only. If the compiler encounters register variable, it triesto store variable in microprocessor's register rather than memory. Value stored in register are much faster than that of memory. In case of larger program, variables that are used in loops and function parameters are declared registervariables. Since, there are limited number of register in processor and if it couldn't store the variable in register, it will automatically store it in memory.
For example:
#include<stdio.h>
 void main()
{
 register int a;
printf(“%d”,a);
 }
 
 Output:
 
 4587
Static Storage Class :

The value of static variable persists until the end of the program. A variable can be declared static using keyword static.
For example:

#include<stdio.h>
void main()
{
auto int a;
 printf(“%d”,a);
 }
 
 
Ques 2: Write the difference between automatic and external vaiable in storage classes in c with the help of example.
Ans:
            AUTOMATIC VARIABLE
                 EXTERNAL VARIABLE
1.      A variable can be declared using keyword
‘auto’.
 
2.      Variable is stored in memory.
 
3.      Default value is garbage value.
 
4.      Scope is local to the block.
 
5.      Life is, with in the block in which the variable is defined.
 
6.      For  example :
 
auto int a;
 
7.    PROGRAM:
#include<stdio.h>
      void main()
      {
     auto int i=10;
     printf(“%d”,i);
     }
   Output:
    10

1.      A variable can be declared using keyword ‘extern’.
 
2.      Variable is also stored in memory in 
External variable.
3.      Default value is zero.
 
4.      Scope is also local to the block.
 
5.      Life is,as long as the program execution doesn’t come to an end.
 
6.      For example :
 
extern int a;
 
7.      PROGRAM:

#include<stdio.h>
 int a;
 void main()
{
extern int b;
 printf(“%d %d”,a,b);
 }
int b=10;
 
Output:
 
0 10
 
 
 
 
 
 

No comments:

Post a Comment