[英]String in c "Hello world"?
I dont understand how the word "word" is printed in line 5. Can somebody explain to me?我不明白第 5 行中的 "word" 一词是如何打印的。有人可以向我解释一下吗?
#include <stdio.h>
int main(void) {
char str[50] = "hello\0 worl\bd";
printf("\n %s ",str);
printf("%s \n",str+str[4]-*str);
return 0;
}
So, step-by-step:所以,一步一步:
"str" points to your string "hello\\0 worl\\bd"
, which is actually "hello\\0 word"
(since \\b deletes the previous character) "str"指向你的字符串
"hello\\0 worl\\bd"
,它实际上是"hello\\0 word"
(因为 \\b 删除了前一个字符)
*str = is the the "content" of your char pointer, which means the first character of your string, that is "h" *str =是 char 指针的“内容”,表示字符串的第一个字符,即“h”
str[4] = is the (4+1)th character of str, that is 'o'
str[4] =是str 的第 (4+1) 个字符,即
'o'
str[4] - *str = 'o'-'h'
= 7 (but why is it 7? 'h' has an ASCII character value of 104 and 'o' a value of 111) str[4] - *str =
'o'-'h'
= 7 (但为什么是 7?'h' 的 ASCII 字符值为 104,'o' 的值为 111)
str + 7 = str[7] str + 7 = str[7]
So, you are basically printing the string starting at index:7 of your initial string.因此,您基本上是打印从初始字符串的 index:7 开始的字符串。
Hence: 'word'
;)因此:
'word'
;)
First of All the String as under:首先字符串如下:
0 => h
1 => e
2 => l
3 => l
4 => o
5 => \0
6 => (space bar)
7 => w
8 => o
9 => r
10 => l
11 => \b
12 => d
now your command is:现在你的命令是:
printf("%s \n",str+str[4]-*str);
C has done the following thing C做了以下事情
str => point of starting printing
str[4] as above is o
*str as above is h
Thus o - h = 7 [i.e. ascii value 111 - 104]
printing would starting from character 7 i.e. [str+7]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.