100% found this document useful (1 vote)
254 views

Chapter 5 - Introduction To C Programming Language

This chapter introduces the C programming language. It describes the characteristics of C including its small size, use of functions and pointers. The chapter outlines the process of developing and executing a C program, including compilation and linking. It differentiates between syntax errors found by the compiler and logical errors. The chapter also describes the typical structure of a C program, including the main function and use of other functions. It discusses libraries that provide common functionality to C programs.

Uploaded by

Freddy Mutua
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
254 views

Chapter 5 - Introduction To C Programming Language

This chapter introduces the C programming language. It describes the characteristics of C including its small size, use of functions and pointers. The chapter outlines the process of developing and executing a C program, including compilation and linking. It differentiates between syntax errors found by the compiler and logical errors. The chapter also describes the typical structure of a C program, including the main function and use of other functions. It discusses libraries that provide common functionality to C programs.

Uploaded by

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

INTRODUCTION TO PROGRAMMING & ALGORITHM

Chapter 5: Introduction to C Programming Language

Chapter Objectives
By the end of this chapter the learner should be able to;
 Describe the characteristics of C programming language.
 Describe the process of developing and Executing a C program
 Describe the compilation process of a C program and C program file naming conventions.
 Differentiate between Syntax and Logical Errors
 Describe the structure / format of a C Program

5.1. Introduction
C is an imperative (procedural) systems implementation language. C is one of a large number of
high level languages which can be used for general purpose programming, that is, anything from
writing small programs for personal amusement to writing complex applications. C is a general-
purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie
at Bell telephone Laboratories.

5.2. Characteristics of C
We briefly list some of C's characteristics that define the language and also have lead to its popularity as
a programming language.
 Small size
 Extensive use of function calls
 Loose typing
 Structured language
 Low level (BitWise) programming readily available
 Pointer implementation - extensive use of pointers for memory, array, structures and functions.

5.3. Advantages, Disadvantages and Features of C program


C has now become a widely used professional language for various reasons.
1) It has high-level constructs.
2) It can handle low-level activities.
3) It produces efficient programs.
4) It can be compiled on a variety of computers.

Features
1) C library, a library is a set of functions
2) C allows meaningful variable names and meaningful function names to be used in programs
without any loss of efficiency and it gives a complete freedom of style;
3) it has a set of very flexible loop constructions (for, while, do) and neat ways of making
decisions.
4) C tries to make the best of a computer by linking as closely as possible to the local
environment.

Chapter 5 Introduction to C Programming Language Page 1


5) The C compiler combines the capabilities of an assembly language with features of a high-
level language thus making it suited for writing both system software and business packages.
6) C program uses a variety of data types and operators thus making programs written in C to
be efficient and fast.
7) C is highly portable and is well suited for structured programming.
8) C is basically a collection of functions that are supported by the C library and because new
functions can be added to the C library, C has the ability to extend itself.

Disadvantages
Its main drawback is that it has poor error detection which can make it off putting to the
beginner. However diligence in this matter can pay off handsomely since having learned the
rules of C we can break them. Not many languages allow this. This if done properly and
carefully leads to the power of C programming.

5.4 Executing a C program


The steps involved in executing a C program includes;
 Creating the program
 Compiling the program
 Linking the program with functions that are needed from the C library
 Executing the program

5.5. Compiling a C Program


A C program is first written in the form of a number of text files using a screen editor. This form
of the program is called the source program. It is not possible to execute this file directly. The C
Compilation process involves the following steps;
1) Compilation. A compiler translates the source code into machine code, and the compiled
code is called the object code.
2) Linker: A linker links the object code other object code that readies the program for
execution. An Executable code is generated

During the stages of compilation, linking, and running, error messages may occur that require
the programmer to make corrections to the program source (debugging). The cycle of modifying
the source code, compiling, linking, and running continues until the program is complete and
free of errors.

5.6. C Program File naming convention


The compiler uses a special convention for the file names, so that we do not confuse their
contents. The name of a source program (the code which you write) is filename.c. The compiler

Chapter 5 Introduction to C Programming Language Page 2


generates a file of object code from this called filename.obj. The final program, when linked to
libraries is called filename.exe

Source code: Filename.c


Object code: Filename.obj
Executable code: Filename.exe

Myprog.c myprog.obj myprog.exe

