#include

M

Mohsen

Hello Everybody,

I have a main program and I would like to include another program
(MN.h) inside my main program. I already know how to use "extern"
for the variables that I have defined as int, double ... in my main
program to use the information of my main program in the program that I
want to include in my main program however I have some variables that I
have used "#define" in my main program but I don't know how I
should use extern for the variable which I have used "#define" for
them. For example in the follwing main program I know how to use extern
to define "int AS" and "int AD" and I have shown how I have
used extern in the other program but I don't know how to use extern
for "M" and "R".
Is there anybody to tell me how I can do that?
Thanks,

MY MAIN PROGRAM:

#include <stdio.h>
#include <stdlib.h>
#include "MN.h"

#define M
#define R

int AS;
int AD;
..
..
..
..


MN.h program that I want to include:

#include <stdio.h>
#include <stdlib.h>

extern int AS;
extern int AD;
..
..
..
..
..
 
I

Ian Collins

Mohsen said:
Hello Everybody,

I have a main program and I would like to include another program
(MN.h) inside my main program. I already know how to use "extern"
for the variables that I have defined as int, double ... in my main
program to use the information of my main program in the program that I
want to include in my main program however I have some variables that I
have used "#define" in my main program but I don't know how I
should use extern for the variable which I have used "#define" for
them. For example in the follwing main program I know how to use extern
to define "int AS" and "int AD" and I have shown how I have
used extern in the other program but I don't know how to use extern
for "M" and "R".

You can't. A macro's scope only extends to the current compilation unit.

Why would you want to use macros for variables?
 
M

Mohsen

Dear Ian,
My main program produces some files which are going to be used in the
"MN.h" program. In order to define those files for the "MN.h", the
"MN.h" needs to know about "M" and "R" but I don't know how to tell the
"MN.h" about them.
Thanks,
Mohsen
 
I

Ian Collins

Mohsen wrote:

Top posting corrected.
Dear Ian,
My main program produces some files which are going to be used in the
"MN.h" program. In order to define those files for the "MN.h", the
"MN.h" needs to know about "M" and "R" but I don't know how to tell the
"MN.h" about them.

A .h file is a header. If you wish to share a variable in one source
module with another, declare it as extern in a header file to be
included by both source files and define it exactly once in one of them.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,159
Messages
2,570,881
Members
47,418
Latest member
NoellaXku

Latest Threads

Top