
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference Between strlen and sizeof for String in C
strlen()
The function strlen() is a predefined function in C language. This is declared in “string.h” header file. It is used to get the length of array or string.
Here is the syntax of strlen() in C language,
size_t strlen(const char *string);
Here,
string − The string whose length is to be calculated.
Here is an example of strlen() in C language,
Example
#include <stdio.h> #include <string.h> int main () { char s1[10] = "Hello"; int len ; len = strlen(s1); printf("Length of string s1 : %d
", len ); return 0; }
Output
Length of string s1 : 10
In the above example, a char type array s1 is initialized with a string and a variable len is stroing the length of s1.
char s1[10] = "Hello"; int len ; len = strlen(s1);
sizeof()
The function sizeof() is a unary operator in C language and used to get the size of any type of data in bytes.
Here is the syntax of sizeof() in C language,
sizeof( type );
Here,
type − Any type or data type or variable, you want to calculate the size of.
Here is an example of sizeof() in C language,
Example
#include <stdio.h> int main() { int a = 16; printf("Size of variable a : %d
",sizeof(a)); printf("Size of int data type : %d
",sizeof(int)); printf("Size of char data type : %d
",sizeof(char)); printf("Size of float data type : %d
",sizeof(float)); printf("Size of double data type : %d
",sizeof(double)); return 0; }
Output
Size of variable a : 4 Size of int data type : 4 Size of char data type : 1 Size of float data type : 4 Size of double data type : 8
Advertisements