C Programming Important Questions Guide
C Programming Important Questions Guide
Escape sequences in C are used to represent special characters within string literals and character constants. They start with a backslash (\). For instance, \n represents a newline, \t a tab space, and \\ a literal backslash. These sequences allow the insertion of invisible characters into output easily .
A C program typically consists of several structural components: preprocessor directives, main function, variable declarations, statement blocks, and function definitions. Preprocessor directives include libraries like #include <stdio.h>. The main function, int main(), serves as the entry point for execution. Within the main, we declare variables that store data used in computations. Statements perform actions, and functions can be defined to encapsulate reusable logic. For example, a simple C program may start with #include <stdio.h> followed by int main() { int a = 10; printf("Value of a is %d", a); return 0; } .
Operator precedence determines the order in which operators are evaluated in expressions. Associativity defines the order of evaluation when operators have the same precedence. For instance, in C, multiplication (*) and division (/) have higher precedence over addition (+) and subtraction (-). Associativity for these is left-to-right, so in 5 + 3 * 2, multiplication happens first. Similarly, the assignment operator (=) has right-to-left associativity .
The compiler translates the source code written in C into object code, checking for errors during the translation. The linker takes this object code and links it with other object files and libraries to create an executable file. The loader then loads this executable into memory, setting the program up for execution by the CPU .
Nested loops involve placing one loop inside another, useful for multidimensional data. Nested `if` allows a conditional to trigger another check: `if (a > b) { if (a > c) { ... } }`. In switch, one statement can appear in a case block of another: `switch(x) { case 1: switch(y) { case 2: ...; } }`. These structures allow complex decision-making and iteration .
The development stages involve writing, compiling, linking, and running the program. Initially, source code is written in a text editor and saved with a .c extension. This source code is compiled, converting into an object code by the compiler, which checks for syntax errors. The linker then combines this object code with libraries to produce an executable code. The loader places the program into memory, and the CPU executes it .
Local variables are defined within a function or block and can only be accessed in that scope. Global variables are defined outside functions and are accessible from any function within the file. For example, in C: int g; void f1() { int l; } Here, 'g' is global, accessible everywhere, whereas 'l' is local, only accessible within f1().
`if-else` is used for conditional branching based on boolean expressions, allowing for complex logical conditions. `switch` is generally simpler, suitable for selection among fixed values. For example, `if-else` can handle ranges, while `switch` works with constant integer cases: `if(a > b) { ... } else { ... }` vs `switch(a) { case 1: ...; break; case 2: ...; break; }` .
Source code is the human-readable instructions written in C, saved as a '.c' file. Object code is the machine-readable code outputted by the compiler, often saved as '.o' files, and is not yet complete for execution since it lacks linked resources. Executable code is the fully compiled and linked machine code that can be run by an operating system, typically in '.exe' format .
Formatted I/O functions, such as printf() and scanf(), allow structured data interaction. printf() outputs formatted text: `printf("%d", integer);`, while scanf() reads formatted input: `scanf("%d", &variable);`. They enable precise data handling, ensuring correct types and formats in operations .