每日一个C语言知识:C 数据类型

C语言数据类型详解

C语言的数据类型可以分为两大类:基本类型 和派生类型。
C语言数据类型 基本类型 派生类型 整型 浮点型 字符型 void类型 short int long long long float double long double 数组 指针 结构体 共用体 枚举


1. 基本数据类型

整型数据类型
类型 大小(字节) 范围 格式说明符
char 1 -128 到 127 %c, %d
unsigned char 1 0 到 255 %c, %u
short 2 -32,768 到 32,767 %hd
unsigned short 2 0 到 65,535 %hu
int 4 -2,147,483,648 到 2,147,483,647 %d
unsigned int 4 0 到 4,294,967,295 %u
long 4 或 8 取决于系统 %ld
unsigned long 4 或 8 取决于系统 %lu
long long 8 -9.2×10¹⁸ 到 9.2×10¹⁸ %lld
unsigned long long 8 0 到 1.8×10¹⁹ %llu

示例:

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

int main() {
    char c = 'A';
    short s = 1000;
    int i = 100000;
    long l = 1000000L;
    long long ll = 10000000000LL;
    
    unsigned int ui = 4000000000U;
    
    printf("char: %c (%d)\n", c, c);
    printf("short: %hd\n", s);
    printf("int: %d\n", i);
    printf("long: %ld\n", l);
    printf("long long: %lld\n", ll);
    printf("unsigned int: %u\n", ui);
    
    // 查看类型范围
    printf("\n类型范围:\n");
    printf("int 范围: %d 到 %d\n", INT_MIN, INT_MAX);
    printf("char 范围: %d 到 %d\n", CHAR_MIN, CHAR_MAX);
    
    return 0;
}
浮点型数据类型
类型 大小(字节) 精度 范围 格式说明符
float 4 约6-7位小数 ±3.4e±38 %f, %e
double 8 约15-16位小数 ±1.7e±308 %lf, %le
long double 10 或 16 更高精度 更大范围 %Lf, %Le

示例:

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

int main() {
    float f = 3.14159f;           // 注意后面的f
    double d = 3.141592653589793;
    long double ld = 3.141592653589793238L; // 注意后面的L
    
    printf("float: %.7f\n", f);
    printf("double: %.15lf\n", d);
    printf("long double: %.15Lf\n", ld);
    
    // 科学计数法表示
    printf("科学计数法: %e\n", d);
    
    // 查看精度信息
    printf("\n精度信息:\n");
    printf("float 精度: %d 位小数\n", FLT_DIG);
    printf("double 精度: %d 位小数\n", DBL_DIG);
    
    return 0;
}
字符型

字符在C语言中实际上是整数(ASCII码值)。

示例:

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

int main() {
    char letter = 'A';
    char digit = '7';
    char symbol = '$';
    
    printf("字符: %c, ASCII值: %d\n", letter, letter);
    printf("字符: %c, ASCII值: %d\n", digit, digit);
    printf("字符: %c, ASCII值: %d\n", symbol, symbol);
    
    // 字符运算
    char next_letter = letter + 1;
    printf("下一个字母: %c\n", next_letter);
    
    // 大小写转换
    char lower = letter + 32;  // 'A'=65, 'a'=97
    printf("小写: %c\n", lower);
    
    return 0;
}
void 类型
  • 表示"无类型"
  • 主要用于:
    • 函数不返回值
    • 函数无参数
    • 通用指针
c 复制代码
#include <stdio.h>

// 函数不返回值
void printHello() {
    printf("Hello, World!\n");
    // 不需要return语句,或使用 return;
}

// 函数无参数
int getNumber(void) {
    return 42;
}

int main() {
    printHello();
    int num = getNumber();
    printf("Number: %d\n", num);
    
    // void指针(通用指针)
    int x = 10;
    void *ptr = &x;
    printf("void指针地址: %p\n", ptr);
    
    return 0;
}

2. 派生数据类型

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

int main() {
    // 整型数组
    int numbers[5] = {1, 2, 3, 4, 5};
    
    // 字符数组(字符串)
    char name[] = "John";  // 自动计算大小,包括'\0'
    char city[10] = "Beijing";
    
    // 二维数组
    int matrix[2][3] = {
        {1, 2, 3},
        {4, 5, 6}
    };
    
    printf("数组元素:\n");
    for(int i = 0; i < 5; i++) {
        printf("numbers[%d] = %d\n", i, numbers[i]);
    }
    
    printf("字符串: %s\n", name);
    printf("二维数组: %d\n", matrix[1][2]); // 输出6
    
    return 0;
}
指针
c 复制代码
#include <stdio.h>

int main() {
    int x = 10;
    int *ptr = &x;  // ptr指向x的地址
    
    printf("变量x的值: %d\n", x);
    printf("变量x的地址: %p\n", &x);
    printf("指针ptr的值: %p\n", ptr);
    printf("通过指针访问的值: %d\n", *ptr);
    
    // 通过指针修改变量值
    *ptr = 20;
    printf("修改后x的值: %d\n", x);
    
    return 0;
}
结构体
c 复制代码
#include <stdio.h>
#include <string.h>

// 定义结构体
struct Student {
    char name[50];
    int age;
    float score;
};