Written in C Written in Written in


machine Machine
language language

Via compiler via linker


Other code from library
Written in machine language

5.7. C Program Errors


Errors are mistakes which we the programmers make. Bbasically there are three types of errors
in C programming:
1) Runtime Errors
2) Compile Errors – Syntax errors
3) Logical Errors – Semantic errors

5.7.1. C Runtime Errors


C runtime errors are those errors that occur during the execution of a c program and generally
occur due to some illegal operation performed in the program. Examples of some illegal
operations that may produce runtime errors are:
 Dividing a number by zero
 Trying to open a file which is not created
 Lack of free memory space

5.7.2. Compile Errors


Compile errors are those errors that occur at the time of compilation of the program. C compile
errors may be further classified as:
1) Syntax Errors: When the rules of the c programming language are not followed, the compiler
will show syntax errors. Every language has got set of rules. If you make a mistake while
using the language, then it is called syntax error.

5.7.3. Logical Errors – Semantic Errors


Errors in goal or purpose (logical errors) occur when you write a program that works, but does not do
what you intend it to do. Logical errors are the errors in the output of the program. The presence of
logical errors leads to undesired or incorrect output and are caused due to error in the logic applied in the
program to produce the desired output.

Chapter 5 Introduction to C Programming Language Page 3


Also, logical errors can not be detected by the compiler, and thus, programmers has to check the
entire coding of a c program line by line.

5.8. C Libraries
In C, a library is a set of functions contained within a single "archive" file. The core of the C
language is small and simple. Special functionality is provided in the form of libraries of ready-
made functions. This is what makes C so portable. Libraries are files of ready-compiled code
which we can merge with a C program at compilation time. Libraries provide frequently used
functionality and, in practice, at least one library must be included in every program: the so-
called C library, of standard functions.

Each library comes with a number of associated header files which make the functions easier to
use. Header files contains the prototypes of the functions contained within the library that may
be used by a program, and declarations of special data types and macro symbols used with these
functions. It is up to every programmer to make sure that libraries are added at compilation time
by typing an optional string to the compiler.

5.9. C Program Structure


C program is can be divided into modules and functions.
1) Modules: A module is a set of functions that perform related operations. A simple program
consists of one file; i.e., one module. More complex programs are built of several modules.
Modules have two parts: the public interface, which gives a user all the information
necessary to use the module; and the private section, which actually does the work.
2) Functions; the basic building block in a C program is the function. In general, functions are
blocks of code that perform a number of pre-defined commands to accomplish something
productive. It must have a name and it is reusable ie it can be executed from as many
different parts in a C Program as required. Information passed to the function is called
arguments and is specified when the function is called. And the function either returns some
value to the point it was called from or returns nothing.

Every C Program will have one or more functions and there is one mandatory function
which is called main() function. This function is prefixed with keyword int which means this
function returns an integer value when it exits. This integer value is returned using return
statement.

A C program includes the following sections


1) Documentation Section
2) Linker Section
3) Definition Section
4) Global Declaration Section
5) Main() Function Section
{
a. Declaration section
b. Executable Section
Chapter 5 Introduction to C Programming Language Page 4
}
6) Sub-program Section
Function 1
Function 2
Function3

5.9.1. Documentation Section


This section consists of a set of comments lines giving the name of the program, the author and
other details which the programmer would like to use later. Comments starts with /* and ends
with */ and enhances readability and understandability. Comment lines are not executable
statements ie. executed and are ignored by the compiler. Comments can be inserted wherever
there is a blank a space but cannot be nested (having a comment within a comment)
Syntax for comment /* comment */
Example /* …………………./* ……………..*/ ……………….*/

5.9.2. Link Section


This section provides instructions to the compiler to link functions from the system library. C
program have predefined functions stored in the C library. Library functions are grouped
category-wise and stored in different file known as header files. To be able to access the library
files it is necessary to tell the compiler about the files to be accessed.
Instruction Format: #include<file_name>

Library Files have a .h file extension


The format for including the header file is #include header.h

Examples of Libraries header files


 Stdio.h, (printf() function): standard input/output library
 maths.h (for mathematical functions) etc.
 conio.h (for handling screen out puts such as pausing program execution getch() function)

5.9.3. Definition Section


This section allows the definition of all symbolic constants. Statements begin with # sign and do
not end with a ; because the statements are compiler directive statements
Example #define PRINCIPLE 10000

