3

C语言-细说函数与结构体

 2 years ago
source link: https://blog.51cto.com/hiszm/5237938
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

C语言-细说函数与结构体

原创

孙中明 2022-04-21 14:52:39 博主文章分类:2022年4月 ©著作权

文章标签 #include 主函数 构造函数 文章分类 C/C++ 编程语言 阅读数182

返回类型 函数名 (函数类型 参数){
	函数主题;
}

定义在所有函数之前

#include<stdio.h>

int x=10;

int main(){
    
    printf("%d",x);
    return 0;
}
#include<stdio.h>

int x=10;

int main(){
    int x=20;
    printf("%d",x);
    return 0;
}

main函数

主函数,无论主函数写在哪里,整个程序都是从主函数第一个语句执行的

函数的嵌套

#include<stdio.h>

int x=10;

int main(){
  	print();
    return 0;
}

void print(){
    printf("%d",x);
}

函数的递归

#include<stdio.h>

int x=10;

void fun(){
    
    fun();
}

int main(){
   fun();
 
}
struct name{

//基本数据结构

};

访问结构体

通过.或者->

struct studentInfo{
    int id;
    char name[10];
    studentInfo* next;
}stu,*p;

//访问

stu.id
stu->name


(*p).id
p->id

结构体的初始化

//基本
stu.id=1;
scanf("%d",&stu.id);

//构造函数

struct studentInfo{
    int id;
    char sex;
    studentInfo(int _id,int _sex){
        id=_id;
        sex=_sex;
    }
};
//也可以简写
struct studentInfo{
    int id;
    char sex;
    studentInfo(int _id,int _sex):id(_id),sex(_sex){}
};

student1=studentInfo(9527,man);

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK