Ask coding questions

← Back to all posts
How to switch running scripts in C?
h
Crcoli7307

I am making a program in C and it has multiple scripts. In python, you can just do exec(open("foo.py")). But how would you do that in C? Like how do I execute a different script in C?

Answered by xxpertHacker [earned 5 cycles]
View Answer
Voters
WindhyDzakiyyah
Crcoli7307
Comments
hotnewtop
xxpertHacker

C doesn't have "scripts", but are you looking for imports or multi-threading?

You can't just execute C on the go, it's a compiled language.

Crcoli7307

@StudentFires

Where did I screw up?

emilyUsesPascal

@Crcoli7307 you cannot have 2 "main" functions as that will confuse the compier. There is no exec command in c or c++ btw. You can call other functions in c++ by using #include "filename.extension". you have to include the quotes

Crcoli7307

@emilyUsesPascal @StudentFires Told me that #import "./foo.c";
would work. But it said this:

emilyUsesPascal

@Crcoli7307 you have 2 functions called main. You cannot have that

Crcoli7307

@emilyUsesPascal What do I call it? Main2?

emilyUsesPascal

@Crcoli7307 you can call it anything you want to, as long as it doesn't already exist

Crcoli7307

@emilyUsesPascal Main2 gives me the same answer.

Crcoli7307

@emilyUsesPascal Everything gives me that error.

emilyUsesPascal

@Crcoli7307 your using #import, not #include. There is no #import

xxpertHacker

@Crcoli7307 You're main must be in the main.cpp, you need to have a good understanding of how multiple files interact in C first.

Crcoli7307

@emilyUsesPascal I can try but It seems to be finding "foo.c" just fine.

xxpertHacker

@Crcoli7307 If you don't need the other file, don't have it.

Crcoli7307

@emilyUsesPascal Same Error.

xxpertHacker

@emilyUsesPascal Well, there is an import, it just isn't with a #.

Crcoli7307

@StudentFires I do tho 😑. I am just getting a function error.

emilyUsesPascal

@Crcoli7307
There are multiple definitions because your treating a .c file that isnt main as a header file. Dont use #include "my_c_file.c", instead, replace the .c with .h .

Also, please put your #include's at the front of the file

xxpertHacker

@Crcoli7307 You don't need two files, why do you want two files?

Crcoli7307

@emilyUsesPascal Ok Attempt 5 in progress.

xxpertHacker

@Crcoli7307 Invite me to the Repl.

Crcoli7307
Crcoli7307

@emilyUsesPascal @StudentFires The problem now isn't so much switching files, but running the function in a file.

emilyUsesPascal

@Crcoli7307 just call the function like it was in the main file

Crcoli7307
emilyUsesPascal

@Crcoli7307 if you have a function called foo() in bar.h, and bar.h is included in main.c, put foo() in your c file

Crcoli7307
emilyUsesPascal
Crcoli7307

Like how can I finish running the code in main.c and start running the code in foo.c?

@StudentFires @TheDrone7

TheDrone7

You can also modify the working of the run button. Learn here how - https://docs.repl.it/repls/dot-replit

xxpertHacker

@TheDrone7 Not really helpful in this situation.