120 likes | 268 Vues
Chapter 5. Basic Input / Outputs. By: Mr. Baha Hanene. LEARNING OUTCOMES. This chapter will cover the learning outcome 02 i.e. 2. Use basic data-types and input / output in C programs (L02). CONTENTS. Formatted Inputs & Outputs Input Statement Combining Inputs & Outputs
E N D
Chapter 5 Basic Input / Outputs By: Mr. BahaHanene
LEARNING OUTCOMES • This chapter will cover the learning outcome 02 i.e. • 2. Use basic data-types and input / output in C programs (L02)
CONTENTS • Formatted Inputs & Outputs • Input Statement • Combining Inputs & Outputs • Things to Remember • Exercise
HOW Scanf() WORKS • It’s looks like printf statement but different in nature!! • As printf is used to display the text & variable values stored in the memory locations. • Scanf is an input statement which is used to store values in different memory locations using variables • For example: • intst_code; • scanf(“ ”, ); What format specifier we use to display Integer values (%li, %f ???) %i The variable in which we want to store value is ?????? & st_code Address of Symbol
MULTIPLE VARIABLES IN ONE Scanf • scanf(“%d %d”, &num1, &num2); • It means that it would stop here and wait for the user to type in two values. Once they have been typed in & the Enter Key pressed, the values are assigned to num1 & num2.
COMBINING INPUT & OUTPUT STATEMENTS • printf(“Input Two Integer Values ”); • scanf(“%d %d”, &num1, &num2); • In the first place, there is a problem that how the user would come to know about the program is waiting for two values? The program should prompt user to enter two values. Now it will be easy for user to understand which type of values he suppose to enter
THINGS TO REMEMBER • Make sure that the specifications agree with the data to be read. • Make sure to put the & character before the variable name so that you specify the address of the variable in memory not the variable itself. • The prompt message should be short and clear
EXERCISE • Design a program to get three integers representing a date (date, month, year) from the user, display each of the day, month, year on the separate lines and then display the full date without spaces in the following form: day/month/year • Your output should be displayed on the screen using the following format: Please enter date (dd, mm, yyyy) : 15 10 2010 ----- DATE CONVERSION ----- Day = 15 Month = 10 Year = 2010 The today’s date is 15/10/2010.