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.