Click Here
home features news forums classifieds faqs links search
6225 members 
Amiga Q&A /  Free for All /  Emulation /  Gaming / (Latest Posts)
Login

Nickname

Password

Lost Password?

Don't have an account yet?
Register now!

Support Amigaworld.net
Your support is needed and is appreciated as Amigaworld.net is primarily dependent upon the support of its users.
Donate

Menu
Main sections
» Home
» Features
» News
» Forums
» Classifieds
» Links
» Downloads
Extras
» OS4 Zone
» IRC Network
» AmigaWorld Radio
» Newsfeed
» Top Members
» Amiga Dealers
Information
» About Us
» FAQs
» Advertise
» Polls
» Terms of Service
» Search

IRC Channel
Server: irc.amigaworld.net
Ports: 1024,5555, 6665-6669
SSL port: 6697
Channel: #Amigaworld
Channel Policy and Guidelines

Who's Online
22 crawler(s) on-line.
 95 guest(s) on-line.
 0 member(s) on-line.



You are an anonymous user.
Register Now!
 Beajar:  22 mins ago
 MEGA_RJ_MICAL:  1 hr 11 mins ago
 AmigaMac:  3 hrs 24 mins ago
 bhabbott:  3 hrs 33 mins ago
 pixie:  3 hrs 46 mins ago
 RobertB:  4 hrs 12 mins ago
 OneTimer1:  4 hrs 20 mins ago
 Tomppeli:  4 hrs 45 mins ago
 michalsc:  4 hrs 54 mins ago
 Amiboy:  5 hrs 18 mins ago

/  Forum Index
   /  Amiga Development
      /  Beginning C
Register To Post

Goto page ( 1 | 2 Next Page )
PosterThread
Swoop 
Beginning C
Posted on 5-Oct-2004 13:01:14
#1 ]
Elite Member
Joined: 20-Jun-2003
Posts: 2163
From: Long Riston, East Yorkshire

O.K. I have a very basic (probably) C programming problem.

I am trying to learn C, so please forgive some (probably very) silly questions.
I have the "C programming in easy steps" book, and the "hello world" program compiles and runs fine. But the next example does not, and I don`t know why.

#include

int main()
{
double pi = 3.141526536;
printf ("The value of pi is approximatley %f", pi );
return 0;
}


The program compiles OK, but does not display the variable pi.
All I get printed to the screen is "The value of pi is approximatley", followed by the prompt.

In theory it should display the value of pi rounded to 6 decimal places.
I have changed the spacing, changed the variable name, and even put void in the brackets of main(), to see if it was a typing error.

I am using gcc 2.95.3 on my A1, and using editpad as my editor.
The program compiles ok, even with the -Wall option, its just that I don`t get the correct output.

I know this is probably something stupid, but any help would be appreciated.

Last edited by Swoop on 05-Oct-2004 at 03:52 PM.

_________________
Peter Swallow.
A1XEG3-800 [IBM 750FX PowerPC], running OS4.1FE, using ac97 onboard sound.

"There are 10 types of people in the world: those who understand binary, and those who don't."

 Status: Offline
Profile     Report this post  
Rogue 
Re: Begining C
Posted on 5-Oct-2004 13:05:19
#2 ]
OS4 Core Developer
Joined: 14-Jul-2003
Posts: 3999
From: Unknown

@Swoop

Quote:
printf ("The value of pi is approximatley %f", pi );


The problem here is that pi is a double, but you are using %f, which is a float. Varargs functions don't have any means to correctly "guess" the type before calling the function.

You should add "-Wall" to the command line when compiling, it will notify you of such problems.

To answer your question, the following two alternatives exist:


printf("The value of pi is %lf\n", pi);


which qualifies the f with a long qualifier, meaning double, or


printf ("the value of pi is %f\n", (float)pi);


which explicitly casts the number to float.

This error is quite common, therefore using the "-Wall" option is a good idea, since it can check and warn against such problems. In general, you will get much more warnings with "-Wall", but if you manage to eliminate these, your program is going to be much more stable.

Last edited by L8-X on 05-Oct-2004 at 02:16 PM.

_________________
Seriously, if you want to contact me do not bother sending me a PM here. Write me a mail

 Status: Offline
Profile     Report this post  
AmiDog 
Re: Begining C
Posted on 5-Oct-2004 13:13:35
#3 ]
Cult Member
Joined: 1-Jun-2004
Posts: 917
From: Kumla, Sweden

@Swoop

