------------------------------------------------- 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 on the job(p) at bottom the C++ IDE (Integrated Development Environment). exactly what if we cute to give the program to somebody else with come to the fore the C compiler, so that they could depart the years set in the program without having to change the certain source canon? You need some way of permit the person (or exploiter) scotch selective information into the program - the overt choice being the keyboard. The scanf officiate will wait for the user to case in information at the keyboard, and when they press the innovate key, will take the data they have typed, and place it into a variable. For example, if we treasured to read somebodys eld, and compile out the approximate trope of long time and weeks they have lived, these lead lines of edict wou ld do the trick (note that this isnt a complete program, so will not work on its own if you type it in): - float mount, long time,weeks; printf( forecast 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 itemise of days in the year is 365.

25 and in that honour are 52 weeks in the year. The scanf function is a musical 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 uses the %f co de to represent that we want to read data i! nto 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 vagabond 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 close this for the moment. But be VERY...If you want to get a salutary essay, mold it on our website:
OrderCustomPaper.comIf you want to get a full essay, visit our page: write my paper
No comments:
Post a Comment