Beginner hobby programmer here. I’m used to running C in OSX compiling with GCC but I recently had to switch to windows. I’m compiling my code using Microsoft visual studio express 2010. The compiling goes fine but after that when I try to run it it only flashes open for a millisecond and then closes. How do I fix this?
This happens to all of the scripts I have tried but here is one in particular, the classic Fahrenheit and Celsius converter from K&R, that does not work.
If this is a stupid question, sorry. Just started learning C two weeks ago.
#include <stdio.h>
main()
{
int fahr, celsius;
int lower, upper, step;
lower = 0;
upper = 300;
step = 20;
fahr = lower;
while (fahr <= upper) {
celsius = 5 * (fahr-32) / 9;
printf("%d\t%d\n", fahr, celsius);
fahr = fahr + step;
}
}
put a
getch()before your final brace}, this will require a keypress before the program exitsthe only reason I suggest this rather than
ctrl-f5is that it teaches you another C command 🙂[edit]
Let me add a little more information to what you’re doing.
The correct signature for main is
int main(int argc, char **argv), this is the value you should have in your program (to replace the single linemain()that is currently there.You don’t have to do anything with those variables (argc & argv), they may be unused by you, the programmer. However, the presence of the preceding
inton the function name (main) means that it is expected to return a value. Again, you probably don’t care. However, in the future, you or someone who is responsible for your code, will care. What this means is that yourmainfunction should return some value, something to indicate it’s success or failure to the underlying operating system (also, something to be used should you employ your programs in a shell script).For the time being, a simple
return(0), after the aforementionedgetch()will do the job nicely.[/edit]