You are on page 1of 15

COMPUTER PROGRAMMING I (TA C162)

Lecture 26 Basics of C Programming g g

Todays Agenda

Introduction to C language

Structure of a C program

Our Text Book

Saturday, March 20, 2010

Biju K Raveendran@BITS Pilani.

About C

General purpose structured programming language Characterized by y the ability y to write very y concise source program Comparatively small instruction set Programs are highly portable

Structure of a C Language Program


/* Program to compute . */ #include<stdio.h> /*Library files inclusion */ /* Global variable declaration */ int main() { /* local variable declarations */ /* initialization */ /* statements */ return(0); t (0) }

Structure of a C Program

Every C program consists E i t of f one or more modules d l called ll d functions. One of the functions must be called main. Execution will always begin by main. Each compound statement is enclosed within a pair of braces
{ Statement1; Statement2; }

Each statement must end with a semicolon (;) Anything within /* */ treated as comments Comments may appear anywhere within program

Our first C Program


/* Converts the distance from miles to kilometer */ #include <stdio.h> #define KMS KMS_PER_MILE PER MILE 1 1.609 609 int main(void) { double miles, kms; /*get the distance in miles*/ printf(Enter the distance in miles : ); scanf(%lf,&miles ); /* /*convert t th the distance di t to t kilometers*/ kil t */ kms=KMS_PER_MILE * miles; /* Display the result in kilometers*/ printf(The printf( The result is %lf , , kms); return(0); }

Our first C Program


/* Converts the distance from miles to kilometer */ #include <stdio.h> #define KMS KMS_PER_MILE PER MILE 1 1.609 609 int main(void) { double miles, kms; /*get the distance in miles*/ printf(Enter the distance in miles : ); scanf(%lf,&miles ); /* /*convert t th the distance di t to t kilometers*/ kil t */ kms=KMS_PER_MILE * miles; /* Display the result in kilometers*/ printf(The printf( The result is %lf , , kms); return(0); }

Comments

Anything An thing between bet een /* */ is interpreted as comment and is ignored by the C compiler Describes the key operations of the program Note: Placing one comment inside the other is not allowed

Comments in C

Used for improving readability of the program Anything commented is ignored by the C compiler 2 ways to comment

/* line(s) of text . */

All the text between /* and */ are commented Commented the text till the end of that line

// One line of text


Saturday, March 20, 2010

Biju K Raveendran@BITS Pilani.

Our first C Program


/* Converts the distance from miles to kilometer */ #include <stdio.h> #define KMS_PER_MILE KMS PER MILE 1 1.609 609 int main(void) { double miles, kms; /*get the distance in miles*/ printf(Enter the distance in miles : ); scanf(%lf,&miles ); /* /*convert t th the distance di t to t kilometers*/ kil t */ kms=KMS_PER_MILE * miles; /* Display the result in kilometers*/ printf(The printf( The result is %lf , , kms); return(0); }

Preprocessor Directives: Modifies the C program before the compilation p begin #include directive gives a program p g access to a library y

stdio.h is a standard C library


In this program, it supports printf, scanf functions

Each library has a standard header file whose name ends e ds with t .h

Our first C Program


/* Converts the distance from miles to kilometer */ #include <stdio.h> #define KMS_PER_MILE 1.609 int main(void) { double miles, kms; /* t th /*get the distance di t in i miles*/ il */ printf(Enter the distance in miles : ); scanf(%lf,&miles ); /*convert / convert the distance to kilometers kilometers*/ / kms=KMS_PER_MILE * miles; /* Display the result in kilometers*/ printf(The result is %lf , kms); return(0); }

Preprocessor Directives:

#define in this program associates i t a constant t t macro KMS_PER_MILE with the meaning 1.609 Preprocessor replaces every occurrences of KMS_PER_MILE with 1.609 before the compilation begins

Data values that never change (or change very rarely) should be given names using a #define

kms=KMS_PER_MILE * miles; is replaced with kms=1.609 * miles; p begins g before the compilation

Our first C Program


/* Converts the distance from miles to kilometer */ #include <stdio.h> #define KMS KMS_PER_MILE PER MILE 1 1.609 609 int main(void) { double miles, kms; /*get the distance in miles*/ printf(Enter the distance in miles : ); scanf(%lf,&miles ); /* /*convert t th the distance di t to t kilometers*/ kil t */ kms=KMS_PER_MILE * miles; /* Display the result in kilometers*/ printf(The printf( The result is %lf , , kms); return(0); }

Function Main:

int main(void) is a function where the execution begins Every C program has a main function The remaining lines (function body) of the function are enclosed within the braces { }

Our first C Program


/* Converts the distance from miles to kilometer */ #include <stdio.h> #define KMS KMS_PER_MILE PER MILE 1 1.609 609 int main(void) { double miles, kms; /*get the distance in miles*/ printf(Enter the distance in miles : ); scanf(%lf,&miles ); /* /*convert t th the distance di t to t kilometers*/ kil t */ kms=KMS_PER_MILE * miles; /* Display the result in kilometers*/ printf(The printf( The result is %lf , , kms); return(0); }

Function Main:

int main(void) { function body } Function body has 2 parts


Declarations Executable statements

Declarations

Tell the compiler the names of memory cells in a program


Example: miles, kms

Programmer uses the data requirement analysis information to identify this Program lines that are converted into machine language instructions and executed by the computer

Executable statements

Saturday, March 20, 2010

Biju K Raveendran@BITS Pilani.

14

Our first C Program


/* Converts the distance from miles to kilometer */ #include <stdio.h> #define KMS KMS_PER_MILE PER MILE 1 1.609 609 int main(void) { double miles, kms; /*get the distance in miles*/ printf(Enter the distance in miles : ); scanf(%lf,&miles ); /* /*convert t th the distance di t to t kilometers*/ kil t */ kms=KMS_PER_MILE * miles; /* Display the result in kilometers*/ printf(The printf( The result is %lf , , kms); return(0); }

int main(void) Returns an integer to the operating p g system y after completing the main function Receives no data from the operating system before it begins execution return(0) : Returns the control from the main function to the operating system

You might also like