Example 1: VariablesDemo.c
Demonstrate Data Types: int, char, float
/* int main(void) printf("\n123456789123456789"); |
Output: |
- In C, char values are stored in 1 byte, and are encoded as numbers using the ASCII encoding.
Sometimes, there are some characters that are not represented by normal forms, such as new line characters. These can be coded by escape characters, such as \x, \n, \t... More on escape characters see 04 examples page. - int x1 = 21; this is in base 8, x1 = 21 in base 10
int x2 = 021; this is in base 8, hence, x2 = 17 in base 10
int x3 = 0x21; this is in base 16, x3 = 33 in base 10 - printf("\nbase 10: %d, Octal: %d, Hexadecimal: %d", x1, x2, x3); print 3 varibles all in base 10; %d = decimal = base 10
- printf("\nx1=%d, x2(octal)=%o, x3(hexadecimal)=%x", x1, x2, x3); print 3 varibles all in 3 different bases 10, 8 then 16;
%d = decimal = base 10
%o = octal = base 8
%x = hex = base 16 - %5d: print decimal number with minimum 5 spaces per number including empty spaces, so number 234 will be shown as __234 but number 123456 still shows as 123456
- 6.2f: print float number with minimum 6 spaces per number including empty spaces and the dot, so number 34.1123 will be shown as _34.11 but number 1123.456 still shows as 1123.45
- +8.2f, -8.2f
(-): Left-justify within the given field width; Right justification is the default (see width sub-specifier).
(+): Forces to precede the result with a plus or minus sign (+ or -) even for positive numbers. By default, only negative numbers are preceded with a - sign.