0% found this document useful (0 votes)
79 views

Module 3 - Basic C Program Its Execution

The document discusses the basics of writing and executing a C program. It explains the steps to write a simple C program to calculate the sum of two numbers. These include writing the code, saving it as a file with a .c extension, compiling the code to generate an executable, and running the executable to output the result. It describes the compilation process involving a preprocessor, compiler, linker and assembler. The executable file is loaded into RAM for execution by the CPU line by line. Comments are encouraged for readability. Errors can occur during compilation or execution.

Uploaded by

ADITYA CHAKRANI
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views

Module 3 - Basic C Program Its Execution

The document discusses the basics of writing and executing a C program. It explains the steps to write a simple C program to calculate the sum of two numbers. These include writing the code, saving it as a file with a .c extension, compiling the code to generate an executable, and running the executable to output the result. It describes the compilation process involving a preprocessor, compiler, linker and assembler. The executable file is loaded into RAM for execution by the CPU line by line. Comments are encouraged for readability. Errors can occur during compilation or execution.

Uploaded by

ADITYA CHAKRANI
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Module 3 – Basic C Program

and its execution


Dr. Jagat Sesh Challa
BITS Pilani Dr. Amitesh Singh Rajput
Pilani Campus
Department of Computer Science & Information Systems
Module Overview

• Basic C Program

• Compilation and Execution of a C Program

Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus

Basic C Program
Steps of Programming Practices
• Step1: Requirements

• Step2: Creating a flow chart

• Step3: Creating algorithms

• Step4: Writing Code

• Step5: Debugging

• Step6: Documentation
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Example – Sum of Two
Numbers

1. START
2. Initialize sum=0
3. Input the numbers num1, num2
4. Set sum = num1 + num2.
5. Print sum
6. END

Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
C Code for Sum of Two
Numbers
/* myfirst.c: to compute the sum of two numbers */
#include<stdio.h> //Preprocessor directive
/*Program body*/
int main()
{
int a, b, sum; //variable declarations
printf("Please enter the values of a and b:\n");
scanf("%d %d", &a, &b);
sum = a + b; // the sum is computed
printf("The sum is %d\n", sum);
return 0; //terminates the program
}
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
C Code for Sum of Two
Numbers

Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Header files
• #include<stdio.h>
• A directive to place the contents of the header file, stdio.h,
in the program
• A header file contains data used by multiple programs
• Processed by a preprocessor

Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Functions
printf("Please enter the values of a and b:\n");
scanf("%d %d", &a, &b);

• Called by its name to execute a set of statements


• Multiple programs can use the same function
• The above functions are used to print something and read something
in a C program

Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Adding comments
Judicious use of comments helps in easy understanding of the code
(for yourself and others)
/* This is a comment */
// And so is this

Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Handling errors
• Syntax errors
• When a statement fails to conform to the rules of the C language
• int a, b, sum
• Semantic errors What is the syntax
• An error in the meaning error here?
• Run-time error
• Eg. Using = instead of == (or vice-versa)

Used for checking if two


variables have the same value,
Used for assigning value of one e.g.: if (a == b)
variable to the other,
e.g. a = b

Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus

Compilation & Execution of a C Program


Steps to run a C Program
1) Write the program and save it Disk
– Where does it get saved?
2) Compile the program to generate its executable
– Where will the executable be saved?
Disk
3) Run the executable
– Where will the executable run?
Executable is
loaded into RAM
and executed line
by line on the CPU

Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Retrospection of Program
execution (in a better way)
• OS loads the
program executable
into the RAM
• Executes it line by
line on CPU

A line Program Executable Program


from exe (exe) (P1)
Program is Compiled
executed Executable
Program of P1 (exe)
on CPU
CPU Executable gets
line by line
loaded into the
RAM

Memory (RAM) DISK


Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
The compilation process
The Preprocessor
• lines beginning with # (preprocessor directives)
• #include… or #define PI 3.142
• Source code expanded, comments removed
The Compiler
• Checks the source code for errors
• Creates object code (not ready to run) (why?)
The Linker
• Acts on unresolved references left by compiler
• printf, scanf… their machine code is added
• This code is ready to run
The Assembler (optional)
• Some compilers first translate to assembly language.
• The assembler converts it to machine code
Source of image: https://www.javatpoint.com/flow-of-c-program Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Compiling C Program
Say, our C program is stored in myfirst.c
To generate executable file, navigate to the directory where
myfirst.c is stored and run the following command:
gcc myfirst.c

Files generated:
a.out

The above process is known as compilation of the program to


generate the executable a.out. To run the executable:
./a.out

Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Compiling C Program (contd.)
C Compiler allows you to generate intermediate temporary files
during its compilation. To generate them use the following
command:
gcc myfirst.c -save-temps -o myfirst.exe
Files generated:
myfirst.exe
myfirst.o
myfirst.s
myfirst.i
myfirst.c (our original C code)

Exercise: Open each of the above files in a suitable text editor


and see what is inside…
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Write your first code
Write a program in C to compute the average of 3 numbers. Take
the numbers as an input by the user.
Incorporate basic error checking as well.

Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Thank You!

Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus

You might also like