讲师中心 微信公众号
AI工具推荐 视频效率加速

C 代码片段:)

夜瑶君_6714

夜瑶君_6714

发布时间:2024-08-18 18:42:48

|

608人浏览过

|

来源于dev.to

转载

数据类型

#include <stdio.h>

// struct datatype
struct person {
    char name[50];
    int age;
    float salary;
};

// enum datatype
enum color {red, green, blue};

int main() {

    // basic data types
    int a = 10; // 4 bytes
    float b = 5.5; //4 bytes
    char c = 'a'; //1 byte
    double d = 2.3; //8 bytes
    long double e; // 16 bytes (64-bit), 12 bytes (32-bit)
    short integer f; // 2 bytes
    long int g; //8 bytes (64-bit), 4 bytes (32-bit)


    // array
    int arr[5] = {1, 2, 3, 4, 5};

    // pointer
    int *ptr = &a;

    // structure
    struct person person1;
    person1.age = 30;
    person1.salary = 55000.50;

    // enumeration
    enum color mycolor = red;

    // print values
    printf("integer: %d\n", a);
    printf("float: %.2f\n", b);
    printf("character: %c\n", c);
    printf("array: %d\n", arr[0]);
    printf("pointer: %d\n", *ptr);
    printf("structure: name = %s", person1.name);
    printf("enumeration: %d\n", mycolor);
    return 0;
}

真实数据类型:
浮动、双倍和长双倍

记忆片段

  1. 文本段(代码段)
    它包含编译后的机器代码

  2. 数据段
    存储由程序员初始化的全局变量和静态变量。
    两种类型:

初始化数据段:包含在程序中显式初始化的全局变量和静态变量。
前任。 int x=2;

未初始化数据段(bss):包含未显式初始化的全局变量和静态变量。
前任。 int x;

  1. 堆
    它用于运行时动态内存分配。

  2. 堆叠
    它存储局部变量、函数参数和返回地址。

c 代码片段:)

函数原型

是函数的声明,指定函数的名称、返回类型和参数,但不提供函数体。

(注意: 放在源文件的开头,可以有多个,但只能定义一个)

#include <stdio.h>

// function prototype with formal parameters
void func(char name[]);

int main() {
    char name[] = "alice";
    //actual parameters
    greet(name);
    return 0;
}

// function definition
void func(char name[]) {
    printf("hello, %s!\n", name);
}

mod/模数

此运算符仅用于整数数据类型。在 float 数据类型上使用模数是非法的

#include <stdio.h>
#include <math.h>

int main() {
    double dividend = 7.5;
    double divisor = 2.0;
    double result;
    int i=2;
    i=i%10;//works on integer types

    result = fmod(dividend, divisor);
    printf("%.1f",result); //for float types

    return 0;
}

for循环

#include<stdio.h>
int main()
{
    int x;
    for(x=1; x<=10; x++)
    {
       // ternary conditional operator
       a <= 20 ? (b = 30) : (c = 30);
    }
    return 0;
}

while 循环

#include<stdio.h>
int main()
{
    int j=1;
    while(j <= 255)
    {
      j++;
    }
    return 0;
}

宏观

它是一段被赋予名称的代码片段,可用于在实际编译之前的预处理阶段在代码中执行文本替换。

#define pi 3.14159
#define square(x) ((x) * (x))

开关盒

#include <stdio.h>
int main() {
    int day = 3;

    switch (day) {
        case 1:
            printf("monday\n");
            break;
        case 2:
            printf("tuesday\n");
            break;
        default:
            printf("invalid day\n");
            break;
    }

    return 0;
}

内存管理

malloc:分配内存但不初始化它。内存中包含垃圾值。

calloc:分配内存并将所有位初始化为零。

#include <stdio.h>
#include <stdlib.h>

int main() {
    int *array;
    int size = 5;

    // allocate memory for an array of integers
    array = (int *)malloc(size * sizeof(int));

    // deallocate the memory
    free(array);

    int *a[3];
    a = (int*) malloc(sizeof(int)*3);
    free(a);

    return 0;
}

指针

它提供了一种直接访问和操作内存的方法。
指针是一个变量,它存储另一个变量的内存地址。指针不是直接保存数据值,而是保存数据在内存中的位置。

基本指针操作

声明:

int *ptr; // declares a pointer to an integer

初始化:

int x = 10;
int *ptr = &x; // `ptr` now holds the address of `x`

取消引用:
访问存储在指定地址的值

int value = *ptr; // retrieves the value of `x` via `ptr`

样品

Web Accessibility (WCAG 2 AA))
Web Accessibility (WCAG 2 AA))

将 WCAG 2 AA 无障碍标准应用于设计系统、UI 组件和网页——通过可运行的验证器进行对比度计算,采用设计 token 模式使颜色天生可访问,设定排版和焦点状态规则,并提供语义检查清单。适用于构建或审查 UI。

下载
#include <stdio.h>

int main() {
    int a = 5;         // normal integer variable
    int *p = &a;       // pointer `p` points to `a`

    printf("value of a: %d\n", a);          // output: 5
    printf("address of a: %p\n", (void*)&a); // output: address of `a`
    printf("value of p: %p\n", (void*)p);   // output: address of `a`
    printf("value pointed to by p: %d\n", *p); // output: 5

    *p = 10; // modifies `a` through the pointer

    printf("new value of a: %d\n", a);      // output: 10

    return 0;
}

指向指针的指针

int x = 10;
int *ptr = &x;
int **pptr = &ptr; // pointer to pointer

指向结构体的指针

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// define a structure
struct person {
    char name[50];
    int age;
};

int main() {
    // declare a pointer to a structure
    struct person *ptr;

    // allocate memory for the structure
    ptr = (struct person *)malloc(sizeof(struct person));
    if (ptr == null) {
        printf("memory allocation failed\n");
        return 1;
    }

    // use the arrow operator to access and set structure members
    strcpy(ptr->name, "alice");
    ptr->age = 30;

    // print structure members
    printf("name: %s\n", ptr->name);
    printf("age: %d\n", ptr->age);

    // free allocated memory
    free(ptr);

    return 0;
}

关键词

外部的

用于声明在另一个文件中或稍后在同一文件中定义的全局变量或函数。

#include <stdio.h>

int main() {
    extern int a;  // accesses a variable from outside
    printf("%d\n", a);
    return 0;
}
int a = 20;

静止的

全局静态变量:
全局静态变量是在任何函数外部使用 static 关键字声明的。
范围仅限于声明它的文件。这意味着它无法从其他文件访问。如果没有显式初始化,全局静态变量会自动初始化为零。

#include <stdio.h>

static int global_var = 10; // global static variable

void display(void) {
    printf("global variable: %d\n", global_var); // accesses global static variable
}

int main(void) {
    display(); // prints 10
    global_var = 20;
    display(); // prints 20
    return 0;
}

局部静态变量
局部静态变量在函数内部使用 static 关键字声明。
范围仅限于声明它的函数。不能从函数外部访问它。如果没有显式初始化,局部静态变量会自动初始化为零。

#include <stdio.h>

void counter(void) {
    static int count = 0; // local static variable
    count++;
    printf("count: %d\n", count); // prints the current value of count
}

int main(void) {
    counter(); // prints 1
    counter(); // prints 2
    counter(); // prints 3
    return 0;
}

注意: 在 c 中,当你有一个同名的全局变量和局部变量时,局部变量会在其作用域内隐藏(或隐藏)全局变量。

结构体

struct book
{
    char name[10];
    float price;
    int pages;
};

类型定义

它用于为现有类型创建别名

typedef unsigned int uint;

int main() {
    uint x = 10;  // equivalent to unsigned int x = 10;
    printf("x = %u\n", x);
    return 0;
}
typedef struct Ntype {
    int i;
    char c;
    long x;
} NewType;

