A Little about the blog.....

This blog contains all the interview questions that i face during my interviews with various MNC's. I am trying to post the solutions for almost all the questions and i have covered few important topics which would be frequent topics in interviews. I hope the blog would be useful not only for the job aspirants but also to the one who tries to upgrade his/her knowledge in C and Embedded concepts......

please enter your mail address in the above mail address box to get the immediate updates of the new interview questions.....

Saturday, November 26, 2011

Compilation and Execution Process of C Programs

The compilation and execution process of C can be divided in to multiple steps:

Preprocessing - Using a Preprocessor program to convert C source code in expanded source code. "#includes" and "#defines" statements will be processed and replaced actually source codes in this step.
Compilation - Using a Compiler program to convert C expanded source to assembly source code.
Assembly - Using a Assembler program to convert assembly source code to object code.
Linking - Using a Linker program to convert object code to executable code. Multiple units of object codes are linked to together in this step.
Loading - Using a Loader program to load the executable code into CPU for execution.

Here is a simple table showing input and output of each step in the compilation and execution process:

Saturday, November 19, 2011

Difference between Declaration, Definition & Intialization of a Variable

Defining Variables:
A variable is a meaningful name of data storage location in computer memory. When using a variable you refer to memory address of computer.

Naming Variables:
The name of variable can be called identifier or variable name in a friendly way. It has to follow these rules:
The name can contain letters, digits and the underscore but the first letter has to be a letter or the underscore. Be avoided underscore as the first letter because it can be clashed with standard system variables.
Keywords cannot be used as a variable name.
Of course, the variable name should be meaningful to the programming context.

Declaring Variables:
To declare a variable you specify its name and kind of data type it can store. The variable declaration always ends with a semicolon, for example:
1.int counter;
2.char ch;

You can declare variables at any point of your program before using it. The best practice suggests that you should declare your variables closest to their first point of use so the source code is easier to maintain. In C programming language, declaring a variable is also defining a variable.

Initializing Variables:
You can also initialize a variable when you declare it, for example:
1.int x = 10;
2.char ch = 'a';

Storage of Variables:
Each variable has its own lifetime (the length of time the variable can be accessible) or storage duration. When you declare your variable you implicitly assign it a lifetime.

Let take a look at this source code example:

