如何从一个C文件访问另一个C文件中的变量?

Sam*_*m H 3 c variables file

我有两个C文件。我想在一个变量中声明一个变量,然后能够从另一个C文件访问它。我对示例字符串的定义可能并不完美,但是您明白了。

//file1.c

char *hello="hello";
Run Code Online (Sandbox Code Playgroud)
//file2.c

printf("%s",hello);
Run Code Online (Sandbox Code Playgroud)

Kar*_*tel 5

// file1.h
#ifndef FILE1_H
#define FILE1_H
extern char* hello;

#endif


// file1.c
// as before


// file2.c
#include "file1.h"
// the rest as before
Run Code Online (Sandbox Code Playgroud)