I want to be able to modify a variable from one C file and see the changes of that variable on the other one
Here are my C files:
file1.c:
#include "myheader.h"
int
main( void )
{
printf("Variable %d\n", var);
}
file2.c:
#include "myheader.h"
int main(void){
int var = 1;
var = var + 1;
}
The header file looks like this:
extern int var;
Now, I want to run file2.c first and than when I run file1.c print the incremented value of var. Ideas?
I think I am doing everything like this answer, but can't make it work.
PS: Just started learning C.
Each of your "files".c has a main function and thus will compile into a separate executable.
So you can't have one executable increment a variable in its own address space and then have another executable see the change in someone else's address space - the other executable will have an own variable in its own address space. Unless you would use advanced inter process communication (but that is Lesson 42).
Related
I've recently started working on a chess engine, and am having trouble with a function to apply moves to the board. I have a structure type for the board state, and another to store moves. These are both in my header file below.
#ifndef DECLARATIONS_H
#define DECLARATIONS_H
#include <stdbool.h>
#define BOARD_SIZE 120
typedef struct move {
int startingSquare;
int endingSquare;
int promotionPiece;
} move;
typedef struct boardState {
int board[BOARD_SIZE];
bool whoseTurn;
int ply;
int threeMoveRuleCount;
int fiftyMoveRuleCount;
bool whiteCastleKingside;
bool whiteCastleQueenside;
bool blackCastleKingside;
bool blackCastleQueenside;
int enPassant;
float evaluation;
move previousMove;
} boardState;
boardState currentBoard;
#endif
Main.c calls the "Process Move" function from movegeneration.c. It passes one of the boardstate types and one of the move types:
#include <stdio.h>
#include <string.h>
#include "declarations.h"
int main () {
move firstMove;
firstMove.startingSquare = 35;
firstMove.endingSquare = 55;
firstMove.promotionPiece = 0;
printf("Value of firstMove.endingSquare: %d\n", firstMove.endingSquare);
printf("Value of firstMove.startingSquare: %d\n", firstMove.startingSquare);
printf("Value of firstMove.promotionPiece: %d\n", firstMove.promotionPiece);
processMove(currentBoard, firstMove);
return 0;
}
Below is movegeneration.c, which contains the function to process the move:
#include <stdio.h>
#include "declarations.h"
boardState processMove(boardState, move);
boardState processMove(boardState oldBoardState, move moveToApply) {
boardState newBoardState = oldBoardState;
printf("Value of moveToApply.endingSquare: %d\n", moveToApply.endingSquare);
printf("Value of moveToApply.startingSquare: %d\n", moveToApply.startingSquare);
printf("Value of moveToApply.promotionPiece: %d\n", moveToApply.promotionPiece);
return newBoardState;
}
And here's the program output:
Value of firstMove.endingSquare: 55
Value of firstMove.startingSquare: 35
Value of firstMove.promotionPiece: 0
Value of moveToApply.endingSquare: 0
Value of moveToApply.startingSquare: 55
Value of moveToApply.promotionPiece: 1996058613
This isn't all the code from the .c files, but it's everything relevant here. What makes this bizarre is that the values of the move change as soon as I call the function. When I put all the code into one large .c file and compile that way, the problem goes away, but I'm trying to keep the code separate to stay organized. This one has me completely stumped. I'd appreciate any help!
There's at least 2 problems here, causing undefined behaviour.
Firstly, there are multiple definitions of boardState currentBoard; . Remember that #include is a plain text replacement, so both of your c files end up with a definition of that object. See here for an in-depth explanation with examples.
If you want to have a global object accessible from multiple translation units then the header file should contain a declaration only (i.e. prepend extern to the current line), and exactly one of the .c files should contain the definition.
Secondly, in main() you call an undeclared function processMove. If you compile in standard mode then the compiler should generate an error message. This is likely the cause of your symptoms, and I would strongly recommend invoking your compiler in modern standard mode with a high diagnostic level, as it would have saved you a lot of time to find out about this problem sooner.
To fix this, the declaration boardState processMove(boardState, move); should be in declarations.h.
I have been reading about C for a while now and decided lets write a little add program, nothing fancy at all. My understanding of C headers is that they are "interfaces" (such as like java and other languages) but where you can also define variable that either have set values or not..
So I wrote this:
#include <stdio.h>
#include <stdlib.h>
#include "sample.h"
int main(int argc, char** argv) {
printf("hello World\n");
add(12, 18);
return (EXIT_SUCCESS);
}
int add(int a, int b){
int value = a+b;
printf("value = %d\n", value);
return 0;
}
It has a header file that looks like such:
#ifndef SAMPLE_H_GUARD
#define SAMPLE_H_GUARD
int add(int a, int b);
#endif
I thought header files, and this is where I am lost on their definition, was suppose to define the use of add, so all I would have to do is call add - From my understanding, I define the rules of add and then implement the functionality of add....
Also, A lot of the material I have read shows one header file for multiple C files. where as a lot of projects today have one header per one c, meaning Sample.h belongs to Sample.c and nothing else.
Can some one shed some light on this?
Could I have done this like so:
main.c
#include <stdio.h>
#include <stdlib.h>
#include "sample.h"
int main(int argc, char** argv) {
printf("hello World\n");
add(12, 18);
return (EXIT_SUCCESS);
}
add.c
#include <stdio.h>
#include <stdlib.h>
#include "sample.h"
int add(int a, int b){
int value = a+b;
printf("value = %d\n", value);
return 0;
}
sample.h
#ifndef SAMPLE_H_GUARD
#define SAMPLE_H_GUARD
int add(int a, int b);
#endif
I believe in the book I was reading: C Programming Language they had a calculator example split up like this, my question is how does C know where add is defined? It knows the rules for it based on the header file, i think, but not where the actual implementation is ....
There example where they split of the files like such doe not have something like #include "add.c" all they do is include the header file in the files that either implement or use this functionality.
Note: obviously the calculator example and my example are going to be different but fundamentally the same - for those who have the book. I am just lost on how to use header files effectively and efficiently.
A header file in C would declare the function add for those modules that need it, but not define the function. The function is still to be defined in its own module (e.g., in your case, add.c).
So in general, to make a function foo available to several modules, you would normally:
Choose a header file (maybe it's own if there are other associated
defines, etc) to declare foo. For example, perhaps foo.h would
have void foo(...);
In some module, perhaps foo.c, you would define the complete
function foo.
In any module that wants to call foo, you would #include "foo.h"
(or whatever header you used) and call the function.
When you compile/link the code, you would make sure all modules,
including foo.o or whatever module has foo defined in it, were
present.
A declaration, given in the header file, provides (of course) the function name, the function return type as well as listing all the parameters and their types. This is all the compiler needs to know to figure out how to call the function from the calling module. At link time, addresses are all resolved so that the modules then know exactly where the function is in its own particular module.
My understanding of C headers is that they are "interfaces" (such as
like java and other languages) but where you can also define variable
that either have set values or not..
This is not correct. You cannot "define" variables - well, you can but that will cause multiple definitions error while compiling code if you include header more than once.
Could I have done this like so:
Regarding your code - both variants are correct. C language uses headers to read declarations and hence headers are optional as well. You can have your code split into as many as you want .h and .c files. Compiler will create an object file for each .c file. All .h files included in a c file are basically embedded in that C file "before compilation" i.e. in preprocessing phase. Linker then comes in picture which combines objects to produce the executable.
Please don't hesitate if something is not clear in my answer.
There is a header file named 'AAA.h'. In this header file we have define a structure called lrd, which looks like:
struct lrd
{
int tc;
char ptc[5];
char stc[5];
char ath[5];
int vc;
};
struct lrd lr;
This header file 'AAA.h' is included in two different files name 'BBB.c' and 'CCC.c'. We have assigned the values for structure variable lr in the 'BBB.c' file as following:
lr.tc=tc;
memcpy(lr.ptc,ptc,sizeof(ptc));
memcpy(lr.stc,stc,sizeof(stc));
memcpy(lr.ath,ath,sizeof(ath));
lr.vc=vc;
Above source variables take the value from database. And we use structure variable lr in the 'CCC.c' file. We are using structure lrd variable as follows:
char *ptc()
{
sprintf(str, "lr.ptc(%s)", lr.ptc);
trace(str);
return lr.ptc;
}
char *stc()
{
sprintf(str, "lr.stc(%s)", lr.stc);
trace(str);
return lr.stc;
}
Variable stc gives the wrong value in the 'CCC.c' file.
Please help me to figure it out.
See what benjarobin says. I am just showing you a small footprint
AAA.h
#ifndef _AAA_H
struct abc
{
int x;
char *y;
char *Z;
};
extern struct abc p;
extern void assign(void);
extern void display (void);
#endif
CCC.c
#include <stdio.h>
#include "AAA.h"
struct abc p; // Deceleration of p
void assign ()
{
p.x=20;
p.y="abcd";
p.Z="xyz0";
return;
}
BBB.c
#include <stdio.h>
#include "AAA.h"
void display (void)
{
printf("x = %d, y =%s, z=%s",p.x,p.y,p.Z);
}
DDD.c contains the main function.
#include <stdio.h>
#include "AAA.h"
void main ()
{
printf("\n\nAssigning Values\n\n");
assign ();
printf("\n\nIN BBB.c\n\n");
display ();
printf("\n\nIN MAIN\n\n");
printf("x = %d, y =%s, z=%s",p.x,p.y,p.Z);
}
output is
Assigning Values
IN BBB.c
x = 20, y =abcd, z=xyz0
IN MAIN
x = 20, y =abcd, z=xyz0
Your code shows:
memcpy(lr.ptc,ptc,sizeof(ptc));
memcpy(lr.stc,stc,sizeof(stc));
memcpy(lr.ath,ath,sizeof(ath));
It doesn't show the declarations of ptc, stc or ath. Since you are copying to lr.ptc etc, the operand of sizeof() should be lr.ptc etc, not the size of the source. Or, if you're being really careful, the minimum of the two sizes.
You could/should debug this with:
printf("%d (%s)(%s)(%s) %d\n", lr.tc, lr.ptc, lr.stc, lr.ath, lr.vc);
immediately after the assignments. If that shows the wrong value, then you know the problem is in the assignments. If that shows the correct values, then you can start looking for stray pointers and other such ghastly problems.
There's no proof that this is the problem because you've not produced an SSCCE (Short, Self-Contained, Correct Example). Specifically, you've only produced fragments of code, not an integrated set of three source files (though we can infer the header from the information given). And without the code that actually demonstrates the problem (all of it, including definitions for all the variables and functions — but not including any superfluous material), we can only speculate.
External variable should be avoid, but if you still want to use a global variable, you shall do these :
Declare the variable in a .c file
struct lrd lr;
Add the external definition in the .h (AAA.h)
external struct lrd lr;
For example: If I have two .h files
process1.h and process2.h
and they contain two function with different output variables.
process1.h:
function(int var)
{
return 2*var;
}
process2.h:
function(int var)
{
return 10*var;
}
Can this be done in main.c:
int main()
{
int a = 2;
#include "process1.h"
printf("%d",function(a)); //output is 4
EXCLUDE #INCLUDE "process1.h" ????? <----can this be done in any way??
#include "process2.h"
printf("%d",function(a)); //output is 20
}
No, you cannot "un-include" a file. Think of all the preprocessor directives (lines starting with #) as happening before the actual C compiler even sees the source file. They just operate on the text of the file, and the preprocessor could be implemented as a separate step that just feeds new text into the actual compiler.
The best way to modify the actions of an include depending on the caller is to use further macros inside the included files, that you can #define before including them.
Still, your overall syntax is off, you can't (typically) nest functions in C.
No, and you should not try to write a program with two functions of the same name.
In the special case that the functions are actually defined in the header file (instead of just prototypes), you can do this:
#define function function_file1
#include "file1.h"
#undef function
#define function function_file2
#include "file2.h"
#undef function
int
main (void)
{
int a = 2;
printf ("%d\n", function_file1 (a));
printf ("%d\n", function_file2 (a));
}
BUT if you rename a function prototype then you haven't actually renamed the real function, so you'll get undefined symbol error when you link.
In any case, if you have two functions defined with the same name then it won't link anyway, not matter what else you do in the sources. (In C++, it is sometimes possible to define two things with the same name, but the One-Definition-Rule means the linker is allowed to assume they are both the same thing really and just pick one.)
This is why libraries are supposed to use names that won't be used elsewhere - usually by adding a common prefix to all symbol names (e.g. my_unique_lib_initialize()).
Why not use array of function pointers. Sure you need to initialize it at the start but I think it probably solves what you want to do.
int process1_function(int var);
int process2_function(int var);
int main(void)
{
int i, a = 10;
int (* functions[2])(int);
functions[0] = process1_function;
functions[1] = process2_function;
for(i=0; i < 2; i++)
{
printf("%d", (functions[i])(a));
}
return 0;
}
If you do not need to dynamically change which function you're going to call you can also just prefix the functions:
int process1_function(int var);
int process2_function(int var);
int main(void)
{
printf("%d",process1_function(a));
printf("%d",process2_function(a));
return 0;
}
I have two files:
hudwidgets.c
#include "osd.h"
#include "osdadd.h"
#include <stdio.h>
struct TextSize tsize;
char buff[50];
void hud_draw_lin_compass(int cx, int cy, int tick_maj, int tick_min, int range, int heading)
{
.. important code that uses buff ..
}
hud.c
#include "hudwidgets.h"
#include "hud.h"
#include "osd.h"
#include "osdadd.h"
.. some code ..
void hud_rc_plane_single_frame()
{
fill_mem(0x0000);
if((frame / 50) % 2 == 0)
{
get_next_warning(buff);
}
.. some more code ..
}
The problem I have is that when I try to compile this, the compiler complains of no buff defined, but when I try to define it, the linker complains of two buff variables. Catch-22. To this end, I am asking two questions:
how to fix this, and;
if it is possible to use the same variable thus saving memory because only one variable needs to be allocated (working with a memory constrained microcontroller.)
You need this in the top of hud.c, and any other .c file you use them from:
extern struct TextSize tsize;
extern char buff[50];
The extern keyword tells the compiler that the "actual" variables already exist, in some other unit (file), and the linker will fix it all up for you in the end.
If you're doing this on a large scale in a project, consider how you might encapsulate globals in one place, and create a shared .h file that declares these things as "extern" for all the places that you use them from.
use "extern char buff[50];" in the files where you have not initialized the buff and want to use it. This tells that the declaration is done in other files and you are using it in this file.