Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How to access C variables in .c files ?

Status
Not open for further replies.

JaMe

Junior Member level 1
Joined
Nov 8, 2006
Messages
19
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Indonesia
Activity points
1,406
I have variable called "Timer1" in file "main.c".
The problem is, is there any way to access those variable in other file, e.g, in file "init.c" ?
 

echo47

Advanced Member level 5
Joined
Apr 7, 2002
Messages
3,942
Helped
637
Reputation
1,272
Reaction score
88
Trophy points
1,328
Location
USA
Activity points
33,178
variable in C

Are main.c and init.c compiled into one executable?
If main() calls init(), then the call could pass a pointer to Timer1.

Code:
#include <stdio.h>

void init(int *value)
{
  printf(" Value = %d\n", *value);
  *value *= 2;
  printf(" Value = %d\n", *value);
}

int main(void)
{
  int Timer1 = 123;
  printf("Timer1 = %d\n", Timer1);
  init(&Timer1);
  printf("Timer1 = %d\n", Timer1);
  return 0;
}
Output:
Code:
Timer1 = 123
 Value = 123
 Value = 246
Timer1 = 246
 

alzomor

Advanced Member level 2
Joined
Jun 9, 2005
Messages
678
Helped
39
Reputation
78
Reaction score
8
Trophy points
1,298
Location
Cairo
Activity points
6,417

    JaMe

    Points: 2
    Helpful Answer Positive Rating

funster

Full Member level 4
Joined
Jun 30, 2005
Messages
233
Helped
19
Reputation
38
Reaction score
4
Trophy points
1,298
Activity points
2,742
Re: variable in C

you can't access it unless you define the variable outside main function.




JaMe said:
I have variable called "Timer1" in file "main.c".
The problem is, is there any way to access those variable in other file, e.g, in file "init.c" ?
 

waterman

Member level 5
Joined
Mar 27, 2002
Messages
80
Helped
2
Reputation
6
Reaction score
2
Trophy points
1,288
Activity points
562
Re: variable in C

in "init.c"

extern int Timer1;

void init(void)
{
Timer1 = 0;
}

//----------------------------------------
in "main.c"

int Timer1;

extern void init(void);

int main(void)
{
init();

//other codes

return 0;
}
 

booklog

Junior Member level 3
Joined
Jun 22, 2006
Messages
30
Helped
3
Reputation
6
Reaction score
0
Trophy points
1,286
Activity points
1,441
Re: variable in C

The variable Timer1 should be global
 

jhbbunch

Full Member level 4
Joined
Feb 21, 2006
Messages
199
Helped
17
Reputation
34
Reaction score
3
Trophy points
1,298
Activity points
2,902
variable in C

U need the extern statement. That plus the function prototype tell the compiler/likerhow to set up the call, what to return from it, what the storage spaces required are and also. extern tells the compiler it will find the function (or a variable) in another file.
 

inirs06

Newbie level 4
Joined
Jan 21, 2007
Messages
7
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,315
variable in C

One way of doing this is to create a main.h, decalre this variable as extern and then include the main.h in the init.c file. This is needed if the compiler enforces ansi stds strictly. Otherwise, make sure that u link both files after compilation, use the extern keyword directly in your init.c file.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top