C Programming — Complete Notes + 50 MCQs

SEO-friendly, mobile-ready page with clean UI. Correct answers highlighted.

Overview

C is a general-purpose, procedural programming language developed by Dennis Ritchie at Bell Labs in the early 1970s. It provides low-level access to memory, efficient mapping to machine instructions, and is widely used for system programming, embedded systems, and performance-critical applications.

Why learn C?

Fundamental Concepts

Syntax & Structure

A C program consists of functions and declarations. Execution begins at main(). Statements end with semicolons.

// basic structure
#include <stdio.h>

int main(void) {
  printf("Hello, world!\n");
  return 0;
}

Data Types & Variables

Primary types: int, char, float, double. Use sizeof() to find storage size. Qualifiers: short, long, signed, unsigned.

Operators

Arithmetic, relational, logical, bitwise, assignment, and ternary (?:) operators. Precedence matters; use parentheses to clarify.

Control Flow

Conditional statements: if, if-else, switch. Loops: for, while, do-while. Use break and continue.

Functions

Declare prototypes, define functions, pass by value (C passes arguments by value). For arrays/large data, pass pointers.

int add(int a, int b) { return a + b; }

Arrays & Strings

Arrays: contiguous memory for elements of same type. Strings are arrays of char terminated by '\0'. Use functions in <string.h> like strcpy, strlen.

Pointers

A pointer stores memory address. Pointer arithmetic, dereference (*), address operator (&). Pointers to pointers and function pointers exist. Understand NULL and pointer safety.

int x=10; int *p = &x; printf("%d", *p); // prints 10

Structures & Unions

struct groups related variables; union shares memory for members. Use typedef for readability.

Dynamic Memory

Use malloc, calloc, realloc and free memory with free(). Avoid memory leaks and dangling pointers.

File I/O

Use fopen, fclose, fread, fwrite, fprintf, fscanf. Check for NULL file pointers.

Preprocessor

Directives: #include, #define, #ifdef. Used for macros and conditional compilation.

Compilation & Debugging

Compile: gcc -o prog prog.c. Compilation stages: preprocessing, compilation, assembly, linking. Use -Wall to enable warnings. Debug using gdb or printf debugging, and tools like Valgrind for memory issues.

Best Practices

50 Practice MCQs — Correct answers highlighted

1. Who developed the C programming language?

  • A) Bjarne Stroustrup
  • B) Dennis Ritchie ✅
  • C) James Gosling
  • D) Ken Thompson

2. Which function is the entry point of a C program?

  • A) start()
  • B) main() ✅
  • C) init()
  • D) program()

3. Which header file is required for printf?

  • A) <stdio.h> ✅
  • B) <stdlib.h>
  • C) <string.h>
  • D) <math.h>

4. Which of the following is a valid variable name in C?

  • A) 2count
  • B) count2 ✅
  • C) #value
  • D) void

5. Size of int on a typical 32-bit system is:

  • A) 1 byte
  • B) 4 bytes ✅
  • C) 8 bytes
  • D) 2 bytes

6. What does the '*' operator denote in declarations like int *p;?

  • A) Pointer to int ✅
  • B) Multiplication
  • C) Dereference
  • D) Address of

7. String in C is terminated by:

  • A) '0'
  • B) '\0' (null character) ✅
  • C) '\n'
  • D) EOF

8. Which declaration declares an array of 10 integers?

  • A) int a[10]; ✅
  • B) int a();
  • C) int a{10};
  • D) int 10[a];

9. By default, arguments in C are passed by:

  • A) Value ✅
  • B) Reference
  • C) Pointer only
  • D) Shared memory

10. Which function allocates memory dynamically?

  • A) malloc() ✅
  • B) free()
  • C) alloc()
  • D) new()

11. Which function releases memory allocated by malloc?

  • A) free() ✅
  • B) delete()
  • C) release()
  • D) freeall()

12. What does sizeof(char) typically return?

  • A) 1 ✅
  • B) 2
  • C) 4
  • D) Depends on OS

13. Which directive includes a header file?

  • A) #include ✅
  • B) #define
  • C) #ifdef
  • D) #pragma

14. What is typedef used for?

  • A) Create type aliases ✅
  • B) Define constants
  • C) Inline functions
  • D) Comment code

15. How do you access members of a struct through pointer p?

  • A) p->member ✅
  • B) p.member
  • C) (*p).member
  • D) Both A and C ✅

16. An enum in C is used to:

  • A) Create functions
  • B) Define named integer constants ✅
  • C) Define arrays
  • D) Allocate memory

17. What is NULL?

  • A) Uninitialized pointer
  • B) Pointer with value zero ✅
  • C) Pointer to code
  • D) End of file marker

18. Which operator gives the address of a variable?

  • A) & ✅
  • B) *
  • C) ->
  • D) %

