点击上方"蓝字"关注我们
01、hello
#include <stdio.h>
int main() {
printf("Hello World!");
return 0;
}
使用 gcc
编译 hello.c
文件
$ gcc -o hello hello.c
运行编译后的二进制文件(hello
)
$ ./hello
# 输出 => Hello World
02、变量
int myNum = 15;
int myNum2; // 不赋值,然后再赋值
myNum2 = 15;
int myNum3 = 15; // myNum3 值为 15
myNum3 = 10; // 现在 myNum3 值为 10
float myFloatNum = 5.99; // 浮点数
char myLetter = 'D'; // 字符
int x = 5;
int y = 6;
int sum = x + y; // 添加变量相加
// 声明多个变量
int x = 5, y = 6, z = 50;
03、常量
const int minutesPerHour = 60;
const float PI = 3.14;
// 最佳实践
const int BIRTHYEAR = 1980;
04、打印文本
printf("I am learning C.");
int testInteger = 5;
printf("Number = %d", testInteger);
float f = 5.99; // 浮点数
printf("Value = %f", f);
short a = 0b1010110; // 2 进制数字
int b = 02713; // 8 进制数字
long c = 0X1DAB83; // 16 进制数字
// 以 8 进制形似输出
printf("a=%ho, b=%o, c=%lo\n", a, b, c);
// 输出 => a=126, b=2713, c=7325603
// 以 10 进制形式输出
printf("a=%hd, b=%d, c=%ld\n", a, b, c);
// 输出 => a=86, b=1483, c=1944451
// 以 16 进制形式输出(字母小写)
printf("a=%hx, b=%x, c=%lx\n", a, b, c);
// 输出 => a=56, b=5cb, c=1dab83
// 以 16 进制形式输出(字母大写)
printf("a=%hX, b=%X, c=%lX\n", a, b, c);
<