C Flashcards
What does the volatile keyword do in C?
Volatile keyword tells the compiler that the variable can be changed externally (e.g. not via code, like with a GPIO) and keeps the compiler from using optimizations that center around the variable not changing
What does the static keyword do in C? What value do they have if not initialized?
Static local variables preserve data values between function calls even after they’re out of scope. Static global variables (or functions) can only be accessed in the same file as they’re declared. A static variable is 0 if not initialized and they can only be initialized with constant literals. They are stored in the data segment of memory, not stack.
What are malloc() and calloc() in C?
malloc() and calloc() are used to allocate dynamic memory (runtime / heap). malloc() takes the number of bytes to allocate as an argument and does not initialize them. calloc() allocates contiguous memory and initializes it. It takes number of blocks and size of each block as an argument. Null return value indicates failure, otherwise a pointer to a block of memory is returned
What are tokens in C?
Identifiers, Keywords, Constants, Operators, Special Characters, Strings
What’s the difference between a global and a static variable in C?
Globals are visible to all functions in the same source file. If in a header, visible to all source files that include it. Static variables have local scope and retain state between function calls, useful for counters
What is a pointer?
Pointers store the address of a variable in memory. They are used primarily to save memory space and speed up execution. They are used to pass arguments by reference, access array elements, return multiple values.
What’s the difference between const char* p and char const* p?
const char* p is a pointer to a const char* p is a pointer to a const char*, since const char and char const are the same, they are the same.
Why does n++ execute faster than = n + 1
n++ is a unary operator and n = n + 1 is a binary operator. In modern compilers, n = n + 1 will be converted to n++ automatically
What is typecasting and how can it be achieved in C?
modifies interpretation of value without changing binary. (type_name) expression. Typecasting can result in loss of data or precision if new type is smaller than old type. Floating point to int would truncate fractional part.
What are macros? What are the advantages of macros over functions?
Macros are preprocessed and their contents are inserted anywhere the macro name is seen in the code. Macros increase code length over functions, but are faster to execute. Macros don’t check compile-time errors whereas functions do.
What are preprocesor directives in C?
Macros, include statements, conditional compilation, directives like #ifdef, #pragma
What’s #pragma?
What’s the memory layout of C programs?
What’s a dangling pointer and how is it different from a memory leak?
How can you dereference a pointer in C?
int* ptr p; declares ptr, &ptr dereferences it accessing the data at the memory location