Having a very newbie problem.

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

Kharthulu
Posts: 21
Joined: Sun Jun 21, 2009 4:37 pm UTC

Having a very newbie problem.

Postby Kharthulu » Mon Nov 02, 2009 1:51 pm UTC

To start off, i have to say i am very much a novice at c programming. I am trying to find out what are the maximum values the various types are but my ignorance of the display syntax might be preventing me from seeing the actual value.

First thing, i've seen conflicting information available here. Does long and long long apply only to integers, and double, and long double apply only to floats? And if i type in long long double is my compiler gonna get confused?

Also, when using printf, what do i type to display the value?

I've tried using the constants from <float.h> and I've tried
printf("%f", maxfloat/*FLT_MAX actually*/)
printf("%f", maxdouble)
printf("%Lf", maxlongdouble)
but for anything bigger than a float it gets confused, and i'm not too trusting of the first number anyway. Also, it doesnt seem to even recognize %Lf

I have thought it might be because i'm running on windows 7 64 bit or i've got an old compiler, but the most likely reason is just ignorance of the right display syntax. Any ideas?

fazzone
Posts: 186
Joined: Wed Dec 10, 2008 9:38 pm UTC
Location: A boat

Re: Having a very newbie problem.

Postby fazzone » Mon Nov 02, 2009 3:24 pm UTC

Well, you could always just look in float.h, but http://www.cplusplus.com/reference/clibrary/cstdio/printf/ is an excellent reference for printf.
*/

Kharthulu
Posts: 21
Joined: Sun Jun 21, 2009 4:37 pm UTC

Re: Having a very newbie problem.

Postby Kharthulu » Mon Nov 02, 2009 7:16 pm UTC

Well for instance i typed this is:

long double testing = .987654321987654321;

printf("testing: %Lf\n", testing);

and it spits out this garbage number. I've tried %f, %Lf, %llf, and a dozen others and it just spits out this garbage number when i try to display a long double. Something like:

-22865988945971409000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.0000000000000000000000

User avatar
Pesto
Posts: 737
Joined: Wed Sep 05, 2007 5:33 pm UTC
Location: Berkeley, CA

Re: Having a very newbie problem.

Postby Pesto » Sat Nov 07, 2009 6:52 am UTC

This code

Code: Select all

#include <stdio.h>

int main()
{
   long double f = .987654321987654321;

   printf("%Lf\n", f);

   return 0;
}


gives me this output.

Code: Select all

0.987654


Not sure what's going wrong with your computer. :?

User avatar
phlip
Restorer of Worlds
Posts: 7572
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia
Contact:

Re: Having a very newbie problem.

Postby phlip » Mon Nov 09, 2009 7:30 am UTC

What compiler and OS are you using? I think the builtin printf function in Windows XP and earlier (in msvcrt) has troubles with long long ints and long doubles (since XP's so old that the implementations predate C99)... so if you're using, say, MinGW, which supports those variable types, but printing with msvcrt, which doesn't (and is the default), then you need to do some screwing around to print them properly (IIRC you need to use "%I64d" instead of "%lld", and from what I can tell, the only way to print a long double is to downcast it to double first).

If you are using MinGW, you might find some useful info here.

Code: Select all

enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};
void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}
[he/him/his]

User avatar
PM 2Ring
Posts: 3713
Joined: Mon Jan 26, 2009 3:19 pm UTC
Location: Sydney, Australia

Re: Having a very newbie problem.

Postby PM 2Ring » Tue Nov 10, 2009 2:53 pm UTC

I have thought it might be because i'm running on windows 7 64 bit or i've got an old compiler

Are you linking a math library?

You may also need to include <math.h>, so printf() can do the arithmetic required to perform the string to float conversion. (I guess that should be necessary with modern compilers, but I haven't bothered testing this lately, having been bitten badly by this sort of problem a decade or so ago. :) )


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 5 guests