answersLogoWhite

0


Best Answer

The sizeof operator returns the total size, in bytes, of the given operand, whereas the strlen function returns the number of characters in the argument up to but not including the first null-terminator.

Consider a character buffer allocated 50 bytes to which you assign the string "Hello world". The sizeof operator will return 50, but the strlen function returns 11.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Difference between sizeof and strlen
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is the output for the following C program include include void main(void) char str1 Hello world char str2 Hello world printf (d d d d sizeof(str1) sizeof(str2)strlen(str1) strlen(str2))?

What is the output for the following C program? include<stdio.h> int main (void) { char* str1 = "Hello world"; char str2[] = "Hello world"; printf ("%d, %d, %d, %d\n", sizeof(str1), sizeof(str2), strlen(str1), strlen(str2)); return 0; } Assuming sizeof (char*) is 4 bytes (implementation-defined) the output will be: 4, 13, 12, 12


Why use length function to find length of primitive data type array object?

There is no length function in C. You may have thought of sizeof or strlen. Perhaps.


How do you write c plus plus program to display size of the each data type?

The core if it would contain lines like these: printf ("sizeof (char)=%d\n" "sizeof (short)=%d\n" "sizeof (int)=%d\n" "sizeof (long)=%d\n" "sizeof (long long)=%d\n" "sizeof (size_t)=%d\n" "sizeof (void *)=%d\n" "sizeof (ptrdiff_t)=%d\n" "sizeof (va_list)=%d\n" "sizeof (intptr_t)=%d\n" , (int)sizeof (char) , (int)sizeof (short) , (int)sizeof (int) , (int)sizeof (long) , (int)sizeof (long long) , (int)sizeof (size_t) , (int)sizeof (void *) , (int)sizeof (ptrdiff_t) , (int)sizeof (va_list) , (int)sizeof (intptr_t) );


Difference between array and union?

All the members of the structure can be accessed at once,where as in an union only one member can be used at a time. Another important difference is in the size allocated to a structure and an union. for eg: struct example { int integer; float floating_numbers; } the size allocated here is sizeof(int)+sizeof(float); where as in an union union example { int integer; float floating_numbers; } size allocated is the size of the highest member. so size is=sizeof(float);


Write a c program to print the sizes and ranges of different data types in c?

#include<stdio.h> #include<conio.h> void main() { float f1,*Ptr1,*Ptr2; ptr1 = &fl; ptr2 = (&fl+1); printf("%u",(char *)ptr2-(char *)ptr1); getch(); }

Related questions

What is the output for the following C program include include void main(void) char str1 Hello world char str2 Hello world printf (d d d d sizeof(str1) sizeof(str2)strlen(str1) strlen(str2))?

What is the output for the following C program? include<stdio.h> int main (void) { char* str1 = "Hello world"; char str2[] = "Hello world"; printf ("%d, %d, %d, %d\n", sizeof(str1), sizeof(str2), strlen(str1), strlen(str2)); return 0; } Assuming sizeof (char*) is 4 bytes (implementation-defined) the output will be: 4, 13, 12, 12


Why use length function to find length of primitive data type array object?

There is no length function in C. You may have thought of sizeof or strlen. Perhaps.


How do you write c plus plus program to display size of the each data type?

The core if it would contain lines like these: printf ("sizeof (char)=%d\n" "sizeof (short)=%d\n" "sizeof (int)=%d\n" "sizeof (long)=%d\n" "sizeof (long long)=%d\n" "sizeof (size_t)=%d\n" "sizeof (void *)=%d\n" "sizeof (ptrdiff_t)=%d\n" "sizeof (va_list)=%d\n" "sizeof (intptr_t)=%d\n" , (int)sizeof (char) , (int)sizeof (short) , (int)sizeof (int) , (int)sizeof (long) , (int)sizeof (long long) , (int)sizeof (size_t) , (int)sizeof (void *) , (int)sizeof (ptrdiff_t) , (int)sizeof (va_list) , (int)sizeof (intptr_t) );


Difference between array and union?

All the members of the structure can be accessed at once,where as in an union only one member can be used at a time. Another important difference is in the size allocated to a structure and an union. for eg: struct example { int integer; float floating_numbers; } the size allocated here is sizeof(int)+sizeof(float); where as in an union union example { int integer; float floating_numbers; } size allocated is the size of the highest member. so size is=sizeof(float);


Write a c program to print the sizes and ranges of different data types in c?

#include<stdio.h> #include<conio.h> void main() { float f1,*Ptr1,*Ptr2; ptr1 = &fl; ptr2 = (&fl+1); printf("%u",(char *)ptr2-(char *)ptr1); getch(); }


How is the 'sizeof' keyword in a c program used to determine the amount of space of a variable?

It is 'sizeof'. Example: printf ("sizeof(int)=%d\n", sizeof (int));


What is a sizeof function in C programming?

Sizeof is an example.


How do you find last char in c string?

(strlen(str) == 0) ? '\0' : str[strlen(str)-1]


How do you find the length of the string using strlen?

int len = strlen("some string"); // len has value 11


Explain the role of sizeof operator in java?

There is no sizeOf() operator in Java.


What will be the results of the operations sizeof 3 within single quotes and sizeof 3 within double quotes and sizeof 3 and why?

sizeof('3') returns 1 - its the size of a char sizeof("3") returns 2 - its the size of two chars - '3' and '\0' sizeof(3) returns 4 - its the size of an int (in a 32 bit system)


Length of a string using strlen?

char mystring[] = "This is a string"; int mystringlength = strlen(mystring); /* mystringlength is now 16 */