A complete C-program - PowerPoint PPT Presentation

About This Presentation
Title:

A complete C-program

Description:

... more about formating in section 2.5. TDBA66, VT-04 Lecture ... Read about Case Study on page 50. TDBA66, VT-04 Lecture - Ch. 2. 11. Input/output redirection ... – PowerPoint PPT presentation

Number of Views:38
Avg rating:3.0/5.0
Slides: 15
Provided by: perl64
Category:
Tags: complete | program

less

Transcript and Presenter's Notes

Title: A complete C-program


1
A complete C-program
Display Fig. 2.1 and comment on different things
such as Preprocessor directives Header
files Identifiers Directives Constants Variables S
tandard Identifiers (Predefined
functions)1 Punctuations Special symbols Reserved
words
2
Figure 1.11 Miles-to-Kilometers Conversion
Program / Converts distance in miles to
kilometers. / include ltstdio.hgt
/ printf, scanf definitions / define
KMS_PER_MILE 1.609 / conversion constant
/ int main(void) double miles,
/ input - distance in miles. /
kms / output - distance in kilometers
/ / Get the distance in miles. /
printf("Enter the distance in milesgt ")
scanf("lf", miles) / Convert the
distance to kilometers. / kms
KMS_PER_MILE miles / Display the
distance in kilometers. / printf("That
equals f kilometers.\n", kms) return
(0) Sample Run Enter the distance in milesgt
10.00 That equals 16.090000 kilometers.
3
User defined identifiers
  • Consists of letters, digits, underscore
  • Cannot begin with a digit
  • A C-reserved word cannot be used
  • A standard identifier should not be redefined
  • Dont use more than 31 charcters
  • Examples
  • Letter, letter_1, myfunc_1, square_meter

4
Variables and data types
  • Variables are names of different memory cells
    that can hold values of different types
  • The name of a variable is an identifier
  • A variable should be declared as a certain data
    type
  • Examples
  • int n, no_of_students
  • float score, sum, mean_value

5
Example cont.
  • double miles / miles from here to there/
  • char letter, digit
  • Variables of type int can store integers in
    INT_MIN, INT_MAX and similar for float and
    double but they can store a real number with an
    integral and fractional part
  • A char can hold one single charcter

6
How to write int,double,float,charconstants
7
Assignment statements
  • A variable can get its value via an assignment
    statement
  • Ex.
  • define PI 3.141596
  • int n
  • double radius, perimeter
  • n10
  • radiusnn(n-1)/6
  • perimeter2PIradius
  • Comment on evaluation order etc.(more on this in
    Ch. 3)

8
Syntax of printf()
  • printf(format string, print list)
  • printf(format string)
  • Ex.
  • printf(\nRadius is f and perimeter, radius)
  • printf( is 10.2f\n, perimeter)
  • This is a prompting message or prompt
  • printf(Type radius in cm )
  • scanf(lf, radius)
  • Read more about formating in section 2.5

9
Syntax of scanf()
  • scanf(format string, input list)
  • Ex.
  • int age
  • double weight / in kilogram /
  • printf(Type your weight and age in one line )
  • scanf(lfd, weight, age)
  • Note
  • age denotes a pointer to (address to) the
    variable age

10
Program Style
  • Use spaces in the code
  • Use comments in a clever way (say what the
    intention is)
  • See Program Comment on page 49 in text book
  • Read about Case Study on page 50

11
Input/output redirection
  • Instead of giving the input interactivly from the
    keyboard data can be taken from a file (text
    file)
  • Ex. assume the executable file is called myprog
  • Run by typing
  • ./myprog ltmyin_1 gtmyout_1
  • Now scanf() reads from myin_1 and printf() writes
    to myout_1
  • myin_1 should be a text file in the same
    directory as myprog
  • Note! No prompting in the program in this case

12
Program controlled Input/output from/to text files
  • See page 59-61 for an overview but you can leave
    it for now
  • (uses file pointers)

13
Run-time errors
  • When reading to char-varibles there is a risk
    that inputed characters hanging in the input
    buffer will cause subsequent reading to fail
  • See Fig. 2.13 page 65

14
Figure 2.13 Function main for Disc
Characteristics with Revised Beginning
int main(void) char part_id1, part_id2,
/ input - 3-character part id /
part_id3 double radius, / input -
radius of disc in cm / area,
/ output - area of disc in cm2 /
circum / output - circumference of disc (cm)
/ / Get the disc radius /
printf("Enter radius (in centimeters)gt ")
scanf("lf", radius) fflush(stdin)/otherwise
part_id1 fails/ / Get the part
identifier / printf("Enter a 3-character
part i.d.gt ") scanf("ccc", part_id1,
part_id2, part_id3) / Calculate the
area / area PI radius radius
/ Calculate the circumference / circum 2
PI radius / Display the part i.d.,
area and circumference / printf("For part
ccc, ", part_id1, part_id2, part_id3)
printf("the disc area is f cm2.\n", area)
printf("The circumference is f cm.\n", circum)
return (0)
Write a Comment
User Comments (0)
About PowerShow.com