printf (multiple variables)
int age = 33;
int weight = 55;
int boredom = 55;
printf (“age: %d\n weight: %d\n boredom: %d\n”, age, weight, boredom”);
The instruction in printf
printf: The Width Option
Example
By using “%5d”
(default)
every variable will be ensured to have at least 5 spaces, if variables shorter than 5, extra whitespaces will be added on the left(right-aligned)
(default).
Cause 473895 is longer than 5 numbers, no extra white space is required and whole number will also be sent out.
Left-aligned
By using “%-5d”
(default)
Output
printf: Zero-Filled Option
By using “%07d”
(default).
It will print 47 with five 0 (to keep hold 7 numbers)
Negative numbers print with a negative sign:
If positive sign is required, “+” should be added
printf: Printing Floating-Point Numbers
Decide: how many decimal places should be shown after the decimal point
By using “%.2f” (only 2decimals left after the point):
Combined with width opinion to use
Using “%6.2f” (print the only 2 decimals with 6 characters
)
Using “%06.2f” (print the only 2 decimals with 6 characters, and 0 will be used to fill the space to 6 characters
)
% something
depend on the type of data which will be print out
%[flags][width][.precision][length]specifier
printf("%06.2f\n", pi);
Notification: must notice the type of data variables all the time
%d print a int; %f print a float; %c print a ASCII character
Print(“the address of data is: %d”, &example); // using “&” means print the address of the variable “example”
Input: scanf
Similar to printf:
Calculate the number within the printf:
Same as
If
Will not calculate, as it will performance instruction from left to right when inside the printf
Notice the type of data between the scanf and printf
Const Keyword (make numbers or characters meaningful rather than just itself)
When using
const flaot PI = 3.14159f;
this const do not need to be changed during the run time, so make it as const and give it a meaningful name