Download presentation
Presentation is loading. Please wait.
1
Lecture2
2
Lecture Outline Program Layout Executable Statements
Input/Output Statements Input/Output Operation scanf function printf function Assignment Statements Arithmetic Operations The Return Statement Comments in Programs A Complete C Program Mixed Data Types in Assignment Statements Expressions with multiple operators Rules for Expression Evaluation Writing mathematical Formulas in C Types of Errors
3
Program Layout provides an instruction to the preprocessor.
Preprocessor Directive int main(void) { Declaration Executable Statements } A C program line beginning with # that provides an instruction to the preprocessor. Examples: #include, #define. Function main indicates the start of the program. The part of a program that tells the compiler the names of memory cells and their data types. Program commands that are converted to machine code by the compiler.
4
Executable Statements
Getting information in and out of a program. Input/Output Statements (functions) Doing calculations. Assignment Statements
5
Input/Output Operations
Input Operation An instruction that copies data from an input device into memory. Output Operation An instruction that displays information stored in memory.
6
Input/Output Functions
Input Functions Perform input operations. Example: scanf. Output Functions Perform output operations. Example: printf.
7
scanf The scanf function gets data entered by the user and copies it into memory. Syntax scanf(format string, input list); Example scanf("%c%i", &firstinitial, &age); H32 placeholder
8
Placeholder A placeholder is a symbol beginning with % in a format string that indicates where to display the output value. Placeholder Variable Type Function Use %c char printf/scanf %i or %d int printf/scanf %f float printf/scanf
9
Examples scanf("%i",&workhours); scanf("%c",&letter);
scanf("%i",&student_ID); scanf("%f",&tot_score); scanf("%f",&temperature); scanf("%f",&working_hours); scanf("%i%c",&population,&firstinitial);
10
printf The printf function displays information on the screen. Syntax
printf(format string); printf(format string, print list); Note: a format string is always enclosed in " " placeholder newline escape sequence Example printf("That equals %fKilometers.\n",kms); function name format string print list
11
New Line Escape Sequence "\n"
Is the newline escape sequence symbol which is placed in a printf to skip to a new line.
12
Examples printf("Please enter the student's grades:");
printf("The class average is %f ",average); The class average is 95.5. printf("The total area is %f and the total cost is %i S.R.",tarea,tcost); The total area is 60.2 and the total cost is 4530 S.R. printf("The student received an %c grade in the course.",grade); The student received an A grade in the course.
13
Examples (Continue) printf("The grade is %c%c", grade, gradesymb);
The grade is A+ printf("I am the first line\n"); printf("\n I am the second line\n"); I am the first line I am the second line
14
Formatting Numbers in Program Output
Value Format Displayed Output %4i %5i %1i %4i %5i %2i
15
Formatting Numbers in Program Output
Value Format Displayed Output %6.2f %6.2f %6.2f %6.2f %6.2f %6.2f
16
Formatting Numbers in Program Output
Value Format Displayed Output %5.2f %3.2f %4.2f %.3f %4.2f %.4f
17
Assignment Statements
It stores a value or a computational result in a variable. It is used to perform most arithmetic operations in a program. Variable=expression; Syntax Examples Total_Grades = Grade1 + Grade2 + Grade3; No_Students = 3; Average = Total_Grades / No_Students;
18
Arithmetic Expressions
Arithmetic Meaning Operator addition subtraction * multiplication / division % remainder
19
The % Operation (Remainder)
Examples 3 % 5 = 3 4 % 5 = 4 5 % 5 = 0 6 % 5 = 1 7 % 5 = 2 8 % 5 =3 5 % 3 = 2 5 % 4 = 1 15 % 5 = 0 15 % 6 = 3
20
C Language Elements #include <stdio.h>
#define KMS_PER_MILE 1.609 int main(void) { float miles, kms; printf("Enter the distance in miles: "); scanf("%f", &miles); kms = KMS_PER_MILE * miles; printf("That equals %.2f kilometers.\n", kms); return(0); }
21
The Return statement The return statement transfers control from a function back to the activator of the function. For function main, control is transferred back to the operating system The value of expression is returned as the result of the function execution. Syntax return expression; Example return(0);
22
Comments in programs Syntax /* comment text*/ Examples
/* This is a one line comment */ /* * This is a multiple line comment in which the stars not immediately * proceeded or followed by slashes have no special syntactic * significance */
23
A Complete C Program /* converts distances from miles to kilometers */
#include <stdio.h> /* printf, scanf definitions */ #define KMS_PER_MILE /* conversion constant */ int main(void) { float miles, /* distance in miles */ kms; /* equivalent distance in kilometers */ /* get the distance in miles */ printf("Enter the distance in miles: "); scanf("%f", &miles); /* convert the distance to kilometers */ kms = KMS_PER_MILE * miles; /* display the distance in kilometers */ printf("That equals %.2f kilometers.\n", kms); return(0); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.