You are on page 1of 26

OVERVIEW OF C

INTRODUCTION
C an offspring of Basic Combined
Programming Language (BCPL) called B
B Language developed in 1960s at
Cambridge University.
B language modified by Dennis Ritchie and
implemented at Bell Laboratories in 1972,
new language was named C
C - Developed with the UNIX OS
UNIX OS developed at Bell Laboratories
and was coded in C

IMPORTANCE OF C
C Programs are efficient, fast and highly portable
Portable: C program written for one computer can be
run on another computer
Middle-Level Language: C compiler combines the
capabilities of an assembly language with the features of
high level language and therefore it is suited for writing
both system software and application software
(business package)
Structured Language: Programming with structured
languages is efficient and fast due to modular approach
and it is easy for debugging, testing, and maintenance
Robust: Rich set of built-in functions and operators
can be used to write any complex programs
Ability to extend: New functions can be added to the C
library (Collection of Functions)
There are 32 keyword and more built-in function

SAMPLE C PROGRAM
Printing a Message
main()
{
/*printing begins*/
printf(WELCOME TO MIT);
/*printing ends*/
}

MAIN FUNCTION
main()

Name of the program


Execution begins at this line
Tell the compiler where the program starts
Every program must have one main function
Empty pair of parentheses indicates that the
function main has no arguments or parameters

The opening brace { indicates the


beginning of the function main and closing
brace } indicates the end of the function

FUNCTION BODY &


COMMENT
Function Body
All the statements between two braces form
the function body
Contains set of instructions to perform the task

Comment Statement
Line beginning with /* and ending with */ are
comment lines
Not an executable statement
Comment line is ignored by the compiler
Comment can be inserted anywhere in the
program

PRINTF FUNCTION
printf() function
Predefined, standard C function for printing output
Predefined: function already been written, compiled
and linked together with our program at the time of
linking
Everything between the starting and the ending
quotation marks will be printed out
Output is:
WELCOME TO MIT

The print line ends with a semicolon (;)


Every statement in C should end with a semicolon (;)
The information contained between the parentheses
is called arguments of the function

NEWLINE CHARACTER
Newline Character
printf(WELCOME \n);
printf(TO MIT\n);

The arguments of printf has two characters \ and n


which are collectively called as newline character
It instructs the computer to go to the next (new)
line
Output is:
WELCOME
TO MIT

Two or more lines of output by one printf


statement:
printf(WELCOME \n TO MIT);

FORMAT OF SIMPLE C
PROGRAM

SAMPLE PROGRAMS
main()
{
int number;
float amount;
number = 100;
amount = 30.75 + 75.35;
printf(%d\n, number);
printf(%5.2f, amount);
}
OUTPUT:
100
106.10

DECLARATION STATEMENTS
int number;
float amount;
number and amount are variable names
Store numeric data
All variables in C must be declared before they are
used
number is an integer data (int abbreviation of
integer) and amount is floating (float) number
Must appear at the beginning of the function
End with a semicolon
int and float are keywords
Keywords cannot be used as variable names

ASSIGNMENT & OUTPUT


STATEMENT
Assignment Statements
number = 100;
amount = 30.75 + 75.35;
Data is stored in a variable by assigning a data value to it
Must end with a semicolon

Output Statement
printf(%d\n, number);
The first argument %d tells the compiler that the value of the
second argument number should be printed as decimal integer
Two arguments are separated by a comma

printf(%5.2f, amount);
%5.2f tells the compiler that the output must be in floating
point, with 5 places in all and 2 places to the right of the
decimal point

SAMPLE PROGRAM
#define PRINCIPLE 1000
main()
{
float simpleinterest, year, rate;
year = 10;
rate = 8.5;
simpleinterest = (PRINCIPLE*rate*year)/100;
printf(SIMPLE INTEREST = %f,
simpleinterest);
}
OUTPUT:
SIMPLE INTEREST: 85000.00

#DEFINE INSTRUCTIONS
#define PRINCIPLE 1000
#define instruction defines value of a symbolic
constant

