C 语言学习-1【基本语法】

1、符号常量

#define 符号常量名 常量
使用符号常量计算圆柱体的体积:

c 复制代码
#include <stdio.h>
#define PI 3.1415926

int main() {
    float r, h, volum;
    printf("Please enter the radius: ");
    scanf("%f", &r);
    printf("Please enter the height of the cylinder: ");
    scanf("%f", &h);
    volum = PI * r * r * h;
    printf("The volume of a cylinder is %f\n", volum);
    return 0;
}
  • 运行结果:

2、变量的赋值和初始化

输入两个数据,交换他们的值:

c 复制代码
#include <stdio.h>

int main() {
    int a, b, t;
    a = 5;
    b = 8;
    printf("a = %d, b = %d\n", a, b);
    printf("Exchange two data: \n");
    t = a;
    a = b;
    b = t;
    printf("a = %d, b = %d\n", a, b);
    return 0;
}
  • 运行结果:

3、有符号数和无符号数

输入两个无符号数据,比较他们的大小:

c 复制代码
#include <stdio.h>

int main() {
    unsigned int data1, data2;
    printf("Please enter two numbers: ");
    scanf(" %d %d ", &data1, &data2);
    if (data1 > data2) {
        printf("The first number is larger than the second");
    } else {
        printf("The first number is smaller than the second");
    }
    return 0;
}
  • 运行结果:

4、字符型数据

大写转小写:

c 复制代码
#include <stdio.h>

int main() {
    char c;
    printf("Please enter character: ");
    c = getchar();
    if (c >= 'A' && c <= 'Z') {
        c += 32;
    }
    printf("Output: \n");
    printf("%c\t%d", c, c);
    return 0;
}
  • 运行结果:

5、隐式转换

输入半径,计算圆的面积:

c 复制代码
#include <stdio.h>

int main() {
    float pi = 3.1416;
    int r, area;
    printf("Please enter radius: ");
    scanf("%d", &r);
    area = pi * r * r;
    printf("Area is: %d", area);
    return 0;
}
  • 运行结果:

6、显示转换

输入学生的 3 门课程的成绩,求其平均成绩:

c 复制代码
#include <stdio.h>

int main() {
    int mathscore, average_int;
    float chinscore, engscore, average_implicit, average_explicit_int;
    printf("Please enter the results of math, Chinese and English: \n");
    scanf("%d %f %f", &mathscore, &chinscore, &engscore);
    average_implicit = (mathscore + chinscore + engscore) / 3;
    average_explicit_int = (mathscore + (int)(chinscore) + (int)(engscore)) / 3;
    average_int = (int)((mathscore + (int)(chinscore) + (int)(engscore)) / 3);
    printf("%.1f %.1f %d", average_implicit, average_explicit_int, average_int);
    return 0;
}
  • 运行结果:

7、算术表达式

从键盘上输入一个三位的整数,分别输出个位、十位、百位上的数字

c 复制代码
#include <stdio.h>

int main() {
    int num;
    int ones, tens, hundreds;
    printf("Please enter a number: ");
    scanf("%d", &num);
    hundreds = num / 100;
    tens = (num - hundreds * 100) / 10;
    ones = num % 10;
    printf("The ones place is %d, the tens place is %d, and the hundreds place is %d", ones, tens, hundreds);
    return 0;
}
  • 运行结果:

8、关系表达式

输出关系表达式:

c 复制代码
#include <stdio.h>

int main() {
    int score = 95, a, b, c;
    a = 70;
    b = 'a' == 'a';
    c = 'a' != 'A';
    printf("a = %d, b = %d, c = %d\n", a, b, c);
    return 0;
}
  • 运算结果:

9、逻辑表达式

输入一个年份,判断其是否是闰年:

c 复制代码
#include <stdio.h>

int main() {
    int year;
    printf("Please enter the year: ");
    scanf("%d", &year);
    if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
        printf("%d is leao year\n", year);
    } else {
        printf("%d is not leap year\n", year);
    }
    return 0;
}
  • 运算结果:

10、赋值表达式

复合赋值运算符在程序中的简单使用:

c 复制代码
#include <stdio.h>

void main() {
    int a = 3, b = 8;
    a += a -= a * a;
    b %= 4 - 1;
    b += b *= b -= b *= 3;
    printf("a = %d, b = %d", a, b);
}
  • 运算结果:

