Thread: edit?
View Single Post
Old 11-18-2002, 10:28 PM   #7 (permalink)
sdeming
Code Monkey
 
Join Date: Jul 2002
Location: Michigan
Posts: 85
sdeming is on a distinguished road
Variable declaration is to be done at the start of any block, not just the start of the program or function at global scope...

Example:
Code:
int main()
{
  int x = 10;

  if (x == 10) {
    int y = 3;
    printf("x is %d and y is %d\n", x, y);
  }

  return 0;
  
}
Completely legitimate, even tested in gcc to make sure my C++ isn't flowing into ANSI C.

Regarding the 33,000 thing, the true range for a 16-bit signed integer is -32768 to 32767, but int's haven't really been seen as 16-bit in any mainstream compiler for quite some time. To really discover the limits of a most types, include <limits.h> then use the sizeof(type) operator, multiply by CHAR_BIT then shift the value of 1 to the left that many times minus 1 (signed), don't subtract if you want the max for the unsigned value. IE:
Code:
#include <limits.h>
#include <stdio.h>

int main()
{
  unsigned long x = (1 << ((sizeof(int) * CHAR_BIT)-1));

  printf("the max size of a signed int is: %lu\n", x);

  return 0;
}
Execute this and you'll see results that look something along the lines of:
Quote:
the max size of a signed int is: 2147483648
... assuming a 32-bit compiler. Of course, you don't really have to test these because there are predefined values available in limits.h that look something like:
Code:
#define USHRT_MAX       0xffff
#define SHRT_MAX        0x7fff
#define SHRT_MIN        (-0x7fff - 1)
#define UINT_MAX        0xffffffffU
#define INT_MAX         0x7fffffff
#define INT_MIN         (-0x7fffffff - 1)
but it is always more fun to figure these things out. I think.
__________________
Scott
B4 09 BA 09 01 CD 21 CD 20 53 63 6F 74 74 24
sdeming is offline   Reply With Quote