19. Which has higher precedence?

  • A) Multiplication (*) over Addition (+) ✅
  • B) Addition over Multiplication
  • C) Both equal
  • D) Depends on compiler

20. Which loop will execute body at least once?

  • A) for
  • B) while
  • C) do-while ✅
  • D) None

21. Which function reads formatted input from stdin?

  • A) scanf() ✅
  • B) gets()
  • C) fgets()
  • D) read()

22. What is the format specifier for double?

  • A) %f
  • B) %lf ✅
  • C) %d
  • D) %c

23. fopen with mode "r" does what?

  • A) Opens file for reading ✅
  • B) Opens for writing
  • C) Appends
  • D) Reads and writes

24. Standard error stream is:

  • A) stderr ✅
  • B) stdin
  • C) stdout
  • D) stdlog

25. A macro is defined using:

  • A) #define ✅
  • B) typedef
  • C) #include
  • D) #pragma

26. The qualifier 'const' means:

  • A) The variable is read-only ✅
  • B) Variable is volatile
  • C) Variable is static
  • D) Variable is extern

27. Static variable inside a function retains its value between calls. True or false?

  • A) True ✅
  • B) False
  • C) Depends on compiler
  • D) Only for global

28. Which operator performs bitwise AND?

  • A) & ✅
  • B) &&
  • C) |
  • D) ^

29. sizeof operator returns size in:

  • A) Bytes ✅
  • B) Bits
  • C) Words
  • D) Kilobytes

30. volatile keyword tells compiler that variable may change unexpectedly. This prevents optimization. True or false?

  • A) True ✅
  • B) False
  • C) Only for globals
  • D) Only for pointers

31. In old C, function declarations without return type defaulted to int. Modern compilers warn against this. True?

  • A) True ✅
  • B) False
  • C) Depends on standard
  • D) Only in C++

32. A function pointer can be used to:

  • A) Call different functions at runtime ✅
  • B) Allocate memory
  • C) Define macros
  • D) Replace structs

33. Recursion is:

  • A) A function calling itself ✅
  • B) Loop unrolling
  • C) Pointer arithmetic
  • D) Dynamic allocation

34. Segmentation fault often occurs due to:

  • A) Invalid memory access (e.g., dereferencing NULL) ✅
  • B) Syntax error
  • C) Linker error
  • D) IO error

35. Linker stage does:

  • A) Resolve external references and combine object files ✅
  • B) Convert source to assembly
  • C) Execute program
  • D) Preprocess code

36. Which header provides malloc and free?

  • A) <stdlib.h> ✅
  • B) <stdio.h>
  • C) <string.h>
  • D) <math.h>

37. getchar() reads from:

  • A) stdin ✅
  • B) stdout
  • C) stderr
  • D) File

38. Pointer arithmetic increments by size of pointed type. True or false?

  • A) True ✅
  • B) False
  • C) Only on 32-bit
  • D) Only for char*

39. fopen mode "w" will:

  • A) Create file for writing (truncate if exists) ✅
  • B) Open for reading
  • C) Append only
  • D) Read+write without truncate

40. Macro vs function: macro is expanded by preprocessor. True or false?

  • A) True ✅
  • B) False
  • C) Macros are runtime
  • D) Functions are preprocessed

41. sizeof returns size for types and expressions. It is evaluated at compile time for types. True?

  • A) True ✅
  • B) False
  • C) Only for built-ins
  • D) Only for variables

42. Implicit type conversion is called:

  • A) Coercion ✅
  • B) Casting
  • C) Promotion
  • D) Demotion

43. Left shifting 1 by 3 (1<<3) gives:

  • A) 8 ✅
  • B) 3
  • C) 4
  • D) Undefined

44. errno is used to:

  • A) Report error codes from system calls ✅
  • B) Store last printf output
  • C) Track memory usage
  • D) Manage file descriptors

45. Undefined behavior in C means compiler may do anything. True or false?

  • A) True ✅
  • B) False
  • C) It prints error
  • D) It is runtime exception

46. Reentrant function is safe to call from signal handlers. True?

  • A) True ✅
  • B) False
  • C) Only for static
  • D) Only for globals

47. Which header provides fixed width integer types like int32_t?

  • A) <stdint.h> ✅
  • B) <inttypes.h>
  • C) <stddef.h>
  • D) <limits.h>

48. fflush(stdout) is used to:

  • A) Flush output buffer ✅
  • B) Close stream
  • C) Read from stream
  • D) Clear stdin

49. exit(0) indicates successful termination. True?

  • A) True ✅
  • B) False
  • C) exit(-1) is success
  • D) Depends on OS

50. Which function compares two memory blocks?

  • A) memcmp() ✅
  • B) strcmp()
  • C) memcpy()
  • D) memset()