LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-21-2003, 09:44 PM   #1
Randall
Member
 
Registered: Oct 2001
Location: Ontario, Canada (for now, i'm from NJ )
Distribution: Redhat 7.2
Posts: 106

Rep: Reputation: 15
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
 
Old 01-21-2003, 09:54 PM   #2
oulevon
Member
 
Registered: Feb 2001
Location: Boston, USA
Distribution: Slackware
Posts: 438

Rep: Reputation: 30
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.
 
Old 01-21-2003, 10:17 PM   #3
Randall
Member
 
Registered: Oct 2001
Location: Ontario, Canada (for now, i'm from NJ )
Distribution: Redhat 7.2
Posts: 106

Original Poster
Rep: Reputation: 15
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
 
Old 01-21-2003, 10:25 PM   #4
oulevon
Member
 
Registered: Feb 2001
Location: Boston, USA
Distribution: Slackware
Posts: 438

Rep: Reputation: 30
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);

}

Last edited by oulevon; 01-21-2003 at 10:27 PM.
 
Old 01-22-2003, 05:53 AM   #5
aizkorri
Member
 
Registered: Feb 2002
Location: Basque Country
Distribution: Fedora 14, Ubuntu 14.04
Posts: 434
Blog Entries: 1

Rep: Reputation: 55
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.
 
Old 01-22-2003, 06:50 PM   #6
Randall
Member
 
Registered: Oct 2001
Location: Ontario, Canada (for now, i'm from NJ )
Distribution: Redhat 7.2
Posts: 106

Original Poster
Rep: Reputation: 15
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
 
Old 01-22-2003, 07:08 PM   #7
oulevon
Member
 
Registered: Feb 2001
Location: Boston, USA
Distribution: Slackware
Posts: 438

Rep: Reputation: 30
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.
 
Old 01-22-2003, 08:27 PM   #8
Randall
Member
 
Registered: Oct 2001
Location: Ontario, Canada (for now, i'm from NJ )
Distribution: Redhat 7.2
Posts: 106

Original Poster
Rep: Reputation: 15
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
 
Old 01-22-2003, 08:48 PM   #9
oulevon
Member
 
Registered: Feb 2001
Location: Boston, USA
Distribution: Slackware
Posts: 438

Rep: Reputation: 30
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.
 
Old 01-22-2003, 09:43 PM   #10
Randall
Member
 
Registered: Oct 2001
Location: Ontario, Canada (for now, i'm from NJ )
Distribution: Redhat 7.2
Posts: 106

Original Poster
Rep: Reputation: 15
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
 
Old 01-22-2003, 09:52 PM   #11
oulevon
Member
 
Registered: Feb 2001
Location: Boston, USA
Distribution: Slackware
Posts: 438

Rep: Reputation: 30
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.
 
Old 12-06-2003, 12:08 AM   #12
Inuyasha-kun
LQ Newbie
 
Registered: Nov 2003
Distribution: Red Hat Linux 8
Posts: 20

Rep: Reputation: 0
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?

Last edited by Inuyasha-kun; 12-06-2003 at 01:25 AM.
 
Old 12-06-2003, 01:25 AM   #13
oulevon
Member
 
Registered: Feb 2001
Location: Boston, USA
Distribution: Slackware
Posts: 438

Rep: Reputation: 30
Post the code to your program.
 
Old 12-06-2003, 01:27 AM   #14
Inuyasha-kun
LQ Newbie
 
Registered: Nov 2003
Distribution: Red Hat Linux 8
Posts: 20

Rep: Reputation: 0
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.

Last edited by Inuyasha-kun; 12-06-2003 at 01:32 AM.
 
Old 12-06-2003, 10:26 AM   #15
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
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.

Last edited by deiussum; 12-06-2003 at 10:29 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
which programming language is used to do tcp/ip programming?? gajaykrishnan Linux - Networking 9 12-21-2012 05:16 AM
im new to programming but..... boxerboy Programming 6 08-26-2005 06:17 AM
Difference between Top Down programming & Bottom up programming minil Programming 1 06-17-2005 02:42 AM
C programming oranj Linux - Networking 1 12-07-2004 12:40 AM
Qt Programming... jinksys Programming 1 08-06-2003 04:33 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:14 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration