Download presentation
Presentation is loading. Please wait.
Published byLogan Harrington Modified over 9 years ago
2
GE 211 Programming in C Dr. Ahmed Telba Room :Ac -134 http://faculty.ksu.edu.sa/atelba/default.aspx
3
2 Comment Example // omar's Homework #1 // This program is awesome! #include /* This program computes the coefficient of expansion of the universe to 27 decimal places. */ int main() { cout << 1.0000000000000000000001; }
4
3 Another C++ Program // C++ Addition of integers #include int main() { int integer1, integer2, sum; cout << "Enter first integer\n"; cin >> integer1; cout << "Enter second integer\n"; cin >> integer2; sum = integer1 + integer2; cout << "Sum is " << sum << endl; return 0; } // C++ Addition of integers #include int main() { int integer1, integer2, sum; cout << "Enter first integer\n"; cin >> integer1; cout << "Enter second integer\n"; cin >> integer2; sum = integer1 + integer2; cout << "Sum is " << sum << endl; return 0; }
5
Statements
6
Declaration statements
9
Operation –Assignment
11
Printf() Format
13
Printf format
16
Scanf()
17
/* Lesson for 3_1 */ #include void main(void) { int month; float expense, income; month = 12; expense = 111.1; income=100.; printf("Month=%2d, Expense=$%9.2f\n",month,expense); month = 11; expense = 82.1; printf("For the %2dth month of the year\n" "the expenses were $%5.2f \n" "and the income was $%6.2f\n\n", month,expense,income); }
19
/* Lesson for 3_2 */ #include #define DAYS_IN_YEAR 365 #define PI 3.14159 void main (void) { float income = 1234567890.12; printf ("CONVERSION SPECIFICATIONS FOR INTEGERS \n\n"); printf ("Days in year = \n" "[[%1d]] \t(field width less than actual)\n" "[[%9d]] \t(field width greater than actual)\n" "[[%d]] \t(no field width specified) \n\n\n", DAYS_IN_YEAR, DAYS_IN_YEAR, DAYS_IN_YEAR); printf ("CONVERSION SPECIFICATIONS FOR REAL NUMBERS\n\n"); printf ("Cases for precision being specified correctly \n"); printf ("PI = \n" "[[%1.5f]] \t\t(field width less than actual) \n" "[[%15.5f]] \t(field width greater than actual)\n" "[[%.5f]] \t\t(no field width specified) \n\n", PI,PI,PI); printf ("Cases for field width being specified correctly \n"); printf ("PI = \n" "[[%7.2f]] \t\t(precision less than actual) \n" "[[%7.8f]] \t\t(precision greater than actual)\n" "[[%7.f]] \t\t(no precision specified) \n\n", PI,PI,PI); printf ("PRINTING SCIENTIFIC NOTATION \n\n"); printf ("income = \n" "[[%18.2e]] \t(field width large, precision small) \n" "[[%8.5e]] \t(field width and precision medium size)\n" "[[%4.1e]] \t\t(field width and precision small) \n" "[[%e] \t(no specifications) \n\n", income, income, income, income); printf ("USING A FLAG IN CONVERSION SPECIFICATIONS \n\n"); printf ("Days in year= \n" "[[%-9d]] \t\t(field width large, flag included)\n", DAYS_IN_YEAR); }
21
#include void main(void) { int i,j,k,p,m,n; float a,b,c,d,e,f,g,h,x,y; i=5; j=5; k=11; p=3; x=3.0; y=4.0; printf("...... Initial values......\n"); printf("i=%4d, j=%4d\nk=%4d, p=%4d\nx=%4.2f, y=%4.2f\n\n", i,j,k,p,x,y); /*--------------- Section 1 -------------------*/ a=x+y; b=x-y; c=x*y; d=x/y; e=d+3.0; f=d+3; i=i+1; j=j+1; printf("...... Section 1 output......\n"); printf("a=%5.2f, b=%5.2f\nc=%5.2f, d=%5.2f\n" "e=%5.2f, f=%5.2f\ni=%5d, j=%5d \n\n", a,b, c,d, e,f, i,j); /*--------------- Section 2 -------------------*/ m=k%p; n=p%k; i++; ++j; e--; --f; printf("...... Section 2 output......\n"); printf("m=%4d, n=%4d\ni=%4d, j=%4d\n" "e=%4.2f, f=%4.2f\n",m,n, i,j, e,f); }
25
/* Lesson for 3_5 */ #include void main(void) { float income; double expense; int month, hour, minute; printf("What month is it?\n"); scanf("%d", &month); printf("You have entered month=%5d\n",month); printf("Please enter your income and expenses\n"); scanf("%f %lf",&income,&expense); printf("Entered income=%8.2f, expenses=%8.2lf\n", income,expense); printf("Please enter the time, e.g.,12:45\n"); scanf("%d : %d",&hour,&minute); printf("Entered Time = %2d:%2d\n",hour,minute); }
27
/* Lesson for 3_7 */ #include void main(void) {double x=3.0, y=4.0, a,b,c,d,e,f; float g; a=sin(x); b=exp(x); c=log(x); d=sqrt(x); e=pow(x,y); f=sin(y)+exp(y)-log10(y)*sqrt(y)/pow(3.2,4.4); g=log(x); printf("x=%4.1f y=%4.1f \n\n\r\ a=sin(x) = %11.4f\n\r\ b=exp(x) = %11.4f\n\r\ c=log(x) = %11.9f\n\n\r\ d=sqrt(x) = %11.4f\n\r\ e=pow(x,y) = %11.4f\n\r\ f=sin(y)+exp(y)log10(y)*sqrt(y)/pow(3.2,4.4)= %11.4f\n\n\r\ g=log(x) = %11.9f\n",x,y, a,b,c,d,e,f,g); }
28
/* For application A3_2*/ #include void main(void) { double degC, degF; printf ("Table of Celsius and Fahrenheit degrees\n\n" " Degrees Degrees \n" " Celsius Fahrenheit \n"); degC = 0.; degF = degC * 9./5. +32.; printf ("%16.2f %20.2f\n", degC, degF); degC += 20.; degF = degC * 9./5. +32.; printf ("%16.2f %20.2f\n", degC, degF); degC += 20.; degF = degC * 9./5. +32.; printf ("%16.2f %20.2f\n", degC, degF); degC += 20.; degF = degC * 9./5. +32.; printf ("%16.2f %20.2f\n", degC, degF); degC += 20.; degF = degC * 9./5. +32.; printf ("%16.2f %20.2f\n", degC, degF); degC += 20.; degF = degC * 9./5. +32.; printf ("%16.2f %20.2f\n", degC, degF); }
30
/* Lesson for 3_6 */ #include void main(void) { double xx ; int ii, kk; FILE *inptr; inptr=fopen ("C3_6.IN","r"); fscanf(inptr,"%d",&ii); fscanf(inptr,"%d %lf",&kk,&xx); fclose(inptr); printf("ii=%5d\nkk=%5d\nxx=%9.3lf\n",ii, kk, xx); }
31
30 scanf() Example: scanf(“%d”, &x); printf() Example: printf(“The value of x is %d\n”, x); #include Recall
32
31 Input/Output Program
33
32 Streams Text input or output is dealt with as a sequence of characters A stream serves as a channel to convey characters between I/O and programs
34
33 Streams: Input -- Example 135 25.5 _ 135 25.5\n int item; float cost; scanf(“%d %f”, &item, &cost); input buffer
35
34 Streams: Input -- Example (cont) 135 25.5 _ 135 25.5\n int item; float cost; scanf(“%d %f”, &item, &cost); itemcost
36
35 Streams: Input – Example (cont) 135 25.5 _ 25.5\n int item; float cost; scanf(“%d %f”, &item, &cost); item 135 cost
37
36 Streams: Input – Example (cont) 135 25.5 _ \n int item; float cost; scanf(“%d %f”, &item, &cost); item 135 cost 25.5
38
37 Streams: Output -- Example Hello!\n printf(“Hello!\n”); output buffer
39
38 Hello!\n printf(“Hello!\n”); Streams: Output – Example (cont)
40
39 ello!\n H printf(“Hello!\n”); Streams: Output – Example (cont)
41
40 llo!\n He printf(“Hello!\n”); Streams: Output – Example (cont)
42
41 lo!\n Hel printf(“Hello!\n”); Streams: Output – Example (cont)
43
42 o!\n Hell printf(“Hello!\n”); Streams: Output – Example (cont)
44
43 !\n Hello printf(“Hello!\n”); Streams: Output – Example (cont)
45
44 \n Hello! printf(“Hello!\n”); Streams: Output – Example (cont)
46
45 Hello! _ printf(“Hello!\n”); Streams: Output – Example (cont)
47
46 Streams From the program's point of view, the characters are queued in a pipe The sequence of characters is organized into lines Each line: – can have zero or more characters – ends with the "newline" character '\n'
48
47 "Standard" Streams Standard streams : – stdin - standard input usually from keyboard – stdout - standard output usually to screen – stderr - standard error usually to screen must have at the top of your program #include can be redirected
49
48 stdin: Input Data is read in from stdin (into a variable) using the scanf() function When input ends, the scanf() function returns a special value: EOF
50
49 Example: ReadData Input name, age, gender, idNumber
51
50 #include
52
51 #include /*************************************\ Read in important info about a student \**************************************/
53
52 #include /*************************************\ Read in important info about a student \**************************************/ int main() { return 0; }
54
53 #include /*************************************\ Read in important info about a student \**************************************/ int main() { char name[100] ; float age ; char gender ; int idNumber ; return 0; }
55
54 #include /*************************************\ Read in important info about a student \**************************************/ int main() { char name[100] ; float age ; char gender ; int idNumber ; scanf("%s %f %c %d", name, &age, &gender, &idNumber); return 0; }
56
55 #include /*************************************\ Read in important info about a student \**************************************/ int main() { char name[100] ; float age ; char gender ; int idNumber ; scanf("%s %f %c %d", name, &age, &gender, &idNumber); return 0; } Alkady 19.2 M 3825 Input: Akady 19.2 M 3825
57
56 stdout: Output Data (e.g., from a variable) is written out to stdout using the printf() function.
58
57 Example: WriteData Set name to “Alkady” Set age to 18.2 Set gender to ‘M’ Set idNumber to 3825 Output name, age, gender, idNumber
59
58 #include /*****************************************\ Write out important info about a student \*****************************************/ int main() { char *name = ”Akady" ; float age = 18.2; char gender = ’M'; int idNumber = 3825 ; printf("%s\n%f\n%c\n%d\n", name, age, gender, idNumber); return 0; } Alkady 18.2 M 3825 _
60
59 Formatted Input and Output General form: printf( format-control-string, other-arguments ); scanf( format-control-string, other-arguments ); Examples: printf("%s\n%f\n%c\n%d\n",name,age,gender,idNumber); scanf("%s %f %c %d", name, &age, &gender, &idNumber);
61
60 Describes the format of the data for output Contains “conversion specifiers” and “literal characters” Example: printf(“%s is %d years old.\n”, name, age); printf -- Format-Control-String
62
61 Describes the format of the data for output Contains “conversion specifiers” and “literal characters” Example: printf(“%s is %d years old.\n”, name, age); conversion specifiers printf -- Format-Control-String (cont)
63
62 printf(“%s is %d years old.\n”, name, age); literal characters Describes the format of the data for output Contains “conversion specifiers” and “literal characters” Example: printf -- Format-Control-String (cont)
64
63 For printf : variables containing data for output Example: (“ printf(“%s is %d years old.\n”, name, age); printf -- Other-Arguments
65
64 Describes the format of the data given as input Contains “conversion specifiers” scanf -- Format-Control-String Example: scanf("%s %f %c %d", name, &age, &gender,&id); conversion specifiers
66
65 For scanf: “pointers” to variables where the input will be stored scanf -- Other-Arguments scanf("%s %f %c %d", name, &age, &gender, &id); Example:
67
66 ‘&’ is for scanf only! scanf("%s %f %c %d", name, &age, &gender, &id); scanf -- Other-Arguments (cont) For scanf : “pointers” to variables in which the input will be stored Example: Variables of type int, float or char need ‘&’ Do NOT use ‘&’ with strings!
68
67 Common Conversion Specifiers for Numerical Information decimal integer: %d printf(“What is %d plus %d?\n”, x, y); scanf(“%d”, &sum); float: %f printf(“%f squared is...? ”, x); scanf(“%f”, &ans); double : printf(“%f squared is...? ”, x); scanf(“%lf”, &ans);
69
68 Conversion Specifiers for Alphanumeric Information char : %c printf(“What letter follows %c?\n”,ch); scanf(“%c”, &nextchar); string : %s printf(“Name: %s\n”, name); scanf(“%s”, name);
70
69 i or d : display a signed decimal integer f : display a floating point value e or E : display a floating point value in exponential notation g or G : display a floating point value in either f form or e form L : placed before any float conversion specifier to indicate that a long double is displayed printf : Conversion Specifiers
71
70 scanf : Conversion Specifiers d : read an optionally signed decimal integer i : read an optionally signed decimal, octal, or hexadecimal integer i and d : the argument is a “pointer” to an integer int idNumber; scanf("%d", &idNumber);
72
71 scanf : Conversion Specifiers (cont) h or l : placed before any integer conversion specifiers to indicate that a short or long integer is to be input long int idNumber; scanf("%ld", &idNumber); l or L : placed before any float conversion specifiers to indicate that a double or long double is to be input
73
72 Conversion Example Input octal integer Output integer as decimal
74
73 Conversion Example (cont) #include int main() { int i ; scanf("%o", &i); printf("%d\n", i); return 0; }
75
74 Conversion Example (cont) #include int main() { int i ; scanf("%o", &i); printf("%d\n", i); return 0; } _
76
75 Conversion Example (cont) #include int main() { int i ; scanf("%o", &i); printf("%d\n", i); return 0; } _
77
76 Conversion Example (cont) #include int main() { int i ; scanf("%o", &i); printf("%d\n", i); return 0; } _ i
78
77 Conversion Example (cont) #include int main() { int i ; scanf("%o", &i); printf("%d\n", i); return 0; } _ i
79
78 Conversion Example (cont) #include int main() { int i ; scanf("%o", &i); printf("%d\n", i); return 0; } 70 _ i
80
79 Conversion Example (cont) #include int main() { int i ; scanf("%o", &i); printf("%d\n", i); return 0; } 70 _ i 56
81
80 Conversion Example (cont) #include int main() { int i ; scanf("%o", &i); printf("%d\n", i); return 0; } 70 _ i 56
82
81 Conversion Example (cont) #include int main() { int i ; scanf("%o", &i); printf("%d\n", i); return 0; } 70 56 _ i 56
83
82 Skipping Characters in Input Stream Skipping blank spaces scanf("%d %d %d", &day, &month, &year); Skipping dashes – Enter data as dd-mm-yyyy: 16-3-1999 – Store each number in date variables scanf("%d-%d-%d", &day, &month, &year);
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.