0
点赞
收藏
分享

微信扫一扫

C语言中结构体的四种赋值方法


C语言中结构体的四种赋值方法_#include


C语言中结构体的四种赋值方法_c语言_02

C语言中结构体的四种赋值方法_#include_03

C语言中结构体的四种赋值方法_开发语言_04

//
// Created by 王东梁 on 2023/9/17.
//
#include<stdio.h>

//通常在函数外部声明结构类型,这样就可以被多个函数所使用了
struct date{
    int month;
    int day;
    int year;
};

int main() {
    //和本地变量一样,在函数内部声明的结构类型只能在函数内部使用
//    struct date{
//        int month;
//        int day;
//        int year;
//    };
//    struct date today;
    //赋值方法一
//    today.month=07;
//    today.day=31;
//    today.year=2014;
    //赋值方法二
    struct date today={07,31,2014};
    //赋值方法三
    struct date thismonth={.month=7,.year=2014};
    //赋值方法四
//    today=(struct date){07,31,2014}
    //结构体可以直接运算
    //eg thismonth=today,这种赋值操作

    printf("Today's date is %i-%i-%i.\n",
           today.year,today.month,today.day);
    printf("This month is %i-%i-%i.\n",
           thismonth.year,thismonth.month,thismonth.day);
    return 0;
}


举报

相关推荐

0 条评论