Module 3 - Basic C Program Its Execution
Module 3 - Basic C Program Its Execution
• Basic 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
• 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);
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)
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus
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
Files generated:
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)
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Thank You!
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus