sizeof和strlen有什么区别 [英] what is difference between sizeof and strlen

查看:78
本文介绍了sizeof和strlen有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

sizeof(" abcd")和strlen(" abcd")之间有什么区别?为什么

两个函数在应用于同一个字符串时给出不同的输出

" abcd"。

我尝试了以下示例。

#include< stdio.h>

#include< string.h>

void main()

{

char * str1 =" abcd";

char str2 [] =" abcd";

printf("%d% d%d,sizeof(str1),sizeof(str2),sizeof(abcd));

printf("%d%d%d",strlen(str1),strlen (str2),strlen(" abcd"));

}

hello,
what is difference between sizeof("abcd") and strlen("abcd")? why
both functions gives different output when applied to same string
"abcd".
I tried following example for that.
#include <stdio.h>
#include <string.h>
void main()
{
char *str1="abcd";
char str2[]="abcd";
printf("%d %d %d",sizeof(str1),sizeof(str2),sizeof("abcd"));
printf("%d %d %d",strlen(str1),strlen(str2),strlen("abcd"));
}

推荐答案

ra*******@gmail.com 写道:
你好,
sizeof(abcd)和strlen(abcd)之间有什么区别?为什么


1. Sizeof是一元运营商。 Strlen是一个在
string.h中定义的函数。


2. Strlen查找第一个null(''\ 0'')并在找到时返回

数字

在空字符之前的字符。

两个函数在应用于相同的字符串时给出不同的输出


见上文。 < abcd"。
我尝试了以下示例。
#include< stdio.h>
#include< string.h>
void main()
应该是

int main(){
char * str1 =" abcd" ;;
char str2 [] =" abcd" ;;
除其他外,strlen()返回一个size_t对象,你应该使用

的'z''长度修饰符,然后是转换说明符d ,i,o,u,x

或X.


Sizeof再次产生一个对象的大小并使用
$ b的说明符$ b size_t

(和演员,我不确定是否需要,在打印之前)应该再次优惠
。 printf("%d%d%d",sizeof(str1),sizeof(str2),sizeof(" abcd"));
printf("%d%d%d",strlen(str1) ),strlen(str2),strlen(abcd));
}
hello,
what is difference between sizeof("abcd") and strlen("abcd")? why
1. Sizeof is an unary operator. Strlen is a function defined in
string.h.

2. Strlen looks for the first null(''\0'') and when found returns the
number
of characters that precede the null character.
both functions gives different output when applied to same string
See above. "abcd".
I tried following example for that.
#include <stdio.h>
#include <string.h>
void main() should be
int main() {
char *str1="abcd";
char str2[]="abcd"; Among other things, strlen() returns a size_t object, and you should
use
the `z'' length modifier, followed by a conversion specifier d,i,o,u, x
or X.

Sizeof again yields the size of an object and using the specifier for
size_t
(and a cast, I''m not sure if required, before printing) should again be
preferred. printf("%d %d %d",sizeof(str1),sizeof(str2),sizeof("abcd"));
printf("%d %d %d",strlen(str1),strlen(str2),strlen("abcd"));
}




做类似的事情

char str [12];

strcpy(str," hello");

printf("%zu vs%zu \ n",DIM(str),strlen (str));

你会知道的。



Do something like
char str[12];
strcpy(str, "hello");
printf("%zu vs %zu\n", DIM(str), strlen(str));
and you''ll know.


ra ******* @ gmail.com 写道:
你好,
sizeof(")之间的区别是什么? abcd")和strlen(" abcd")?为什么
这两个函数在应用于相同的字符串时会给出不同的输出
abcd。
我尝试了以下示例。
#include< stdio.h>
#include< string.h>
void main()
hello,
what is difference between sizeof("abcd") and strlen("abcd")? why
both functions gives different output when applied to same string
"abcd".
I tried following example for that.
#include <stdio.h>
#include <string.h>
void main()



^^^^

为什么这么多人发帖没有
b)查看常见问题

甚至

c)从中学习关于C的第一件事最基本的教科书?


不可能认真对待那些能够搞砸了一些基本的东西的人。


^^^^
Why do so many people post without
a) following the newsgroup
b) checking the FAQ
or even
c) learning the first thing about C from even the most basic textbook?

It is impossible to take seriously a question from someone who could
screw up something so basic.


Martin Ambuhl写道:
Martin Ambuhl wrote:
ra ******* @ gmail .com 写道:
你好,
sizeof(" abcd")和strlen(" abcd")有什么区别?为什么
这两个函数在应用于相同的字符串时会给出不同的输出
abcd。
我尝试了以下示例。
#include< stdio.h>
#include< string.h>
void main()
hello,
what is difference between sizeof("abcd") and strlen("abcd")? why
both functions gives different output when applied to same string
"abcd".
I tried following example for that.
#include <stdio.h>
#include <string.h>
void main()


^^^^
为什么这么多人发帖没有
a)关注新闻组< b)检查常见问题
甚至
c)从最基本的教科书中学习关于C的第一件事?

不可能认真对待一个问题有人能够搞砸一些如此基本的东西。


^^^^
Why do so many people post without
a) following the newsgroup
b) checking the FAQ
or even
c) learning the first thing about C from even the most basic textbook?

It is impossible to take seriously a question from someone who could
screw up something so basic.




确实有可能。如果你将返回类型设置为无效,大多数编译器都不会发出警告。他们将默默地插入代码以返回0

并继续。



Indeed it is possible. Most compilers won''t issue a warning if you set
the return type to void. They will silently insert code to return a 0
and just go on.


这篇关于sizeof和strlen有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