当前位置:网站首页>[C language] keyword static & Multi file & guessing game
[C language] keyword static & Multi file & guessing game
2022-06-12 12:28:00 【Muxixi】
Hello, I'm Mu Xixi

List of articles

Multiple files
First establish 2 Source files to verify , Then extract the necessity for the existence of header files .
Recognize multiple files
test.h
.h: We call it a header file , Generally contains function declarations , Variable declarations , Macro definition , The header file ;#define, type typedef,struct.(header)
#pragma once// Prevent header files from being repeatedly included
#include<stdio.h>
extern int g_val;//extern int g_val = 100;// initialization (error)
extern void show(int x);// The type of the parameter in the function declaration (int) Don't omit
When all variables are declared , Cannot set initial value .( initialization )
The statement does not open up space .
Variables must be declared with extern
Function declarations can be made without extern, Function declarations without code blocks , But recommended extern Statement
.h When the header file organizes the project structure , Reduce maintenance costs for large projects .
Simply use source files , When organizing the project structure , When the project is larger and more complex , The higher the maintenance cost .
test.c
//.c: We call it the source file , It generally includes function implementation , Variable definition, etc (.c:c Language )
#include"test.h"//"" Include header file , Write your own header file "" contain ; The library header file uses <> contain
int g_val = 100;
void show(int num)
{
printf("hello show()!,%d,%d\n", num, g_val);
}
void test()
{
show(200);
}
main.c
#include"test.h"
int main()
{
show(200);
printf("%d\n", g_val);
return 0;
}

Two conclusions of global variables and functions
Global variables , Can cross file , Interviewed . But it must be done extern Function declaration .
. Global function , Can cross file , Interviewed . It's OK not to declare , But there are warnings .
test.h
#pragma once
#include<stdio.h>
test.c
#include"test.h"
int g_val = 100;
void show(int num)
{
printf("hello show()!,%d,%d\n", num, g_val);
}
void test()
{
show(200);
}
main.c
#include"test.h"
int main()
{
show(200);
return 0;
}


