What is the difference between strlen() and sizeof() string in C

2 Answers

0 votes
#include <stdio.h>
#include <string.h>

int main(int argc, char **argv)
{ 
	char s[] = "C Programming"; 
	
    printf ("%d\n", (int)sizeof(s)); // 13 + null = 14
    printf ("%d\n", (int)strlen(s)); // 13
	
	return 0; 
}   
 

/*
run:
 
14
13
 
*/

 



answered Jan 18, 2019 by avibootz
0 votes
#include <stdio.h>
#include <string.h>

int main(int argc, char **argv)
{ 
	char s[30] = "C Programming"; 
	
    printf ("%d\n", (int)sizeof(s)); // 30
    printf ("%d\n", (int)strlen(s)); // 13
	
	return 0; 
}   
 

/*
run:
 
30
13
 
*/

 



answered Jan 18, 2019 by avibootz
edited Jan 24, 2019 by avibootz

Related questions

1 answer 238 views
1 answer 202 views
202 views asked Jan 18, 2019 by avibootz
1 answer 119 views
1 answer 136 views
1 answer 153 views
...