strlen

Sizeof vs Strlen

﹥>﹥吖頭↗ 提交于 2019-11-26 13:01:48
问题 #include \"stdio.h\" #include \"string.h\" main() { char string[] = \"october\"; // october is 7 letters strcpy(string, \"september\"); // september is 9 letters printf(\"the size of %s is %d and the length is %d\\n\\n\", string, sizeof(string), strlen(string)); return 0; } Output: the size of september is 8 and the length is 9 Is there something wrong with my syntax or what? 回答1: sizeof and strlen() do different things. In this case, your declaration char string[] = "october"; is the same as

Add … if string is too long PHP

蓝咒 提交于 2019-11-26 05:21:09
问题 I have a description field in my MySQL database, and I access the database on two different pages, one page I display the whole field, but on the other, I just want to display the first 50 characters. If the string in the description field is less than 50 characters, then it won\'t show ... , but if it isn\'t, I will show ... after the first 50 characters. Example (Full string): Hello, this is the first example, where I am going to have a string that is over 50 characters and is super long, I

解析sizeof与strlen的用法

筅森魡賤 提交于 2019-11-26 03:30:59
一、sizeof sizeof(…)是运算符,在头文件中typedef为unsigned int,其值在编译时即计算好了,参数可以是数组、指针、类型、对象、函数等。 它的功能是:获得保证能容纳实现所建立的最大对象的字节大小。 由于在编译时计算,因此sizeof不能用来返回动态分配的内存空间的大小。实际上,用sizeof来返回类型以及静态分配的对象、结构或数组所占的空间,返回值跟对象、结构、数组所存储的内容没有关系。 具体而言,当参数分别如下时,sizeof返回的值表示的含义如下: 数组——编译时分配的数组空间大小; 指针——存储该指针所用的空间大小(存储该指针的地址的长度,是长整型,在32位系统是4,在64系统是8); 类型——该类型所占的空间大小; 对象——对象的实际占用空间大小; 函数——函数的返回类型所占的空间大小。函数的返回类型不能void。 二、strlen strlen是函数,要在运行时才能计算。参数必须是字符型指针。当数组名作为参数传入时,实际上数组就退化成指针了。 它的功能是:返回字符串的长度。 该字符串可能是自己定义的,也可能是内存中随机的,该函数实际完成的功能是从代表该字符串的第一个地址开始遍历,直到遇到结束符NULL(’\0’)。返回的长度大小不包括’\0’ ! 三、举例: char arr [ 15 ] = "wang\0miao" ; int len

C语言中的strlen和sizeof的区别

纵饮孤独 提交于 2019-11-25 19:18:31
@strlen和sizeof的区别 #strlen是求得的是字符串的长度,不包括结尾标志符'\0'的。 #sizeof是计算字符串占的总内存空间,包括结尾字符'\0'是被包括在里面的。 @例子1: #char s[] = "China"; #printf("%d %d\n", strlen(s), sizeof(s)); #输出5 6 @例子2: #char s[20] = "China"; #printf("%d %d\n", strlen(s), sizeof(s)); #输出5 20 来源: CSDN 作者: glfxml 链接: https://blog.csdn.net/glfxml/article/details/103238136