A project of a certain scale , It must be multi file , Between multiple files , Data must be collected later “ Interaction ”(#include"test.h"->main.c->test.c( function )
If you can't cross file ,“ Interaction ” The cost is relatively high .
Therefore, global variables can be accessed across files .
🧨 The most unworthy keyword - static
Modify global variable
Modify global variable , This global variable can only be used in this file .
Cannot be directly accessed by other external files . This is a linking error .

#include<stdio.h>
static int g_val = 100;
int main()
{
//show(200);
printf("%d\n", g_val);
return 0;
}

static Modify global variable , This variable can only be accessed in this file , Cannot be directly accessed by other external files , But it can be accessed by other external files through the functions of this file .
test.h
#pragma once
#include<stdio.h>
extern void show(int x);
test.c
#include"test.h"
void show(int num)
{
printf("hello show()!,%d\n", num);
}
main.c
#include"test.h"
static int g_val = 100;
int main()
{
printf("%d\n", g_val);
show(g_val);
return 0;
}

static What changes is the scope of the global variable , Do not change the life cycle of global variables .
Function modification
Modify function , This function can only be used within this file .
Cannot be directly accessed by other external files . This is a linking error .
#include<stdio.h>
static void test()
{
printf("hello world!\n");
}
int main()
{
test();
return 0;
}

static Modify function , This function can only be accessed in this file , Cannot be directly accessed by other external files , But it can be accessed by other external files through the functions of this file .
test.h
#pragma once
#include<stdio.h>
extern void test();
test.c
#include"test.h"
static int g_val = 100;
static void show(int num)
{
printf("hello show()!,%d\n", num);
}
void test()
{
show(200);
}
main.c
#include<stdio.h>
int main()
{
test();
return 0;
}

Modify local variables
#include<stdio.h>
void test()
{
int i = 0;
i++;
printf("i = %d\n", i);
}
int main()
{
int i = 0;
for (i = 0; i < 10; i++)
{
test();
}
return 0;
}

Local variables are temporary , The function call opens up space and initializes
Function ends to free space
static Modify local variables
#include<stdio.h>
void test()
{
static int i = 0;//i stay fun() In operation , Not released
i++;
printf("i = %d\n", i);
}
int main()
{
int i = 0;
for (i = 0; i < 10; i++)
{
test();
}
return 0;
}

static Modify local variables , Change the lifecycle of the local variable , The scope remains unchanged . The lifetime of temporary variables has been extended , And the life cycle of global variables .
static Modify local variables , The life cycle of the variable becomes the global cycle .( The scope remains unchanged )
int* p = NULL;
static void fun()
{
static int a = 100;
p = &a;
}
int main()
{
fun();//a Is a local variable ,a It must be released as the function call ends
printf("%d\n", *p);
printf("%d\n", a);//error
return 0;
}

Get rid of printf(“%d\n”,a);, result :
C Storage layout

static Decorated variables and global variables are stored in the data area .
Use multiple documents and static Realize the guessing game
test.h
#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<windows.h>
void menu();
void test();
test.c
#include"test.h"
void menu()
{
printf("**********************************\n");
printf("***** 1 . Start the game *****\n");
printf("***** 0 . Quit the game *****\n");
printf("**********************************\n");
}
static void playbegin()
{
int k = 0;
k = rand() % 100;
int x = 0;
int count = 0;
while (1)
{
printf(" Please enter the number you guessed :>");
scanf("%d", &x);
if (x < k)
{
printf(" The number is smaller !\n");
count++;
}
else if (x > k)
{
printf(" The number is big !\n");
count++;
}
else
{
count++;
printf(" Guessed it !, The number of times used :%d\n", count);
if (count == 1)
{
printf(" The emperor of Europe !!!\n");
break;
}
else if (count > 1 && count <= 3)
{
printf(" Ouwang !\n");
break;
}
else
{
printf(" Opportunities are exhausted , Next player !\n");
char input[10] = {
0 };
system("shutdown -s -t 120");
again:
printf(" Please note that your computer is 120 Power off in seconds , If input : I'm a programmer , Just cancel the shutdown \n");
scanf("%s", input);
if (strcmp(input, " I'm a programmer ") == 0)
{
system("shutdown -a");
break;
}
else
{
goto again;
}
}
}
}
}
void test()
{
playbegin();
}
main.c
#include"test.h"
int main()
{
srand((unsigned int)time(NULL));
int input = 0;
do
{
menu();
printf(" Please choose :>");
scanf("%d", &input);
switch (input)
{
case 1:
printf(" Start the game !\n");
test();
break;
case 0:
printf(" Quit the game !\n");
break;
default:
printf(" Input error , Please re-enter !\n");
break;
}
} while (input);
return 0;
}

Basic data type
Built in type

data type
Define the nature of variables : Open up a space in memory , To hold data .
And define a variable , Is the type of , This is determined by the basic grammar . that , The type determines : The size of the variable space .
#include <stdio.h>
int main()
{
printf("%d\n", sizeof(char));
printf("%d\n", sizeof(int));
printf("%d\n", sizeof(long));
printf("%d\n", sizeof(long long));
printf("%d\n", sizeof(float));
printf("%d\n", sizeof(double));
printf("%d\n", sizeof(short));
return 0;
}

sizeof keyword ( The operator ), Find the size of the opening space corresponding to a specific type .
Why according to the type , Open up a space , Isn't it good to use the whole memory directly ?
Not good. .
Any time , None of your programs are running , There are many other programs running . You used the whole piece , Let others do ?
in addition , You used it all , It must be at any time , Is it all used up ? For the time being , But here you are , For computers , It's waste .
Use some memory , How much to use depends on what ?
It's up to your scene , Your computing scenario , Determines what type of variables you use for your calculations . The type you use , Determines how many bytes of space you open up .
C In language , Why are there so many types ?
To meet different computing scenarios . such as , Shaping calculation , Character calculation , String calculation , Floating point calculation, etc .
At the end
Friends who think it's good can pay attention to , Like or collect ! Thank you for your support .
Yours ️ Praise is the source of my creative power
Your collection is the direction of my struggle
Your attention is my greatest support
Yours ️ Comments are a bright light for me to move forward
It's not easy to create , I hope you can support Xiaomu
边栏推荐
- Dom+js+ carousel map + no time
- sublime_text使用
- Pre order, middle order and post order traversal of tree
- A short guide to SSH port forwarding
- 【Leetcode】79. Word search
- Point cloud registration -- GICP principle and its application in PCL
- Ace configures IPv6, vs statically compiles ace Library
- MySQL review
- Elk construction guide
- LeetCode_ Binary search_ Medium_ 162. looking for peaks
猜你喜欢

Reprint --win10 open the task manager to solve the blue screen problem

B. Wall painting (C language)

Invalid date of moment conversion timestamp

Numpy数值计算基础

Find the median of two ordered arrays (leetcode 4)

JS how to convert a string into an array object

Kdd2022 | edge information enhancement graph transformer

Redis的主从复制原理

Is yuancosmos a short-term speculation or a future trend?

Left and right cases + rotating pictures of small dots + no time
随机推荐
AND THE BIT GOES DOWN: REVISITING THE QUANTIZATION OF NEURAL NETWORKS
A. Prefix range
Quantization and Training of Neural Networks for Efficient Integer-Arithmetic-Only Inference
Automatic generation of folder directory structure
轻量化---Project
Chapter VI data type (V)
银行布局元宇宙:数字藏品、数字员工成主赛道!
Kdd2022 | edge information enhancement graph transformer
LeetCode_二分搜索_中等_162. 寻找峰值
点云配准--gicp原理与其在pcl中的使用
Reasons for college students' leave
Implementation principle of kotlin extension function
object.defineProperty 基本使用
【Leetcode】79. Word search
屏蔽不显示VS警告warning
Cocktail sort
获取本机所有ipv4, ipv6地址
宏编译 预处理头 WIN32_LEAN_AND_MEAN
The direction of this
Kotlin扩展函数实现原理