C Programming Tricky Questions
Tricky Questions on Variables, Datatypes & Keywords
What happens when you declare a variable without initializing it in C?
An uninitialized variable contains garbage value (random value from memory location). Using it before assignment leads to undefined behavior. However, static and global variables are automatically initialized to zero.
Tricky Point: Many programmers assume variables default to zero, but only static/global variables do. Local auto variables contain garbage.
What is the difference between int, signed int, and unsigned int?
int and signed int are the same - both store signed integers (positive and negative). unsigned int stores only non-negative values (0 to 65535 typically), effectively doubling the positive range.
Can a variable name start with a digit in C?
No, variable names cannot start with a digit. They must begin with a letter (a-z, A-Z) or underscore (_). After the first character, they can contain digits, letters, and underscores.
What is the difference between float and double?
float is single-precision (4 bytes, about 6-7 decimal digits precision). double is double-precision (8 bytes, about 15-16 decimal digits precision). double provides better precision but uses more memory.
What is the size of int data type in C?
The size of int is implementation-dependent and architecture-specific. Typically: 2 bytes on 16-bit systems, 4 bytes on 32/64-bit systems. You can check using sizeof(int).
What is a volatile variable in C?
A volatile variable tells the compiler that the variable's value may change at any time without any action being taken by the code. Prevents compiler optimization on that variable. Commonly used for hardware registers and shared memory.
What is the difference between const and #define?
const creates a read-only variable with type checking and scope rules. #define is a preprocessor macro that performs text substitution before compilation. const variables occupy memory, while #define doesn't.
What is register variable in C?
register is a storage class that suggests the compiler to store the variable in CPU register for faster access. It's a hint, not a command - compiler may ignore it. Cannot use & (address-of) operator on register variables.
What is the maximum length of a variable name in C?
According to ANSI C standard, only the first 31 characters of a variable name are significant (for external identifiers). For internal identifiers, first 63 characters are significant. However, compilers may support longer names.
What happens when you assign a float value to an int variable?
The fractional part is truncated (not rounded). Example: int x = 3.14; stores 3. No error or warning in most cases - implicit type conversion happens.
What is the difference between char and unsigned char?
char can be signed or unsigned (implementation-dependent). Range: -128 to 127 if signed, 0 to 255 if unsigned. unsigned char explicitly specifies 0-255 range. Important for character manipulation and bit operations.
Can keywords be used as variable names?
No, keywords (reserved words like int, float, if, else) cannot be used as variable names. They have special meaning to the compiler.
What is the difference between local and global variables?
Local variables: declared inside a function, accessible only within that function, destroyed when function exits. Global variables: declared outside all functions, accessible from any function in the file, exist for program lifetime, initialized to zero by default.
What is type promotion in C?
When different data types are used in an expression, the smaller type is automatically converted to the larger type. Example: int + float = float. Also happens in function calls when argument type doesn't match parameter type.
What is the difference between auto and static storage classes?
auto: default for local variables, created when function is called, destroyed when function exits. static: retains value between function calls, initialized only once, default value is zero, persists for program lifetime.
What is a typedef in C?
typedef creates an alias for an existing data type. Syntax: typedef existing_type new_name; Example: typedef int integer; then integer x; declares x as int. Useful for complex declarations and code clarity.
What is the range of values for short int?
Typically -32768 to 32767 for signed short int (2 bytes). For unsigned short int: 0 to 65535. Exact range depends on implementation and can be checked using limits.h header (SHRT_MIN, SHRT_MAX, USHRT_MAX).
What is the difference between ++i and i++?
++i (pre-increment): increments first, then uses the value. i++ (post-increment): uses the value first, then increments. Both increment i by 1, but differ in when the increment happens relative to value usage.
Can you change the value of a const variable?
Direct modification is not allowed. However, you can use pointer trickery: const int x = 10; int *p = (int*)&x; *p = 20; This is undefined behavior and should be avoided. Some compilers may optimize const variables.
Tricky Point: While technically possible through pointers, modifying const variables leads to undefined behavior and should never be done in production code.
What is the difference between sizeof() and strlen()?
sizeof() is a compile-time operator that returns size of variable/type in bytes. strlen() is a runtime function that returns length of null-terminated string (excluding null character). Example: char s[] = "hello"; sizeof(s)=6, strlen(s)=5.
What is a void pointer in C?
void* is a generic pointer that can point to any data type. Cannot be dereferenced directly - must be cast to appropriate type first. Used for generic functions like qsort(), malloc().
What is the difference between 0, '0', "0", and NULL?
0: integer zero. '0': character zero (ASCII 48). "0": string containing '0' and null terminator. NULL: pointer constant representing null pointer (usually (void*)0).
What is type casting in C?
Explicitly converting one data type to another. Syntax: (type) expression. Example: float f = 3.14; int i = (int)f; (i becomes 3). Different from implicit type conversion which happens automatically.
What is the difference between extern and static for global variables?
extern: declares variable (tells compiler variable exists elsewhere). Used for accessing global variables from other files. static for global: limits variable scope to current file only (file scope).
What is an enumeration in C?
enum creates a user-defined type consisting of named integer constants. Example: enum days {SUN, MON, TUE}; By default, values start at 0 and increment by 1. Can assign explicit values: enum {RED=1, GREEN=2};
Note: These tricky questions test deep understanding of C variables, datatypes, and keywords. Many interview questions are based on these subtle differences and edge cases.