Learning C-Style C++by Direct Approach Mentoring "%d",number)6. Variable Screen Output
If you ever have an area of malfunctioning code, try printing out the values as it runs to see what's happening. This is an excellent way to troubleshoot. Let's say you have an intcallednand you want to print its value to the screen. Try something like this:intn;"The value of n is %d at this moment.", n );Notice the %d above. printfchecks the string you give it for any format tags. A format tag is some text that starts with a percent sign (%) and is (usually) followed by one letter.printfwill replace any format tags it finds with the values in variables of your choice. Format tags only work in strings you send to printf. They are your way to tell printf what you want it to do.Let's say we want to print out the values in 3 different kinds of variables, a number, a decimal, and a character: intnumber;floatdec;charc;"Number: %d Decimal: %f Character: %c", number, dec, c );The order has to match! If the string uses int,float,charthen the list of variables after the string must also beint,float,char.
Next: 7. Input
|