那如何配合 變數型式顯示?
名稱稱為 "輸出格式化"
我們之所以會用到是因為他本身是不法正常寫入字串內部執行,需要有擷取方式到字串中。
下方是我們常見的格式化輸出。
參考
結論
int out content =已經是字串不能再更改 ,%d 告訴這個擷取逗號後的 i 值到 %d 內。
所以會變成 => int out content 5。
int i =5;
printf(" int out content %d", i );
補充
下方為 stdio.h 的函數
_Check_return_opt_
_CRT_STDIO_INLINE int __CRTDECL printf(
_In_z_ _Printf_format_string_ char const* const _Format,
...)
#if defined _NO_CRT_STDIO_INLINE
;
#else
{
int _Result;
va_list _ArgList;
__crt_va_start(_ArgList, _Format);
_Result = _vfprintf_l(stdout, _Format, NULL, _ArgList);
__crt_va_end(_ArgList);
return _Result;
}
#endif
Scanf() 輸入函式
Scanf("%f", &miles);
呼叫 scanf() 複製資料至變數 miles,miles 資料 是從標準輸入裝置而取得資料。輸入裝置可能會是鍵盤之類的輸入裝置,電腦都會將資料傳送到miles 傳送。
scanf(),是將&字元加在變數前,& 是 位址運算元 的意思,會告訴scanf() 要接收新的值的變數會在何處,若沒有加上 &,scanf 只會知道現有的值,卻不能知道記憶體位置,所以無法將新的值傳入現有的變數。
printf("enter two integer >> ");
scanf("%d %d",&m , &n);
m =m+5;
n = (n++);
printf("m = %d \n n =%d\n",m ,n);
補充
在執行過程中,命令管理員(Command)遇到亂碼之類的時候,打上chcp 65001
轉 UTF-8 可以解決 中文亂碼問題。
輸出函式-練習
1. 寫一個長方形面積
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char const *argv[])
{
//長方形面積 (長*寬)
float width = 2.9;
float height = 2.9;
printf("面積 >> %d", (width * height));
system("PAUSE");
return 0;
}
\
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
int main(int argc, char const *argv[])
{
//長方形面積 (長*寬)
float width = 2.9;
float height = 2.9;
cout << width * height;
system("PAUSE");
return 0;
}
2. BMI
BMI值計算公式: BMI = 體重(公斤) / 身高2(公尺2)
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char const *argv[])
{
// BMI值計算公式: BMI = 體重(公斤) / 身高2(公尺2)
int weight = 2.9;
float height = 2.9;
int Bmi;
height /=100;
Bmi= weight/(height*height);
printf("Bmi = %d", Bmi);
system("PAUSE");
return 0;
}
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
int main(int argc, char const *argv[])
{
//長方形面積 (長*寬)
int weight = 2.9;
float height = 2.9;
int Bmi;
height /= 100;
Bmi = weight / (height * height);
cout << "BMI = " << Bmi;
system("PAUSE");
return 0;
}
3.輸出格式化數字
C/C++ 中,Printf() 格式字串中,可以在% 與 d 中間加入數字。此數字表示欄寬(field width),是顯示數值行數。
printf("Int out number = %3d \n", inches);
inches 位置是有保留 (%3d ) 3個列印空間(欄位),印此未超過3個位置會看得出,數值有向右對齊的效果。
因次有三個結論