CLA (C language) flashcards
CLA (C language) · Build recall with a focused review session.
32 cards. Read the term and the definition here. The deck at the top of this page is optional.
- main function return
- int main returns 0 to indicate successful termination; other values indicate failure. The host environment receives this status.
- printf format specifier
- printf uses %d for int, %f for double, %c for char, %s for string, %p for pointer. scanf requires addresses, e.g., &x with %d.
- Integer division truncation
- Dividing two ints discards the fractional part toward zero: 7/2 is 3, -7/2 is -3. The % operator yields the remainder with the dividend's sign.
- do-while loop
- A do-while loop executes its body at least once before testing the condition. Syntax: do { statements } while (condition);
- switch statement
- switch selects among constant case labels using an integer or char expression. break exits the switch; without break, execution falls through to the next case.
- sizeof operator
- sizeof yields the size in bytes of a type or expression as size_t. It is computed at compile time except for variable-length arrays.
- static storage class
- A static local variable is initialized once and retains its value between calls. A static global has internal linkage, visible only in its translation unit.
- const qualifier
- const declares that an object cannot be modified through that name. It enables compiler checks and may allow optimization but does not make a variable a compile-time constant in C.
- Bitwise XOR
- The ^ operator returns 1 for each bit position where operands differ, 0 where they match. Useful for toggling bits and simple checksums.
- Conditional compilation
- Preprocessor directives #if, #ifdef, #ifndef, #else, #elif, #endif include or exclude code before compilation, commonly used for header guards and platform-specific code.
- malloc and free
- malloc(size) allocates uninitialized heap memory and returns void* or NULL. free releases it. Every successful malloc must be freed exactly once to avoid leaks or double free.
- strcmp return value
- strcmp compares two strings lexicographically. It returns 0 if equal, a negative value if the first is less, and a positive value if greater.
- Function prototype
- A prototype declares a function's return type, name, and parameter types before use, allowing the compiler to check calls and perform conversions.
- Pass by value
- C passes arguments by value, copying them into parameters. To modify the caller's variable, pass its address and dereference the pointer inside the function.
- Array decay
- When passed to a function, an array name decays to a pointer to its first element. The function cannot determine the array length with sizeof.
- Pointer dereference
- The unary * operator accesses the object a pointer points to. The pointer must hold a valid address; dereferencing NULL or an uninitialized pointer is undefined behavior.
- Pointer arithmetic
- Adding an integer n to a pointer advances it by n times the size of the pointed-to type. Subtracting two pointers into the same array yields the element count.
- Function pointer
- A function pointer stores the address of a function with a matching signature, e.g., int (*fp)(int). It is called as fp(args) or (*fp)(args).
- Stack versus heap
- The stack holds automatic local variables and grows downward; the heap holds dynamically allocated memory managed by malloc and free. Returning a local's address is invalid.
- struct member access
- Use the dot operator for a struct object (p.x) and the arrow operator for a pointer (p->x), which is shorthand for (*p).x.
- Structure padding
- Compilers insert padding bytes between struct members to satisfy alignment requirements, so sizeof(struct) may exceed the sum of member sizes.
- Union
- A union overlays its members in the same memory; its size equals the largest member. Only the most recently written member should be read.
- fopen modes
- fopen mode strings: "r" read, "w" write (truncate), "a" append, with + for update and b for binary. fopen returns NULL on failure.
- fgets function
- fgets(buf, size, fp) reads at most size-1 characters or until newline, stores them in buf, and null-terminates. It returns NULL on error or end-of-file.
- fread and fwrite
- fread(ptr, size, count, fp) reads count items of size bytes into ptr; fwrite writes them. Both return the number of items successfully transferred.
- fseek and ftell
- fseek(fp, offset, origin) moves the file position indicator; origin is SEEK_SET, SEEK_CUR, or SEEK_END. ftell(fp) returns the current position as long.
- Macro parameter parentheses
- Macro parameters and the whole body must be parenthesized, e.g., #define MAX(a,b) ((a)>(b)?(a):(b)), to prevent precedence errors when substituted.
- Stringize operator
- In a macro, # before a parameter converts the argument into a string literal. For example, #define STR(x) #x makes STR(hello) become "hello".
- Token-pasting operator
- The ## operator in a macro concatenates two tokens into one. For example, #define CAT(a,b) a##b makes CAT(var, 1) become var1.
- Header guard
- A header guard prevents multiple inclusion: #ifndef NAME_H, #define NAME_H, declarations, #endif. Alternatively, #pragma once is widely supported.
- Predefined __FILE__ macro
- __FILE__ expands to a string literal containing the current source file name. __LINE__ expands to the current line number as an integer.
- The -> operator
- The arrow operator accesses a structure member through a pointer: p->member is equivalent to (*p).member. It is used with pointers to structs.