Question: What is the difference between 'char *str' and 'char str[]' in C?
|
Answer: The 'char *str' declares a pointer to a character, while 'char str[]' declares an array of characters.
The former can be used to point to a string literal or dynamically allocated memory, while the latter is used to store a fixed-size string.
|
Question: Can you explain the difference between '++i' and 'i++' in C?
|
Answer: Both '++i' and 'i++' increments the value of 'i', but the difference lies in their usage within expressions.
'++i' is a pre-increment operator, which increments 'i' and then returns the updated value.
And 'i++' is a post-increment operator, which returns the current value of 'i' and then increments it.
|
Question: What are bitwise operators in C?
|
Answer: Bitwise operators in C include '&' (AND), '|' (OR), '^' (XOR), '~' (NOT), '<<' (left shift), and '>>' (right shift).
int a = 5; // 0101 in binary;
int b = 3; // 0011 in binary
int result_and = a & b; // 0001 (AND)
int result_or = a | b; // 0111 (OR)
int result_xor = a ^ b; // 0110 (XOR)
The bitwise operators in C allow a low-level manipulation of data stored in computer's memory.
|
Question: What is the purpose of the 'sizeof' operator in C?
|
Answer: The 'sizeof' operator is used to determine the size of a variable or data type in bytes.
It is commonly used in dynamic memory allocation and for array declarations.
|
Question: What is the difference between strcpy() and strncpy() functions in C?
|
Answer: The 'strcpy()' function copies a string from the source to the destination until it encounters a null character.
While 'strncpy()' copies at most 'n' characters from the source to the destination, padding with null characters as necessary.
|
Question: What is the ternary operator in C?
|
Answer: The ternary operator '(? :)' is a conditional operator that evaluates an expression based on a condition. Example:
int x = 5;
int y = (x > 0) ? 10 : 20; // If x is greater than 0, y equals 10; otherwise, y equals 20.
The ternary operator is a syntactic and readability convenience, not a performance shortcut.
|
Question: What is the significance of the 'volatile' keyword in C?
|
Answer: The 'volatile' keyword tells the compiler that a variable's value may change unexpectedly,
typically due to external factors such as hardware.
It prevents the compiler from optimizing away accesses to that variable.
|