B CompE Bachelor of Computer Engineering Bachelor of Computer Engineering C Language 3 — Questions and Answers
Question 1: What is the purpose of the `#pragma once` directive in C header files?
- Marks the file as executable once
- Prevents the header from being included more than once per compilation unit (Correct answer)
- Forces a single-pass compilation
- Disables all macros in the file
Correct answer: Prevents the header from being included more than once per compilation unit
`#pragma once` is a non-standard but widely supported guard that ensures a header is included only once during compilation.
Question 2: In C, what is the difference between `calloc` and `malloc`?
- `calloc` allocates from the stack; `malloc` from the heap
- `calloc` zero-initializes the allocated memory; `malloc` does not (Correct answer)
- `malloc` takes two arguments; `calloc` takes one
- `calloc` is faster than `malloc`
Correct answer: `calloc` zero-initializes the allocated memory; `malloc` does not
`calloc(n, size)` allocates `n` elements of `size` bytes each and zero-initializes the block, while `malloc` leaves memory uninitialized.
Question 3: Which C operator is used to access a struct member through a pointer?
- .
- -> (Correct answer)
- ::
- *
Correct answer: ->
The `->` operator dereferences a pointer and accesses the named member in one step, equivalent to `(*ptr).member`.
Question 4: What does `strncpy(dest, src, n)` do differently from `strcpy`?
- It copies at most n bytes, padding with null bytes if src is shorter (Correct answer)
- It always null-terminates the destination
- It copies n characters starting from position n
- It compares strings instead of copying them
Correct answer: It copies at most n bytes, padding with null bytes if src is shorter
`strncpy` copies at most `n` characters from `src` to `dest` and pads with null bytes if `src` is shorter than `n`, but may not null-terminate if `src` is longer.
Question 5: In C, what is the value of `NULL`?
- 0
- -1
- An unspecified non-zero value
- Implementation-defined but guaranteed to compare equal to a null pointer (Correct answer)
Correct answer: Implementation-defined but guaranteed to compare equal to a null pointer
The C standard defines `NULL` as an implementation-defined null pointer constant that compares equal to a null pointer; it is typically defined as `0` or `(void*)0`.
Question 6: What is the purpose of `setjmp` and `longjmp` in C?
- Thread synchronization
- Non-local jumps for error recovery across function boundaries (Correct answer)
- Dynamic memory resizing
- Interrupt handling
Correct answer: Non-local jumps for error recovery across function boundaries
`setjmp` saves the current execution environment, and `longjmp` restores it, enabling non-local jumps for error handling without using exceptions.
Question 7: What happens when you pass an array to a function in C?
- A full copy of the array is passed
- The array decays to a pointer to its first element (Correct answer)
- A reference to the array is passed
- The compiler raises a type error
Correct answer: The array decays to a pointer to its first element
In C, arrays passed to functions decay to a pointer to the first element; the function receives only the address, not a copy of the data.
What is the purpose of the `#pragma once` directive in C header files?