#include"stdio.h"
int global_variable = 10;// global variable
void func();
void main()
{
int i; // test static variable
for(i = 0; i < 5 ; i++) { func(); printf("after %d call \n",i); } } void func() { static int counter = 0;// static variable counter++; printf("func is called %d time(s)\n",counter); int local_variable = 10; } Explanations:
global_variable is a global variable. It is visible and accessible to all functions. It has static life time (static means variable is retained until the program executes). It is suggested that we should avoid using global variable because it is difficult to maintain, and we don’t know exactly the state of global variable because any functions can change it at any time of program execution.

The local_variable and i are only existed until function completed. We cannot access it anymore. In this case it is call automatic lifetimes.

In case you want a local variable has static lifetimes, you can use static keyword like counter variable in func(). It retains until program executes even when the func() completed.

extern and register keywords:
You can use extern keyword when declaring a variable or function to imply that the variable is implemented elsewhere and it will be implement later on.

register keyword is used when you want a variable which is accessed many time and required fast memory access. Be noted that, declaring a variable with register keyword acts as a directive. It means it does not guarantee the allocation of a register for storing values.

Scope of Variables:
You can define variable in a block of code which specifies by {and} braces. The variable has the scope inside block it is declared.

Thursday, November 17, 2011

A small example for Dynamic Memory allocation

#include"stdio.h"
#include"conio.h"
#include"stdlib.h"
void main()
{
int* a,n,i;
clrscr();
printf("how many numbers you want to enter");
scanf("%d",&n);
a=(int*)malloc(n*sizeof(int)); // calloc can be used as //a=(int*)calloc(n,sizeof(int));
printf("\nenter the numbers\n");
for(i=0;i {
scanf("%d",a+i);
}
printf("enter a new value for n");
scanf("%d",&n);
a=(int*)realloc(a,n*sizeof(int));
printf("the entered numbers are");
for(i=0;i {
printf("%d\n",*(a+i));
}
getch();
}

find the longest string in a given group of strings using 2-D arrays

#include"stdio.h"
#inlcude"conio.h"
#include"string.h"
void main()
{
int a[10],x=0,n,l=0,max=0;
char b[10][30],temp[30];
clrscr();
printf("enter how many persons data\n");
scanf("%d",&n);
printf("enter the number and name\n");
for(x=0;xmax)
{
max=l;
strcpy(temp,b[x]);
}
}
printf("the entered data is\n");
for(x=0;x {
printf("%d\t",a[x]);
printf("%s\n",b[x]);
}
printf("the longest string is %s",temp);
getch();
}

copying a string from one array to another array using pointers

#include"stdio.h"
#include"conio.h"
#include"string.h"
void main()
{
int i
;
char a[10],b[10];
char *p,*q;
clrscr();
printf("enter the string\t");
for(i=0;i<10;i++)
{
scanf("%c",&a[i]);
}
p=&a;
q=p;
for(i=0;i<10;i++)
{
b[i]=*q;
printf("%c",b[i]);
q++;
}
printf("\nthe values of p and q are %u,%u",p,q);
getch();
}

Wednesday, November 16, 2011

Difference between Static Memory Allocation and Dynamic Memory Allocation

In C language, there are two types of memory allocations to any variable.

generally in normal programming, we define and declare the variable with the data type and storage class of the variable and the memory is allocated to the variable. This is called static memory allocation.

But in certain cases where in we don't know about how many variables we are going to use and how much of memory is to be allocated, we opt for at the instant memory allocation during the execution of the program which is called as Dynamic Memory allocation.

for dynamically allocating memory, we make use of
malloc,calloc and realloc functions.

coming to the memory, In static memory allocation (SMA),the memory is allocated in STACK of the RAM and in Dynamic Memory Allocation, the memory is allocated in HEAP of the RAM.

what do u mean by a volatile in C langauge.

The keyword "VOLATILE" is used to define a variable so as to inform the compiler that the variable value can change at any instant of time and is not a constant through out the program.

Interview Question: How the compiler treats any volatile variable?
Explanation: A volatile variable is one whose VALUE CAN CHANGE UNEXPECTEDLY. Consequently, the compiler can make NO ASSUMPTIONS about the value of the variable. In particular,the optimizer must be careful to RELOAD the variable every time it is used instead of holding a copy in a register.

Examples of volatile variables are:

(a) Hardware registers in peripherals (e.g., status registers)
(b) Non-stack variables referenced within an interrupt service routine.
(c) Variables shared by multiple tasks in a multi-threaded application.

difference between constant and static

we use the key words "CONSTANT" and "STATIC" while declaring the variables.

lets get in detail:

when we want the scope of the variable to be valid through out the program we declare the variable as static. to be more clear, the changes made to a static variable stands valid even when we move from one function to other function in the same program.

for example if we declare static int x=10 in main function and we incremented x and jumped to some other function and if we print x there, the value will be printed as 11.

coming to the CONSTANT, when we want the value of the variable to be constant all the time and should not be changed through out the program, we declare it as a constant.

hope u have got a clear idea by now. don't get confused with these two keywords as they look alike in sense.

difference between structures and unions

A structure is a group of variables of different data types whereas even an union is a group of variables of different data types.

what makes the union different from structure is the memory allocated for each variable in an union is the space required for the largest member in the union.
where as in the case of a structure the memory is allocated differently for different data type variables.

more ever,in the case of a union,a union can accommodate only one variable at a time, when a new variable enters, it automatically erases the old variable and new variable is stored in the place of old variable.


Explanation:
A union, is a collection of variables of different types, just like a structure. However, with unions, you can only store information in one field at any one time.

You can picture a union as like a chunk of memory that is used to store variables of different types. Once a new value is assigned to a field, the existing data is wiped over with the new data.

A union can also be viewed as a variable type that can contain many different variables (like a structure), but only actually holds one of them at a time (not like a structure). This can save memory if you have a group of data where only one of the types is used at a time. The size of a union is equal to the size of it's largest data member. In other words, the C compiler allocates just enough space for the largest member. This is because only one member can be used at a time, so the size of the largest, is the most you will need.

print a semicolon using Cprogram without using a semicolon any where in the C code in ur program!!

#include"stdio.h"
#include"conio.h"
int x=59; // ascii value for semicolon
void main()
{
int x;
clrscr();
printf("enter the ascii value");
scanf("%d",&x);
printf("the charcter for ascii value %d: is %c",x,x);
getch();
}

Tuesday, November 15, 2011

swapping of two numbers without using a temporary variable

#include"stdio.h"
#include"conio.h"
void main()
{
int x=10,y=20;
clrscr();
x=x+y;
y=x-y;
x=x-y;
printf("%d %d",x,y);
getch();
}

finding the size of a variable without using the "sizeof()" operator.

int main()
{
double a = 5;
int b,c;
void *p = &a;
void *q = &a + 1;
b=(int)p;
c=(int)q;
printf("%d",c-b);
return 0;
}

Thursday, November 10, 2011

Different types of Pointers

The concept of pointers is the Beauty behind the use of C language. We know that, in general pointer refer to some address location in the memory.

There are different types of Pointers in C language that any C aspirant is ought to know about. let's have a brief glance about them.

1. pointer depending upon the type of the variable it is referring to.
ex: integer pointer, Character pointer, float pointer etc;

2. Dangling Pointer.
explanation: It is the pointer to a memory location where is varibale is stored
earlier but not present now ( destroyed or cleared or freed by the program).simply OBJECT DESTROYED BUT PATH PRESENT.

3. NULL Pointer.
explanation: To avoid the errors caused by the dangling pointer, the concept of a NULL POINTER is introduced. Whenever the NULL POINTER is dereferenced/freed, there is no scope for data corruption.
ex: #DEFINE NULL 0 //defining a null value
int *a; // defining a integer pointer
free(a); // the value at a is destroyed, hence a is a Dangling pointer
a=NULL; // now 'a' is a safe pointer

4. VOID Pointer.
explanation: also called as generic pointer. a void pointer can be of any type.
ex: *(int)p, *(float)p, *(char)p

5. CONSTANT POINTER.
explanation: the constant pointer's value cannot be changed once it is declared as CONSTANT.
ex: const int* const p= &a;

6. FUNCTION POINTER.

explanation: To store the address of a function,we need a function pointer.
ex: void main()
{void(*fp)();
fp= calling function name;
fp();
}

Wednesday, November 9, 2011

Things to Remember : Storage classes in C

* SCOPE OF A VARIABLE: A variable defined in a block can be accessed in that block and all its internal blocks only (In default,a variable defined in block cannot be accessed by any other external blocks). It is called as the scope of a variable.

* To make the variable valid and accessible through out our operations, we define the variable with different storage classes as required per our needs.

* In default storage class (AUTO), the variable defined is valid as long as the control is in the block where it is defined. when the control comes out of the loop, the variable dies and the memory allocated is unreserved.

* For STATIC & GLOBAL variables, the memory is allocated as soon as the program gets loaded into the memory (before the execution of main() function) and hence these variables are also called as LOAD TIME VARIABLES. The memory for these variable gets de-allocated just before the termination of the program from the memory.

LIFETIME of a VARIABLE: The period for which after a variable is defined and allocated with memory and the memory allocated is deallocated is called the lifetime of the variable.

Note: The lifetime of a declared element is the period of time during which it is available for use. Variables are the only elements that have lifetime; for this purpose, the compiler treats procedure arguments and function returns as special cases of variables. The lifetime of a variable represents the period of time during which it can hold a value. Its value can change over its lifetime, but it always holds some value.

Storage classes in C: tutorial by Trinadh

Generally in C language, when we are defining a variable we do mention its data type, but not the storage class.

but by default, for every variable declared, some storage class in assumed by the compiler.To fully define any variable, we need to mention its data type and its storage class.

There are 4 data types of storage classes in C language:

1. Auto (automatic: default storage class)
2. Static
3. Register
4. Extern

The storage class gives the following information about the variable

* Default Initial Value of the Variable as soon as it is defined.
* The location of the variable where it is created and stored.
* The scope of the variable till how far the variable is accessible.
* The life of variable till how long the memory created for variable is reserved.

The following table gives the detail information about various storage classes.