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

Module 3 - Basic C Program Its Execution

Uploaded by

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

Module 3 - Basic C Program Its Execution

Uploaded by

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

Module 3 – Basic C Program

and its execution


BITS Pilani Dr. Tejasvi Alladi
Pilani Campus
Department of Computer Science & Information Systems
Module Overview

• Basic C Program

• Compilation and Execution of a C Program

• Errors in C Programs

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
Start

Declare variables num1,


num2 and sum 1. START
2. Initialize sum=0
Read num1 and 3. INPUT the numbers num1, num2
num2
4. Set sum = num1 + num2.
sum = num1+num2 5. PRINT sum
6. END
Display sum

Stop

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() The main() function where any
program’s execution begins
{
int a, b, sum; //variable declarations
printf("Please enter the values of a and b:\n");
scanf("%d %d", &a, &b); Block of the
sum = a + b; // the sum is computed main() function.
printf("The sum is %d\n", sum); Any block is
enclosed in {…}
return 0; //terminates the program
} a, b, sum are variables or A block consists of
placeholders for values statements ending with ;
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
• printf is used to print some value to the screen
• scanf is used to read some value from the user
• main is a function
• A program can consist of many more user-defined functions.

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

/* This is a comment */
// And so is this

Judicious use of comments helps in easy understanding of the code


(for yourself and others)

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
– Where does it get saved? Disk
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
(Illustrated)

Pre- Expanded Assembly


C Program Compiler
Processor Source Code Code
simple.c simple.i simple.s

Assembler

Execution on Executable
Loader Linker Object Code
RAM and CPU Code
simple_exe simple.o

Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
The compilation process
The Preprocessor
• A Text manipulation phase that gives expanded source code
to be fed into the compiler
• Removes comments
• Processes lines beginning with # (preprocessor directives)
• #include… or #define PI 3.142

The Compiler
• Checks the source code for errors
• Creates object (or machine) code (not ready to run) (why?)

Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
The compilation process
(contd.)
The Linker
• Acts on unresolved references left by compiler
• printf, scanf… their machine code is added
• Combines multiple object files resultant of compiler phase
• This code is ready to run

The Assembler (optional)


• Some compilers first translate to assembly language.
• The assembler converts it to machine code

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 (executable): 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
BITS Pilani
Pilani Campus

Errors in C Programs
Errors in C Programs

Syntax Error

Run-time Error

Logical Error

Semantic Error

Linker Error

Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Syntax Errors
• Occur when programmers make mistakes in typing the code's
syntax correctly
• Rules of C syntax are not followed
• Detected in the compiler phase
• Programmers can detect and rectify them easily
• Most common syntax errors:
– Missing semi-colon (;)
– Missing or open parenthesis ({})
– Assigning value to a variable without declaring it

Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Exercise: Find out Syntax Errors
in this Program
#include <stdio.h>
undeclared
void main() variable
{ var
int a = 10;
missing
var = 5;
semi-colon
printf("The variable is: %d", var)
for (int i = 0;) { wrong
syntax
printf(”BITSians on ROLL");
}
missing
parenthesis

Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Run-time Errors
• Errors that occur during the execution of a program
• Occur after the program has been compiled successfully
• Identified only when a program is running
• Common run-time errors:
– Arithmetic errors (e.g., division by zero)
– calculating square root of -1
– array index out of bounds
– infinite loops due to missing terminating conditions
– memory leaks

Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Example
#include <stdio.h>

void main() {
int numerator = 10, denominator = 0;
int result = numerator / denominator; //
Division by zero
printf("Result: %d\n", result);
}

Output:
Floating point exception (core dumped)

Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Logical Error
• Code successfully runs without compilation and/or run-time
errors
• But output is not as intended
• There is a logical error

Consequence:
In 1999, NASA lost a spacecraft due to a logical error

Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Example of Logical Error
#include <stdio.h>
void main() {
// Thruster data in metric units (newton-seconds)
double metricThrusterData = 1000.0;

// Navigation software expects thruster data in


imperial units (pound-seconds)
double navigationExpectation = 225.0; // Expected
imperial thruster data

// Error: Using metric thruster data directly in


navigation software
if (metricThrusterData == navigationExpectation) {
printf("Navigation is on track.\n");
} else {
printf("Navigation is off track.\n");
}
}
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Example of Logical Error
#include <stdio.h>
void main() {
float a = 10, b = 5;
if (b = 0) // we wrote = instead of ==
{
printf("Division by zero is not possible");
}
else
{
printf("The output is: %f", a/b);
}
}

Output:
The output is INF

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

Errors that occur because the compiler is unable to understand the


written code, although the code adheres to the syntax structure.

Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Example:
#include <stdio.h>
void main()
{
int a, b, c;
a * b = c; // This will generate a semantic error
}

Output:
error: lvalue required as left operand of assignment

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

Can occur when we try linking multiple files to create an


executable.

We will study in one of the lab sheets.

Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Don’t be errored!

Don’t worry if you haven’t understood everything!

You will experience all these errors when you start coding.

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.

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

Thank you
Q&A

You might also like