
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
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
- Related Articles
- Difference between strlen() and sizeof() for string in C Program
- What's the difference between sizeof and alignof?
- Difference between String and StringBuilder in C#
- What is the difference between String and string in C#?
- Difference between string and char[] types in C++
- Sizeof operator in C
- Why is not sizeof for a struct equal to the sum of sizeof of each member in C/C++?
- Difference between C++ string constants and character constants
- Difference between Relational operator(==) and std::string::compare() in C++
- Why isn't sizeof for a struct equal to the sum of sizeof of each member in C/C++?
- What is strlen function in C language?
- Difference between String and StringBuffer.
- Difference between String buffer and String builder in Java
- Difference between string and StringBuffer in Java.
- What is sizeof operator in C++?

Advertisements