C Programming Interview Questions C Basic questions

C Programming Interview Questions and Answers

  1. 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.
  2. Who developed C language?
    Answer: C language was developed by Dennis Ritchie at Bell Labs in 1972.
  3. 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
  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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 //.
  6. 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().
  7. What are the different data types to declare a variable in C?
    Answer: int, float, char, double, short, long
  8. What is type casting?
    Answer: Type casting is explicitly converting a variable type to another data type. For example char c = (char)100;
  9. What are operators in C langauge?
    Answer: C language provides multiple types of operators like:
  1. Arithmetic operators (+,-,*,/)
  2. Relational operators (==, !=, <, >)
  3. Logical operators (&&, ||, !)
  4. Bitwise operators (&, |, ^, ~)
  1. What are control statements in C?
    Answer: if, if-else, switch, while, do-while, for, break, continue, goto
  2. 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.
  3. What are different types of loops in C?
    Answer: There are 3 types of loops in C – while loop, for loop, do-while loop.
  4. What is an infinite loop?
    Answer: A loop running endlessly without a terminating condition leading to unpredictable output is called infinite loop.
  5. 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.
  6. 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.
  7. 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.
  8. What is an array?
    Answer: An array is collection of similar data type variables referenced by a common name and distinguished by indices.
  9. 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.
  10. 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.
  11. 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.
  12. 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.
  13. 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.
  14. 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.
  15. 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.
  16. What is typedef?
    Answer: Typedef assigns alternative names to existing data types. It is used to simplify code syntax for complex data structures.
  17. 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.
  18. 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.
  19. 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.
  20. What does #define do?
    Answer: #define is used to define macros which replace occurrences macro name with its value during pre-processing before compilation.
  21. 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 (\).
  22. 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.
  23. 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.
  24. 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.
  25. 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
  26. 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.
  27. 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 .
  28. 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.
  29. What is storage class?
    Answer: Storage class defines lifetime, scope and initialisation of variables and functions. Storage classes in C – auto, register, static, extern.
  30. 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.
  31. 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.
  32. 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.
  33. 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.
  34. 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.
  35. 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.
  36. 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.
  37. 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.
  38. What are different variable storage classes in C?
    Answer: auto, static, extern, volatile and register. They define variable scope, default initialization, lifetime and visibility.

C Programming Interview Questions

Leave a Reply

Your email address will not be published. Required fields are marked *