Lect26 Basics of C Programming

15
COMPUTER PROGRAMMING I (TA C162) Lecture 26 Basics of C Programming

Transcript of Lect26 Basics of C Programming

Page 1: Lect26 Basics of C Programming

8/13/2019 Lect26 Basics of C Programming

http://slidepdf.com/reader/full/lect26-basics-of-c-programming 1/15

COMPUTER PROGRAMMING I

(TA C162)

Lecture 26 – Basics of C Programming

Page 2: Lect26 Basics of C Programming

8/13/2019 Lect26 Basics of C Programming

http://slidepdf.com/reader/full/lect26-basics-of-c-programming 2/15

 Today’s Agenda

 

Structure of a C program

2

Page 3: Lect26 Basics of C Programming

8/13/2019 Lect26 Basics of C Programming

http://slidepdf.com/reader/full/lect26-basics-of-c-programming 3/15

Our Text Book 

Saturday, March 20, 2010 Biju K Raveendran@BITS Pilani. 3

Page 4: Lect26 Basics of C Programming

8/13/2019 Lect26 Basics of C Programming

http://slidepdf.com/reader/full/lect26-basics-of-c-programming 4/15

 About C General purpose structured programming

language

Characterized b the abilit to write ver

concise source program

Comparatively small instruction set

Programs are highly portable

4

Page 5: Lect26 Basics of C Programming

8/13/2019 Lect26 Basics of C Programming

http://slidepdf.com/reader/full/lect26-basics-of-c-programming 5/15

Structure of a C Language Program/* Program to compute …………………………. */

#include<stdio.h>

/*Library files inclusion *//* Global variable declaration …………………*/

 

{

/* local variable declarations */

/* initialization */

/* statements */

re urn ;}

5

Page 6: Lect26 Basics of C Programming

8/13/2019 Lect26 Basics of C Programming

http://slidepdf.com/reader/full/lect26-basics-of-c-programming 6/15

Structure of a C Program very program cons s s o one or more mo u es ca efunctions.

One of the functions must be called main.

Execution will always begin by main.

Each compound statement is enclosed within a pair ofbraces

{

Statement1;

Statement2;………

}

Each statement must end with a semicolon (;)

 Anything within /* */ treated as comments Comments may appear anywhere within program

6

Page 7: Lect26 Basics of C Programming

8/13/2019 Lect26 Basics of C Programming

http://slidepdf.com/reader/full/lect26-basics-of-c-programming 7/15

Our first C Program/* Converts the distance from miles to kilometer

*/

#include <stdio.h>

  _ _ .

int main(void){

double miles, kms;

/*get the distance in miles*/

printf(“ Enter the distance in miles : ” );

scanf(“ %lf” ,&miles );conver e s ance o ome ers

kms=KMS_PER_MILE * miles;

/* Display the result in kilometers*/

“ ”  ,

return(0);

}

Page 8: Lect26 Basics of C Programming

8/13/2019 Lect26 Basics of C Programming

http://slidepdf.com/reader/full/lect26-basics-of-c-programming 8/15

Our first C Program/* Converts the distance from miles to kilometer

*/

#include <stdio.h>

Comments

 _ _ .

int main(void){

double miles, kms;

ny ng e ween

is interpreted ascomment and is

/*get the distance in miles*/

printf(“ Enter the distance in miles : ” );

scanf(“ %lf” ,&miles );

 

compiler 

conver e s ance o ome ers

kms=KMS_PER_MILE * miles;

/* Display the result in kilometers*/

“ ”

escr es e ey

operations of the

program  ,

return(0);

}  Note: Placing one

comment inside the

Page 9: Lect26 Basics of C Programming

8/13/2019 Lect26 Basics of C Programming

http://slidepdf.com/reader/full/lect26-basics-of-c-programming 9/15

Comments in C Used for improving readability of the program

 

compiler 

 

/* line(s) of text ……. */

 All the text between /* and */ are commented

// One line of text

Commented the text till the end of that line

Saturday, March 20, 2010 Biju K Raveendran@BITS Pilani. 9

Page 10: Lect26 Basics of C Programming

8/13/2019 Lect26 Basics of C Programming

http://slidepdf.com/reader/full/lect26-basics-of-c-programming 10/15

Our first C Program/* Converts the distance from miles to kilometer

*/

#include <stdio.h>

Preprocessor Directives:

Modifies the C program

before the com ilation _ _ .

int main(void){

double miles, kms;

begin #include directive gives a

ro ram access to a librar /*get the distance in miles*/

printf(“ Enter the distance in miles : ” );

scanf(“ %lf” ,&miles ); 

stdio.h is a standard Cconver e s ance o ome ers

kms=KMS_PER_MILE * miles;

/* Display the result in kilometers*/

“ ”

In this program, it supports

printf, scanf functions

  ,

return(0);

} Each l ibrary has a standard

header fi le whose name

ends with .h

Page 11: Lect26 Basics of C Programming

8/13/2019 Lect26 Basics of C Programming

http://slidepdf.com/reader/full/lect26-basics-of-c-programming 11/15

Page 12: Lect26 Basics of C Programming

8/13/2019 Lect26 Basics of C Programming

http://slidepdf.com/reader/full/lect26-basics-of-c-programming 12/15

Our first C Program/* Converts the distance from miles to kilometer

*/

#include <stdio.h>

Function Main:

  _ _ .

int main(void){

double miles, kms;

 

function where theexecution begins

/*get the distance in miles*/

printf(“ Enter the distance in miles : ” );

scanf(“ %lf” ,&miles );

Every C program has a

main functionconver e s ance o ome ers

kms=KMS_PER_MILE * miles;

/* Display the result in kilometers*/

“ ”

The remaining lines

(function body) of the,

return(0);

}

 within the braces { }

Page 13: Lect26 Basics of C Programming

8/13/2019 Lect26 Basics of C Programming

http://slidepdf.com/reader/full/lect26-basics-of-c-programming 13/15

Our first C Program/* Converts the distance from miles to kilometer

*/

#include <stdio.h>

Function Main:

  _ _ .

int main(void){

double miles, kms;

 

{function body

/*get the distance in miles*/

printf(“ Enter the distance in miles : ” );

scanf(“ %lf” ,&miles );

}

Function body has 2conver e s ance o ome ers

kms=KMS_PER_MILE * miles;

/* Display the result in kilometers*/

“ ”

parts

Declarations

Executable statements  ,

return(0);

}

Page 14: Lect26 Basics of C Programming

8/13/2019 Lect26 Basics of C Programming

http://slidepdf.com/reader/full/lect26-basics-of-c-programming 14/15

Tell the compiler the names of memory cells in aprogram

Example: miles, kms

Programmer uses the data requirement analysis

Executable statements

 

language instructions and executed by the computer 

Saturday, March 20, 2010 Biju K Raveendran@BITS Pilani. 14

Page 15: Lect26 Basics of C Programming

8/13/2019 Lect26 Basics of C Programming

http://slidepdf.com/reader/full/lect26-basics-of-c-programming 15/15

Our first C Program/* Converts the distance from miles to kilometer

*/

#include <stdio.h>

int main(void)

Returns an integer to the

o eratin s stem after _ _ .

int main(void){

double miles, kms;

completing the main

function

Receives no data from the/*get the distance in miles*/

printf(“ Enter the distance in miles : ” );

scanf(“ %lf” ,&miles );

operating system before

it begins execution

conver e s ance o ome ers

kms=KMS_PER_MILE * miles;

/* Display the result in kilometers*/

“ ”

return(0) :

Returns the control from,

return(0);

}

 

operating system