The following table summarizes the variable types.
Type | Typical number of bytes | Typical range of values |
---|---|---|
char | 1 | -128 to +127 or 0 to +255 |
unsigned char | 1 | 0 to +255 |
short | 2 | -32,768 to +32,767 |
unsigned short | 2 | 0 to +65,535 |
int | 2 or 4 | -32,768 to +32,767 or -2,147,438,648 to +2,147,438,647 |
unsigned int | 4 | 0 to +65,535 or 0 to +4,294,967,295 |
long | 4 | -2,147,438,648 to +2,147,438,647 |
unsigned long | 4 | 0 to +4,294,967,295 |
long long | 8 | -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807 |
unsigned long long | 8 | 0 to +18,446,744,073,709,551,615 |
float | 4 | +/-3.4E+/-38 (6 digits) |
double | 8 | +/-1.7E+/-308 (15 digits) |
long double | 12 | +/-1.2E+/-4932 (19 digits) |
Some Symbolic Constants from limits.h
Symbolic Constant | Represents |
---|---|
CHAR_BIT | Number of bits in a char |
CHAR_MAX | Maximum char value |
CHAR_MIN | Minimum char value |
SCHAR_MAX | Maximum signed char value |
SCHAR_MIN | Minimum signed char value |
UCHAR_MAX | Maximum unsigned char value |
SHRT_MAX | Maximum short value |
SHRT_MIN | Minimum short value |
USHRT_MAX | Maximum unsigned short value |
INT_MAX | Maximum int value |
INT_MIN | Minimum int value |
UINT_MAX | Maximum unsigned int value |
LONG_MAX | Maximum long value |
LONG_MIN | Minimum long value |
ULONG_MAX | Maximum unsigned long value |
LLONG_MAX | Maximum long long value |
LLONG_MIN | Minimum long long value |
ULLONG_MAX | Maximum unsigned long long value |
Uses defined constants from limit.h and float.
#include <stdio.h>
#include <limits.h> // integer limits
#include <float.h> // floating-point limits
int main(void)
{ //from ww w. ja va 2 s . c om
printf("Some number limits for this system:\n");
printf("Biggest int: %d\n", INT_MAX);
printf("Smallest long long: %lld\n", LLONG_MIN);
printf("One byte = %d bits on this system.\n", CHAR_BIT);
printf("Largest double: %e\n", DBL_MAX);
printf("Smallest normal float: %e\n", FLT_MIN);
printf("float precision = %d digits\n", FLT_DIG);
printf("float epsilon = %e\n", FLT_EPSILON);
return 0;
}
The code above generates the following result.
converts 2 fathoms to feet
#include <stdio.h>
int main(void)
{//from ww w .ja va 2 s. c om
int feet, fathoms;
fathoms = 2;
feet = 6 * fathoms;
printf("There are %d feet in %d fathoms!\n", feet, fathoms);
printf("Yes, I said %d feet!\n", 6 * fathoms);
return 0;
}
The code above generates the following result.
The following code shows how to read an int value from commandline.
#include <stdio.h>
int main(void)
{//from w w w. java 2s .c om
int dogs;
printf("How many dogs do you have?\n");
scanf("%d", &dogs);
printf("So you have %d dog(s)!\n", dogs);
return 0;
}
The code above generates the following result.
The following code shows how to use portable names for integer types.
/* w w w.ja v a2 s. c o m*/
#include <stdio.h>
#include <inttypes.h> // supports portable types
int main(void)
{
int32_t me32; // me32 a 32-bit signed variable
me32 = 45933945;
printf("First, assume int32_t is int: ");
printf("me32 = %d\n", me32);
printf("Next, let's not make any assumptions.\n");
printf("Instead, use a \"macro\" from inttypes.h: ");
printf("me32 = %" PRId32 "\n", me32);
return 0;
}
The code above generates the following result.
The following code shows how to print 100 in decimal, octal, and hex.
#include <stdio.h>
int main(void)
{//ww w .j av a 2 s . co m
int x = 100;
printf("dec = %d; octal = %o; hex = %x\n", x, x, x);
printf("dec = %d; octal = %#o; hex = %#x\n", x, x, x);
return 0;
}
The code above generates the following result.
The following code declares and uses various int types.
/* ww w .j a v a 2s .co m*/
#include <stdio.h>
int main(void)
{
unsigned int un = 3000000000; /* system with 32-bit int */
short end = 200; /* and 16-bit short */
long big = 65537;
long long verybig = 12345678908642;
printf("un = %u and not %d\n", un, un);
printf("end = %hd and %d\n", end, end);
printf("big = %ld and not %hd\n", big, big);
printf("verybig= %lld and not %ld\n", verybig, verybig);
return 0;
}
The code above generates the following result.
The following code displays float value in two ways.
#include <stdio.h>
int main(void)
{/*from ww w .ja va2 s . c o m*/
float aboat = 32000.0;
double abet = 2.14e9;
long double dip = 5.32e-5;
printf("%f can be written %e\n", aboat, aboat);
// next line requires C99 or later compliance
printf("And it's %a in hexadecimal, powers of 2 notation\n", aboat);
printf("%f can be written %e\n", abet, abet);
printf("%Lf can be written %Le\n", dip, dip);
return 0;
}
The code above generates the following result.
The following code shows what would happen if the value exceeds maximum int size.
#include <stdio.h>
int main(void)
{//from w w w. ja va2 s . co m
int i = 2147483647;
unsigned int j = 4294967295;
printf("%d %d %d\n", i, i+1, i+2);
printf("%u %u %u\n", j, j+1, j+2);
return 0;
}
The code above generates the following result.
The following code prints out type sizes.
#include <stdio.h>
int main(void)
{//from www . j a va 2s . c om
/* c99 provides a %zd specifier for sizes */
printf("Type int has a size of %zd bytes.\n", sizeof(int));
printf("Type char has a size of %zd bytes.\n", sizeof(char));
printf("Type long has a size of %zd bytes.\n", sizeof(long));
printf("Type long long has a size of %zd bytes.\n",
sizeof(long long));
printf("Type double has a size of %zd bytes.\n",
sizeof(double));
printf("Type long double has a size of %zd bytes.\n",
sizeof(long double));
return 0;
}
The code above generates the following result.
#include <stdio.h>
#include <stdint.h>
#include <wchar.h>
#include <limits.h>
#include <float.h>
#include <math.h>
int main(void)
{// w w w. ja v a 2 s.c om
printf("PTRDIFF_MIN = %td\n", PTRDIFF_MIN);
printf("PTRDIFF_MAX = %+td\n", PTRDIFF_MAX);
printf("SIZE_MAX = %zu\n", SIZE_MAX);
printf("SIG_ATOMIC_MIN = %+jd\n",(intmax_t)SIG_ATOMIC_MIN);
printf("SIG_ATOMIC_MAX = %+jd\n",(intmax_t)SIG_ATOMIC_MAX);
printf("WCHAR_MIN = %+jd\n",(intmax_t)WCHAR_MIN);
printf("WCHAR_MAX = %+jd\n",(intmax_t)WCHAR_MAX);
printf("WINT_MIN = %jd\n", (intmax_t)WINT_MIN);
printf("WINT_MAX = %jd\n", (intmax_t)WINT_MAX);
printf("CHAR_BIT = %d\n", CHAR_BIT);
printf("MB_LEN_MAX = %d\n", MB_LEN_MAX);
printf("\n");
printf("CHAR_MIN = %+d\n", CHAR_MIN);
printf("CHAR_MAX = %+d\n", CHAR_MAX);
printf("SCHAR_MIN = %+d\n", SCHAR_MIN);
printf("SCHAR_MAX = %+d\n", SCHAR_MAX);
printf("UCHAR_MAX = %u\n", UCHAR_MAX);
printf("\n");
printf("SHRT_MIN = %+d\n", SHRT_MIN);
printf("SHRT_MAX = %+d\n", SHRT_MAX);
printf("USHRT_MAX = %u\n", USHRT_MAX);
printf("\n");
printf("INT_MIN = %+d\n", INT_MIN);
printf("INT_MAX = %+d\n", INT_MAX);
printf("UINT_MAX = %u\n", UINT_MAX);
printf("\n");
printf("LONG_MIN = %+ld\n", LONG_MIN);
printf("LONG_MAX = %+ld\n", LONG_MAX);
printf("ULONG_MAX = %lu\n", ULONG_MAX);
printf("\n");
printf("LLONG_MIN = %+lld\n", LLONG_MIN);
printf("LLONG_MAX = %+lld\n", LLONG_MAX);
printf("ULLONG_MAX = %llu\n", ULLONG_MAX);
printf("\n");
printf("FLT_RADIX = %d\n", FLT_RADIX);
printf("DECIMAL_DIG = %d\n", DECIMAL_DIG);
printf("FLT_MIN = %e\n", FLT_MIN);
printf("FLT_MAX = %e\n", FLT_MAX);
printf("FLT_EPSILON = %e\n", FLT_EPSILON);
printf("FLT_DIG = %d\n", FLT_DIG);
printf("FLT_MANT_DIG = %d\n", FLT_MANT_DIG);
printf("FLT_MIN_EXP = %d\n", FLT_MIN_EXP);
printf("FLT_MIN_10_EXP = %d\n", FLT_MIN_10_EXP);
printf("FLT_MAX_EXP = %d\n", FLT_MAX_EXP);
printf("FLT_MAX_10_EXP = %d\n", FLT_MAX_10_EXP);
printf("FLT_ROUNDS = %d\n", FLT_ROUNDS);
printf("FLT_EVAL_METHOD = %d\n", FLT_EVAL_METHOD);
}
The code above generates the following result.