strlen

C语言中strlen和sizeof

廉价感情. 提交于 2019-11-28 09:28:59
定义 sizeof是C/C++中的一个运算符(关键字),计算对象或者类型在内存中所占用的字节数。 strlen是C语言中的库函数,计算字符串长度,不包括尾0。 引用头文件#include<stdio.h>。 库函数原型:size_t strlen(const char *s) 注:sizeof是关键字,不是函数。strlen是函数。 举例 char test[20] = “hello”; char *test1 = “hello!”; char test2[] = “hello!!”; strlen(test) //5 strlen计算的是字符串的实际长度,不包括尾0,以第一个’\0’为结束符。 sizeof(test) //20 定义的char型数组,占用了20个字节。 strlen(test1) //6 trlen计算的是字符串的实际长度,不包括尾0,以第一个’\0’为结束符。 sizeof(test1) // 8 计算了指针类型所在的字节数,取决于32/64位机,32位占4字节,64位占8字节。 strlen(test2) //7 strlen计算的是字符串的实际长度,不包括尾0,以第一个’\0’为结束符。 sizeof(test2) // 8 定义的char型数组,占用了8个字节,其中包括了’\0’为结束符。 strlen的返回值为unsigned int类型,为无符号。

C语言中关于 strlen 和 sizeof 的用法及区别(含例题及解析)

本秂侑毒 提交于 2019-11-28 09:28:42
一、前言 首先我们需要知道的是,sizeof既是一个单目操作符,也是一个关键字,其作用是求操作数的 类型长度 (以字节为单位)。 而strlen是一个字符串操作函数,是一个 参数为指针类型 返回值为size_t(unsigned int)的函数,求的是 字符串 的长度。 所以现在我们知道 sizeof是一个求操作数类型长度的操作符(关键字),而strlen是一个求字符串长度的字符串操作函数 。 二、sizeof和strlen的用法 2.1 sizeof操作符在简单变量中的用法 int a = 10; char c = 'c'; printf("%d\n",sizeof(a)); //答案是4(操作数a的类型为整型,32位机器中占4个字节,64位机器中占8个字节) printf("%d\n",sizeof(int)); //答案是4 printf("%d\n",sizeof a); //答案是4(求变量的大小时可以去掉括号) printf("%d\n",sizeof int); //错误(求类型的大小时不能去掉括号) printf("%d\n",sizeof(c)); //答案是1(操作数c的类型为字符型,占1个字节) printf("%d\n",sizeof(char)); //答案是1 printf("%d\n",sizeof c); //答案是1(求变量的大小时可以去掉括号)

C语言中strlen()和sizeof()的区别

假如想象 提交于 2019-11-28 09:28:21
C语言中strlen()和sizeof()的区别 先看下面的一个程序 # include <stdio.h> # include <string.h> # define PRAISE "My name is Bai Jiangwei" int main ( void ) { char name [ 40 ] = "BaiJiangWei" ; printf ( "%u %u \n" , strlen ( name ) , sizeof ( name ) ) ; printf ( "%u %u \n" , strlen ( PRAISE ) , sizeof ( PRAISE ) ) ; } 程序的结果如下 11 40 23 24 Press any key to continue 结论 对于一个字符串变量,strlen()的结果是字符串本身的长度,在本例中BaiJiangWei长度为11;而sizeof(name)的结果是该字符串变量在定义时的长度,在本例中char name[40],故sizeof(name)结果为40 对于一个字符串常量,我们定义时并没有定义其长度,所以strlen()的结果就是字符串本身长度,而sizeof()的结果是strlen()+1,多出来的长度1,实际上就是\0 来源: CSDN 作者: 纪晓岚爱coder 链接: https://blog.csdn

C语言中的strlen与sizeof的区别

我怕爱的太早我们不能终老 提交于 2019-11-28 09:28:03
sizeof与strlen是有着本质的区别,sizeof是求数据类型所占的空间大小,而strlen是求字符串的长度,字符串以/0结尾。区别如下: (1) sizeof是一个C语言中的一个单目运算符,而strlen是一个函数,用来计算字符串的长度。 (2)sizeof求的是数据类型所占空间的大小,而strlen是求字符串的长度 实例1: printf("char=%d/n",sizeof(char)); //1 printf("char*=%d/n",sizeof(char*)); //4 printf("int=%d/n",sizeof(int)); //4 printf("int*=%d/n",sizeof(int*)); //4 printf("long=%d/n",sizeof(long)); //4 printf("long*=%d/n",sizeof(long*)); //4 printf("double=%d/n",sizeof(double)); //8 printf("double*=%d/n",sizeof(double*)); //4 可以看到,char占1个字节,int占4个字节,long点4个字节,而double占8个字节。但 char*,int*,long*,double*都占4个字节的空间。 这是为什么呢? 在C语言中,char,int,long

