Basics of C Flashcards
(26 cards)
List the phases of the C program life cycle.
Editing, Preprocessing, Compilation, Assembly, Linking, Loading, Execution, Termination
Describe the basic structure of a C program, including the main function syntax.
A C program typically includes Preprocessor Directives (header files), global declarations (optional), the main() function (entry point), and function definitions. The main function has the syntax int main() { … return 0; }.
What are the typical size and data range for the int primitive data type?
Typically 4 bytes. The data range is approximately −2^31 to 2^31−1.
How do you safely read a string (including spaces) from standard input into a character array name of size 50?
fgets(name, 50, stdin);
Explain the purpose and syntax of the increment (++) and decrement (–) operators.
They are unary operators used to increase or decrease the value of a variable by one.
Prefix: ++x (increments then uses value), –x (decrements then uses value).
Postfix: x++ (uses value then increments), x– (uses value then decrements).
What is the syntax for the ternary (conditional) operator, and how does it work?
condition ? expression_if_true : expression_if_false;. If the condition is true, expression_if_true is evaluated; otherwise, expression_if_false is evaluated.
When and how is explicit type casting used in C?
(dataType) expression;
Name the four primitive data types in C and their format specifiers for printf/scanf.
char (%c), int (%d), float (%f), double (%lf).
Size of float, double, Short Integer, Long integer
Float : 4 bytes , double : 8 bytes , Short Integer : 16 , Long Integer : 32
What are the rules for C identifiers?
Must start with an alphabet or underscore. Can contain alphabets, digits, and underscores. Cannot be a C keyword. Case-sensitive.
What is the difference between a Variable and a Constant in C?
Variable: Named storage location whose value can change during execution.
Constant: Named storage location whose value cannot be changed after initialization (declared with const or #define).
Explain the difference between printf() and puts().
printf(): Prints formatted output, requires format specifiers, and does not automatically add a newline.
puts(): Prints a string and automatically adds a newline character at the end.
Explain the difference between scanf() and gets().
scanf() ignores newline but gets() considers it
Life Cycle - Editing
- You write your program in a text editor (e.g., VSCode), saved as program.c
Preprocessing
- Handles lines starting with # like #include, #define. This expands code.
Expanded Source Code
- Now your code has library content and macros expanded (not seen by you)
Compilation
- Compiler checks syntax and converts expanded code to assembly instructions
Assembly
- Assembly code is converted to machine code (Object Code / .o)
Assembler
- Translates assembly into actual object file
Object Code
- Intermediate .o or .obj file – not runnable yet
Linking
- Combines object code with standard library code (printf, scanf, etc.)
Executable Code
- Final .exe file is created (platform-dependent)
Final .exe file is created (platform-dependent)
- Program is loaded into RAM by OS
Execution
- CPU starts executing the instructions