Preprocessor
What is preprocessor command?
A preprocessor is a text substitution tool and it tells the compiler to do required pre-processing before the actual compilation.
All preprocessor commands begin with a hash symbol (#).
What is header file?
A header file is a file with extension .h which contains C function declarations and macro definitions to be shared between several source files.
What is the default header file that comes with the C compiler?
stdio.h
What is the difference between #include<filename> and #include "filename"?
#include<filename>: It searches directories pre-designated by the compiler.
#include "filename": It searches in the same directory as the file containing the directive.
main() function
Define main() function?
main() is a compiler-declared function defined by the user, which is invoked automatically by the operating system when the program is executed.
What is the default prototype of main() function?
Default return of a main() function is int and default parameter is void.
int main(void){
//body
return 0;
}
What are the arguments of main() function?
int main(int argc, char *argv[], char * env[])
A main() function has 3 arguments:
- argc (Argument Count) - Stores the number of command-line arguments passed by the user including the name of the program.
- argv (Argument Vector) - An array of strings which stores values passed from the command line.
- env (Environment Vector) - Contains all environmental variables of the system.
What is the use of return statement in main() function?
A return statement in the main() function tells the compiler about the type of termination. A return value of 0 means normal termination, and a non-zero value means abnormal termination.
Is it possible to execute user defined functions before and after main() function in C?
#include<stdio.h>
void before_main();
void after_main();
#pragma startup before_main
#pragma exit after_main
void before_main(){ printf("Before main() function.\n"); }
void after_main(){ printf("After main() function.\n"); }
int main(){ printf("Inside main() function.\n"); return 0; }
Note: The above code executes in Turbo C compiler, but not in the GCC compiler.
Storage Classes
What is Storage Class?
In C programming, storage classes are used to define the storage area, lifetime, and visibility of a variable.
Storage Classes (Continued)
How many types of storage classes?
In C, there are four types of storage classes:
- auto: Stored in stack area, accessed within declared block, initial value is garbage value.
- extern: Stored in data segment, accessed throughout program, initial value is zero.
- static: Stored in data segment, accessed within declared block, initial value is zero.
- register: Stored in CPU register, accessed within declared block, initial value is garbage value.
Data Types
Difference between %f, %e and %g?
%f: Represents floating-point data, takes up to 6 decimal points.
%e: Represents data in scientific format or exponential power.
%g: Represents data like %f and %e but removes trailing zeros.
Difference between type conversion and type casting?
- Type Conversion: Data is converted from one type to another automatically by the compiler at compile time.
- Type Casting: Data is converted from one type to another explicitly by the programmer using casting operator
(type).
What is an empty datatype?
void
Control Statements
What is the default conditional value in for loop?
true (non-zero)
What is entry controlled and exit controlled loops?
- Entry Controlled Loop: Checks condition before entering the loop (e.g.,
for,while). - Exit Controlled Loop: Executes body first and checks condition afterwards (e.g.,
do-while).
Difference between break and continue?
break: Used to completely terminate a loop or switch block.
continue: Used to skip the current iteration and jump to the next iteration.
Arrays
Define an Array?
An Array is a collection of similar types of elements stored in sequential/contiguous memory locations.
What is meant by base address of an Array?
The memory address of the first element in the array is known as the Base address.
Assume ary is an array variable containing 5 elements. What is the difference between ary[1] and *(ary+1)?
No difference. Both ary[1] and *(ary+1) access the element present at the 1st index position.
Pointers
Define Pointer?
A pointer is a variable which stores the address (memory location) of another variable.
What is pointer to pointer?
A pointer to pointer means a pointer that stores the address of another pointer variable.
Define void pointer?
A generic pointer that can store the address of any type of variable is called a void pointer.
What is wild pointer?
An uninitialized pointer is known as a wild pointer.
What is dangling pointer?
A pointer that points to a memory location that has already been freed or deleted is known as a dangling pointer.
What is NULL pointer?
A pointer that doesn't point to any memory location and is initialized with NULL is known as a NULL pointer.
What is the size of pointer?
In a 64-bit compiler, the size is 8 bytes. In a 32-bit compiler, the size is 4 bytes.
Note: Size depends on the operating system architecture, not the data type.
What are the advantages of pointers?
- Allows direct manipulation of memory locations.
- Enables dynamic memory allocation and deallocation.
Structures and Unions
Define Structure?
A structure is a collection of elements of different datatypes grouped together. Each element is called a member.
Define Union?
A union is a collection of elements of different datatypes stored in the same shared memory location.
Structures and Unions (Continued)
Difference between structures and unions?
| Structures | Unions |
|---|---|
struct keyword is used. |
union keyword is used. |
| Size is the sum of sizes of individual data members. | Size is the size of the largest data member. |
| Every data member has its own distinct memory location. | All members share the same memory location (only one value active at a time). |
Define self-referential structure?
A self-referential structure is a structure that contains a pointer to an instance of the same structure.
Functions
Define function?
A function contains a set of statements enclosed in curly braces {} to perform a specific task.
What is meant by prototype of function?
Prototype of a function is its declaration specifying its return type, name, and parameters.
What is the default return type of user-defined function?
int
In how many ways can we call a function?
Two ways: Call by Value and Call by Reference.
Difference between actual parameters and formal parameters?
Actual parameters: The values passed when a function is called.
Formal parameters: The variables declared at the function definition to store those values.
Define Recursion?
A function calling itself directly or indirectly is known as recursion.