- 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 Program
As we know that in programming string can be defined as the collection of characters. Now for the requirement of finding how many characters are being used to create a string, C provides two approaches which are strlen() and sizeof().
As mentioned in above point both of these methods are used to find out the length of target operand but on the basis of their internal implementation following are some basic differences between both.
|1||Definition||strlen() is a predefined function defined in a Header file named string.h in C.||On other hand sizeof() is a Unary operator and not a predefined function.|
|2||Implementation||strlen is internally implemented as it primarily counts the numbers of characters in a string excluding null values, i.e returns the length of null terminating string.||While sizeof is implemented in such way that it calculates actual size of any type of data (allocated) in bytes (including the null values).|
|3||Null handling||strln excludes null and do not include it in the total computation of length of string.||On other hand sizeof doesn't care about the values of variable and compute actual size of any type of data (allocated) in bytes (including the null values).|
- Related Articles
- Difference between strlen() and sizeof() for string in C
- What's the difference between sizeof and alignof?
- Difference between String and StringBuilder in C#
- Write a C program demonstrating strlen library function
- What is the difference between String and string in C#?
- Difference between string and char types in C++
- C Program for Difference between sums of odd and even digits?
- 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 Structure and Union in C Program
- C Program for the Difference between sums of odd and even digits?
- 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++?
- Difference between Abstract Class and Interface in C# Program