------------------------------------------------- Introducing scanf to designate selective information into variables from the keyboard The program be looked at in the previous session was fine to use when workings at bottom the C++ IDE (Integrated Development Environment). exactly what if we precious to give the program to somebody else with emerge the C compiler, so that they could vary the years set in the program without having to change the professional source canon? You need some way of allow the person (or user) scotch selective information into the program - the clear choice being the keyboard. The scanf officiate will wait for the user to case in information at the keyboard, and when they press the arrive key, will take the data they have typed, and place it into a variable. For example, if we cute to read somebodys eld, and compile out the approximate come in of long time and weeks they have lived, these lead lines of write in code would do the tric k ( none that this isnt a complete program, so will not work on its own if you type it in): - float get along with, days,weeks; printf( count on age in years: ); scanf(%f,&age); days = age*365.25; weeks = age*52.0; printf( You have lived for %5f days and %4f weeks, days, weeks ); This assumes the average modus operandi of days in the year is 365.
25 and in that complaisance are 52 weeks in the year. The scanf function is a melodic composition like the opposite of the printf function - printf writes data out to the console, scanf reads data in from the console. Notice that scanf also u ses the %f code to represent that we want t! o read data into a floating-point variable. If we wanted to read into an integer variable, we would use %d instead. The &age shows that the %f that is read in from the keyboard will be mark into the age variable. Why do we need the & in drive of the variable? We will cover this in a posterior session, when we deal with passing variables to functions by reference. Dont worry more or less this for the moment. But be VERY...If you want to get a total essay, mold it on our website: BestEssayCheap.com
If you want to get a full essay, visit our page: cheap essay
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.