Introduction to Programming Lecture 5: Interaction.

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

CSC Programming for Science Lecture 5: Actual Programming.
41 A Depth Program #include int main(void) { int inches, feet, fathoms; //declarations fathoms = 7; feet = 6 * fathoms; inches = 12 * feet; printf(“Wreck.
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Declarations/Data Types/Statements. Assignments Due – Homework 1 Reading – Chapter 2 – Lab 1 – due Monday.
1 CSE1301 Computer Programming: Lecture 9 Input/Output.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
1 Agenda Variables (Review) Example Input / Output Arithmetic Operations Casting Char as a Number (if time allow)
C Programming Day 2 based upon Practical C Programming by Steve Oualline CS550 Operating Systems.
A simple C program: Printing a line of text #include main() { printf(“hello, world\n”); } Program output: hello, world.
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.
Lecture No: 16. The scanf() function In C programming language, the scanf() function is used to read information from standard input device (keyboard).
Programming Variables. Named area in the computer memory, intended to contain values of a certain kind (integers, real numbers, characters etc.) They.
Chapter 5: Data Input and Output Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
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 Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
C programming for Engineers Lcc compiler – a free C compiler available on the web. Some instructions.
Lecturer: Omid Jafarinezhad Sharif University of Technology Department of Computer Engineering 1 Fundamental of Programming (C) Lecture 4 Input and Output.
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.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
Chapter 7 Formatted input and output. 7.1 introduction Tax: This result is correct; but it would be better Maybe as $13, Make formatting.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Introduction to Programming
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.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
 Integers and Characters  Character Strings  Input and Output.
CGS 3460 Input and Output Revisited. CGS 3460 Display Integer Variables – I #include Preprocessor: interact with input/output of your computer Start point.
Standard I/O Functions – printf() Without specifying any display formatting, the printf() function will use DEFAULT setting: Print in a field of minimum.
CSE1301 Computer Programming: Lecture 6 Input/Output.
© 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.
Department of Electronic & Electrical Engineering IO reading and writing variables scanf printf format strings "%d %c %f"
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
C Language Elements Preprocessor Directives # (sign for preprocessor directive commands) #include Standard header file (.h) Library.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 2.
Data Types and Input Output in C. Size of a Character in C #include int main() { char ch; ch = 'A'; printf("ch = %c\n",ch); printf("size of char = %d.
Introduction to Computing Lecture 03: Basic input / output operations Introduction to Computing Lecture 03: Basic input / output operations Assist.Prof.Dr.
Formatted Input and Output
Formatted Input/Output
Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA
Input/output.
Chapter 2 Overview of C.
ICS103 Programming in C Lecture 3: Introduction to C (2)
Introduction to C CSE 2031 Fall /3/ :33 AM.
Formatted Input/Output
Plan of the Day: More on type conversions scanf printf format strings
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.
Formatted Input/Output
Programming in C Input / Output.
C Formatted Input / Output Review and Lab Assignments
CSI 121 Structured Programming Language Lecture 7: Input/Output
توابع ورودي-خروجي.
Formatted Input/Output
Introduction to CS Your First C Programs
C Programming Variables.
אבני היסוד של תוכנית ב- C
מ- C++ ל- C קרן כליף.
Chapter 4 Managing Input and Output Operations
Formatted Input/Output
Formatted Input/Output
Formatted Input/Output
EECE.2160 ECE Application Programming
Introduction to C EECS May 2019.
EECE.2160 ECE Application Programming
Introduction to C CSE 2031 Fall /15/2019 8:26 AM.
Presentation transcript:

Introduction to Programming Lecture 5: Interaction

2  Produce output  Get input values

3 Interaction  Produce output  Get input values

4 Printing  Printing messages printf("This is message \n"); printf("This is " "message\n");  Printing variables  printf( " format ", parameters); int i = 20; char c = 'a'; printf("%d, %c", i, c); printf("i is %d and char is %c", i, '6');