Try to use the "-lm" option to GCC to link with the correct printf, that is, one which can handle floating point.

 Status: Offline
Profile     Report this post  
Serpi 
Re: Begining C
Posted on 5-Oct-2004 13:16:19
#4 ]
Cult Member
Joined: 31-Jul-2003
Posts: 547
From: Germany

@Rogue

I think he also (or even only) needs to compile with the "-lm" switch to include the math link library for float operation (wich is also needed by printf() to print float/double values).

Ciao, Alfred

 Status: Offline
Profile     Report this post  
Swoop 
Re: Begining C
Posted on 5-Oct-2004 13:55:46
#5 ]
Elite Member
Joined: 20-Jun-2003
Posts: 2163
From: Long Riston, East Yorkshire

@Rogue

Quote:
You should add "-Wall" to the command line when compiling, it will notify you of such problems.


I have used the -Wall option, and received no error messages.

Quote:
printf("The value of pi is %lf\n", pi);


With this I get "In function `main`; use of `l` character with `f` type character".

Quote:
printf ("the value of pi is %f\n", (float)pi);


With this version I get the same original output, the text printed to the screen, followed by the prompt.


@Amidog

Using the -lm option on gcc, I still get the original output.


@Serpi

Do I need to include a maths header ?


Thanks for your help.

By the way this is gcc 2.95.3 in the pre-release SDK. You guys are probably running 3.4+.

_________________
Peter Swallow.
A1XEG3-800 [IBM 750FX PowerPC], running OS4.1FE, using ac97 onboard sound.

"There are 10 types of people in the world: those who understand binary, and those who don't."

 Status: Offline
Profile     Report this post  
Swoop 
Re: Begining C
Posted on 5-Oct-2004 14:01:53
#6 ]
Elite Member
Joined: 20-Jun-2003
Posts: 2163
From: Long Riston, East Yorkshire

@all

even if I change "double pi " to "float pi", the value of pi is not output, just the text.

_________________
Peter Swallow.
A1XEG3-800 [IBM 750FX PowerPC], running OS4.1FE, using ac97 onboard sound.

"There are 10 types of people in the world: those who understand binary, and those who don't."

 Status: Offline
Profile     Report this post  
AlexC 
Re: Begining C
Posted on 5-Oct-2004 14:12:56
#7 ]
Super Member
Joined: 22-Jan-2004
Posts: 1301
From: City of Lost Angels, California.

@Swoop
Just tried with 2.95.3 and it works as expected.

#include

int main()
{
double pi = 3.141526536;
printf ("The value of pi is approximatley %f\n", (float)pi );
return 0;
}

or with "float pi" instead of "double pi",
and "pi" instead of "(float)pi"

Both return
[/z] pi
The value of pi is approximatley 3.141526
[/z]

I'm using QNX but shouldn't matter
[/z] gcc -v
Reading specs from /usr/lib/gcc-lib/ntox86/2.95.3qnx-nto/specs
gcc version 2.95.3qnx-nto 20010315 (release)

_________________
AlexC's free OS4 software collection

AmigaOne XE/X1000/X5000/UAE-PPC OS4 laptop/X-10 Home Automation

 Status: Offline
Profile     Report this post  
Swoop 
Re: Begining C
Posted on 5-Oct-2004 14:39:45
#8 ]
Elite Member
Joined: 20-Jun-2003
Posts: 2163
From: Long Riston, East Yorkshire

@AmigAlex

I`ve just checked my listing against yours, and it matches but i don`t get the variable pi displayed at all.

If I use gcc -v I get

gcc 2.95.3 20010315 (Amiga, Inc. build 20030831)

I don`t know if this a bug, and as this is only my second `C` program i don`t know enough to test if the variable display works correctly. (????)

_________________
Peter Swallow.
A1XEG3-800 [IBM 750FX PowerPC], running OS4.1FE, using ac97 onboard sound.

"There are 10 types of people in the world: those who understand binary, and those who don't."

 Status: Offline
Profile     Report this post  
AlexC 
Re: Begining C
Posted on 5-Oct-2004 15:23:06
#9 ]
Super Member
Joined: 22-Jan-2004
Posts: 1301
From: City of Lost Angels, California.

@Swoop

I have no idea what could be wrong.

I just compiled it with "gcc -o pi pi.c -Wall"

If you have gdb you could try

1.RAM: > gdb pi

then type

