如何访问一个变量从其他一个C文件? [英] How do I access a variable in a one C file from another?

查看:114
本文介绍了如何访问一个变量从其他一个C文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个C文件。我想在一个声明一个变量,那么可以从另一个C文件访问。我的定义的例如的字符串可能不是完美的,但你的想法。

  // file1.c中字符*你好=你好;


  // file2.c中的printf(%S,你好);


解决方案

  // file1.h
的#ifndef FILE1_H
#定义FILE1_H
EXTERN字符*你好;#万一
// file1.c中
// 像之前一样
// file2.c中
#包括file1.h
//其余如前

I have two C files. I want to declare a variable in one, then be able to access it from another C file. My definition of the example string might not be perfect, but you get the idea.

//file1.c

char *hello="hello";


//file2.c

printf("%s",hello);

解决方案

// 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

这篇关于如何访问一个变量从其他一个C文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