GE 211 Programming in C Dr. Ahmed Telba Room :Ac -134

Slides:



Advertisements
Similar presentations
Dale Roberts Basic I/O – printf() CSCI 230 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Department of.
Advertisements

Dale Roberts Basic I/O – scanf() CSCI 230 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Department of.
CS1061 C Programming Lecture 16: Formatted I/0 A. O’Riordan, 2004.
1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
Introduction to C Programming CE Lecture 2 Basics of C programming.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
1 CSE1301 Computer Programming: Lecture 9 Input/Output.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 6P. 1Winter Quarter I/O in C Lecture 6.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
C Formatted Input/Output /* Using Integer Conversion Specifiers */ #include int main ( ) { printf( "%d\n", 455 ); printf( "%i\n", 455 ); printf( "%d\n",
CS 161 Introduction to Programming and Problem Solving Chapter 13 Console IO Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim from.
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
Chapter 18 I/O in C. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Standard C Library I/O commands.
Input/Output  Input/Output operations are performed using input/output functions  Common input/output functions are provided as part of C’s standard.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Streams Streams –Sequences of characters organized.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
CMPE13 Cyrus Bazeghi Chapter 18 I/O in C. CMPE Standard C Library I/O commands are not included as part of the C language. Instead, they are part.
1 IPC144 Session 11 The C Programming Language. 2 Objectives To format a #define statement correctly To use a #define statement in a C program To construct.
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
Chapter 9 Formatted Input/Output Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
EPSII 59:006 Spring Introduction In this lecture  Formatted Input/Output scanf and printf  Streams (input and output) gets, puts, getchar, putchar.
Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
Yu Yuanming CSCI2100B Data Structures Tutorial 3
Adv. UNIX:io/91 Advanced UNIX v Objectives of these slides: –look in some detail at standard input and output in C Special Topics in Comp.
Chapter 18 I/O in C.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
1 Pertemuan 4 Fungsi-Fungsi Bahasa C Matakuliah: T0016/Algoritma dan Pemrograman Tahun: 2005 Versi: versi 2.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Dale Roberts Basic I/O (Chap. 9) CSCI 230 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Department of.
Lecture 3: Getting Started & Input / Output (I/O) “TRON” Copyright 1982 (Walt Disney Productions)
3. FORMATTED INPUT/OUTPUT. The printf Function The first argument in a call of printf is a string, which may contain both ordinary characters and conversion.
Chapter 3 Input and Output
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Basic I/O in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Stream Model of I/O header file: A stream provides a connection.
CSE1301 Computer Programming: Lecture 6 Input/Output.
E-1 University of Washington Computer Programming I Lecture 5: Input and Output (I/O) © 2000 UW CSE.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
CS 1704 Introduction to Data Structures and Software Engineering.
CCSA 221 Programming in C INPUT AND OUTPUT OPERATIONS IN C – PART 1 1.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 2.
Introduction to Computing Lecture 03: Basic input / output operations Introduction to Computing Lecture 03: Basic input / output operations Assist.Prof.Dr.
Input & Output Operations Week 6 SCP1103 Basic C Programming SEM1 2010/2011.
Lecture 3: Getting Started & Input / Output (I/O)
Chapter 9 - Formatted Input/Output
C Formatted Input/Output
Zhang Hongyi CSCI2100B Data Structures Tutorial 3
Chapter 9 C Formatted Input/Output
Input/output.
TMF1414 Introduction to Programming
Input & Output Operations
Chapter 18 I/O in C.
Programming in C Input / Output.
Input and Output Lecture 4.
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
CSI 121 Structured Programming Language Lecture 7: Input/Output
Lecture 13 Input/Output Files.
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Chapter 9 - Formatted Input/Output
Chapter 4 Managing Input and Output Operations
Programming in C Input / Output.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
DATA TYPES There are four basic data types associated with variables:
Presentation transcript:

GE 211 Programming in C Dr. Ahmed Telba Room :Ac

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 << ; }

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; }

Statements

Declaration statements

Operation –Assignment

Printf() Format

Printf format

Scanf()

/* 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); }

/* Lesson for 3_2 */ #include #define DAYS_IN_YEAR 365 #define PI void main (void) { float income = ; 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); }

#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 */ 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 */ 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); }

/* 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); }

/* 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); }

/* 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./ ; printf ("%16.2f %20.2f\n", degC, degF); degC += 20.; degF = degC * 9./ ; printf ("%16.2f %20.2f\n", degC, degF); degC += 20.; degF = degC * 9./ ; printf ("%16.2f %20.2f\n", degC, degF); degC += 20.; degF = degC * 9./ ; printf ("%16.2f %20.2f\n", degC, degF); degC += 20.; degF = degC * 9./ ; printf ("%16.2f %20.2f\n", degC, degF); degC += 20.; degF = degC * 9./ ; printf ("%16.2f %20.2f\n", degC, degF); }

/* 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); }

30 scanf() Example: scanf(“%d”, &x); printf() Example: printf(“The value of x is %d\n”, x); #include Recall

31 Input/Output Program

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

33 Streams: Input -- Example _ \n int item; float cost; scanf(“%d %f”, &item, &cost); input buffer

34 Streams: Input -- Example (cont) _ \n int item; float cost; scanf(“%d %f”, &item, &cost); itemcost

35 Streams: Input – Example (cont) _ 25.5\n int item; float cost; scanf(“%d %f”, &item, &cost); item 135 cost

36 Streams: Input – Example (cont) _ \n int item; float cost; scanf(“%d %f”, &item, &cost); item 135 cost 25.5

37 Streams: Output -- Example Hello!\n printf(“Hello!\n”); output buffer

38 Hello!\n printf(“Hello!\n”); Streams: Output – Example (cont)

39 ello!\n H printf(“Hello!\n”); Streams: Output – Example (cont)

40 llo!\n He printf(“Hello!\n”); Streams: Output – Example (cont)

41 lo!\n Hel printf(“Hello!\n”); Streams: Output – Example (cont)

42 o!\n Hell printf(“Hello!\n”); Streams: Output – Example (cont)

43 !\n Hello printf(“Hello!\n”); Streams: Output – Example (cont)

44 \n Hello! printf(“Hello!\n”); Streams: Output – Example (cont)

45 Hello! _ printf(“Hello!\n”); Streams: Output – Example (cont)

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'

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

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

49 Example: ReadData Input name, age, gender, idNumber

50 #include

51 #include /*************************************\ Read in important info about a student \**************************************/

52 #include /*************************************\ Read in important info about a student \**************************************/ int main() { return 0; }

53 #include /*************************************\ Read in important info about a student \**************************************/ int main() { char name[100] ; float age ; char gender ; int idNumber ; return 0; }

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; }

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

56 stdout: Output Data (e.g., from a variable) is written out to stdout using the printf() function.

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

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 _

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);

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

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)

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)

63 For printf : variables containing data for output Example: (“ printf(“%s is %d years old.\n”, name, age); printf -- Other-Arguments

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

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:

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!

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);

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);

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

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);

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

72 Conversion Example Input octal integer Output integer as decimal

73 Conversion Example (cont) #include int main() { int i ; scanf("%o", &i); printf("%d\n", i); return 0; }

74 Conversion Example (cont) #include int main() { int i ; scanf("%o", &i); printf("%d\n", i); return 0; } _

75 Conversion Example (cont) #include int main() { int i ; scanf("%o", &i); printf("%d\n", i); return 0; } _

76 Conversion Example (cont) #include int main() { int i ; scanf("%o", &i); printf("%d\n", i); return 0; } _ i

77 Conversion Example (cont) #include int main() { int i ; scanf("%o", &i); printf("%d\n", i); return 0; } _ i

78 Conversion Example (cont) #include int main() { int i ; scanf("%o", &i); printf("%d\n", i); return 0; } 70 _ i

79 Conversion Example (cont) #include int main() { int i ; scanf("%o", &i); printf("%d\n", i); return 0; } 70 _ i 56

80 Conversion Example (cont) #include int main() { int i ; scanf("%o", &i); printf("%d\n", i); return 0; } 70 _ i 56

81 Conversion Example (cont) #include int main() { int i ; scanf("%o", &i); printf("%d\n", i); return 0; } _ i 56

82 Skipping Characters in Input Stream Skipping blank spaces scanf("%d %d %d", &day, &month, &year); Skipping dashes – Enter data as dd-mm-yyyy: – Store each number in date variables scanf("%d-%d-%d", &day, &month, &year);