1)How would you give a short and good explaination to someone who is a beginner in programming
of the following
- short
- long
- typedef
- const
short and long are two of the numeric data types (char, short, int, long, float and double). Only char has a guarenteed size, which is 1 byte with a range of -127 to 126. The size of all the others is compiler-dependent, so you have to look in the compiler's header file limits.h to find out what they are.
typedef just creates an alias name for something else. For example, typedef int MYINT;
creates a new datatype name MYINT that's the same as int.
const just means the value of the variable or function can't be changed.
Only char has a guarenteed size, which is 1 byte with a range of -127 to 126.
Off by one. The guaranteed size is -127 to 127.
Yes, it was off, the range is -128 to 127
define SCHAR_MIN (-128) /* minimum signed char value */
#define SCHAR_MAX 127 /* maximum signed char value */
Even char is not strictly defined as one byte, by my reading of the standard.
Objects declared as characters (char) shall be large enough to store any member of the implementation’s basic character set.
Could there be an implementation that uses a larger character set?
Or smaller? I know the number of bits im a byte varies quite a bit from one patform to another, but AFAIK a char is always 1 byte.
I know we're getting a bit off track from the OP, but here's an interesting FAQ on this char size issue: http://www.parashift.com/c++-faq-lite/sizeof-char.html
Nice find there vmanes.
Yes, it was off, the range is -128 to 127
You're still off. The standard doesn't assume two's complement, so the minimum required range is 127 to 127. Your limits.h header will be more specific to the hardware, so it's best to get these minimum ranges right from the standard document.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.