Thread: Variable Min and Max Program

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    14

    Question Variable Min and Max Program

    I'm making a program that measures the max and min of different variables, and the program works find signed and unsigned char and short, but when it comes to int, it doesn't work... seems to loop forever... here's my code for the int part:

    Code:
    #include <stdio.h>
    
    main()
    {
    
        /* SIGNED INT */
        signed int int_signed;
        signed int int_signedP;
        
        for (int_signed = 0; int_signed > (int_signedP = int_signed-1); --int_signed)
            ;
        printf("signed int min: %d\n", int_signed); 
        
        for (int_signed = 0; int_signed < (int_signedP = int_signed+1); ++int_signed)
            ;
        printf("signed int max: %d\n", int_signed);
        
        printf("\n");
    
        
        /* UNSIGNED INT */
        unsigned int int_unsigned;
        unsigned int int_unsignedP;
        
        for (int_unsigned = 0; int_unsigned > (int_unsignedP = int_unsigned-1); --int_unsigned)
            ;
        printf("unsigned int min: %d\n", int_unsigned);
        
        for (int_unsigned = 0; int_unsigned < (int_unsignedP = int_unsigned+1); ++int_unsigned)
            ;
        printf("unsigned int max: %d\n", int_unsigned);
        
        printf("\n");
        
    }
    Any ideas?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    This code isn't C because C89/90 doesn't support declarations anywhere except the beginning of a block. Such a feature exists in C99, but C99 doesn't support implicit int, so that argument falls flat.

    Perhaps it would be safer and easier just to use the macros in limits.h?
    Code:
    #include <stdio.h>
    #include <limits.h>
    
    int
    main(void)
    {
      /* Signed integer */
      printf("Signed int min: %d\n", INT_MIN);
      printf("Signed int max: %d\n", INT_MAX);
      /* Unsigned integer */
      printf("Unsigned int min: %u\n", 0);
      printf("Unsigned int max: %u\n", UINT_MAX);
    
      return 0;
    }
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    it doesn't work... seems to loop forever...
    Even a computer will take a while to count up to 2.1 billion or so... patience (or take
    Prelude's advice and use the built in macros)

    p.s. I don't have the patience to see if your code does eventually give the correct result.
    DavT
    -----------------------------------------------

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    14
    Oh wow. Stupid me, I just had to wait. Thank you.

  5. #5
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Quote Originally Posted by Prelude
    This code isn't C because C89/90 doesn't support declarations anywhere except the beginning of a block. Such a feature exists in C99, but C99 doesn't support implicit int, so that argument falls flat.

    Perhaps it would be safer and easier just to use the macros in limits.h?
    for the sake of discussion. Why isn't it C, C99 is still C. so is 89/90. in fact if you want to get really pedantic anything from k&r isn't C.
    Just curious about that, cause your not the first person i've encountered that seems to dislike the additons in c99. why's that??
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    While I can't speak for everyone, it's not that it's a dislike, rather, it's pointing out the differences to people so we don't have to answer:

    "Well it's C! Why isn't it working on my compiler?"

    Thus, the difference between standard versions is pointed out, because most compilers are not C99 compilant right now. Hell, there are a huge huge number of people still using Turbo C 3.0 that post here!

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Why isn't it C
    Because it uses features from both C89/90 and C99 that are illegal in the other. That's highly, highly suspect.

    >C99 is still C. so is 89/90. in fact if you want to get really pedantic anything from k&r isn't C.
    C was standardized in 1989. That standard was replaced in 1999. If you want to get really pedantic, anything but C99 isn't C because C99 is the most current standard in effect. Until C99 is widely implemented, however, we're forced to use the common subset of features between C89/90 and C99. That's surprisingly easy because C99 enforces what is considered good style in C89/90. But for the most part around here, we assume that you're either working in that subset or we tolerate a pure C89/90 style while suggesting the better style that C99 requires.

    >cause your not the first person i've encountered that seems to dislike the additons in c99. why's that??
    I don't see how any part of my post suggests I dislike the additions in C99. Of course, that isn't to say that I don't disapprove of some of the additions to C with the new standard, nor that I disapprove of them all. Like all standards, there are good things and bad things.
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    This is a problem
    Code:
    for (int_unsigned = 0; int_unsigned > (int_unsignedP = int_unsigned-1); --int_unsigned)
            ;
    int_unsigned is unsigned, or only positive. and you're doing --int_unsigned, and your variable becomes 0xffffffff, which is about 4 thousand millions, positive, and it loops until 0 is reached again.
    And here's the big problem!!!:
    Code:
    1.
    int_unsigned > (int_unsignedP = int_unsigned-1)
    2.
    int_unsigned > int_unsigned-1
    expression 1 is your condition for the for to continue, and expression 2 is a simplification, that shows what's happening. That's ALWAYS true! unless int_unsigned is 0, which makes 0 > 0xffffffff, which is false.

  9. #9
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    well cool of you guys to answer, Just curious.
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help finding the max and min of a 2d array
    By finalreign in forum C Programming
    Replies: 6
    Last Post: 04-29-2009, 11:39 AM
  2. Max Min Problem
    By smithc2005 in forum C Programming
    Replies: 7
    Last Post: 10-22-2008, 10:38 AM
  3. Ranged numbers
    By Desolation in forum Game Programming
    Replies: 8
    Last Post: 07-25-2006, 10:02 PM
  4. Double output!
    By Spurnout in forum C++ Programming
    Replies: 3
    Last Post: 11-02-2002, 03:35 AM
  5. Max & Min value and refine histogram
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 07-20-2002, 08:04 AM