Sei sulla pagina 1di 13

CSE251

Computer Programming
Overview of C
Programming Language
• A programming language is designed to process
certain kinds of data consisting of numbers,
characters and strings and to provide useful output
known as information.
• The processing of data is accomplished by executing
a sequence of precise instruction called program.
• The instructions are formed using certain symbols
and words according to some rigid rules known as
syntax rules or grammar.
C Language
• The C language was written in 1970 for the first UNIX
system by Dennis Ritchie and was implemented at
Bell Laboratories.
• Today, C is running under a number of operating
system including MS-DOS, Windows, and Linux etc.
• In 1988, the American National Standards Institute
(ANSI) adopted a new and improved version of C,
known today as “ANSI C”.
Importance of C
• It is a strong language whose rich set of built in functions and
operators can be used to write any complex program.
• The compiler combines the capabilities of an assembly
language with the feature of a high level language.
• Program written in C are efficient and fast. It is many times
faster than BASIC.
– For Example a program to increment a variable from 0 to 15000 takes
one second in C while it takes more than 50 seconds in BASIC.
• C is highly portable .This means that C programs written for
one computer can be run on another with little or no
modification.
A First Program
/* Hello World Program*/
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("Hello World\n");
getch();
}
Running the program
1. Save the file in hello.c extension.
2. Press Alt+F9 to compile
3. Press ctrl+F9 to execute
Example Explained
• The program will start form “main” function. The
“main” function establishes the overall logic of the
code.
• All C codes must have a “main” function….
• Use of more than one main() is illegal.
• The empty pair of parentheses immediately following
main indicated that the function main has no
arguments or parameters.
Example Explained(Cont...)
• The opening brace ‘{‘ indicates the beginning of the
function and closing’}” indicates the end of the
function.
• All the statements between these two braces form the
function body. The function body contains a set of
instructions to perform the given task.
• printf(), an output function form the I/O (
input/output )library (defined in the file stdio.h )
• “\n” prints a “new line” character, which brings the
cursor onto the next line.
• Every statement in C should end with a semicolon (;)
mark.
Example Explained(Cont...)
• The first statement is “#include <stdio.h>”. The “.h”
files are by convention “header files“ which contain
definition of variables and functions necessary for
the functioning of a program.
• The lines beginning with /* and ending with */ are
known as comment lines. These are used in a
program to enhance its readability. Comment lines
are not executable statements and therefore
anything between /* and */ is ignored by the
complier.
The C Compilation Model
• We will briefly highlight key features of the C
Compilation model here
The C Compilation Model(Cont...)
• The Preprocessor
– The Preprocessor accepts source code as input and is
responsible for
• removing comments
• interpreting special preprocessor directives denoted by #.
– For example
• #include - includes contents of a named file. Files usually
called header files. e.g
– #include <math.h> - standard library maths file.
– #include <stdio.h> - standard library I/O file
• #define - defines a symbolic name or constant. Macro
substitution.
– #define MAX_ARRAY_SIZE100
The C Compilation Model(Cont...)
• C Compiler
– The C compiler translates source to assembly
code. The source code is received from the
preprocessor.
• Assembler
– The assembler creates object code. On a UNIX
system you may see files with a .o suffix (.OBJ on
MSDOS) to indicate object code files. Object code
can be referred as machine language code.
The C Compilation Model(Cont...)
• Link Editor
– If a source file references library functions or
functions defined in other source files, the link
editor combines these functions (with main()) to
create an executable file. External Variable
references resolved here also.

Potrebbero piacerti anche