微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

C语言中结构是什么?

C语言中结构是:

  • 结构是一种用户定义的数据类型,允许在一个单元中存储多种类型的数据。它占据了所有成员的内存之和。
  • 结构成员只能通过结构变量来访问。
  • 结构变量访问同一结构,但为每个变量分配的内存将是不同的。

语法:

struct structure_name  
{  
  Member_variable1;  
 Member_variable2  
.  
.  
}[structure variables];

示例代码:

#include <stdio.h>  
struct student  
{  
    char name[10];       // structure members declaration.  
    int age;  
}s1;      //structure variable  
int main()  
{  
    printf("Enter the name");  
    scanf("%s",s1.name);  
    printf("\n");  
    printf("Enter the age");  
    scanf("%d",&s1.age);  
    printf("\n");  
    printf("Name and age of a student: %s,%d",s1.name,s1.age);  
    return 0;  
}

运行上面示例代码,得到以下结果 -

Enter the name jb51.cc
Enter the age 23
Name and age of a student: jb51.cc,23

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

相关推荐