r + enter to run, and
c + enter to continue
(k is kill and q is quit but you probably won't need these)

I don't know what you could get out of it but after "c" it should only print:
Continuing.
[pi is approx ...]
(no debugging symbols found)...
Program exited normally.

If no error shows up, try the program using a simple int e.g.
int pi = 1;
printf("pi: %d\n", pi)
to see if at least it will print that, otherwise there's a problem but I wouldn't know where.

_________________
AlexC's free OS4 software collection

AmigaOne XE/X1000/X5000/UAE-PPC OS4 laptop/X-10 Home Automation

 Status: Offline
Profile     Report this post  
Swoop 
Re: Begining C
Posted on 5-Oct-2004 15:36:06
#10 ]
Elite Member
Joined: 20-Jun-2003
Posts: 2163
From: Long Riston, East Yorkshire

@AmigAlex

ran gdb SDK:My_progs/firstvar

I get the response :-

Quote:
Reading symbols from LIBS:locale.library.....done
The value of pi is approximatley

Program terminated with signal SIGQUIT, quit.
The program no longer exists.


and then the (gdb) prompt.

I`m getting more confused!!!!

_________________
Peter Swallow.
A1XEG3-800 [IBM 750FX PowerPC], running OS4.1FE, using ac97 onboard sound.

"There are 10 types of people in the world: those who understand binary, and those who don't."

 Status: Offline
Profile     Report this post  
Swoop 
Re: Begining C
Posted on 5-Oct-2004 15:46:54
#11 ]
Elite Member
Joined: 20-Jun-2003
Posts: 2163
From: Long Riston, East Yorkshire

@AmigAlex

Quote:
If no error shows up, try the program using a simple int e.g.
int pi = 1;
printf("pi: %d\n", pi)
to see if at least it will print that, otherwise there's a problem but I wouldn't know where.


Using the above the value of pi is still not displayed.

The project I have in mind has to be able to display/store/retrieve numeric values i.e. currency. I know I am a very long way from attempting that project, and this does not give me any faith in gcc, or at least the Amiga inc build of it.

I overwrote my Linux installation when I installed OS4pre, or else I would have tried it on that. Maybe the soon to arrive update will have a stable version of gcc3.4 and I`ll wait and try it on that.

_________________
Peter Swallow.
A1XEG3-800 [IBM 750FX PowerPC], running OS4.1FE, using ac97 onboard sound.

"There are 10 types of people in the world: those who understand binary, and those who don't."

 Status: Offline
Profile     Report this post  
shoe 
Re: Begining C
Posted on 5-Oct-2004 16:33:27
#12 ]
Super Member
Joined: 14-Sep-2003
Posts: 1585
From: Gothenburg, Sweden

@Swoop

> type pi.c
#include

int main()
{
double pi = 3.141526536;
printf ("The value of pi is approximatley %f\n", (float)pi );
return 0;
}
> g++ pi.c -o pi
> pi
The value of pi is approximatley 3.141526
> g++ -v
Reading specs from /gcc/lib/gcc-lib/ppc-amigaos/2.95.3/specs
gcc version 2.95.3 20010315 (Amiga, Inc. build 20030831)


Works here... ?
Couldn't say what's wrong though. But don't give up!

/shoe

Last edited by shoe on 05-Oct-2004 at 04:34 PM.

 Status: Offline
Profile     Report this post  
AlexC 
Re: Begining C
Posted on 5-Oct-2004 16:42:14
#13 ]
Super Member
Joined: 22-Jan-2004
Posts: 1301
From: City of Lost Angels, California.

@Swoop

So, no error in gdb and no int printed out either :(

Maybe something went wrong when your SDK was installed.
There should be an update soon, but in the meantime you could try commenting out the assigns in startup-sequence and/or user-startup and re-installing the SDK to an empty drawer.

I've compiled a few apps with 2.95.3 on the A1 so I know the SDK is working.

_________________
AlexC's free OS4 software collection

AmigaOne XE/X1000/X5000/UAE-PPC OS4 laptop/X-10 Home Automation

 Status: Offline
Profile     Report this post  
Bean 
Re: Begining C
Posted on 5-Oct-2004 16:51:25
#14 ]
Super Member
Joined: 4-Apr-2003
Posts: 1225
From: U.K.

@Swoop

Try it again with the compile statement like this:

gcc filename.c -lm

and run the resulting

a.out

It should work fine.

Bean.


_________________
OS4.1 + SAM Flex
RIP my A1XE.. that used to have an appetite for batteries!

 Status: Offline
Profile     Report this post  
JCC 
Re: Beginning C
Posted on 5-Oct-2004 16:59:43
#15 ]
Regular Member
Joined: 15-Sep-2003
Posts: 254
From: NY/NJ, US

@Swoop

I am using float with gcc 2.95.3 on my A1 w/o any problem (but I also had some "teething" problems).

I'll check out your code when I get home tonight - I can't do it now, my office has succumbed to the Evil Empire of Wintel

[EDIT]

The Answer is ...

#include // <<== You forgot this...
#include

int main()
{
    double pi = 3.141526536;
    printf ("The value of pi is approximatley %f", pi );
    return 0;
}

I compiled it using:
    gcc -Wall pi.c -lm -o pi

Also, remember to     #include     when using atof, atoi, etc.

[/EDIT]

Regards,
        JCC

Last edited by JCC on 06-Oct-2004 at 12:28 PM.

 Status: Offline
Profile     Report this post  
tafka 
Re: Beginning C
Posted on 5-Oct-2004 17:31:35
#16 ]
Regular Member
Joined: 12-Mar-2003
Posts: 285
From: Bristol, UK

@Swoop

I saw this problem a while ago, but I thought I was going mad. In the end I just assumed it was a bug in the compiler and used Visual Studio .Net on the PC instead to write the app I needed.

So, I never found out what the solution was and I haven't seen it since but it was definitely there as I copied and pasted exactly the same code on the PC and it worked fine.

I sympathise, try putting some other code around it, maybe the compiler will sort it's life out. It certainly is annoying to be stopped by something like this when you are just starting. Keep going though, it will be worth it in the end.

By the way, keep your eye on my site (see below), pretty soon I will be putting a "from scratch" Reaction tutorial up there if that interests you?

_________________
www.amigaguru.com
www.dragons-fire.co.uk

 Status: Offline
Profile     Report this post  
shoe 
Re: Beginning C
Posted on 5-Oct-2004 18:39:07
#17 ]
Super Member
Joined: 14-Sep-2003
Posts: 1585
From: Gothenburg, Sweden

Quote:

tafka wrote:

I saw this problem a while ago, but I thought I was going mad. In the end I just assumed it was a bug in the compiler and used Visual Studio .Net on the PC instead to write the app I needed.

Looser


Quote:
By the way, keep your eye on my site (see below), pretty soon I will be putting a "from scratch" Reaction tutorial up there if that interests you?


Wow, looking forward to that! I've been looking around for that for a while! Thanks!


/shoe

 Status: Offline
Profile     Report this post  
ssolie 
Re: Beginning C
Posted on 5-Oct-2004 21:02:12
#18 ]
Elite Member
Joined: 10-Mar-2003
Posts: 2755
From: Alberta, Canada

@Swoop
Your program is valid as it is. Try something like this to compile it:
gcc -Wall -o pi pi.c -lm

If that doesn't work I'd suspect you ran into an old clib2 bug. I remember seeing talk of one a long time ago but never paid much attention since I wasn't interested in floating point at that time.

I also checked my copy of the ISO/ANSI standard and %f is of type double and the 'l' (ell) modifier is supposed to have no effect on %f. Your friendly neighbourhood language lawyer

_________________
ExecSG Team Lead

 Status: Offline
Profile     Report this post  
Cyberus 
Re: Beginning C
Posted on 5-Oct-2004 23:02:20
#19 ]
Member
Joined: 23-Aug-2004
Posts: 32
From: Unknown

@Swoop

Hi. As an aside, you might want to check out the Amiga Beginners C group.
I haven't had time to do much myself, but there's a few guys on there.

regards,
Cyberus

edit: Doh! Here's the link:
http://groups.yahoo.com/group/amiga_bcg/

Last edited by Cyberus on 05-Oct-2004 at 11:06 PM.

_________________

 Status: Offline
Profile     Report this post  
Intuitioned 
Re: Beginning C
Posted on 5-Oct-2004 23:15:42
#20 ]
Super Member
Joined: 27-Oct-2003
Posts: 1340
From: Unknown

@tafka

Look forward to your Reaction tutorial.

_________________

 Status: Offline
Profile     Report this post  
Goto page ( 1 | 2 Next Page )

[ home ][ about us ][ privacy ] [ forums ][ classifieds ] [ links ][ news archive ] [ link to us ][ user account ]
Copyright (C) 2000 - 2019 Amigaworld.net.
Amigaworld.net was originally founded by David Doyle