C 代码片段:)

数据类型

#include // struct datatypestruct person {    char name[50];    int age;    float salary;};// enum datatypeenum 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", a);    printf("float: %.2f", b);    printf("character: %c", c);    printf("array: %d", arr[0]);    printf("pointer: %d", *ptr);    printf("structure: name = %s", person1.name);    printf("enumeration: %d", mycolor);    return 0;}

登录后复制

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

记忆片段

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

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

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

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


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

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

c 代码片段:)

函数原型

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

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

#include // function prototype with formal parametersvoid func(char name[]);int main() {    char name[] = "alice";    //actual parameters    greet(name);    return 0;}// function definitionvoid func(char name[]) {    printf("hello, %s!", name);}

登录后复制

mod/模数

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

#include #include 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循环

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

登录后复制

while 循环

#includeint main(){    int j=1;    while(j <= 255)    {      j++;    }    return 0;}

登录后复制

宏观

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

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

登录后复制

开关盒

#include int main() {    int day = 3;    switch (day) {        case 1:            printf("monday");            break;        case 2:            printf("tuesday");            break;        default:            printf("invalid day");            break;    }    return 0;}

登录后复制

内存管理

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

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

#include #include 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`

登录后复制

样品

#include int main() {    int a = 5;         // normal integer variable    int *p = &a;       // pointer `p` points to `a`    printf("value of a: %d", a);          // output: 5    printf("address of a: %p", (void*)&a); // output: address of `a`    printf("value of p: %p", (void*)p);   // output: address of `a`    printf("value pointed to by p: %d", *p); // output: 5    *p = 10; // modifies `a` through the pointer    printf("new value of a: %d", a);      // output: 10    return 0;}

登录后复制

指向指针的指针

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

登录后复制

指向结构体的指针

#include #include #include // define a structurestruct 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");        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", ptr->name);    printf("age: %d", ptr->age);    // free allocated memory    free(ptr);    return 0;}

登录后复制

关键词

外部的

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

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

登录后复制

静止的

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

#include static int global_var = 10; // global static variablevoid display(void) {    printf("global variable: %d", global_var); // accesses global static variable}int main(void) {    display(); // prints 10    global_var = 20;    display(); // prints 20    return 0;}

登录后复制

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

#include void counter(void) {    static int count = 0; // local static variable    count++;    printf("count: %d", 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", x);    return 0;}

登录后复制

typedef struct Ntype {    int i;    char c;    long x;} NewType;

登录后复制

连锁

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

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

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

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

以上就是C 代码片段:)的详细内容,更多请关注【创想鸟】其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2455591.html

(0)
上一篇 2025年3月3日 20:52:08
下一篇 2025年2月25日 10:01:05

AD推荐 黄金广告位招租... 更多推荐

相关推荐

  • C++ 函数最佳实践在优化内存管理中的心得

    c++++ 函数的最佳内存管理实践包括:使用智能指针来自动管理动态分配的内存。传递指针或引用,而不是通过值传递对象。使用对象池来提高分配和释放对象的性能。使用 raii 原则来确保在对象超出作用域时释放资源。避免使用全局变量,以减少内存使用…

    2025年3月3日
    200
  • C++ 函数最佳实践在大型项目开发中的保障

    遵循 c++++ 函数最佳实践对于提高大型项目开发中的代码质量和可维护性至关重要。最佳实践包括:1. 明确函数职责;2. 使用有意义的函数名;3. 保持接口简洁;4. 避免全局变量;5. 处理错误情况;6. 编写可测试的函数。 C++ 函数…

    2025年3月3日
    200
  • C++ 函数命名中命名空间的作用是什么?

    命名空间在 c++++ 中的作用是组织函数和变量,避免命名冲突。其主要作用如下:创建作用域,使函数和变量具有唯一标识符。避免函数和变量之间因命名重复而产生的冲突。通过使用命名修饰符,明确指定函数或变量所属的命名空间。 C++ 函数命名中命名…

    2025年3月3日
    200
  • 命名空间如何影响 C++ 函数的链接?

    在 c++++ 中,命名空间的作用是组织代码并防止函数名称冲突。链接器链接函数时,它会根据命名空间解析函数符号:1)不在任何命名空间中的函数在当前和引用模块中查找;2)在命名空间中声明的函数在其命名空间和所有父命名空间中查找;3)使用解析操…

    2025年3月3日
    200
  • C++ 匿名函数与函数对象的实现原理

    摘要: c++++匿名函数和函数对象是闭合的可调用对象。匿名函数通过捕获外部变量实现闭包,而函数对象通过成员变量存储状态。它们都用于函数式编程,提供代码复用。 C++ 匿名函数与函数对象的实现原理 简介 匿名函数和函数对象是 C++ 中提供…

    2025年3月3日
    200
  • C++ 中命名空间对函数重载有何影响?

    在 c++++ 中,命名空间会影响函数重载的解析:优先解析当前作用域和嵌套命名空间中的函数。其次解析全局命名空间中的函数。最后解析标准库命名空间中的函数。命名空间修饰符可用于显式调用特定命名空间中的函数,确保明确的调用并避免名称冲突。 命名…

    2025年3月3日
    200
  • C++ 命名空间是否适用于所有函数类型?

    是的,命名空间适用于所有函数类型,包括全局函数、成员函数和静态函数。命名空间可以组织和管理代码,提高其可读性、可维护性和可扩展性。 C++ 命名空间是否适用于所有函数类型? 在 C++ 中,命名空间是一种组织和管理代码的机制。它允许您将相关…

    2025年3月3日
    200
  • c语言高精度除法运算源代码

    在 C 语言中,高精度除法运算可以通过使用额外的辅助函数和数据结构来实现。首先,需要定义一个高精度整数结构,其中包含数字数组和长度信息。随后,可以使用数组来存储数字,并通过初始化、创建和除法运算函数进行处理。除法运算需要归一化、更新商和减去…

    2025年3月3日
    200
  • c语言如何对除法保留高精度小数

    在 C 语言中保留除法的精度:使用浮点数:可以使用浮点数类型保留小数精度,但当精度要求较高时存在局限性。使用 long double:long double 类型提供了比浮点数更高的精度,适用于需要保留更多小数位的情况。使用自定义函数:对于…

    2025年3月3日
    200
  • 如何处理 C++ 中跨命名空间的函数重载?

    处理跨命名空间函数重载的方法:使用作用域解析运算符 (::) 指定函数所在命名空间。使用 using 声明在当前命名空间中使用来自其他命名空间的标识符。 如何处理 C++ 中跨命名空间的函数重载 当不同命名空间中存在重载的函数时,C++ 编…

    2025年3月3日
    200

发表回复

登录后才能评论