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

Week 2_Basic C Program

This document provides a comprehensive guide on writing a basic C program, including essential elements like preprocessor directives, functions, and comments. It details the steps to compile and run a C program, along with explanations of common errors encountered during compilation and execution. The reading culminates in a practical example of a C program that calculates the sum of two numbers.

Uploaded by

Bibin K Bino
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Week 2_Basic C Program

This document provides a comprehensive guide on writing a basic C program, including essential elements like preprocessor directives, functions, and comments. It details the steps to compile and run a C program, along with explanations of common errors encountered during compilation and execution. The reading culminates in a practical example of a C program that calculates the sum of two numbers.

Uploaded by

Bibin K Bino
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

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

# include <stdio.h> /* Preprocessor Directive */


/* Program Body */
int main()
{
int num1,num2,sum; /* Variable declarations */

printf("Enter values of num1 and num2:\n");


scanf("%d %d",&num1,&num2);

sum = num1 + num2; /* Sum computed */


printf("Sum = %d\n",sum);

return 0; /* Terminates the program */


}

Code Snippet 0: Program to print the sum of two numbers

Code Snippet 0 shows an example program to determine the sum of two numbers.

# include <stdio.h> /* Preprocessor Directive */


/* Program Body */
int main()
{
int num1,num2,sum; /* Variable declarations */

num1 = 34;
num2 = 54;

sum = num1 + num2; /* Sum computed */


printf("Sum = %d\n",sum);
return 0; /* Terminates the program */
}
Code Snippet 1: Shown above is an interactive code snippet for you to try it out hands on. Go
ahead, change the values of num1 and num2 and convince yourself that it actually works as you
intend it to (while you're at it, change to the printf message as well if you like).
Let us understand the individual elements of the program.

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.*/

It is worth noting that a multi-line comment cannot be nested.

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.

How to Compile and Run a C Program?

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.

Steps to run a C program


● Firstly, we write a C program in any file with the extension .c and store it on disk. The .c
extension denotes files written in the C programming language.
● Then, we compile our program and generate an executable file using the gcc <filename>.c
command on the Terminal (for example, Windows Powershell or Ubuntu Terminal). For
example, gcc myFirst.c
● The above compilation generates an executable file named a.out and stores it on disk.
● Now, we can execute our program by running the ./a.out command (again on the Terminal).
● When we run the above command, the OS loads the executable file onto the RAM and
instructs the CPU to execute it line by line.

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

You might also like