首页 > 试题广场 >

下列选项中,选项()是该段代码的正确输出。

[单选题]
有如下一段代码,下列选项中,选项()是该段代码的正确输出。
#include<stdio.h>
#include<string.h>
#define PRAISE "NowCoder is benefit to everyone concerned."

int main(){
    printf("The phrase of PRAISE has %zd letters ",strlen(PRAISE));
    printf("and occupies %zd memory cells.",sizeof(PRAISE));
    return 0;
}
  • The phrase of PRAISE has 42 letters and occupies 42 memory cells.
  • The phrase of PRAISE has 42 letters and occupies 43 memory cells.
  • The phrase of PRAISE has 43 letters and occupies 42 memory cells.
  • The phrase of PRAISE has 43 letters and occupies 43 memory cells.
1. strlen 是函数,sizeof 是运算符。 2. strlen 测量的是字符的实际长度,以'\0' 结束。而sizeof 测量的是字符的分配大小。
发表于 2022-01-16 08:24:41 回复(2)
使用strlen可得到字符串中包括空格和标点符号在内的字符数。
使用sizeof运算符,得到的数会更大,因为它会把字符串末尾不可见的空字符也计算在内(比如\0")。
答案:B
发表于 2022-07-25 12:48:57 回复(0)
sizeof用来计算字符串在内存中存储大小 strlen计算字符串有效长度
发表于 2022-10-18 09:58:55 回复(0)