5 Printing Integers  %d, %i, %ld printf( " %d ", 100); 100 printf( " %d, %d ", +1000, -100); 1000, -100 printf( " %i ", 100); 100 printf( " %ld, %i ", +1000, -100); 1000, -100

6 Printing Unsigned Integers  %u (base 10), %o (base 8), %x (base 16) and %X (base 16) unsigned int i = 26; printf("%u\n", i);//26 printf("%o\n", i);//32 printf("%x\n", i);//1a printf("%X\n", i);//1A

7 Printing Floats  %f, %e, %E, %lf printf( " %f ", 100.5f); float f = -2; double d = 100; printf( " %f, %f ", f, d); , printf( " %f, %e ", 1e3, 1e3); , e+003

8 Printing Chars  %c printf( " %c ", 'a'); a printf( " %c, %c ", 'a','b'); a, b char c1 = 'a'; printf( " %c, %c, %c ", c1, 'b', 65); a, b, A

9 Special Character  Characters in printf result \nnewline \rcarriage return \bbackspace \"" %% \%

10 Printing Strings  %s printf("This is message"); This is message char str1[20] = "This is message"; printf("%s", str1); This is message char str2[10] = "message"; printf("This is %s", str2); This is message

Field length  Field length is a number  Comes after % (and before the type char)  It is the minimum space reserved for print  If value is smaller than the space  Empty space  If value is larger than the space  No effect 11

12 Field length printf("|%4d|\n", 1);// | 1| printf("|%4d|\n", 12345);// |12345| printf("|%4d|\n", );// |-12345| printf("|%4f|\n", );// | | printf("|%15f|\n", ); // | | printf("|%10g|\n", );// | 1234| printf("|%10g|\n", 1234e-10);// |1.234e-007| printf("|%4c|\n", 'A');// | A| printf("|%4s|\n", "ABC");// | ABC| printf("|%4s|\n", "ABCDE");// |ABCDE|

Precision  Precision is a.number and comes after %  For Integer  The minimum number of digits  For floats  With %f, %e  The number of digits after.  For strings  The maximum number of characters 13

14 Precision printf("|%.4d|\n", 1);// |0001| printf("|%.4d|\n", 12345);// |12345| printf("|%.4d|\n", );// |-12345| printf("|%.4f|\n", );// | | printf("|%.10f|\n", );// | | printf("|%.4g|\n", );// |1235| printf("|%.10g|\n", );// |1234.5| printf("|%.4s|\n", "ABC");// |ABC| printf("|%.4s|\n", "ABCDEF");// |ABCD|

Field length and Precision  This is a number with format a.b  Comes after %  First b determines the precision  Then a specifies the field length 15

16 Field length and Precision printf("|%10.5d|\n", 12); | 00012| printf("|%3.5d|\n", 12); |00012| printf("|%10.5f|\n", ); | | printf("|%0.5f|\n", ); | | printf("|%15.10s|\n", "Hello, world"); | Hello, wor| printf("|%5.10s|\n", "Hello, world"); |Hello, wor|

17 Cast in printing (do NOT use) int i = -60; unsigned int j = ; float f = ; printf("i = %f\n", i); i = printf("i = %u\n", i); i = printf("j = %d\n", j); j = printf("f = %d\n", f); f =

18 Interaction  Produce output  Get input values

19 Reading  Read from keyboard (console)  What should be determined in reading  Keyboard enters “characters”, so, how to read int, char, …?  Which type the chars should be converted?  Where should be saved?  scanf (“format”, parameters)  Format: The type that input should be converted to  Parameters: Where should be saved  Needs ‘Enter’ at the end to start reading (why?!)  Reads from beginning until to white spaces (except reading chars)

20 Reading Integers (base 10)  %d, %u, %ld, %lu int i; unsigned int j; long int l; scanf("%d", &i); scanf("%u", &j); scanf("%ld",&l); -90  -90 is saved in memory location i 78 60L  78 is saved in memory location j  60 is saved in memory location l Spaces at the beginning are ignored

