0% found this document useful (0 votes)
0 views38 pages

Program Structure 1

The document outlines the structure and components of C programs, emphasizing the importance of the main function, preprocessor commands, functions, variables, and the compilation process. It details the sequential steps of editing, preprocessing, compiling, assembling, linking, and loading, along with rules for identifiers and keywords in C. Additionally, it explains data types, constants, and the differences between const variables and #define macros.

Uploaded by

jsaurav2468
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views38 pages

Program Structure 1

The document outlines the structure and components of C programs, emphasizing the importance of the main function, preprocessor commands, functions, variables, and the compilation process. It details the sequential steps of editing, preprocessing, compiling, assembling, linking, and loading, along with rules for identifiers and keywords in C. Additionally, it explains data types, constants, and the differences between const variables and #define macros.

Uploaded by

jsaurav2468
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 38

Program structure

 C programs built with the help of function. It is essential that one


function must have name main.
 The main function is the part of program, from where the execution
starts, which may access other functions.
 any other function definitions must be defied separately , either ahead
of or after main function.
 C program has following elements
Preprocessor Commands
 Directives like #include<stdio.h> tell the compiler to include necessary
files before compilation
Functions
 Building blocks of C program, including the mandatory main() function,
which returns an integer value.
Variables
 Used to store data such as numbers, strings and complex informationfor
computations
Statements & Expression
 Statements are made of expression, assignment, function calls, and
control flow structures enclosed in {}
 Comments
 Used to provide additional information about the program. They can be
multiline(/**/) or single line (//).
Example program

#include<stdio.h> /*This header file need to use printf() function */


int main()
{
Printf(“C Programming”); /*displays content inside quotation*/
return 0; /*main/() returns integer value*/
}
Output:
C Programming
Explaination

 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

 It is case sensitive programming language.


 IF is not same as if
 Every statement should end with semicolon ;
 Multiple statements can be on same line.
 White spaces are ignored
 Statements can continue over multiple lines
Compilation Process

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

 In the C programming language, the character set refers to a set of all


the valid characters that we can use in the source program for forming
words, expressions, and numbers.
 The source character set contains all the characters that we want to
use for the source program text.
 On the other hand, the execution character set consists of the set of
those characters that we might use during the execution of any
program.
 Thus, it is not a prerequisite that the execution character set and the
source character set will be the same, or they will match altogether
Types of Characters in C

 The C programming language provides support for the following types


of characters. In other words, these are the valid characters that we
can use in the C language:
 Alphabets
 Digits
 Special Characters
 All of these serve a different set of purposes, and is used in different
contexts in the C language.
 Alphabets
 The C programming language provides support for all the alphabets that we
use in the English language. Thus, in simpler words, a C program would
easily support a total of 52 different characters- 26 uppercase and 26
lowercase.

Type of Character Description Characters

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.

Type of Character Description Characters

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

Special Characters `~@!$#^*%&()[]{}<>+=


_–|/\;:‘“,.?
White spaces

 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

 Identifiers are names used to identify various program elements such as


variables, functions, arrays, structures, unions, and labels. They serve as
unique labels to refer to and manipulate these elements within the code.
int total;
Double remainingBalance;
Here, total and remainingBalance are identifiers
Rules
 The first character of an identifier must be an alphabet (uppercase or lowercase)
or an underscore. It cannot be a digit.
 Identifiers can consist of alphanumeric characters (A-Z, a-z, 0-9) and the
underscore symbol (\_).
 Identifiers cannot be C keywords (reserved words like int, for, while, if, etc.).
Identifiers cannot be C keywords (reserved words like int, for, while, if, etc.).
 Identifier are case sensitive, meaning that myVar, MyVar, and MYVAR are
considered distinct identifiers.
 Identifiers cannot contain spaces or other special characters (e.g., @, #, $)
 While an identifier can be arbitrarily long, only the first 31 characters are typically
considered significant by ANSI C compilers.
 Valid Identifiers: age, total_score, _data, functionName, variable1
 Invalid Identifiers: 1stVariable (starts with a digit), my
name (contains a space), if (is a keyword), total@value (contains a
special character)

Keywords

 Keywords in C are reserved words that have predefined meanings and


purposes within the language. They are integral to the syntax and
structure of C programs and cannot be used as identifiers (like variable
names or function names).
 All keywords in C are case-sensitive and must be written in lowercase.
Example

auto break case char const continue default do

double else enum extern float for goto if

int long register return short signed sizeof static

struct switch typedef union unsigned void volatile while


C utilizes various data types to define the type of data a variable
can hold
Int datatypes

 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

 The float data type in C is used to store single-precision floating-point


numbers, which are numbers that can have a decimal point or be
expressed in scientific notation..
Char datatype

 The char data type is used to store a single character.


 The character must be surrounded by single quotes, like 'A' or 'c', and
we use the %c format specifier to print it:

 int myNum = 5; // Integer (whole number)
float myFloatNum = 5.99; // Floating point number
char myLetter = 'D'; // Character

// Print variables
printf("%d\n", myNum);
printf("%f\n", myFloatNum);
printf("%c\n", myLetter);
constant

 A constant is a value that cannot be modified once it has been


defined. Its value remains fixed throughout the program's
execution. Constants are used to represent values that are known at
compile-time and do not change during runtime, improving code
readability and maintainability.
 The const keyword is a type qualifier that declares a variable as
constant, meaning its value cannot be changed after initialization.
 The #define directive creates a symbolic constant, which is a macro
that the preprocessor replaces with its defined value before
compilation
Differences:

 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.

You might also like