Symbolic constants are usually written in upper case to distinguish them from lower case
variables. Values defined here remain constant throughout the program.

5.9.4. Declaration Section


Section used to declare global variables. The section is also used to declare user defined
functions.

Chapter 5 Introduction to C Programming Language Page 5


5.9.5. main() Function Section
The main() function is a special function used by C system to tell the computer where the
program starts. Every program must have exactly one main function. The main function is the
point by where all C programs start their execution, independently of its location within the
source code. It does not matter whether there are other functions with other names defined
before or after it - the instructions contained within this function's definition will always be the
first ones to be executed in any C program. For that same reason, it is essential that all C
programs have a main function. The main function section has the following sections
 Declaration part: where all variables used in the executable section are declared.
 Executable part: consist of the statements to be executed.
{
Declaration part
Executable part
}
There must be a least one statement in the executable part. The two part must be included
between the opening {and the closing}. Program execution begins at the opening { and closes at
the closing }, which signifies the logical end of the program. All the statements between the {
and } forms the function body and are the instructions to perform the given task. All statements
in the Declaration and Executable parts must end with a semicolon (;)
Formats of main()
 main()
 int main()
 void main()
 main(void)
 int main(void)
( ) or word void means the function has no arguments or parameter and thus does not return any
information to the operating system. int means the function return an integer value to the
operating system

5.10. Sub-program section


Contains all user defined functions that are called in the main function
main() /* Main program */
{
do_nothing(); /* Function call */
}
/******************************************************/
do_nothing() /* Function called */
{
}
Program Example 5.1 Program Example 5.2
main() /* program to print Hello */
{ #include <stdio.h>
printf (“This is my \n”); #include<conio.h>

Chapter 5 Introduction to C Programming Language Page 6


printf(“computer book”);
getch(); int main()
} {
OR printf("hello, world\n");
main(() getch();
{ }
print(“This is \n my computer book”);
getch();
}
Program Example 5.3 Program Example 5.4
/* program to calculate the Interest rate */
/* program for addition */ #include <stdio.h>
#include<stdio.h> #include<conio.h>
#include<conio.h> #define PERIOD 10
main() #define PRINCIPAL 5000.00
{ int number; int main()
float amount; {
number = 100; int year;
amount = 30.75 + 75.35; float amount, value, inrate;
printf("%d\n", number); amount = PRINCIPAL;
printf("%5.2f", amount); inrate = 0.11;
getch(); /* pause the results on year = 0;
the screen */ while(year <= PERIOD)
} {printf("%2d %8.2f\n", year, amount);
value = amount + inrate * amount;
year = year + 1;
amount = value;
}
getch();
}

Syntax for C Program


 C is a case sensitive programming language. It means in C printf and Printf will have
different meanings.
 C has a free-form line structure. End of each C statement must be marked with a semicolon.
 Multiple statements can be done on the same line.
 White Spaces (ie tab space and space bar) are ignored.
 Statements can continue over multiple lines.
 Printf is a predefined C function for printing out put
 Everything between the starting and ending quotation marks “ ” to be printed.
 To print on separate line; Use the command \n
 int = integer data type
 float = floating point number data type

Chapter 5 Introduction to C Programming Language Page 7


 %d = formatting command that prints the output as a decimal integer
 %5.2f” = formatting command that prints the output as a floating point integer with five
places in all and two places to the right of the decimal point.

5.11. Breaking out early


Return statement
The program can simply call return(value) anywhere in the function and control will jump out of
any number of loops or whatever and pass the value back to the calling statement without having
to finish the function up to the closing brace }.

5.12. The exit() function


The function called exit() can be used to terminate a program at any point, no matter how many
levels of function calls have been made. This is called with a return code, like this:
#define CODE 0
exit (CODE);
This function also calls a number of other functions which perform tidy-up duties such as
closing open files etc.

Chapter Review Questions


1. How is a library file incorporated into a C program? Name the most common library file in
C.
2. What is another name for a library file?
3. Describe the structure of C Program
4. Distinguish between
a) main() and main(void)
b) int main() and void main()
5. Find errors if any in the following
#include <stdio.h>
Void main()
{ Print (“Hello C);
}

Chapter 5 Introduction to C Programming Language Page 8

You might also like