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 226 views
1 answer 105 views
1 answer 122 views
1 answer 141 views
1 answer 121 views
...