Even though the char
来源:互联网 发布:福建广电网络宽带帐号 编辑:程序博客网 时间:2024/06/03 16:48
Even though the char data type is an integer (and thus follows all of the normal integer rules), we typically work with chars in a different way than normal integers. Characters can hold either a small number, or a letter from the ASCII character set. ASCII stands for American Standard Code for Information Interchange, and it defines a mapping between the keys on an American keyboard and a number between 1 and 127 (called a code). For instance, the character ‘a’ is mapped to code 97. ‘b’ is code 98. Characters are always placed between single quotes.
The following two assignments do the same thing:
char
chValue =
'a'
;
char
chValue2 = 97;
cout outputs char type variables as characters instead of numbers.
The following snippet outputs ‘a’ rather than 97:
char
chChar = 97;
// assign char with ASCII code 97
cout << chChar;
// will output 'a'
If we want to print a char as a number instead of a character, we have to tell cout to print the char as if it were an integer. We do this by using a cast to have the compiler convert the char into an int before it is sent to cout:
char
chChar = 97;
cout << (
int
)chChar;
// will output 97, not 'a'
The (int) cast tells the compiler to convert chChar into an int, and cout prints ints as their actual values. We will talk more about casting in a few lessons.
The following program asks the user to input a character, then prints out both the character and it’s ASCII code:
#include "iostream";
int
main()
{
using
namespace
std;
char
chChar;
cout <<
"Input a keyboard character: "
;
cin >> chChar;
cout << chChar <<
" has ASCII code "
<< (
int
)chChar << endl;
}
Note that even though cin will let you enter multiple characters, chChar will only hold 1 character. Consequently, only the first character is used.
One word of caution: be careful not to mix up character (keyboard) numbers with actual numbers. The following two assignments are not the same
char
chValue =
'5'
;
// assigns 53 (ASCII code for '5')
char
chValue2 = 5;
// assigns 5
Escape sequences
C and C++ have some characters that have special meaning. These characters are called escape sequences. An escape sequence starts with a \, and then a following letter or number.
The most common escape sequence is ‘\n’, which can be used to embed a newline in a string of text:
#include <iostream>
int
main()
{
using
namespace
std;
cout <<
"First line\nSecond line"
<< endl;
return
0;
}
This outputs:
Another commonly used escape sequence is ‘\t’, which embeds a tab:
#include <iostream>
int
main()
{
using
namespace
std;
cout <<
"First part\tSecond part"
;
}
Which outputs:
- Even though the char
- My first EI paper, even though the acceptance rate is a little high。
- Grid - last row not removed when deleted even though reload
- He will come on time even though it rains.
- giuseppe sneakers even though he failed in betting
- SpringBoot WebSocket 跨域报错:Origin header value not allowed. Even though URL is allowed
- Even though a view can define a padding, it does not provide any support for margins. However, view
- even
- even
- It appears as though you do not have permission to view information for any of the hosts you request
- API EnumFontFamilies Though Mrsd1
- make though multi core
- Why we declare the constructors as protected or even private?
- non-zero winding number rule and the even-odd rule
- nagios报错 It appears as though you do not have permission to view information for any of the hosts yo
- Oakley Jawbone Matte Black Though
- calculate the length of char*
- Algorithm: Make all the odd numbers precede all the even numbers in an array.
- 第五讲:使用html5中的canvas动态画出物理学上平抛运动
- linux shm共享内存
- 第三周作业——冒泡排序和归并排序
- 基于redis通信协议的客户端RedisServer
- Android四大组件----Service
- Even though the char
- PHP中去掉HTML标签
- 第四周作业——图的表示
- hdu 1025 Constructing Roads In JGShining's Kingdom (LIS)
- In order to understand C++
- Android漫游记(2)---ELF可执行文件格式
- 黑马程序员——C语言的冒泡排序
- In previous chapters,
- 第五周作业——有向图邻接表表示及反向图构造