C++ Crash Course: a fast-Paced Introduction


A literal is a hardcoded value in a program. You can use one of four  hardcoded, integer literal representations:  binary



Yüklə 7 Mb.
Pdf görüntüsü
səhifə64/71
tarix20.09.2023
ölçüsü7 Mb.
#145939
1   ...   60   61   62   63   64   65   66   67   ...   71
C Crash Course A Fast-Paced Introduction by Josh Lospinoso

33
literal is a hardcoded value in a program. You can use one of four 
hardcoded, integer literal representations: 
binary
Uses the prefix 
0b
octal
Uses the prefix 
0
decimal
This is the default
hexadecimal
Uses the prefix 
0x
These are four different ways of writing the same set of whole numbers. 
For example, Listing 2-1 shows how you might assign several integer vari-
ables with integer literals using each of the non-decimal representations.
#include
int main() {
unsigned short a = 0b10101010; 
u
printf("%hu\n", a);
int b = 0123; 
v
printf("%d\n", b);
unsigned long long d = 0xFFFFFFFFFFFFFFFF; 
w
printf("%llu\n", d);
}
170 
u
83 
v
18446744073709551615 

Listing 2-1: A program that assigns several integer variables and prints them with the 
appropriate format specifier
This program uses each of the non-decimal integer representations 
(binary 
u
, octal 
v
, and hexadecimal 
w
) and prints each with 
printf
using 
the appropriate format specifier listed in Table 2-1. The output from each 
printf
appears as a following comment.
N O T E
 
Integer literals can contain any number of single quotes (
'
) for readability. These are 
completely ignored by the compiler. For example, 
1000000
 and 
1'000'000
 are both inte-
ger literals equal to one million.
Sometimes, it’s useful to print an unsigned integer in its hexadecimal 
representation or (rarely) its octal representation. You can use the 
printf
specifiers 
%x
and 
%o
for these purposes, respectively, as shown in Listing 2-2.
#include
int main() {
unsigned int a = 3669732608;
printf("Yabba %x
u
!\n", a);
unsigned int b = 69;
printf("There are %u
v
,%o
w
leaves here.\n", b
x
, b
y
);
}


34
Chapter 2
Yabba dabbad00
u
!
There are 69
v
,105
w
leaves here.
Listing 2-2: A program that uses octal and hexadecimal representations of unsigned integers
The hexadecimal representation of the decimal 
3669732608
is 
dabbad00

which appears in the first line of output as a result of the hexadecimal 
format specifier 
%x
u
. The decimal 69 is 105 in octal. The format speci-
fiers for unsigned integer 
%u
v
and octal integer 
%o
w
correspond with the 
arguments at 
x
and 
y
, respectively. The 
printf
statement substitutes these 
quantities 
vw
into the format string, yielding the message 
There are 69,105 
leaves in here
.

Yüklə 7 Mb.

Dostları ilə paylaş:
1   ...   60   61   62   63   64   65   66   67   ...   71




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin