C Programming Interview Questions C Basic questions
C Programming Interview Questions and Answers
What is C language? Answer: C is a general-purpose programming language, mainly used for developing system software. It is a procedural language and supports structured programming.
Who developed C language? Answer: C language was developed by Dennis Ritchie at Bell Labs in 1972.
What are the basic data types in C? Answer: The basic data types in C are:
int – used for integral values
float – used for floating point values
char – used for character values
double – used for double precision floating point values
What is a compiler? Answer: A compiler is a computer program that translates code written in one language into another language. For C, the compiler generates machine code for the target processor from C source code.
What is a linker? Answer: A linker combines multiple compiled object code modules into a single executable file. It resolves references between modules and libraries included during compilation.
What is the difference between interpreted language and compiled language? Answer: Interpreted languages convert source code into machine code line by line during runtime. Compiled languages convert the entire source code into machine code at once before runtime.
What is a header file and what is it used for? Answer: A header file contains function declarations, macros and type definitions used across multiple source code modules. Header files allow sharing of common declarations without needing to recompile the source code files.
What are comments in C? Answer: Comments are lines ignored by the compiler meant to add clarity and documentation for human readers. Comments start with /* and end */ or start with //.
What is the purpose of a main() function in C? Answer: The main() function is the entry point for program execution. When compiled into an executable, the program will start execution from main().
What are the different data types to declare a variable in C? Answer: int, float, char, double, short, long
What is type casting? Answer: Type casting is explicitly converting a variable type to another data type. For example char c = (char)100;
What are operators in C langauge? Answer: C language provides multiple types of operators like:
Arithmetic operators (+,-,*,/)
Relational operators (==, !=, <, >)
Logical operators (&&, ||, !)
Bitwise operators (&, |, ^, ~)
What are control statements in C? Answer: if, if-else, switch, while, do-while, for, break, continue, goto
What is recursion in C? Answer: Recursion is technique where a function calls itself repeatedly to solve a problem. Recursion should have a termination condition to avoid infinite loops.
What are different types of loops in C? Answer: There are 3 types of loops in C – while loop, for loop, do-while loop.
What is an infinite loop? Answer: A loop running endlessly without a terminating condition leading to unpredictable output is called infinite loop.
What is a pointer? Answer: A pointer is a variable that stores the memory address of another variable. It is used to access the stored variable directly without needing to copy it.
What is dereferencing a pointer? Answer: Dereferencing a pointer means accessing the value stored at the memory location pointed to by the pointer variable. It is done using the * operator.
What is NULL pointer? Answer: A pointer pointing to nothing and has a value of 0 is called NULL pointer. Dereferencing NULL pointer can cause segmentation faults.
What is an array? Answer: An array is collection of similar data type variables referenced by a common name and distinguished by indices.
What are multi-dimensional arrays? Answer: A multi-dimensional array is array of arrays allowing storage of data in multiple dimensions including 2D and 3D formats.
What is the difference between array and pointer? Answer: Array name refers to constant pointer pointing to first index address while pointer is variable pointing to any memory address. Array indices are checked while pointers can point anywhere.
What is a string? Answer: A string is an array of characters terminated by null character ‘\0’. The stdlib.h header file provides multiple string handling functions.
What are user defined data types? Answer: Structures, unions and enums which allow grouping data of different types are called user defined data types. They expand the existing primitive C data types.
What is a structure? Answer: A structure creates a data type that can store data items of different types like int, float, array, pointer etc under one name. Access is provided by the variable name.
What is a union? Answer: A union allows storing different data types in same memory location. A union variable can store only one data type value at a time. The stored value size is equal to max member size.
What are enums? Answer: Enums are user defined types which consist of named integer constants. They improve code readability as mnemonic names represent integer numeric values.
What is typedef? Answer: Typedef assigns alternative names to existing data types. It is used to simplify code syntax for complex data structures.
What are bitwise operators? Answer: Bitwise operators like &, |, ^, ~, <<, >> operate on bits of the operands rather than numeric values. They are used in low-level programming like device drivers and embedded systems.
What is a preprocessor directive? Answer: Preprocessor directives give instructions to the compiler to preprocess source code before compilation. Directives start with # like #include, #define, #ifdef etc.
What does the #include directive do? Answer: #include inserts entire contents of the specified C library header file into source code during pre-processing before compilation.
What does #define do? Answer: #define is used to define macros which replace occurrences macro name with its value during pre-processing before compilation.
What are escape sequences? Answer: Escape sequences consist of backslash \ followed by character and are used to represent special characters like newline (\n) and tab (\t) or display characters like backslash (\).
What does storage class specifiers do? Answer: Storage class specifiers like auto, extern, static, register define scope, visibility, life and storage location of variables and functions.
What is a Makefile? Answer: Makefile consists of compiler related settings and commands in make format to automate build process activities like compile, link, execute source code by invoking make command.
What are variable scope, life and extent? Answer: Variable scope refers to accessing context like local or global. Life defines state when memory is allocated and deallocated. Extent denotes duration for which variable exists and memory remains allocated.
What are basic data type qualifiers? Answer: const – makes variables read only volatile – prevents compiler optimisations on variable restrict – indicates pointer is the only means to access variable
What is static variable?
A static local variable retains its value during entire program life while global variable has file scope visibility. Static global variables have visibility only within source file.
What are recursive and iterative functions? Answer: Recursive functions call themselves repeatedly to address sub-problems while iterative functions use looping constructs like while, for to address sub-problems .
What are memory leaks and how to prevent them? Answer: Failure to de-allocate dynamically allocated memory when no longer used causes memory leaks over time. They can be prevented by freeing allocated memory after usage.
What is storage class? Answer: Storage class defines lifetime, scope and initialisation of variables and functions. Storage classes in C – auto, register, static, extern.
What are binary and text files? Answer: Binary files contain encoded data bytes while text files contain ASCII characters. Binary files are processed faster compared to text files by computer programs.
What are syntax and logical errors? Answer: Syntax errors occur due to incorrect code with respect to language rules while logical errors occur due to faulty program logic causing unintended behaviour.
What debugging methods can you use in C? Answer: Debugging methods – using compiler warnings and debug flags, print statements and logs, debuggers like GDB, Valgrind memory check.
What are wild pointers? Answer: Uninitialized pointers pointing to some arbitrary memory address are called wild pointers. They can cause memory corruption or segmentation faults.
What is object code, source code and executable code? Answer: Source code is program code in high level language. Object code is compiler output with machine code before linking. Executable code is linked machine code loadable in memory.
What is code optimization? Answer: Code optimization refers to improving code to make better use of available system resources – CPU usage, memory, disk space, network bandwidth etc to boost performance.
What are compile time and run time errors? Answer: Compile time errors occur at compilation stage due to issues like syntax errors, semantic errors, declaration errors. Run time errors happen during execution due to memory access issues, divide by zero etc.
What are macros and what are they used for? Answer: Macros are preprocessor directives that replace occurrences macro name with its value before compilation. They are used to simplify usage of constant values and code optimization.
What are different variable storage classes in C? Answer: auto, static, extern, volatile and register. They define variable scope, default initialization, lifetime and visibility.