C Interview Questions
C Interview Questions
C PROGRAMMING QUESTIONS
Pre-processing:
Compiler
Compiler translates source program into machine language program ie. 0’s and 1’s. A program
must be compiled before it is executed.
Press “Alt + F9” to Compile the program.
Press “F9” to Compile and Link.
Press “Ctrl + F9” to Compile, Link and Execute.
Note: We must recompile every time we make changes in the program. If not recompiled,
Compiled version is executed.
Linker
Linker when called, function address is substituted in the calling function. This is know as
Linking. This is done by the Linker.
Thus calling function and called function are linked. Eg:f1( ) ;
Function call f1 is replaced by the address of f1 function.. C.P.U branches to the address and
executes the corresponding function.
Loader
Loader loads executable program into main memory of the computer ie. RAM.
The address at which program is loaded is informed to C.P.U. Thus program is transferred from
hard disk into main memory.
C.P.U
It is the operating system which runs the program with the help of C.P.U.
Source program
It contains “c” program with “#define” and “#include”. Extension is “ .c ”.
Executable program
It is same as machine language program. Function calls are linked here. It is ready to execute
extension ie exe. This is the O/P of linker.
Text segment:
When we compile any program, it creates an executable file like a.out, .exe, etc., that gets
stored in the text or code section of the RAM memory.
Data Segment:
The data used in our program will be stored in the data section
It divide into 2 types.
Data
Segment
Variables declared inside the main() function are stored in the stack.
Variables declared outside the main() function are stored in the Data Segment.
.bss stores global, extern variables .If not initialised by default it take zero.
Example:
#include<stdio.h>
UNINITIALIZED
int var1;
int var2 = 10; Data Segment GLOBAL VARIABLE
void function1() INITIALIZED
{
printf("I am function1");
}
int main()
{
int num=10; LOCAL VARIABLE
function1();
return 0;
}
Stack:
Variables declared inside the main() (or any other function) function are stored in the stack.
Because all the variables are defined in the function, and the size of the variables is also
defined at the compile time.
Whenever the function is called, a new stack frame is created.
Heap:
Example:
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
} book;
Dot Operator
The processor does not read 1 byte at a time. It reads 1 word at a time.
32-bit processor 1 word = 4 bytes
64 bit processor 1 word = 8 bytes
Example 1(32-bit processor):
struct student
{
char a; // 1 byte
char b; // 1 byte
int c; // 4 bytes
}
(i.e) 1+1+2+4=8 bytes
Example 2:
struct student
{
char a; // 1 byte
int b; // 4 byte
char c; // 1 bytes
} (i.e) 1+3 + 4 + 1+3=12 bytes
15.Define Function.
Functions are self contained modules of code that accomplish a specific task.
16.Mention the types of function
1) NULL pointer
2) Void pointer
3) Dangling pointer
4) Zero reference pointer
5) Wild pointer.
Literal meaning of NULL pointer is a pointer which is pointing to nothing. NULL pointer points the
base address of segment.
int *ptr=NULL;
1. Suppose we have to declare integer, character, float pointer then we need to declare 3 pointer
variables.
2. Instead of declaring different types of pointer variable it is feasible to declare single pointer
variable which can act as a integer pointer, character pointer.
Example:
void *ptr; // ptr is declared as Void pointer
char cnum;
int inum;
float fnum;
3)ZERO REFERENCE POINTER: Zero reference pointer is a pointer which is pointing to nothing
(that is zero)
Examples:
int *ptr=0;
4)DANGLING POINTER:
A pointer pointing to a memory location that has been deleted (or freed) is called dangling
pointer.
int *ptr = malloc(sizeof(int));
free(ptr);
//From here if ptr is accessed it is a dangling pointer.
Ideally ptr should be set to NULL after free()
ptr = NULL;
5)WILD POINTER
Uninitialized pointer is called Wild pointer.
int main()
{
int *p; /* wild pointer */
int x = 10;
// p is not a wild pointer now
p = &x;
return 0;
}
Example:
#include <stdio.h>
int main () {
int var;
int *ptr;
int **pptr;
var = 3000;
/* take the address of var */
ptr = &var;
/* take the address of ptr using address of operator & */
pptr = &ptr;
/* take the value using pptr */
printf("Value of var = %d\n", var );
printf("Value available at *ptr = %d\n", *ptr );
printf("Value available at **pptr = %d\n", **pptr);
return 0;
}
Output:
Value of var = 3000
Value available at *ptr = 3000
Value available at **pptr = 3000
Array of pointer:
“Array of pointers” is an array of the pointer variables. It is also known as pointer arrays.
Syntax:
int *var_name[array_size];
Declaration of an array of pointers:
int *ptr[3];
We can make separate pointer variables which can point to the different values or we can make one
integer array of pointers that can point to all the values.
// Example of array of pointers.
#include <stdio.h>
const int SIZE = 3;
void main()
{
int arr[] = { 1, 2, 3 };
int i, *ptr[SIZE];
for (i = 0; i < SIZE; i++) {
// assigning the address of integer.
ptr[i] = &arr[i];
}
// printing values using pointer
for (i = 0; i < SIZE; i++) {
printf("Value of arr[%d] = %d\n", i, *ptr[i]);
}
}
Output:
Value of arr[0] = 1
Value of arr[1] = 2
Value of arr[2] = 3
1 A copy of the parameters to the function are Address of the parameters are passed to the
passed into the function function
2 Changes made inside the function is limited to Changes made inside the function reflect outside
the function only. The values of the actual of the function also. The values of the actual
parameters do not change by changing the formal parameters do change by changing the formal
parameters. parameters
3 Actual and formal arguments are created at the Actual and formal arguments are created at the
different memory location same memory location
4 Example: Example:
#include <stdio.h> #include <stdio.h>
void swap(int , int ); void swap(int *, int *);
int main() int main()
{ {
int a = 10; int a = 10;
int b = 20; int b = 20;
printf("Before swap a=%d,b= %d\n",a,b); printf("Before swap a = %d, b = %d\n",a,b);
swap(a,b); swap(&a,&b);
printf("After swap a = %d, b = %d\n",a,b); printf("After swap a = %d, b = %d\n",a,b);
} }
void swap (int a, int b) void swap (int *a, int *b)
{ {
int temp; int temp;
temp = a; temp = *a;
a=b; *a=*b;
b=temp; *b=temp;
} }
Output: Output:
Before swap a=10,b=20 Before swap a=10,b=20
After swap a=10,b=20 After swap a=20,b=10
Memory leak occurs when programmers create a memory in heap and forget to delete it.
Memory leaks are particularly serious issues for programs like daemons and servers which by
definition never terminate.
Example:
/* Function with memory leak */
#include <stdlib.h>
void f()
{
int *ptr = (int *) malloc(sizeof(int));
/* Do some work */
return; /* Return without freeing ptr*/
}
To avoid memory leaks, memory allocated on heap should always be freed when no longer
needed
/* Function without memory leak */
#include <stdlib.h>;
void f()
{
int *ptr = (int *) malloc(sizeof(int));
/* Do some work */
free(ptr);
return;
}
31.What is macro? Rules for it.
Defining a Macro
A “macro” can be a value, expression or a group of statements. A “macro” is defined by
“#define”, where “#” is Preprocessor Directive and #define is preprocessor compiler directive and not
a statement. #define instruction defines value to a symbolic constant for use in the program. When ever
a symbolic name is encountered, the compiler substitutes the value associated with the name
automatically. #define lines should not end with a semicolon.
Syntax:
1. #define identifier string
Wherever “identifier” is encountered, “string” is substituted.
(or)
2. #define macro name body
Wherever “macroname” is encountered, “body” is substituted
RULES:
1. No blank space between the pound sign (#) and the word “ define ”.
2. #define must not end with a semicolon.
3. Equal to signs (=) must not be used.
4. #define is not used for declaring variables like “ typedef ”.
5. #define statements can appear anywhere in the program.