C Programming Basics
Learn C with multiple-choice questions that trace short code: integer type sizes and fixed-width types, integer promotion and signed/unsigned conversion, operator precedence, bitwise operations, switch statements, pass by value and pointers, static variables, arrays and pointer arithmetic, strings and sizeof, const, function pointers, malloc, free and memory leaks, dangling pointers, buffer overflows, struct padding, unions and endianness, bit-fields, macros and conditional compilation, volatile, and undefined behavior.
C Programming Basics: key points
C is still widely used for software close to the hardware, such as operating systems, device drivers and microcontroller firmware. It lets you handle memory addresses directly and write fast code, but it also has many rules whose mistakes are hard to notice unless you know them: the size of int differs between implementations, types change in the middle of a calculation, and nothing stops a program that writes past the end of an array.
This topic centers on questions that trace the output of short code when it is actually run. It covers integer type sizes and the fixed-width integer types of stdint.h, integer division, integer promotion and signed/unsigned comparison, operator precedence, bitwise operations, short-circuit evaluation, fall-through in switch statements, pass by value and passing pointers, static variables and lifetimes, pointer arithmetic and arrays, the layout of two-dimensional arrays, strings with strlen and sizeof, where to put const, and function pointers. It then deals with malloc and free, memory leaks, dangling pointers, buffer overflows, null pointers, struct padding, unions and endianness, bit-fields, macros and include guards, conditional compilation, volatile in embedded code, and undefined behavior and compiler optimization. The program outputs have been checked by actually compiling the code with gcc on x86-64 Linux. Pseudocode tracing is covered in "Programming Basics (Pseudocode Tracing)", interrupts and register access with volatile in "Embedded Systems & Firmware", compiler optimizations in detail in "Compilers", and memory layout and linking in "Linkers, Loaders & Executables".
All C Programming Basics questions (40)
- Which of the following is what the C standard specifies about the size of the int type?
- In embedded programs, variables that hold the value of a 32-bit peripheral register are often declared with uint32_t from stdint.h rather than unsigned int. Which of the following is the main reason?
- Which of the following is the output when the code above, which includes a division of integers, is executed?
- Which of the following is the output when the code above, which uses prefix and postfix ++, is executed?
- Which of the following is the output when the code above, with = written in the condition of an if statement, is executed?
- The code above was written to check whether bit 2 of the register value reg is 1. Which of the following is the result when it is executed?
- uint8_t is an 8-bit unsigned integer type. Which of the following is the output when the code above is executed?
- uint8_t is an 8-bit unsigned integer type. Which of the following is the output when the code above, which increments x ten times, is executed?
- unsigned char is 8 bits and int is 32 bits. Which of the following is the output when the code above is executed?
- uint8_t is an 8-bit unsigned integer type and int is 32 bits. Which of the following is the result when the code above is executed?
- int and unsigned int are 32 bits. Which of the following is the result when the code above is executed?
- unsigned int is 32 bits. Which of the following describes the behavior when the code above is executed?
- A program that sets "char c = 200;" and prints c with %d in printf printed −56 when compiled and run with gcc for x86-64 Linux, and 200 with gcc for Arm Linux. Which of the following is the reason for the difference?
- Which of the following is the output when the code above, with b++ on the right side of && and ||, is executed?
- Which of the following is the output when the switch statement above, which contains a case without break, is executed?
- Which of the following is the output when the program above is executed?
- When the function f above is called three times in a row, which of the following is the return value of the third call?
- In C, which of the following correctly pairs the lifetimes of (1) a local variable declared inside a function without static, (2) a region allocated with malloc, and (3) a variable declared outside any function?
- int is 32 bits (maximum 2147483647). Which of the following is the largest n for which the function fact(n) above returns the factorial of n correctly, without overflow?
- int is 4 bytes and pointers are 8 bytes. When the value (address) of a pointer to int p is 0x1000, which of the following is the value of p + 3?
- int is 4 bytes and pointers are 8 bytes. Which of the following is the output when the program above is executed?
- int is 4 bytes. Which of the following is the output when the code above is executed?
- Which of the following is the output when the code above, which concerns a char array holding a string, is executed?
- After the declarations above, one of the statements A "p = t;", B "*p = 'x';", C "q = t;" or D "*q = 'x';" is written. Which of the following is the combination that causes compile errors?
- Which of the following is the output when the program above, which uses an array of function pointers, is executed?
- fill and use are functions defined elsewhere. Which of the following is a problem with the function above?
- Which of the following is true about the pointer returned by the function make above?
- In C, which of the following is the most appropriate reason it is recommended to write p = NULL; right after free(p);?
- The 8-character string "ABCDEFGH" was passed to the function save above. Which of the following is what happens?
- In C, which of the following describes what happens when a null pointer is dereferenced (*p is read while p is NULL)?
- Assume a typical environment where char is 1 byte and int is 4 bytes, with int placed at addresses that are multiples of 4. Which of the following is the output when the code above is executed?
- uint32_t and uint8_t are 32-bit and 8-bit unsigned integer types, and the environment is little-endian. Which of the following is the output when the code above is executed?
- Which of the following is the output when the code above, which stores 9 in a 3-bit bit-field, is executed?
- Which of the following is the output when the code above, which uses the macro SQUARE defined with #define, is executed?
- The header file sensor.h is written as above. Which of the following is the purpose of the three lines #ifndef, #define and #endif?
- Which of the following is the output when the code above is written inside the main function and executed?
- A microcontroller output register is declared and used as above. Which of the following correctly describes this declaration?
- Which of the following describes undefined behavior as defined by the C standard?
- int and unsigned int are 32 bits. Which of the following is undefined behavior according to the C standard?
- When the function above is compiled with gcc with optimization enabled, the check if (p == NULL) may be removed. Which of the following is the reason?
Books on C Programming Basics Ad
As an Amazon Associate I earn from qualifying purchases.
Other topics in Computer systems
- Computer Arithmetic IEEE 754, fixed point, rounding error
- Probability, Statistics and Queueing Counting, distributions, estimation and testing, M/M/1
- ISA & Assembly RISC-V, addressing modes, calling conventions
- Compilers Parsing, SSA, optimizations, register allocation
- Linkers, Loaders & Executables ELF, symbols, relocation, dynamic linking
- OS & Hardware Interrupts, system calls, page faults
- Linux and Command-Line Basics Shell, permissions, pipes, scripts
- Memory Hierarchy Caches, replacement policies, 3 types of misses
- Multicore & Synchronization MESI, atomic instructions, memory barriers
- GPU Programming Warps, shared memory, coalescing, occupancy
- AI Hardware & Software Stack Inference vs training, quantization, roofline
- Machine Learning Basics Learning types, evaluation metrics and neural networks
- Embedded Systems & Firmware GPIO, UART/SPI/I2C, RTOS
- HDL & Verification Verilog, latches, state machines, testbenches
- I/O Interfaces & Buses PCIe, USB, SerDes, CXL
- Computer Networking Basics TCP/IP, subnetting, DNS, TLS
- Web Technology Basics HTTP, cookies, REST, CORS, caching
- Wireless Communication Basics Modulation, OFDM, MIMO and RF circuits
- Information Security Basics Crypto, authentication, web attacks
- Database Basics SQL, normalization, transactions
- Data Structures and Algorithms Complexity, search, sorting and graphs
- Programming Basics (Pseudocode Tracing) Pseudocode, tracing, arrays and recursion
- Software Development and Project Management Processes, testing, schedule and cost control
- Business Strategy and Accounting Basics Accounting, break-even, strategy, marketing
- IT Service Management and Systems Audit SLAs, incidents and problems, audits and internal control
- System Architecture and Reliability Redundancy, RAID, backups and cloud
- Cloud and Container Basics Docker, Kubernetes, shared responsibility, IaC
- Performance Analysis Amdahl's law, CPI, bottlenecks
- Hardware Security Side channels, secure boot, TEE, PUF
- Power Management DVFS, power gating, sleep states