You are on page 1of 14

Introduction to C

Introduction to computer programming and


Problem Solving- CSE 101

L Jeganathan
jeganathan.l@vit.ac.in

Lecture 02

September 17, 2010

Introduction to computer programming and Problem solving - CSE


Introduction to C

W HAT IS C?

C is a programming language developed at AT &T Bell


laboratories in 1972.
Designed and developed by Dennis Ritchie on a
DEC-PDP-11 machine with UNIX.
C is a development of a language called B.
B was a development of a language -Basic Combined
Programming Language (BCPL).
C is a small language ; small is always beautiful; -minimum
number of keywords.
C is portable - codes written on one machine can be used
in another machine; Standard library function work the
same on all machines; C has built-in preprocessor which
will isolate the system-dependent code.

Introduction to computer programming and Problem solving - CSE


Introduction to C

A BOUT C - CONTINUED

C is modular - A long program can be divided into smaller


programs called modules; modules can be designed,
tested and debugged separately. no nesting of modules
allowed.
C is structured language - (Structure :hierarchy of modules
; control is passed down the structure without
unconditional branches to the higher level.)
C is terse - has a powerful set of operators to the extent
that the programmer can access the machine to the level
of bits.
Learning C is a first step towards C++ and Java; C is basic
for C++ and java.

Introduction to computer programming and Problem solving - CSE


Introduction to C

ANSI C

American National Standards Institute- ANSI- Institute


responsible for setting the standards of many kinds of
computer system and programming languages.
Responsible for setting the standard of C
C standardized by ANSI is called as ANSI C or standard C
ANSI C is also approved by International organization of
Standardization (ISO)
All the C compilers follow ANSI C

Introduction to computer programming and Problem solving - CSE


Introduction to C

M ODEL C P ROGRAM

# include <stdio.h>
int main(void)
{
printf (Hello, world!\n);
return 0;
}

Introduction to computer programming and Problem solving - CSE


Introduction to C

#include < stdio.h >

A preprocessor built into the computer- Code is first


preprocessed and then compiled.
Lines that begin with # communicate with the
preprocessor.
#include causes the preprocessor to include the file
stdio.h in the code at this point of the program. The entire
file (which is a ready-made program) will be included in the
source code.
The header file is provided by the C-system (what is a
C-system?)

Introduction to computer programming and Problem solving - CSE


Introduction to C

#include < stdio.h > CONTINUED

The angular brackets indicate that the file is in its usual


place. That is in the standard directories. we can also use
our own files (which contains codes of certain functions).
In that case angular brackets are not required.
#include sin.h. it will search for the sin in the current
directory. If the file is not there, it will search in the
standard directories.
Since the code contains printf command which will print
the out put , we require the header file < stdio.h >

Introduction to computer programming and Problem solving - CSE


Introduction to C

int main(void)

main is a function. A program can have many functions. A


program should have at least one function to perform.
Every program should have this main
Any function should have some arguments. For eg., sine
function sin x; x is an argument of the function sine.
main(void) tells that main is a function with no arguments
at all.
Only the parenthesis () that follows main tells that main is a
function.
int main(void) tells that the main is a function which has
no arguments but returns an integer.

Introduction to computer programming and Problem solving - CSE


Introduction to C

{}

braces {} enclose statements of the function. Here the


function is main.

Introduction to computer programming and Problem solving - CSE


Introduction to C

printf (Hello, world!\n);

C has standard library functions.


printf is a library function which prints the arguments in the
screen.
We included the header file stdio.h, since the header files
contain some information about the library function printf.
The argument of the printf function is a string constant.
(What is a string constant?)
The two characters \n (read as : \ n) at the end of the
string constant stands for a new line
\n is a non-printable character. It advances the cursor to a
new line.
since printf (Hello, world!\n) is a statement, it ends with
a semicolon. All the statements in C end with a semicolon.
The out put in the screen will be as Hello, world!
Introduction to computer programming and Problem solving - CSE
Introduction to C

return 0;

It returns a value zero to the operating system.

Introduction to computer programming and Problem solving - CSE


Introduction to C

I S ANYTHING DIFFERENT IN THIS PROGRAM ?

#include < stdio.h >


int main(void)
{
printf (Hello, world!\n);
return 0;
}
T
he comment line is a non-executable statement. It is optional.
Compilers do not consider the statements enclosed between /
and /

Introduction to computer programming and Problem solving - CSE


Introduction to C

C AN YOU WRITE A CODE TO PRINT YOUR PERMANENT ADDRESS ?

C AN YOU WRITE A CODE TO PRINT YOUR PERMANENT ADDRESS ,


ENCLOSED IN A RECTANGLE WITH THE SYMBOL +?

Introduction to computer programming and Problem solving - CSE


Introduction to C

A NOTHER C PROGRAM

/ This program prints converts centigrade to fahrenheit ;


F = 59 c + 32 /
#include < stdio.h >
int main(void)
{
intcentigrade;
floatfahrenheit;
centigrade = 40;
fahrenheit = 9.0/5.0 centigrade + 32.0;
printf (\n 40 degree centigrade is equal to %f fahrenheit , \n\n, f
return 0;
}

Introduction to computer programming and Problem solving - CSE

You might also like