0

I am a beginner programmer. Using Dev C++ I need to print the range of double, long long int, unsigned long long int, signed long long int, unsigned int, unsigned long int.

I have done this so far. All sizes are in bits.

#include <stdio.h>  
#include <conio.h>    
#include <math.h>

int main()
{
    unsigned long int
        d = pow(2, sizeof(double) * 8),
        lli = pow(2, sizeof(long long int) * 8),
        ulli = pow(2, sizeof(unsigned long long int) * 8),
        slli = pow(2, sizeof(signed long long int) * 8),
        ui = pow(2, sizeof(unsigned int) * 8),
        uli = pow(2, sizeof(unsigned long int) * 8);

    printf("double%d to %d", -(d / 2), (d / 2) - 1);
    printf("longlongint%d to %d", -(lli / 2), (lli / 2) - 1);
    printf("unsigned longlongint%d", ulli - 1);
    printf("signed long longint%d to %d", -(slli / 2), (slli / 2) - 1);
    printf("unsigned int%d", ui - 1);
    printf("unsignedlongint%d ", uli - 1);
    getch();

    return 0;
}

but still it's not printing the range...

  • 4
    Use `` and `` and `` ..... – Basile Starynkevitch Mar 16 '13 at 07:37
  • lol did it and nothing happended .. kindly read the whole post 1st and then reply... x.x – bilal siddique Mar 16 '13 at 07:40
  • 1
    That's not the way to do it; the values are defined by macros in the headers. Also, using `%d` prints integers; you won't print the maximum `unsigned long long int` with `%d`. – Jonathan Leffler Mar 16 '13 at 07:50
  • Don't forget to enable all warnings and debugging information, and learn how to use the debugger (e.g. to run step by step your program). On Linux (which is a system very friendly to developers: you can study the source code of everything since it is free software) you would compile with `gcc -Wall -g` and debug using `gdb` – Basile Starynkevitch Mar 16 '13 at 08:05

2 Answers2

3

The ranges for the signed integers are usually asymmetric — there's usually one more negative value than there are positive values.

The header <limits.h> defines the ranges for the integer types you specify.

  • ULONG_MAX
  • ULLONG_MAX

etc.

The header <float.h> defines the ranges for the floating point types:

  • DBL_MAX
  • DBL_MIN

etc.

You must print the values using the correct format specifiers.

Jonathan Leffler
  • 666,971
  • 126
  • 813
  • 1,185
  • +1 :: Pointing out very obvious mistake- "You must print the values using the correct format specifiers" – Abhineet Mar 16 '13 at 08:00
  • 1
    Note that `DBL_MIN` means the smallest positive number (not the most-negative number as the integer types' `_MIN` means) – M.M Jan 31 '17 at 05:54
0

try to add "\n" to each print sentence:

printf("double%d to %d\n",-(d/2),(d/2)-1); 
printf("longlongint%d to %d\n",-(lli/2),(lli/2)-1);  
printf("unsigned longlongint%d\n",ulli-1);    
printf("signed long longint%d to %d\n",-(slli/2),(slli/2)-1);    
printf("unsigned int%d\n",ui-1 );   
printf("unsignedlongint%d\n",uli-1);  

UPDATE

As @Abhineet suggested, further information about buffered streaming could helps you, so reading this SO question may be interesting

Community
  • 1
  • 1
Miguel Prz
  • 13,227
  • 26
  • 36
  • the problem description has nothing to do with compiler concerns – Miguel Prz Mar 16 '13 at 08:12
  • I tried it before I post my solution and it works for me, did you do the same thing before vote and comment? thanks – Miguel Prz Mar 16 '13 at 08:19
  • 1
    yes, because of buffered streams: http://stackoverflow.com/questions/1716296/why-does-printf-not-flush-after-the-call-unless-a-newline-is-in-the-format-strin – Miguel Prz Mar 16 '13 at 08:26
  • OMG, I never knew this. I have only one upvote or I would have upvoted you multiple times including your above comment. – Abhineet Mar 16 '13 at 08:38
  • My vote is locked in unless you edit something. Would you please change your "try to add "\n" to each print sentence" to "printf sentence" ? – Abhineet Mar 16 '13 at 08:40
  • 1
    done, and thanks for commenting the sense of your vote, that is very helpful, and may clarify the whole question and answer – Miguel Prz Mar 16 '13 at 08:53