C Programming Interview Prep Questions
C Programming Interview Prep Questions
Basics Refresher
Datatypes
What is the size of int in C for different platforms? Is it same?
What is the size of integral & real types in C?
How do you ensure that size of datatypes is same across platforms?
How does a char get stored?
Note: Show some programs illustrating char storage and printing
What is ascii value of 0 & 5
What is ascii value of a & d, A & d
What is ascii value of '\0' & \n'
What is ascii value of ' ' (Space)
Qualifiers 1
How is signed and unsigned int stored? What is the range?
What is const keyword?
Explain register keyword?
Operators
Arithmetic Operators
What is the difference between pre and post increment? Show examples.
Logical Operators
Overview & few code snippets.
Bitwise Operators
Overview of bitwise operators
Get, Set, Clear bit
Get, Set, Clear nth bit
Get, Set, Clear N bit
Macros for above operations
Toggle bit(s)
What is practical use of XOR (^)?
WAP to print bits
WAP to count the number of set bits (Count 1's)
WAP to count number of 0s in a number
WAP to check whether number is Even or odd using bitwise operators
WAP to reverse the bits in an integer
WAP to check whether a number is power of 2 without loops
Ternary Operator
Usage and example.
WAP to find largest of 2 nos using ternary op
WAP to find largest of 3 nos using ternary op
Conditionals & Loops
Switch case
Explain switch case with an good example
Programs
Pattern program 1
5
54
543
5432
54321
Pattern program 2
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
Ref: https://www.programiz.com/c-programming/examples/pyramid-pattern
WAP to multiple two numbers (m x n) without using multiplication operator
WAP to reverse a number
WAP to convert decimal to hex
What is the o/p of the code?
int main ()
{
int i = 1, j = 1;
for (--i && j++; i < 10; i += 2)
{
printf ("loop ");
}
return 0;
}
WAP to reverse a number
int a=1234;
int reverse; //reverse should contain 4321
break & continue
Overview & examples
WAP to check whether a number is prime or not using break
Example of nested loop with break
Typecasting
Explain typecasting with useful examples
Overflow/Underflow
What is overflow and underflow in integers
Pointer Basics
Overview of basic pointer rules.
What is the use of pointers.
Note: Discuss more concepts including void pointer, NULL pointer, Dynamic memory allocation, Double
pointer, const pointer, Endianess etc in Part 2
Functions
What is the difference between pass by value and pass by reference. Show examples.
WAP to swap two integers
WAP to swap two integers without using temp variable
WAP to find product of 2 numbers using pass by reference
WAP to find sum and product of 2 numbers using pass by reference (Extension of previous program)
Arrays
Quick refresher of arrays.
How to pass arrays to functions
Array Programs (Use functions)
WAP to find the largest and smallest number in an array
WAP to find the second largest number in an array
WAP to remove duplicates in an array. Return a new array without duplicates and size of new array.
Recursion
Explain recursion with examples.
WAP to generate Factorial series using recursion
WAP to generate Fibonacci series using recursion
Add some What is the o/p type questions
Storage Classes & Memory Segments
Explain below with good code examples
What is the difference between local and global variable.
Q: Can a local variable's address be returned?
What is the difference between local and static local variable.
What is the difference between global and static global variable.
Describe Memory segments in brief
Avoid explaining command line arguments, program header etc. Just explain CS, DS, BSS, Stack and
Heap
Scope and Lifetime of variables.
What is the use of extern keyword. Show usage of extern of variables and functions with simple
examples.
Q: What is a static function?
What is a block variable?
Summarize storage classes.
Strings
Overview of string
WAP to read your name and print it
Note: use scanf("%s", name);
How to pass strings to function?
WAF to prints a string:
void myputs(char *str);
void myputs(char str[]);
WAF to find length of a string
WAF to check whether a string is palindrome or not
Solution: https://drive.google.com/file/d/1luXPRa98IidFifuqnsQYDR6Ula2lYsGK/view?usp=share_link
WAF to reverse a string
WAF to implement strcpy, strcat
Q: Explain the difference between:
char str1[10] = "Hello";
char *str2 = "Hello";
Q: Have a look at this code
char str1[10] = "Hello";
char *str2 = str1;
*str2 = 'P';
str2[1] = 'i';
puts(str2);
Will this code give a segfault. Why?
Why is scanf/gets not recommended to use. Why is fgets recommended. Usage of fgets
Pointers 2
Explaining remaining pointer rules
Dynamic memory allocation
Explaining Dynamic memory allocation briefly using malloc and free
Explain calloc and its difference with malloc
What is realloc?
WAP to allocate an integer array of size n. Store values in the array and find its sum.
Void Pointer
What is void pointer. Show usage of void pointer
WAP to Implement a generic swap function using void pointer
const pointer
Difference between const char *ptr and char * const ptr
What about const char * const ptr
More topics
What is segmentation fault?
What is a dangling/wild pointer?
2D Array
Basics of 2D arrays
Creation and access
How to pass 2D array to functions
How to Dynamically allocate 2D array of size m x n (Show only both dynamic method)
Array of strings & Command line arguments
How to define and access array of strings
Command line arguments usage
WAP to find the sum and average of command line args
Preprocessing
#define constant - brief
Macros
Macros syntax
Find sum of 2 numbers using a macro
#include <stdio.h>
#define SUM(a,b) ((a) + (b))
int main()
{
int x = 5, y = 10, res;
res = SUM(x, y);
printf("%d\n", res);
res = SUM(10, 20);
printf("%d\n", res);
return 0;
}
Define macros for:
Largest of 2 nos
#define MAX(a, b) ( a > b ? a:b)
Usage in main:
int x = 5, y = 10, large;
large = MAX(x, y);
printf("%d\n", large);
Largest of 3 nos
Swap a nibble in a byte/char
Get, Set, Clear, Toggle bit
Macros to find size of data type without using sizeof operator
SWAP two integers
Difference between macros and functions
Show gcc -E output
Explain stages of compilation
User defined datatypes
struct
Overview of structures and usage
union
Overview of union and usage
Difference between struct and union
Show usage of structure pointers
Explain structure padding with examples
Enums
Explain enums and usage
Typedef
Explain typedef and usage
typedef of struct
Bitfields
Explain bitfields and usage
Other topics
Explain volatile keyword. When should you use it?
What is an inline function
What is a memory leak. How to detect it?
How to define function pointer. What is the use?
What are callback functions?
How to debug a program using gdb
Files
Write program to illustrate file related functions:
fopen - Open and create a file
fgetc,fputc - Read and write a char from a file
fprintf, fscanf - printf into file and scanf from file (formatted I/O)
fread, fwrite - read and write data stream of bytes (Used to read/write data in chunks)
fseek - Seek into file (Change position of file stream)
ftell - Get current file position
feof - Check for EOF
ferror - Check for Error
WAP to copy one file to another
WAP to implement word count (char, word and line count - wc) on a file