#define is a compiler directive and not a statement


#define line should not end with a semicolon
#define instructions are placed at the beginning before
main() function

Symbolic Constants

Symbolic constant values remain constant


throughout the execution of the program
Symbolic constants are written in uppercase
Symbolic constants are not declared in declaration
section

SAMPLE PROGRAM
#include <math.h>
main()
{
int angle=40;
float x,y;
x=cos(angle);
printf(Cosine Value = %f, x);
}
OUTPUT:
Cosine Value = 0.7660

#INCLUDE INSTRUCTIONS
Is a compiler directive that instructs the
compiler to link the specified functions
from the library
Standard mathematical functions are
defined in C math library
To use mathematical functions in our
program we add #include <math.h> in
the program
#include <stdio.h> refers to standard I/O
header file containing input and output
functions

BASIC STRUCTURE OF C
PROGRAMS
DOCUMENTATION SECTION

LINK / PREPROCESSOR SECTION


DEFINITION SECTION
GLOBAL DECLARATION SECTION
main() function Section
{
Declaration part;
Executable Part;
}
sub program section (user defined functions)
{
Body of the subprogram;
}

STRUCTURE OF A C
PROGRAM
C program can be viewed as a group
of building blocks called functions
A function is a subroutine that may
include one or more statements
designed to perform a specific task
To write a C program, we create
functions and them put them
together

STRUCTURE OF A C
PROGRAM
Documentation Section
Consists of a set of comments lines giving the
name of the program, the author and other
details for programmer for later use.

Link Section
Provides instructions to the compiler to link
functions from the system library

Definition Section
Defines all symbolic constants

Global Declaration Section


Some variables are used in more than one
functions and such variables are called as global
variables

STRUCTURE OF A C
PROGRAM
main() function section
Has 2 parts
Declaration part - Declaration part declares all variables
used in executable part
Executable part - Executable part contains at least one
statement

Subprogram section
Contains all user defined functions that can be
called in the main functions
Placed immediately after main function

All section, except main function may be


present when they are not required

PROGRAMMING RULES

C makes distinction between uppercase and lowercase: In C,


everything is written in lowercase letters. Uppercase letters are only used for
symbolic constants

Blank spaces may be inserted between the words. This improves the
readability of the statements. However, it is not used while declaring a
variable, keyword, constant and function

Free-Form Language: It is not necessary to fix the position of statement in


the program, i.e., the programmer can write the statement anywhere
between the two braces following the declaration part. The user can also
write one or more statements in one line separating them with a semicolon(;)

The opening and closing braces should be balanced, for example, if opening
braces are four, then closing braces should also be four

EXECUTING A C PROGRAM
Steps involved in executing C
program are
Creating the program
Compiling the program
Linking the program with functions that
are needed from the C library
Executing the program

EXECUTING A C PROGRAM
Creation of program:
Programs should be written in C editor.
The file name does not necessarily include extension C.
The default extension is C.

Compilation:
The source program statements should be translated into object
programs which is suitable for execution by the computer.
The translation is done after correcting each statement.
If there is no error, compilation proceeds and translated program
are stored in another file with the same filename with extension
.obj.
If any errors are there the programmer should correct them.

Linking:
The object code of the function should be brought from the library
of the system and linked to the main() program.
Example: program using pow() function, the object code of this
function is brought from math.h library of the system and linked to
the main() program

Executing the program:


After the compilation the executable object code will be loaded in
the computers main memory and the program is executed

Creating the file

UNIX OS

Text editor is used, either ed or vi


ed filename

The program must be entered in a file


File name consists of letters, digits and special characters, followed
by a dot and a letter c
Hello.c

File is known as source program

Compiling and Linking

Compilation command is cc Hello.c


The program is translated into another file with name Hello.o.
This program is called object code
Linking is the process of putting together other program files and
functions that are required by the program

Compiled and linked program is called the executable object


code and is stored automatically in another file named a.out
Executing the Program
The command is a.out

THE END

You might also like