Week 2_Basic C Program
Week 2_Basic C Program
Reading Objective
In this reading, you will learn to write a C program. You will gain insights into the essential elements
of a C program. You will then learn how to compile the program using a compiler on Coursera Labs
and execute it on the processor.
How to Write a C Program?
First, we must understand the various elements essential to writing a C program. Then we need to
define each step the program will take before we write the actual code.
Let’s get started with our first C program. We will write a program to find out the sum of two
numbers.
The program steps are as follows:
1. START
2. Input the numbers num1, num2
3. Initialize sum = 0
4. Set sum = num1 + num2
5. Print sum
6. END
Code Snippet 0 shows an example program to determine the sum of two numbers.
num1 = 34;
num2 = 54;
a. Preprocessor directive
In Code Snippet 0, we see the line #include<stdio.h> that instructs the preprocessor to insert the
contents of the file stdio.h at the beginning of the program. We must know that a file with a .h
extension is a header file that informs the C compiler about standard pre-written C functions that
other users can use in their program. Now, stdio.h is a header file that contains information about
functions in the C programming language to perform input and output operations, where stdio
means Standard Input Output. This file is stored in a standard directory where the compiler stores
all the header files.
b. Comments
In Code Snippet 0 and 1, we can see a lot of texts written in green color. These are comments.
Comments are statements written in natural language, usually English, to enhance the readability of
our code for the programmer and others. Programmers use these comments to explain the logic
they implement in a particular line or function. Comments can be employed to convey related
information to the human reader of the program to help them understand the code better. There are
two ways we can create comments in C. First, we can write a // to make a single-line comment
extending until the end of the current line. And second, we can write any text between /* and */,
which is considered a multi-line comment.
/* This is a multi-line
comment.*/
c. Functions
There are some tasks that we want to perform repeatedly in a program. For this purpose, we can
use lines 8 and 9 in Code Snippet 0, which are called the printf( ) and the scanf( ) functions,
respectively. We can easily observe the parenthesis indicating printf and scanf names of functions in
C.
● printf() function allows us to print something on the screen
● scanf() function allows us to take input from the user into a C program
We can call a function by its name to execute a predefined task/functionality. We can also pass
some parameters to a function to instruct it on how to perform the task. For example, we can write a
function that calculates the square of a number and passes the number to the function as a
parameter. Once we have written a function, multiple programs can use it to execute the same
function.
We can also look at the compilation, another commonly used terminology in computer programming.
The compilation converts high-level code written in a particular programming language into low-level
machine code understandable by the computer.
d. Handling errors
We can encounter some problems called errors during the compilation or execution of a computer
program. Errors can also occur after successful compilation and execution, but with incorrect output.
There are three types of errors: compile-time errors, run-time errors, and semantic errors.
● Compile-time errors occur when some statements in our code do not follow the rules of the
programming language in which they are written. These errors are raised by the compiler
while trying to compile our code.
For example, in Fig. 1, we can see that all the statements between lines 6 to 11 in the C program
end in a semicolon. If we forget to put a semicolon, it raises a compile-time error. int a, b, sum;
● Run-time errors occur during the execution of our program but are not detected during
compilation.
For example, a statement a = b / c; can cause an error if the value of c is zero.
● Semantic errors occur when the code compiles and executes successfully but does not
perform the intended task.
For example, if we use == instead of using =, the meaning of the expression will change completely.
However, the program will still compile and execute properly. a = b means the value of b is assigned
to a (and in this case it’ll assign b to a AND evaluate to True), whereas a == b is a Boolean
expression whose value is True if values of a and b are the same, and False otherwise.
Let us now look at compiling and running a C program. Fig. 2 shows the steps involved in running a
C program. It shows how the operating system loads the program into RAM and then executes it line
by line on the CPU.
Reading Summary:
In this reading, you have learned the following:
● Essential elements of a C program, like preprocessor directives, functions, and comments
● How to write your first C program
● Compiling and handling some basic kinds of errors in C programs
● Running the commands and executing a C program