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

Sunday, 23 March 2014

C Questions with Answers









Friday, 21 March 2014

C Questions with answers





The indirection operator can be used in a pointer to a pointer to an integer, a single-dimensional array of pointers to integers, a pointer to a char, and a pointer to an unknown type.

 The indirection operator is also known as the
“ dereference operator  ” .


The following C statements illustrate the usage of the indirection operator:
int  a = 1,  b;          // line 1
int *ptr = &a;      // line 2
b = *ptr;              // line 3
*)  In the line1 , a and b are integer variables and a is assigned a 
       value of 1.
*)  In line 2, the address of a is stored in the integer pointer ptr
       (line 2).
*)  The indirection operator is used in line 3 to assign the value
       the address pointed to by ptr to the integer variable b. 


void main()
  {
  int n=5;

  int *pn=&n;

  int **ppn=&pn;
 
  printf("Value of n = %u",n);
  printf("Indirect value = %u",*pn);
  printf(“Double indirect value = %u",**ppn);
  printf(“Address of n = %u",pn);
  printf(“Address of n via indirection = %u",*ppn);
  getch();
  }



  void main()
  {
   int a=1, b;
   int *ptr=&a;
   b=*ptr;
   printf("Value in a =  %u ",a);
   printf("Value in b = %u ",b);
   printf("Value in ptr = %u ",ptr);
   printf("Value in *ptr = %u ",*ptr);
   getch();
   }



Unformatted data files in c

Some applications involve the use of data files to store block of data, where each block consists of a fixed number of contiguous bytes.

Each block will generally represent a complex data structure, such as a structure or an array.

For example, a data file may consist of multiple structures having the same composition, or it may contain multiple arrays of the same type and size.

For such applicators it may be desirable to read the entire block from the data, or write the entire block to the data file, rather than reading or writing the individual components (i.e., structure members of array elements) within each block separately.
The library functions fread and fwrite are intended to be used in situations of this type.

There functions are often referred to as unformatted read and write functions. Similarly, data files of this type are often referred to as unformatted data file.

Each of these functions requires four arguments: a pointer to the data block, the size of the data block, the number of data blocks being transferred, and the stream pointer.

Thus, a typical fwrite function might be written as:
fwrite(&customer, sizeof(record), 1, fpt);

Where customer is a structure variable of type record, and fpt is the stream pointer associated with a data file that has been opened for output.



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
 
 
 
 
 
 

Wednesday, 19 March 2014

Last Year Questions on C




Important Questions on C Language(Last Year Questions with Answers)



Question :     What are pointers ? What are the uses of pointers in c ? How  a pointer variable is different from ordinary variable ? how pointer is useful in declaring 2D array. Explain with suitable example.

Answer :Pointer is a user defined data type which creates special types of variables which can hold the address of primitive data type like  charintfloatdouble or user defined data type like function, pointer etc. or derived data type like array, structure, union,enum. A pointer is a variable that contains the address of another variable in memory.
A developer has to very carefully make use of pointers in order to avoid some problems that can be nightmare to debug.
Example :
In the above declaration
int * ptr;
int a=5;
ptr=&a;

Declaration :
Syntax :   <pointer_type>*<pointer_name>

1)    Pointer type : It specifies the type of pointer. It can be int,float,etc. thus the type specifies the type of variable whose address the pointer can store.
2)    Pointer name : It can be any name specified by the user.

EG. Char*ptr;
Int*ptr;
 Float*ptr;
In the above declaration char, int , float  signifies  the pointer type and ptr is the name of the pointer while the asterisk  (*) means it is a pointer variable.

Initialization :
Pointers are initialized in the following way :
1.    <pointer_declaration>=<address of variable>
2.    <pointer_declaration><name of variable>=<address of variable>
EG.     Char ch=’c’;
            Char*ptr=&ch;
#include<stdio.h>
int main()
{
int x=25;
int *ptr=&x; //statement one
int **temp=&ptr; //statement two
printf(“%d%d%d”.x.*ptr,**temp);
 return 0;
}

Output: 25 25 25
Oridinary variable different from pointer variable :
Pointers store the addresses of other variables as well as other pointers(pointer to a pointer). On the other hand, ordinary variablessimply store data values. A pointer is differenced using the * operatorto refer to its data value. For example:
 int a; // this is an ordinaryvariable
 int *p = &a; // this is a pointer to the integer variable.
a and the & symbol inthe above example is the address-of an operator. It returns the address of a variable a.

Pointer useful in declaring 2D array

A 2D array is viewed as an array of 1D arrays. That is, each row in a 2D array is a 1D array. Therefore given a 2D array A,
 int A[m][n]
 we can think of A[0] as the address of row 0, A[1] as address of row 1 etc..
Hence to find, say A[0][2] we do the following :

A[0][2] = *(A[0] + 2)

In general, A[i][j] = *(A[i] + j)
The syntax of writing 2-D array as 1-D array of pointers-
Data_type*arrayname(pointer name)[size];

Here data type refers to data type of original name whose name is array name and size is +ve integer value thet indicates the max. elements associated with that array.

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
int (*a)[4];
clrscr();
printf("ennter elements of array:");
for(i=0;i<3;i++)
{
 for(j=0;j<3;j++)
 {
   scanf("%d",(*(a+i)+j));
 }
}
printf("output is:\n");

for(i=0;i<3;i++)
{
 for(j=0;j<3;j++)
 {
   printf("%d\t",(*(*(a+i)+j)));
 }
 printf("\n");
}
getch();
}

QUESTION 2 :DIFFERENCE B/W STRUCTURE AND UNION IN C? HOW ARE THEY REPRESENTED IN C?

Structure

Union

1.The keyword  struct is used to define a structure
1. The keyword union is used to define a union.
2.syntax
struct struct_name
{
    structure element 1;
    structure element 2;
 ----------
 ----------
    structure element n;
}struct_var_nm;
2.syntax
union union_name
{
    union element 1;
    union element 2;
 ----------
 ----------
    union element n;
}union_var_nm;
2. When a variable is associated with a structure, the compiler allocates the memory for each member. The size of structure is greater than or equal to the sum of  sizes of its members. The smaller members may end with unused slack bytes.
2. When a variable is associated with a union, the  compiler allocates the  memory by considering the size of the largest memory. So, size of union is equal to the size of largest member.
3. Each member within a structure is assigned unique storage area of location.
3. Memory allocated is shared by individual members of union.
4. The address of each member will be in ascending order This indicates that memory for each member will start at different offset values.
4. The address is same for all the members of a union. This indicates that every member begins at the same offset value.
5 Altering the value of a member will not affect other members of the structure.
5. Altering the value of any of the member will alter other member values.
6. Individual member can be accessed at a time
6. Only one member can be accessed at a time.
7. Several members of a structure can initialize at once.
7. Only the first member of a union can be initialized.
 
 
9.Example:
struct item_mst
{
    int rno;
    char nm[50];
}it;
9.Example:
union item_mst
{
    int rno;
    char nm[50];
}it;

Representation of union in c:

union [union tag]
{
   member definition1;
   member definition2;
   member definition3;
} [one or more union variables]; 

Example :
#include <stdio.h>
#include <string.h>
 union Data
{
   int i;
   float f;
   char  str[20];
};
 int main( )
{
  union Data data;       
 printf( "Memory size occupied by data : %d\n", sizeof(data));
return 0;
}

Representation of structure in c :

struct Point
 {
 int x;
   int y;
};

Example :

#include<stdio.h>
#include<conio.h>
Struct person
{
Char name[20];
Int day;
%
Int year;
};
Main()
{
Struct person p1, p2;
Clrscr();
Printf(“\nenter values\n\n”);
Scanf(“%s%d%d%d”,p1.name,&p1.day,&p2.manth,&p1.year);
Scanf(“%s%d%d%d”,p2.day,&p2.day,&p2.month,&p2.year);
Printf(“\nvalues you entered \n”);
Printf(“\n%s : %d %d\n”,p1.name,p1.month,p1.year);
Printf(“\n%s : %d %d”,p2.name.p2.day,p2.month,p2.year);
Getch();
}