c programming
if i use vi to write a simple c program and named it pgm.c how would i go about compiling it ? i have red hat 8.0 and i chose the development package.
as you guys may have surmised i know next to nothing about this stuff. thanks in advance for any help anyone can give |
at the shell prompt type:
gcc somefile.c Afterward you can use a.out to run it. You may need to type ./a.out though. This assumes you have gcc installed. i think it is by default on redhat. |
i've tried that and it keeps giveing me parse errors
and it's the simplest programme ever #include <stdio.h) int main (void) { int f; double c; printf (Enter number here -> ") ; scanf (d%", &f); c = (5.0/9.0)*(f-32) ; printf("%d = %f\n", f, c) ; return(0); } that's the programme above i wrote it with vi and saved it as blah.c typed gcc blah.c and this is what i got back blah.c: In function 'main' : blah.c:18: parse error before ')' token |
you have a number of errors. try this:
Code:
#include <stdio.h> |
To compile use this
gcc -o execname -c yourprogram.c If you don't use the -o option it will create a a.out where the executabe is. To execute the program write ./execname. |
i used the gcc -o execname -c blah.c command (where blah.c is the name of the file)
the command prompt was returned i then used the ./execname command and it told me -bash: ./execname : Permission denied |
You have to use chmod.
I think chmod a+x execname should do it. There are a lot of options with chmod, so you might want to take a look at it in the man pages, or on the internet. |
commands so far
gcc -o execblah.c -c blah.c chmod a+x execblah.c ./execblah.c this last one returns -bash: ./execblah.c: cannot execute binary file |
man gcc will tell you all about the compiler including it's options. I don't know why you'd use the -c option with it.
gcc -o blah blah.c chmod a+x blah blah or ./blah works fine on my computer. |
i got it to work
i used cc blah.c -o blah.exe ./blah.exe and it ran fine thanks for all your help it was most appreciated and informative |
Well I hope i helped, and didn't confuse you too much. I really need to start playing around with more options on the compiler.
|
Excuse me, I found much of your information helpful. But, I keep getting "Segmentation fault" whenever I try to execute my C program. Can you tell me a way to fix this?
|
Post the code to your program.
|
Code:
#include <stdio.h> I'd also like to mention that I could never get VC++ to work with "Double"s, back when I mainly ran WinXP. |
I see a few problems with that code.
1: In some of your printfs you have %n, but no parameters following your string. I think what you want in these is \n instead. 2: Your scanfs are passing in a, b, c instead of &a, &b, &c. You need to use a pointer to your variables so that the scanf function can access the memory to fill them in. Segmentation faults in general are USUALLY because you are touching memory that you have no right touching. In this case you are passing in garbage values to scanf (since your variables aren't initialized), scanf thinks those values are an address to a memory location, (which they aren't), and tries to write memory at that location... Hope that helps. |
All times are GMT -5. The time now is 04:06 AM. |