11、自增、自减运算符

前置加和后置加大的区别:

c 复制代码
#include <stdio.h>

int main() {
    int a, b, c;
    a = 7;
    printf("a = %d\n", a);
    b = ++a;
    printf("(1) ++a = %d a = %d\n", b, a);
    a = 7;
    c = a++;
    printf("(2) a++ = %d a = %d\n", c, a);
    return 0;
}
  • 运算结果:

12、逗号运算符

逗号表达式的应用:

c 复制代码
#include <stdio.h>

void main() {
    int a, b, c, d;
    a = b = 1;
    d = (c = a++, ++b, b++);
    printf("a = %d, b = %d, c = %d, d = %d", a, b, c, d);
}
  • 运算结果:

13、条件运算符

求最大值:

c 复制代码
#include <stdio.h>

void main() {
    int a, b, max;
    printf("Please enter two numbers: ");
    scanf("%d %d", &a, &b);
    max = a > b ? a : b;
    printf("The maximum value of both %d and %d is: %d", a, b, max);
}
  • 运算结果:

14、位运算赋值运算符

取一个整数的二进制形式从右端开始的若干位,并以八进制形式输出:

c 复制代码
#include <stdio.h>

int main() {
    unsigned short a, b, c, d;
    int i, k;
    printf("Please enter this integer: ");
    scanf("%o", &a);
    printf("Please enter the start position of the intercept bit (the low position starts from 0) and the end position (the high position): \n");
    scanf("%d %d", &i, &k);
    b = a >> (k - i + 1);
    c = ~(~0 << (k - i + 1));
    d = b & c;
    printf("The integer %o truncated from %d to %d bits is %o\n", a, i, k, d);
    return 0;
}
  • 运算结果

15、四则运算计算器

输入两个整数,计算它的和、差、积、商、余数:

c 复制代码
#include <stdio.h>

int main() {
    int num1, num2, i = 1, add, sub, mul, div, rem, flag;
    printf("Please enter the first and second numbers: ");
    scanf("%d %d", &num1, &num2);
    add = num1 + num2;
    sub = num1 - num2;
    mul = num1 * num2;
    if (num2 != 0) {
        flag = 1;
        div = (float)num1 / (float)num2;
        rem = num1 % num2;
    } else {
        flag = 0;
    }
    printf("%d.Sum is %d\n", i++, add);
    printf("%d.Difference is %d\n", i++, sub);
    printf("%d.Product is %d\n", i++, mul);
    (flag == 0) ? printf("%d.The input divisor is 0, and the quotient cannot be calculated\n", i++) : printf("%d.Quotient is %d\n", i++, div);
    (flag == 0) ? printf("%d.The input divisor is 0, and the remainder cannot be calculated\n", i++) : printf("%d.Remainder is %d\n", i++, rem);
    return 0;
}
  • 运算结果:

相关推荐
向上的车轮1 分钟前
ODOO学习笔记(8):模块化架构的优势
笔记·python·学习·架构
HC182580858321 小时前
“倒时差”用英语怎么说?生活英语口语学习柯桥外语培训
学习·生活
学习路上_write1 小时前
FPGA/Verilog,Quartus环境下if-else语句和case语句RT视图对比/学习记录
单片机·嵌入式硬件·qt·学习·fpga开发·github·硬件工程
非概念2 小时前
stm32学习笔记----51单片机和stm32单片机的区别
笔记·stm32·单片机·学习·51单片机
iiiiiankor3 小时前
C/C++内存管理 | new的机制 | 重载自己的operator new
java·c语言·c++
小辛学西嘎嘎3 小时前
C/C++精品项目之图床共享云存储(3):网络缓冲区类和main
c语言·开发语言·c++
无敌最俊朗@3 小时前
stm32学习之路——八种GPIO口工作模式
c语言·stm32·单片机·学习
EterNity_TiMe_3 小时前
【论文复现】STM32设计的物联网智能鱼缸
stm32·单片机·嵌入式硬件·物联网·学习·性能优化
L_cl4 小时前
Python学习从0到1 day28 Python 高阶技巧 ⑤ 多线程
学习
前端SkyRain4 小时前
后端Node学习项目-用户管理-增删改查
后端·学习·node.js