1 / 12

scanf and printf in C Language Lecture-5

King Fahd University of Petroleum and Minerals Affiliated Colleges at Hafr Al-Batin (ACHB / HBCC )  Computer Science & Engineering Technology Unit (CSET). CSET101: Computer programming. scanf and printf in C Language Lecture-5. Introduction. printf (print formatted) function

iman
Download Presentation

scanf and printf in C Language Lecture-5

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. King Fahd University of Petroleum and Minerals Affiliated Colleges at Hafr Al-Batin (ACHB / HBCC )  Computer Science & Engineering Technology Unit (CSET) CSET101: Computer programming scanf and printf in C Language Lecture-5

  2. Introduction • printf (print formatted) function • printf function performs output to the standard output device, typically monitor. • printf requires a format string that defines text to print out and specifications on how to print out values of variables. • Examples: • printf("the ASCII value of %c is %d", 65, 65); // code of A is 65 • printf("the size of int is %d bytes", sizeof(int)); • printf(“My age is %f years”, age); //age as float variable • scanf (scan formatted) function • scanf performs input from the standard input device, typically keyboard. • scanf also requires format string or control string that controls how input will be performed. • scanf does type conversion from ASCII to the type specified in format string. • Examples • scanf(“%c”, &initials); // note the & symbol before variable • scanf(“%f”, &radius); // the & means address of • scanf(“%d %c %f %s”, &count, &code, &ratio, &name);

  3. Introduction

  4. Example-1 #include <stdio.h> void main() { int number; printf("Type in a number \n"); scanf("%d", &number); printf("The number you typed was %d\n", number); } ---------------------------------------------------------------------------- Sample Program Output :- Type in a number 23 The number you typed was 23

  5. Explanation of Example-1 • An integer variable called number is defined. • A prompt to enter in a number is then printed using the statement : • printf("Type in a number \n:"); • The scanf routine, which accepts the response, has two arguments. • The first ("%d") specifies what type of data type is expected (i.e. , char, int, or float).  • The second argument (&number) specifies the variable into which the typed response will be placed. In this case the response will be placed into the memory location associated with the variable number. • This explains the special significance of the & character (which means the address of).

  6. Example-2 #include < stdio.h > main() { int sum; char letter; float money; printf("Please enter an integer value "); scanf("%d", &sum ); printf("Please enter a character "); scanf(" %c", &letter ); printf("Please enter a float variable "); scanf("%f", &money ); printf("\nThe variables you entered were\n"); printf("value of sum = %d\n", sum ); printf("value of letter = %c\n", letter ); printf("value of money = %f\n", money ); } Sample Program Output:- Please enter an integer value 34 Please enter a character W Please enter a float variable 32.3 The variables you entered were value of sum = 34 value of letter = W value of money = 32.300000

  7. Explanation of Example-2 • This program illustrates several important points. • the c language provides no error checking for user input. The user is expected to enter the correct data type. For instance, if a user enters a character when an integer value was expected, the program may enter an infinite loop or abort abnormally. • It is up to the programmer to validate data for correct type and range of values.

  8. Formatted Input • Formatted input refers to an input data that has been arranged in a particular format. • This is possible in C by using scanf function. • The general form of scanf is: scanf(“control string” , arg 1, arg 2, ………., arg n); • The control stringspecifies the field format consisting of conversion character ‘%’ • The arg 1, arg 2, ………., arg n specify the address location. • The control string and arguments are separated by commas( , ).

  9. Inputting Integer Numbers (%d) • The field specification for reading an integernumber is %d • The percent sign (%) indicates that a conversion specification follows. • d known as data type character, indicates integer mode. • For example: scanf(“%d %d”, &num1, &num2);

  10. Inputting Real Numbers (%f) • The field specification for reading an real number is %f • The percent sign (%) indicates that a conversion specification follows. • f known as data type float, indicates real mode. • For example: scanf(“%f %f %f”, & x, & y, & z);

  11. Inputting Character Strings(%c or %s) • The field specification for reading character is %c or %s • The percent sign (%) indicates that a conversion specification follows. • c or s known as data type character, indicates character or string mode. • For example: scanf(“%c %c”, &num1, &num2);

  12. Reading mixed Data Types • It is possible to used one scanf statement to input a data line containing mixed mode data. • In such cases, care should be taken that the input data items match the control specification in order and type. • For example: scanf(“%d %c %f %s”, &count, &code, &ratio, &name); • Will read the data: 15 p 1.573 coffee

More Related