該代碼全部在Visual Studio 2015中編寫,有關VS2015的安裝流程后期在寫相關的博文
首先讓我們來輸出一下hello, world!
1、首先新建一個main.cpp的文件,然后在該文件中寫入下面的代碼
#include <stdio.h>
int main() {
printf("hello, world!\n");
return 0;
}
2、在VS中編譯運行結果如下

輸出中文
1、程序main.cpp代碼如下
#include <stdio.h>
int main() {
printf("你好,中國\n");
return 0;
}
2、運行結果如下

格式化輸出整數
1、程序main.cpp代碼如下
#include <stdio.h>
int main() {
//使用%d當做一個占位符,該占位符接收一個整數
printf("number : %d ok\n", 3);
printf("number : %d ok\n", 33);
printf("number : %d ok\n", 333);
return 0;
}
2、運行結果如下

格式化輸出整數 --> 輸出對齊
可以看到上面的例子由於值的長短不一樣,導致輸出看到的效果不是那么美觀,可以使用下面的方式來進行輸出
1、程序main.cpp代碼如下
#include <stdio.h>
int main() {
// %3d用來指定該占位符所占的寬度,為了輸出看着比較整齊
printf("number : %3d ok\n", 3);
printf("number : %3d ok\n", 33);
printf("number : %3d ok\n", 333);
return 0;
}
2、運行結果如下

格式化輸出小數
1、程序main.cpp代碼如下
#include <stdio.h>
int main() {
//使用%f當做一個占位符,該占位符接收一個小數
printf("x = %f, y = %f \n", 12.35, 90.01);
return 0;
}
2、運行結果如下

格式化輸出小數 --> 保留小數位數
1、程序main.cpp代碼如下
#include <stdio.h>
int main() {
//%.2f 表示接收一個小數,只保留小數點后面的2位
printf("x is %.2f \n", 123.456789);
return 0;
}
2、運行結果如下

例子
1、輸出123*456的值
#include <stdio.h>
int main() {
printf("123 * 456 = %d \n", 123 * 456);
return 0;
}

2、輸出123.456 * 123.456 的值,並保留4位小數
#include <stdio.h>
int main() {
printf("123.456 * 123.456 = %.4f \n", 123.456 * 123.456);
return 0;
}
