Template CTSD-1
Template CTSD-1
BACHELOR OF TECHNOLOGY
Laboratory
Manual
PREFACE
It gives us immense pleasure to present the first edition of Computational Thinking for
Structure Design -1 for the B.Tech. 1st year students for PARUL UNIVERSITY.
Instructions to students
CERTIFICATE
Date of Submission:
HoD’s Signature
INDEX
CTSD - 1
Practical 1
Introduction to C Language
The basic structure of a C program is divided into 6 parts which makes it easy to read, modify,
document, and understand in a particular format. C program must follow the below-mentioned
outline in order to successfully compile and execute. Debugging is easier in a well-structured C
program.
a. Documentation
b. Preprocessor Section
c. Definition
d. Global Declaration
e. Main() Function
f. Sub Programs
1. Documentation
This section consists of the description of the program, the name of the program, and the
creation date and time of the program. It is specified at the start of the program in the form of
comments. Documentation can be represented as:
2. Preprocessor Section
All the header files of the program will be declared in the preprocessor section of the program.
Header files help us to access other’s improved code into our code. A copy of these multiple
files is inserted into our program before the process of compilation.
Example:
3. Definition
Preprocessors are the programs that process our source code before the process of compilation.
There are multiple steps which are involved in the writing and execution of the program.
Preprocessor directives start with the ‘#’ symbol. The #define preprocessor is used to create a
constant throughout the program. Whenever this name is encountered by the compiler, it is
replaced by the actual piece of defined code.
Example:
4. Global Declaration
The global declaration section contains global variables, function declaration, and static
variables. Variables and functions which are declared in this scope can be used anywhere in the
program.
Example:
5. Main() Function
Every C program must have a main function. The main() function of the program is written in
this section. Operations like declaration and execution are performed inside the curly braces of
the main program. The return type of the main() function can be int as well as void too. void()
main tells the compiler that the program will not return any value. The int main() tells the
compiler that the program will return an integer value.
Example:
void main()
or
int main()
6. Sub Programs
User-defined functions are called in this section of the program. The control of the program is
shifted to the called function whenever they are called from the main or outside the main()
function. These are specified as per the requirements of the programmer.
Example:
%i Unsigned integer
%lf Double
%o Octal representation
%p Pointer
%s String
%u Unsigned int
%x or %X Hexadecimal representation
%n Prints nothing
%% Prints % character
Escape
Sequence Name Description
\n New Line It moves the cursor to the start of the next line.
\r Carriage Return It moves the cursor to the start of the current line.
Hexadecimal
\xhh It represents the hexadecimal number.
Number
1.1
Input:-
Output:-
1.2
Input:-
Output:-
1.3
Input:-
Output:-
1.4
Input:-
Output:-
1.5
Input:-
Ouput:-
Conclusion:-
Practical 2 on basic C programs with input and output (I/O) involves writing programs to demonstrate the
use of various input and output functions, expressions, and functional blocks. Here are some examples of
I/O functions in C:
printf()
printf is a C function belonging to the ANSI C standard library, and included in the file stdio. h. Its purpose
is to print formatted text to the standard output stream. Hence the "f" in the name stands for "formatted". It
takes the following unusual syntax: printf(string format, items-to-format).
scanf().
The scanf function in C is a standard library function that reads and parses text from the standard input
device, usually the keyboard. It allows the program to pause and wait for the user to enter data. The
function's name is short for "scan formatted".
2.1
Input:-
Output:-
2.2
Input:-
Output:-
2.3
Output:-
2.4
Output:-
2.5
Output:-
Conclusion:-
A branching statement is a computer program instruction that changes the order of instructions executed. It
allows the program's flow of execution to jump to a different part of the program. Branching statements are
used to control the flow of control based on conditions, which helps guide the program's decision-making
process. The common branching statements used within other control structures include: break , continue ,
return , and goto . The goto is rarely used in modular structured programming.
Here are its types:
Multiway branch
This is a form of conditional statement in C that is often the most efficient method of passing control to one
of a set of program labels.
3.1
Input:-
Ouput:-
3.2
Input:-
Output:-
3.3
Input:-
Output:-
3.4
Input:-
Output:-
3.5
Input:-
Ouput:-
3.6
Input:-
Ouput:-
Practical 4
While & Do-While Statements
A while loop in C is a pre-tested loop that executes a section of code while a condition is true. The loop
evaluates the condition before each iteration. If the condition is true, the loop body executes. The loop
repeats until the condition is false, at which point the loop terminates.
For example, if i is initialized to 1 before the loop, and the condition is i <= 10, the loop will execute
until i becomes 10.
While loops can be used to prompt a user to enter information indefinitely. For example, you can use a
while loop to check if a user has typed in a name.
A do while loop in C is a structured loop that executes a code block at least once and repeats it while a
condition is true. The loop body statement is any valid C statement or block, and the condition expression
is evaluated at the end of each loop pass. If the condition expression is false, the loop exits.
A while loop is similar to a do while loop, but it tests the condition expression before each loop pass. This
means that a while loop may execute its loop body statement zero times.
You can use the break command to terminate a do while loop from any point other than the logical end.
Practical 5
The for loop in C language is used to iterate the statements or a part of the program several times. It is
frequently used to traverse the data structures like the array and linked list.
Then, the test expression is evaluated. If the test expression is evaluated to false, the for loop
is terminated.
However, if the test expression is evaluated to true, statements inside the body of the for loop
are executed, and the update expression is updated.
Again the test expression is evaluated.
This process goes on until the test expression is false. When the test expression is false, the loop
terminates.