-2

I am new to computer science and I have doubts :

Doubt 1: If I want the SAME program run 10 times , then would I have to compile the same program 10 times or would I compile the program once and run it 10 times.

Obviously I would compile the program once and run if it is my first time (to look for errors) , but is it necessary to compile and then run for the next times I would be running the program (assuming I am not making any changes to the program )?

Doubt2: Suppose I have a user input program to find if a number "n" is odd or even . The user input is std::cin>>n ; My doubt is if the program would compile for every user input (i.e for every change of the variable n)? if yes , then it would be a waste of time because the compiler would have to iterate through the entire library and code , but the only change we have done would be changing the value of the variable n . (How would I solve the above problem if it had been the case)?

MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • 4
    No you only compile once and run it as many times as you want. No need to re-compile unless the code is changed. – kaylum Jan 06 '21 at 11:23
  • 1
    You don't have to compile your code for changing user input because user input is given at run time. – MikeCAT Jan 06 '21 at 11:24
  • 1
    can you write the code for same problem and show, I guess you will get an answer yourself. – IrAM Jan 06 '21 at 11:27
  • 2
    Think about any normal SW that you use. For example a calculator app. Someone has compiled that app once and has given it to you. Then you can run it as many times as you want and each time you can enter different input. – kaylum Jan 06 '21 at 11:27
  • Another point is you have to compile on the same system you are running it. If running it on a different system, you have to recompile. – costaparas Jan 06 '21 at 14:30
  • @costaparas Not true, executables are portable to the same type of system and [cross compilers](https://en.wikipedia.org/wiki/Cross_compiler) exist that can target completely different operating systems or architectures. – Blastfurnace Jan 06 '21 at 15:02
  • @blastfurnace Yes, I am aware of cross compilers. But you've missed my point (which was targeted for the OP who is a beginner) - you can't rely on a binary working on a system with a completely different architecture, hence the need to recompile (*regardless* of how you do it, on the target system itself, via a cross compiler). – costaparas Jan 06 '21 at 23:06

1 Answers1

1

Compilation is the process of taking source code and producing executable code (i.e. a program). If the source code hasn't changed there is no need to rerun the compiler, and the program can be run as many times as you like.

You can contrast this with interpreted languages where the interpreter has to run every time the program is run. But C++ isn't like that, it's always (AFAIK) implemented as a compiled language.

john
  • 85,011
  • 4
  • 57
  • 81
  • Also, see [question about C++ interpreters](https://stackoverflow.com/questions/69539/have-you-used-any-of-the-c-interpreters-not-compilers) – costaparas Jan 06 '21 at 14:31