int main() {
    // 声明结构体变量
    struct Student stu1;
    
    // 初始化结构体成员
    strcpy(stu1.name, "Alice");
    stu1.age = 20;
    stu1.score = 95.5;
    
    // 声明并初始化
    struct Student stu2 = {"Bob", 22, 88.5};
    
    printf("学生1:\n");
    printf("姓名: %s\n", stu1.name);
    printf("年龄: %d\n", stu1.age);
    printf("分数: %.1f\n", stu1.score);
    
    printf("\n学生2:\n");
    printf("姓名: %s\n", stu2.name);
    printf("年龄: %d\n", stu2.age);
    printf("分数: %.1f\n", stu2.score);
    
    return 0;
}
枚举
c 复制代码
#include <stdio.h>

// 定义枚举
enum Weekday {
    MONDAY,    // 0
    TUESDAY,   // 1
    WEDNESDAY, // 2
    THURSDAY,  // 3
    FRIDAY,    // 4
    SATURDAY,  // 5
    SUNDAY     // 6
};

// 可以指定枚举值
enum Color {
    RED = 1,
    GREEN = 2,
    BLUE = 4
};

int main() {
    enum Weekday today = WEDNESDAY;
    enum Color favorite = BLUE;
    
    printf("Today is day number: %d\n", today);
    printf("My favorite color code: %d\n", favorite);
    
    // 枚举常用于switch语句
    switch(today) {
        case MONDAY:
            printf("今天是星期一\n");
            break;
        case WEDNESDAY:
            printf("今天是星期三\n");
            break;
        case FRIDAY:
            printf("今天是星期五\n");
            break;
        default:
            printf("今天是其他日子\n");
    }
    
    return 0;
}

3. 类型转换

隐式类型转换(自动转换)
c 复制代码
#include <stdio.h>

int main() {
    int i = 10;
    float f = 3.14;
    double d;
    
    // 自动转换:int → float
    f = i;  
    printf("f = %.2f\n", f);
    
    // 自动转换:float → double
    d = f;
    printf("d = %.2lf\n", d);
    
    // 表达式中的自动转换
    double result = i + f;  // i自动转换为float,然后转换为double
    printf("result = %.2lf\n", result);
    
    return 0;
}
显式类型转换(强制转换)
c 复制代码
#include <stdio.h>

int main() {
    int a = 5, b = 2;
    float result;
    
    // 整数除法
    result = a / b;  // 结果是2.0,不是2.5
    printf("整数除法: %.1f\n", result);
    
    // 强制类型转换
    result = (float)a / b;  // 结果是2.5
    printf("浮点数除法: %.1f\n", result);
    
    // 更多强制转换示例
    double pi = 3.14159;
    int integer_pi = (int)pi;  // 截断小数部分
    printf("pi的整数部分: %d\n", integer_pi);
    
    char c = 'A';
    int ascii = (int)c;  // 字符转ASCII值
    printf("'%c'的ASCII值: %d\n", c, ascii);
    
    return 0;
}

4. 类型定义(typedef)

使用 typedef 创建类型别名。

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

// 为现有类型创建别名
typedef int Integer;
typedef float Real;

// 为结构体创建别名
typedef struct {
    char name[50];
    int age;
} Person;

// 为数组创建别名
typedef int Vector[3];

int main() {
    Integer num = 100;  // 实际上就是int
    Real price = 99.99; // 实际上就是float
    
    Person p1 = {"Tom", 25};
    Vector v = {1, 2, 3};  // 相当于int v[3]
    
    printf("数字: %d\n", num);
    printf("价格: %.2f\n", price);
    printf("姓名: %s, 年龄: %d\n", p1.name, p1.age);
    printf("向量: %d, %d, %d\n", v[0], v[1], v[2]);
    
    return 0;
}

相关推荐
炸膛坦客13 分钟前
单片机/C/C++八股:(三十三)取别名
c语言·c++·单片机
听情歌落俗43 分钟前
嵌入式学习1-C语言基础语法与STM32实践
c语言·开发语言
小飞在努力1 小时前
【实现负载均匀在线判题⑤】MVC 架构搭建与 Model 题目数据模块实现
c语言·开发语言·c++·笔记·后端
落羽的落羽3 小时前
【C++】现代容器: tuple、optional、variant、any、string_view
linux·开发语言·数据结构·c++·人工智能·计算机网络·算法
小飞在努力4 小时前
【实现负载均匀在线判题④】接入 cpp-httplib 实现编译服务 HTTP 网络化
c语言·开发语言·c++·笔记·http
炸薯条!5 小时前
Python面向对象基础:类与对象、实例方法、魔法方法和属性详解
java·开发语言·python
CappuccinoRose5 小时前
前端开发交互体系 - 汇总篇
开发语言·前端·javascript·css·html·交互
tryCbest5 小时前
Python 爬虫从入门到实战(二):进阶篇——反爬对抗、并发与动态页面
开发语言·爬虫·python
-梅6 小时前
linux(番外)线程,锁,条件变量封装;日志与线程池
linux·c语言·前端·c++
S_WEAN6 小时前
【C++】STL - Priority_queue的使用及模拟实现与仿函数
开发语言·c++