21 Reading Integers (cont’d)  %o, %x, %X, %i scanf("%o", &i); Input: 12  i = 10 scanf("%x", &i); Input: 1a  i = 26 scanf("%i", &i); Input: 12  i = 12 Input: 012  i = 10 (It reads in base 8) Input: 0x12  i = 18 (It reads in base 16)

22 Reading floats and doubles  %f, %lf, %e, %g float f; double d; scanf("%f", &f); scanf("%lf", &d); 90.9  90.9 is saved in memory f  saved in memory d Spaces at the beginning are ignored

23 Reading floats and doubles float f1, f2; scanf("%f", &f1); scanf("%e", &f2); Input: 1.23  f1 =  f2 = 4.56 Input: 1.23e+1  f1 = e-1  f2 = 0.456

24 Reading chars  %c char c1, c2, c3; scanf("%c", &c1); /* spaces */ scanf("%c", &c2); scanf("%c", &c3); Input: azb  c1 = 'a' c2 = 'z' c3 = 'b' Spaces at the beginning are NOT ignored

25 Reading chars (cont’d)  White spaces (space, tab, enter) are not ignored when reading char  To ignore white spaces, use “ “ before %c scanf("%d%c%d", &i, &c, &j); Input:  I = 123 c = ‘ ‘ j = 45 scanf("%d %c%d", &i, &c, &j); Input:  I = 123 c = ‘4’ j = 56

26 Reading chars (cont’d)  getchar()  Read char after Enter  getch()  Read char without Enter, does NOT show the char  getche()  Read char without Enter, shows the char

27 Reading Strings  %s char str[20]; scanf("%s", str); Input: ABC  str = "ABC" scanf("%s", str); Input: AB C  str = "AB"

28 Reading Strings  How to read a line  Contains spaces (read until end of line)  gets(s) char str[20]; gets(str); Input: ABC DEF  str = "ABC DEF"

29 Field length in scanf  Field length specifies the maximum number of input characters (in the buffer) used for scanning int i, j; scanf("%5d", &i); Input: 122  i = 122 Input:  i = scanf("%5d%d", &i, &j); Input: 1 2  i = 1, j = 2 Input:  i = 12345, j = 67 Input:  i = 12345, j = 6

30 Special input format  If input data has special format with extra characters  scanf can ignore them int sal, mah, rooz; scanf("%d/%d/%d", &sal, &mah, &rooz); Input: 1389/12/1  sal = 1389, mah = 12, rooz = 1

31 Format of actual input data  The format of actual input data MUST match with the format of scanf int a, b; float f; scanf("%d--%d%f", &a, &b, &f); Input:  a = 1, b = 2, f = 3.0 Input:  a = 1, b = 57, f = 0.0 Input:  a = 1, b = 57, f = 0.0

32 #include int main(void){ int i; unsigned int j; unsigned long int k; char c; float f; printf("Enter a char:\n"); scanf(" %c", &c); printf("Enter an int:\n"); scanf("%d", &i); printf("Enter an unsigned int:\n"); scanf("%u", &j); printf("Enter an unsigned long int:\n"); scanf("%lu", &k); printf("Enter a float:\n"); scanf("%f", &f); printf("Your input are:\n"); printf("int = %d, unsigned int = %u, unsigned long int = %lu, ", i, j, k); printf("char = %c and float = %f\n", c, f); return 0; } برنامه‌اي كه با توليد پيغام‌هاي مناسب ورودي‌هاي را از كاربر بگيرد و در انتها ليست ورودي‌ها را به كاربر نشان دهد.

33 Common Bugs  Casting in printf or scanf  printf("%d", );  double d; scanf("%f", &d);  Mismatch between format and the number of expressions  printf("%d %d", 10);  printf("%d", 10, 20);  Using name of variable instead of address  scanf("%d", i);