Download presentation
Presentation is loading. Please wait.
1
TDBA66, VT-03 Lecture - Ch. 21 A complete C-program Display Fig. 2.1 and comment on different things such as Preprocessor directives Header files Identifiers Directives Constants Variables Standard Identifiers (Predefined functions)1 Punctuations Special symbols Reserved words
2
TDBA66, VT-03 Lecture - Ch. 22 Figure 1.11 Miles-to-Kilometers Conversion Program /* * Converts distance in miles to kilometers. */ #include /* 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 miles> "); 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 miles> 10.00 That equals 16.090000 kilometers.
3
TDBA66, VT-03 Lecture - Ch. 23 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 Don’t use more than 31 charcters Examples: Letter, letter_1, myfunc_1, square_meter
4
TDBA66, VT-03 Lecture - Ch. 24 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
TDBA66, VT-03 Lecture - Ch. 25 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
TDBA66, VT-03 Lecture - Ch. 26 How to write int,double,float,char constants ConstantsTypeValue 12.45double12.45 12.45Ffloat12.45 0.5e-5double0.5*10 -5 12.75e3double12750.0 1234int1234 ’a’charSee Appendix A
7
TDBA66, VT-03 Lecture - Ch. 27 Assignment statements A variable can get its value via an assignment statement Ex. #definePI3.141596 int n; double radius, perimeter; n=10; radius=n*n+(n-1)/6; perimeter=2*PI*radius; Comment on evaluation order etc.(more on this in Ch. 3)
8
TDBA66, VT-03 Lecture - Ch. 28 Syntax of printf() printf(format string, print list); printf(format string); Ex. printf(”\nRadius is %f and perimeter”, radius); print(” 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
TDBA66, VT-03 Lecture - Ch. 29 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(”%lf%d”, &weight &age); Note &age denotes a pointer to (address to) the variable age
10
TDBA66, VT-03 Lecture - Ch. 210 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
TDBA66, VT-03 Lecture - Ch. 211 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 myout_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
TDBA66, VT-03 Lecture - Ch. 212 Program controlled Input/output from/to text files See page 59-61 for an overview but you can leave if for now (uses file pointers)
13
TDBA66, VT-03 Lecture - Ch. 213 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
TDBA66, VT-03 Lecture - Ch. 214 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 cm^2 */ circum; /* output - circumference of disc (cm) */ /* Get the disc radius */ printf("Enter radius (in centimeters)> "); scanf("%lf", &radius); /* Get the part identifier */ printf("Enter a 3-character part i.d.> "); scanf("%c%c%c", &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 %c%c%c, ", part_id1, part_id2, part_id3); printf("the disc area is %f cm^2.\n", area); printf("The circumference is %f cm.\n", circum); return (0); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.