连锁

指符号(变量和函数)跨源文件的可见性

外部链接:该符号在多个翻译单元中可见(全局)

内部链接:符号仅在定义它的翻译单元内可见

无(无链接):符号在定义它的块之外不可见(局部变量)

热门AI工具

更多
立刻MV
立刻MV Hot

立刻MV是一款AI文本写作工具,AI 音乐视频(MV)创作工具。

UP简历
UP简历 Hot

一款AI办公效率工具,主要用于基于AI技术的免费在线简历制作工具,适合需要提升相关任务效率的用户。

切问学术

切问学术是一款AI论文写作工具,复旦大学NLP团队推出的AI学术智能体。

豆包大模型

豆包大模型是一款由字节跳动推出的企业级大语言模型服务平台。

Loomy
Loomy Hot

一款AI工具,主要用于科大讯飞发布的桌面级 AI 助理,比 OpenClaw 更易用、更安全!,适合需要提升相关任务效率的用户。

DeepSeek

DeepSeek是一款面向对话、写作、编程和推理场景的AI大模型工具。

Atoms
Atoms Hot

Atoms是一款AI智能体工具,第一支自动构建真实业务的 AI 团队。

二狗PPT
二狗PPT Hot

一款AI演示文稿工具,主要用于专为中式职场打造的AI PPT生成工具,适合需要提升相关任务效率的用户。

WorkBuddy

一款AI办公效率工具,主要用于腾讯云推出的AI原生桌面智能体工作台,适合需要提升相关任务效率的用户。

相关专题

更多
数据类型有哪几种
数据类型有哪几种

数据类型有整型、浮点型、字符型、字符串型、布尔型、数组、结构体和枚举等。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

3111

2023.10.31

php数据类型
php数据类型

本专题整合了php数据类型相关内容,阅读专题下面的文章了解更多详细内容。

554

2025.10.31

c语言 数据类型
c语言 数据类型

本专题整合了c语言数据类型相关内容,阅读专题下面的文章了解更多详细内容。

542

2026.02.12

css中float用法
css中float用法

css中float属性允许元素脱离文档流并沿其父元素边缘排列,用于创建并排列、对齐文本图像、浮动菜单边栏和重叠元素。想了解更多float的相关内容,可以阅读本专题下面的文章。

6867

2024.04.28

C++中int、float和double的区别
C++中int、float和double的区别

本专题整合了c++中int和double的区别,阅读专题下面的文章了解更多详细内容。

804

2025.10.23

java基础知识汇总
java基础知识汇总

java基础知识有Java的历史和特点、Java的开发环境、Java的基本数据类型、变量和常量、运算符和表达式、控制语句、数组和字符串等等知识点。想要知道更多关于java基础知识的朋友,请阅读本专题下面的的有关文章,欢迎大家来php中文网学习。

6164

2023.10.24

Go语言中的运算符有哪些
Go语言中的运算符有哪些

Go语言中的运算符有:1、加法运算符;2、减法运算符;3、乘法运算符;4、除法运算符;5、取余运算符;6、比较运算符;7、位运算符;8、按位与运算符;9、按位或运算符;10、按位异或运算符等等。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

3104

2024.02.23

php三元运算符用法
php三元运算符用法

本专题整合了php三元运算符相关教程,阅读专题下面的文章了解更多详细内容。

1812

2025.10.17

Gradle插件开发入门教程
Gradle插件开发入门教程

本专题介绍Gradle插件开发的基本流程,涵盖插件类编写、自定义任务注册、扩展参数设计和插件应用方法,帮助你理解插件如何组织构建逻辑,减少多个项目之间的重复配置。

80

2026.10.22

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
SQL 教程
SQL 教程

共61课时 | 7.4万人学习

Qt Creator 快速入门文档
Qt Creator 快速入门文档

共0课时 | 0人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn