This is Part-B of commonly asked C programming language interview questions along with their answers.
Table of Contents
Explain the static keyword in C
static
is used to define variables and functions that have a local scope and retain their values between function calls.
void increment() {
static int count = 0; // count retains its value between function calls
count++;
printf("Count: %d\n", count);
}
int main() {
increment(); // Output: Count: 1
increment(); // Output: Count: 2
increment(); // Output: Count: 3
return 0;
}
What are the bitwise operators in C?
Bitwise operators perform operations at the bit level.
The common bitwise operators are: –
- & (AND),
- | (OR),
- ^ (XOR),
- ~ (NOT),
- << (Left Shift), and
- >> (Right Shift).
unsigned int x = 5; // Binary: 0000 0101
unsigned int y = 3; // Binary: 0000 0011
unsigned int result_and = x & y; // Bitwise AND: 0000 0001 (Decimal 1)
unsigned int result_or = x | y; // Bitwise OR: 0000 0111 (Decimal 7)
unsigned int result_xor = x ^ y; // Bitwise XOR: 0000 0110 (Decimal 6)
unsigned int result_not_x = ~x; // Bitwise NOT: 1111 1010 (Decimal 250)
unsigned int result_left_shift = x << 2; // Left Shift by 2: 0001 0100 (Decimal 20)
unsigned int result_right_shift = x >> 1; // Right Shift by 1: 0000 0010 (Decimal 2)
What is the volatile keyword used for?
volatile is used to indicate that a variable’s value may change at any time, even without any action being taken by the code nearby. It prevents the compiler from optimizing access to the variable.
volatile int sensor_value; // Sensor value that may change at any time
What is the purpose of the break statement in switch statements?
The break statement is used to exit a switch statement once a particular case is matched. Without break, the code will continue executing the following cases, even if their conditions are not satisfied.
int day = 3;
switch (day) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n"); // Output: Wednesday
break;
default:
printf("Invalid day\n");
}
What is the difference between #include <file> and #include “file”?
#include <file>
is used for system header files, which are searched for in system directories.
#include "file"
is used for user-defined header files, which are searched for in the current directory first and then in system directories.
// Example using system header
#include <stdio.h>
// Example using user-defined header
#include "myheader.h"
Explain the concept of dynamic memory allocation using malloc and free.
Dynamic memory allocation allows you to allocate memory at runtime. malloc is used to allocate memory, and free is used to release the allocated memory when it’s no longer needed.
int *arr = (int *)malloc(5 * sizeof(int)); // Allocate memory for an array of 5 integers
if (arr != NULL) {
// Use the allocated memory
arr[0] = 10;
arr[1] = 20;
free(arr); // Release the allocated memory when done using it
}
What is the purpose of the typedef keyword?
typedef is used to create an alias for a data type, making code more readable and maintainable.
typedef unsigned int uint; // Create an alias for unsigned int as uint
uint x = 10; // Same as unsigned int x = 10;