- char :1个字节
- char*(即指针变量): 8个字节
- short int : 2个字节
- int: 4个字节
- unsigned int : 4个字节
- float: 4个字节
- double: 8个字节
- long: 8个字节
- long long: 8个字节
- unsigned long: 8个字节
I/O
读入字符串可以用 scanf("%s")
或 getline()
读入字符且忽略空格可以用 scanf(" %c")
,注意 %c 前面的空格
文件的读取及写入:
1
2freopen("myfile.txt","r",stdin);
freopen ("myfile.txt","w",stdout);
Class and Header
It is not possible to create an instance of a class without invoking the constructor
STL的使用
指针的使用
类与结构体的使用
String
Split a String:
strtok
: Reference 1, Reference 21
2
3
4
5
6
7
8
9char str[] ="- This, a sample string.";
char * pch;
pch = strtok (str," ,.-");
while (pch != NULL)
{
printf ("%s\n",pch);
// Note to use NULL the next time you call strtok
pch = strtok (NULL, " ,.-");
}Convert a char array to integer:
sscanf(s, "%i", &imm)
automatically detects whethers
is an decimal142
or a hex0xa2c
(Note the0x
before a hex number is necessary)1
2
3char myarray[5] = {'-', '1', '2', '3', '\0'};
int i;
sscanf(myarray, "%d", &i);Read in both hex and dec number:
scanf("%i", )
Strings in C (char arrays) end with a terminating null-character
'\0'
其它问题
switch statement gives “a label can only be part of a statement…”:
switch
后要加分号;
1
2
3
4
5switch (option) {
case 'a': ;
...
case 'b': ;
... }