Formatted printf, Addressof, scanf, const, Overflow, Truncation

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值