我觉得对于一个编程小白,还是从最基础的C/C++开始学习吧
一切都从现在,最新的开始,打好根基,才能更上一层楼吧
第一个简单的printf程序
#include <stdio.h>
int main()
{
printf("hehe\n");
return 0;
}
打印数字
#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;
}
使用占位符改善数字对齐
#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;
}
输出小数
#include <stdio.h>
int main() {
//使用%f当做一个占位符,该占位符接收一个小数
printf("x = %f, y = %f \n", 12.35, 90.01);
return 0;
}
保留小数位数
#include <stdio.h>
int main() {
//%.2f 表示接收一个小数,只保留小数点后面的2位
printf("x is %.2f \n", 123.456789);
return 0;
}
简单乘法运算
#include <stdio.h>
int main() {
printf("123 * 456 = %d \n", 123 * 456);
return 0;
}
保留固定位数的小数位
#include <stdio.h>
int main() {
printf("123.456 * 123.456 = %.4f \n", 123.456 * 123.456);
return 0;
}