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;
}
  • 运算结果:

相关推荐
Jm_洋洋7 分钟前
【Linux系统编程】程序替换:execve(execl、execlp、execle、execv、execvp、execvpe)
linux·运维·c语言·开发语言·程序人生
小莞尔23 分钟前
【51单片机】【protues仿真】基于51单片机秒表计时器系统(带存储)
c语言·stm32·单片机·嵌入式硬件·物联网·51单片机
闭着眼睛学算法41 分钟前
【双机位A卷】华为OD笔试之【哈希表】双机位A-跳房子I【Py/Java/C++/C/JS/Go六种语言】【欧弟算法】全网注释最详细分类最全的华子OD真题题解
java·c语言·c++·python·算法·华为od·散列表
crary,记忆1 小时前
简介NPM 和 NPX
前端·学习·npm·node.js
代码改善世界1 小时前
C语言内存机制深度解析:指针运算、数组与字符串实战指南
c语言
kitsch0x971 小时前
论文学习_One Bug, Hundreds Behind: LLMs for Large-Scale Bug Discovery
学习·bug
自信150413057591 小时前
初学者小白复盘15之指针(4)
c语言·数据结构·算法
GilgameshJSS2 小时前
STM32H743-ARM27例程-TCP_Server
c语言·arm开发·stm32·单片机·tcp/ip
czy87874752 小时前
用C语言实现建造者模式
c语言
qiuiuiu4132 小时前
正点原子RK3568学习日志19- Linux错误处理 字符驱动框架完全体
linux·服务器·学习·ubuntu