0% found this document useful (0 votes)
21 views8 pages

C Programming Interview Prep Guide

This will help for your C programming interview.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views8 pages

C Programming Interview Prep Guide

This will help for your C programming interview.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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: [Link]

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: [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

Common questions

Powered by AI

Dynamic memory allocation for a 2D array in C can be executed using both single and double pointers. Single pointer allocation involves simulating a 2D array with a contiguous block of memory and using pointer arithmetic to access elements. In double pointer method, memory is allocated for rows first (as pointers to int pointers), followed by individual row allocations using malloc. The structure for double method is: int **array = malloc(rows * sizeof(int*)); for each row, array[i] = malloc(columns * sizeof(int)); This allows dynamic management of both rows and columns .

Endianess refers to byte order at which data bytes are stored in memory. Little endian stores least significant byte at smallest address and big endian does the opposite. It affects reading of multibyte data types like integers. To check endianess: union { int num; char byte; } test = {1}; if (test.byte == 1) printf('Little Endian'); else printf('Big Endian'); This union checks which byte is stored first to determine endianess .

Storage classes define the scope, visibility, and lifespan of variables/functions in C. Static local variables have local scope within their function, retaining value across multiple function calls. They persist for the entire program execution even after the block exits. Global variables are accessible throughout the program and have a lifetime inception at program start until its termination. Thus, static ensures data persistence at function level, while global provides access at program level .

In C, strings are typically passed by reference either as an array or a pointer to a char. Passing a character pointer allows flexible memory use, but changes to strings may lead to undefined behavior if it's a string literal. Character arrays offer stability and mutability within bounds. Example: void func(char *ptr); vs void func(char arr[]);. Arrays allow direct manipulation, but pointers may more efficiently manage dynamic string operations .

When passing arrays to functions, a pointer to the array's first element is transferred, using void function_name(int arr[], int size). The entire sequence is accessible through pointer arithmetic. Individual element passing involves copying the element's value (e.g., int element = arr[i]; function_name(element);). While arrays reference a memory address directly, individual elements are passed by value, leading to separate function-local copies .

The ternary operator is a concise way to handle conditional expressions. For three numbers a, b, and c, one can use nested ternary operators to find the largest number: int max = (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c); This expression first evaluates if a is greater than b, if true, it checks between a and c; otherwise, it checks between b and c, eventually assigning the greatest value to max .

Macros in C are preprocessor directives using #define to create symbolic constants or inline reusable code snippets. They run faster as they are processed before compilation, reducing runtime overhead. However, macros lack type safety and can lead to complex debugging, differing from functions that validate types and provide safer encapsulation. Macros also do not have the reliability of consistent side effects like functions do .

Qualifiers like 'const' and 'volatile' alter variable behavior. 'const' prevents any modification after initialization, ensuring compile-time protection and optimizing read-only data. 'volatile' informs the compiler to refrain from optimizing the variable, emphasizing its potential for changes outside program control, such as hardware I/O, relevant in embedded systems .

Function pointers in C enable dynamic function call capabilities, enhancing flexibility for plugins, callbacks, or advanced message handling. They are crucial for implementing callback functions for event-driven architectures or passing functions as arguments. Syntax: void (*func_ptr) (int) = &function_name; allows dynamic runtime decision making, such as choosing algorithm variants within the same function pointer type .

The XOR operator is versatile in practical applications such as toggling bits and performing specific bit manipulations efficiently without temporary variables. It can be used to swap two variables without a temporary storage due to its properties: applying XOR twice reverts the bit sequence to the original. Here is a swap operation example: For two integers a and b, to swap values: a = a ^ b; b = a ^ b; a = a ^ b; After these operations, a and b are swapped without using extra space .

You might also like