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 小时前
【数据结构】链表(leetcode)
c语言·数据结构·c++·链表·csdn
风尚云网1 小时前
风尚云网前端学习:一个简易前端新手友好的HTML5页面布局与样式设计
前端·css·学习·html·html5·风尚云网
Dola_Pan1 小时前
C语言:数组转换指针的时机
c语言·开发语言·算法
IU宝1 小时前
C/C++内存管理
java·c语言·c++
qq_459730031 小时前
C 语言面向对象
c语言·开发语言
EterNity_TiMe_2 小时前
【论文复现】(CLIP)文本也能和图像配对
python·学习·算法·性能优化·数据分析·clip
sanguine__2 小时前
java学习-集合
学习
lxlyhwl2 小时前
【STK学习】part2-星座-目标可见性与覆盖性分析
学习
nbsaas-boot2 小时前
如何利用ChatGPT加速开发与学习:以BPMN编辑器为例
学习·chatgpt·编辑器
陌小呆^O^2 小时前
Cmakelist.txt之win-c-udp-client
c语言·开发语言·udp