zoukankan      html  css  js  c++  java
  • [C] Struct Test

    #include <stdio.h>
    #include <string.h>
    struct Person
    {
        char name[24];
        char character[30];
        int age;
        struct Birthday
        {
            int day;
            char month[10];
            int year;
        } birthday;
    };
    int main()
    {
        struct Person man1;
        strcpy(man1.name, "Jerry Seinfeld");
        strcpy(man1.character, "fastidious");
        man1.age = 56;
        man1.birthday.day = 4;
        strcpy(man1.birthday.month, "June");// = 6;
        man1.birthday.year = 2020;
        printf("My name is %s, I was born on %s-%d-%d, so I'm %d years old now. And my friends always complain I'm too %s.
    ", 
            man1.name, man1.birthday.month, man1.birthday.day, man1.birthday.year, man1.age, man1.character);
        return 0;
    }

    输出结果为:

    My name is Jerry Seinfeld, I was born on June-4-2020, so I'm 56 years old now. And my friends always complain I'm too fastidious.

    虽然上面的初始化太复杂,但是可以选择性的赋值

    #include <stdio.h>
    struct Person
    {
        char name[24];
        char character[30];
        int age;
        struct Birthday //嵌套一个struct,Birthday也可以删掉,直接写成struct,因为Birthday根本没用到,用的只是birthday
        {
            int day;
            char month[10];
            int year;
        } birthday;
    };
    int main()
    {
        struct Person man1 = {"Jerry Seinfeld", "fastidious", 56, 4, "June", 2020}; //注意对应好,如果缺少,int默认补0, char默认空格
        printf("My name is %s, I was born on %s-%d-%d, so I'm %d years old now. And my friends always complain I'm too %s.
    ", 
            man1.name, man1.birthday.month, man1.birthday.day, man1.birthday.year, man1.age, man1.character);
        return 0;
    }
  • 相关阅读:
    nmap加快扫描速度(转载)
    deepin换源
    利用74HC595实现的流水灯 Arduino
    解决用vscode开发arduino时Serial未定义
    JS获取格式为YYYY-MM-DD的当前日期
    链式前向星存图
    Nginx+FFmpeg实现RTSP转RTMP
    非root用户安装centos的jdk
    Nginx配置请求头
    Eclipse-Che 安装(Centos)
  • 原文地址:https://www.cnblogs.com/profesor/p/13052614.html
Copyright © 2011-2022 走看看