Program Structure 1
Program Structure 1
Here, stdio.in (standard input and output header file) and #include is the command to
use the code from the header file in our source code.
When compiler encounters printf() and doesn’t find stdio.h header file, compiler
shows error.
Every program starts from int main() function.
printf() is the library function to display output which only works if #include<stdio.h>
is i #include<stdio.h>cluded at the beginning.
Return 0 is a function return type which match to function definition.
Our main function returns int value so we are given return 0.
Sequential structure of C program
Step 1: Edit
Type the program I editor (source code)
Step 2: Preprocessing
During this step, the C source code is expanded based on the preprocessor directives like
as #include, #define etc,.
The expanded source code is stored in intermediate file with .i extension.
Step 3: Compilation
The expanded source code is then passed to the compiler, which identifies the syntax
error in the expanded source code. If the expanded source code is error free, the
compiler transfers it to assembly program.
The assembly code is stored I .ASM file.
So .C file would e changed and stored I .ASM
Step 4: Assembling
Assembler translates the .ASM program into relocatable object code.
Assembler translates the .asm file into .OBJ file which is binary file.
Step 5: Linking
Linking is the final step of the process i.e creating an executable program
It finds definition of all external function
It finds definition of all global variables
Combines data section
Combine code section
Step 6: Loading
Once the .EXE file created and stored on the disk, it is ready for
execution.
When it is executed it is brought from the disk into the memory(RAM)
by an OS component called Program Loader.
Rules
Preprocessing:
The preprocessor handles directives (lines starting with #) in the source code. This
includes expanding macros (#define), including header files (#include), and
conditional compilation. The output is an expanded source file.
Compilation:
The compiler translates the preprocessed C code into assembly language code. This
phase also performs syntax checking and various optimizations to improve code
efficiency.
Assembly:
The assembler converts the assembly language code into machine code, which is a
sequence of binary instructions that the computer's processor can understand. This
machine code is stored in an object file (e.g., .o or .obj extension).
Linking:
The linker combines the object files generated from your source code with
necessary library files (containing pre-compiled functions
like printf or scanf). It resolves all references to external functions and
variables, creating a single, complete executable file (e.g., .exe on Windows
or a file without an extension on Linux/macOS).
Loading:
When you run the executable file, the operating system's loader brings the
program's code and data into memory (RAM).
C character set
Lowercase Alphabets a to z a, b, c, d, e, f, g, h, i, j, k,
l, m, n, o, p, q, r, s, t, u,
v, w, x, y, z
Uppercase Alphabets A to Z A, B, C, D, E, F, G, H, I, J,
K, L, M, N, O, P, Q, R, S,
T, U, V, W, X, Y, Z
Digits
The C programming language provides the support for all the digits that help in
constructing/ supporting the numeric values or expressions in a program. These range
from 0 to 9, and also help in defining an identifier.
Thus, the C language supports a total of 10 digits for constructing the numeric values or
expressions in any program.
Digits 0 to 9 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Special characters
Some special characters in the C language for some special purposes, such as
logical operations, mathematical operations, checking of conditions,
backspaces, white spaces, etc.
These characters are used for defining the identifiers in a much better way.
For instance, we use underscores for constructing a longer name for a
variable, etc.
The C programming language provides support for the following types of
special characters
Type of Character Examples
Blank Spaces
Tab
New Line
Escape sequence
In C programming, an escape sequence is a combination of characters
used within string literals or character constants to represent special
characters or actions that are not easily represented directly.
These sequences begin with a backslash (`\`) followed by a specific
character or sequence of characters
Common Escape Sequences in C:
•\n: Newline (moves the cursor to the beginning of the next line).
•\t: Horizontal tab (inserts a tab space).
•\\: Backslash (prints a single backslash character).
•\": Double quote (prints a double quotation mark within a string).
•\': Single quote (prints a single quotation mark within a character constant or string).
•\b: Backspace (moves the cursor back one position).
•\r: Carriage return (moves the cursor to the beginning of the current line).
•\f: Form feed (advances the printer to the next page; behavior in terminals may vary).
•\v: Vertical tab (inserts a vertical tab space).
•\0: Null character (marks the end of a string).
•\ooo: Octal number (represents a character by its octal value, e.g., \101 for 'A').
•\xhh: Hexadecimal number (represents a character by its hexadecimal value, e.g., \x41 for 'A'
IDENTIFIERS AND KEYWORDS
The int data type represents integers, which are whole numbers
(positive, negative, or zero) without any fractional or decimal
components. In programming, it's used to store whole number values.
Float datatype
// Print variables
printf("%d\n", myNum);
printf("%f\n", myFloatNum);
printf("%c\n", myLetter);
constant
Type Safety:
const variables are type-checked by the compiler,
while #define macros are simply text substitutions and are not type-
checked.
Scope:
const variables have block scope (or global scope if declared globally),
while #define macros have file scope from the point of definition.
Debugging:
const variables are actual variables in memory and can be inspected in
a debugger, unlike #define macros.
#include <stdio.h>
int main() {
const int maxUsers = 100; // Define a constant using 'const'
printf("The maximum number of users is: %d\n", maxUsers);
// maxUsers = 200; // Uncommenting this will cause a compile-time
error
return 0;
}
Output:
The maximum number of users is: 100
Explanation:
const int maxUsers = 100; defines a constant integer maxUsers.
The value of maxUsers cannot be changed once it’s defined. Trying to
assign a new value to maxUsers (like maxUsers = 200;) will result in a
compile-time error.
This method provides type safety, as you can define constants with
specific types such as int, float, or char.
#include <stdio.h>
#define MAX_USERS 100 // Define a constant using '#define'
int main() {
printf("The maximum number of users is: %d\n", MAX_USERS);
// MAX_USERS = 200; // Uncommenting this will cause a compile-
time error
return 0;
}
Output:
The maximum number of users is: 100
Explanation:
#define MAX_USERS 100; defines a constant MAX_USERS with the
value 100.
Unlike const, #define doesn't assign a type to the constant, and the
value is substituted at compile-time wherever MAX_USERS is used.
This method is often used for simple constants like configuration
settings but does not provide type safety like the const keyword.