关于Tinyhttpd最全注释解析

我是研究僧i 提交于 2019-11-28 08:37:39
#include <stdio.h> #include <sys/socket.h> #include <sys/types.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> #include <ctype.h> #include <strings.h> #include <string.h> #include <sys/stat.h> #include <pthread.h> #include <sys/wait.h> #include <stdlib.h> #include <stdint.h> #include "/usr/include/mysql/mysql.h" #define ISspace(x) isspace((int)(x)) #define SERVER_STRING "Server: zkphttpd/0.1.0\r\n" #define STDIN 0 #define STDOUT 1 #define STDERR 2 void search_mysql(int, char*); void accept_request(void *); void bad_request(int); void cat(int, FILE *); void cannot_execute

攻防世界--logmein

自闭症网瘾萝莉.ら 提交于 2019-11-28 07:45:31
测试文件: https://adworld.xctf.org.cn/media/task/attachments/a00849bb514c413f8a6526f6bb56c628 1.准备 得到信息 64位文件 obj文件 2.IDA打开 将main函数转换为C语言代码 1 void __fastcall __noreturn main(__int64 a1, char **a2, char **a3) 2 { 3 size_t v3; // rsi 4 int i; // [rsp+3Ch] [rbp-54h] 5 char s[36]; // [rsp+40h] [rbp-50h] 6 int v6; // [rsp+64h] [rbp-2Ch] 7 __int64 v7; // [rsp+68h] [rbp-28h] 8 char v8[8]; // [rsp+70h] [rbp-20h] 9 int v9; // [rsp+8Ch] [rbp-4h] 10 11 v9 = 0; 12 strcpy(v8, ":\"AL_RT^L*.?+6/46"); 13 v7 = 28537194573619560LL; 14 v6 = 7; 15 printf("Welcome to the RC3 secure password guesser.\n", a2, a3); 16

基于Linux购票服务器

不羁的心 提交于 2019-11-28 07:12:25
/date 航班信息文件 /register 用户注册信息文件 /user 用户购票信息文件 server #include "head.h" char chars[10]; char sendbuf[1024]; struct list_node{ char name[10];//用户名 char passwd[10];//密码 char number[10];//航班号 char staddress[10];//起点站 char arraddress[10];//终点站 char date[10];//班期 char type[10];//机型 char stime[10];//起飞时间 char price[10];//票价 char aggregate[10];//总金额 struct list_head list;//内核链表指针域 }; struct list_node_ip{ int connfd; char ip[50]; pthread_t tid; struct list_head list; }; struct list_node_ip *head = NULL; //初始化内核链表头 struct list_node_ip *init_list_head_ip(struct list_node_ip *head) { head = (struct list

why is -1>strlen(t) true in C? [duplicate]

匆匆过客 提交于 2019-11-28 04:20:39
问题 This question already has answers here : void main() { if(sizeof(int) > -1) printf(“true”); else printf(“false”); ; [duplicate] (3 answers) Why sizeof(int) is not greater than -1? [duplicate] (2 answers) Closed 4 years ago . Working on this little piece of code in VS2013, but for some reason it doesn't print.it seems that -1>strlen(str) Anyone got an idea what i'm doing wrong char *str="abcd"; if(-1<strlen(str)) printf("The size of the string is %d", strlen(str)); return 0; 回答1: Anyone got an

Is strlen on a string with uninitialized values undefined behavior?

情到浓时终转凉″ 提交于 2019-11-28 03:54:34
问题 strlen returns the number of characters that precede the terminating null character. An implementation of strlen might look like this: size_t strlen(const char * str) { const char *s; for (s = str; *s; ++s) {} return(s - str); } This particular implementation dereferences s , where s may contain indeterminate values. It's equivalent to this: int a; int* p = &a; *p; So for example if one were to do this (which causes strlen to give an incorrect output): char buffer[10]; buffer[9] = '\0';

Will strlen be calculated multiple times if used in a loop condition?

半腔热情 提交于 2019-11-28 03:37:30
I'm not sure if the following code can cause redundant calculations, or is it compiler-specific? for (int i = 0; i < strlen(ss); ++i) { // blabla } Will strlen() be calculated every time when i increases? Yes, strlen() will be evaluated on each iteration. It's possible that, under ideal circumstances, the optimiser might be able to deduce that the value won't change, but I personally wouldn't rely on that. I'd do something like for (int i = 0, n = strlen(ss); i < n; ++i) or possibly for (int i = 0; ss[i]; ++i) as long as the string isn't going to change length during the iteration. If it might