LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   c programming (https://www.linuxquestions.org/questions/programming-9/c-programming-42361/)

Randall 01-21-2003 09:44 PM

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

oulevon 01-21-2003 09:54 PM

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.

Randall 01-21-2003 10:17 PM

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

oulevon 01-21-2003 10:25 PM

you have a number of errors. try this:
Code:

#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);

}


aizkorri 01-22-2003 05:53 AM

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.

Randall 01-22-2003 06:50 PM

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

oulevon 01-22-2003 07:08 PM

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.

Randall 01-22-2003 08:27 PM

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

oulevon 01-22-2003 08:48 PM

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.

Randall 01-22-2003 09:43 PM

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

oulevon 01-22-2003 09:52 PM

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.

Inuyasha-kun 12-06-2003 12:08 AM

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?

oulevon 12-06-2003 01:25 AM

Post the code to your program.

Inuyasha-kun 12-06-2003 01:27 AM

Code:

#include <stdio.h>
//A is the number to pick the operation
//B and C are the single-precision floating point variables that will be modified
int a;
float b,c;

int main()
{
        printf("NCalc 1.0 for Linux%nPick an op%n1:Add%n2:Sub%n3:Mul%n4:Div");
        scanf("%d",a);
        printf("%nNumber 1:");
        scanf("%f",b);
        printf("%nNumber 2:");
        scanf("%f",c);
        if(a == 1);printf("%f+%f=%f",b,c,b+c);
        if(a == 2);printf("%f-%f=%f",b,c,b-c);
        if(a == 3);printf("%fx%f=%f",b,c,b*c);
        if(a == 4);printf("%f/%f=%f",b,c,b/c);
        return 0;
}

A very simple calculator program. It compiled with no errors or warnings.

I'd also like to mention that I could never get VC++ to work with "Double"s, back when I mainly ran WinXP.

deiussum 12-06-2003 10:26 AM

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.