• Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard
CSEstack

What do you want to Learn Today?

  • Programming
    • Tutorial- C/C++
    • Tutorial- Django
    • Tutorial- Git
    • Tutorial- HTML & CSS
    • Tutorial- Java
    • Tutorial- MySQL
    • Tutorial- Python
    • Competitive Coding Challenges
  • CSE Subject
    • (CD) Compiler Design
    • (CN) Computer Network
    • (COA) Computer Organization & Architecture
    • (DBMS) Database Management System
    • (DS) Data Structure
    • (OS) Operating System
    • (ToA) Theory of Automata
    • (WT) Web Technology
  • Interview Questions
    • Interview Questions- Company Wise
    • Interview Questions- Coding Round
    • Interview Questions- Python
    • Interview Questions- REST API
    • Interview Questions- Web Scraping
    • Interview Questions- HR Round
    • Aptitude Preparation Guide
  • GATE 2022
  • Linux
  • Trend
    • Full Stack Development
    • Artificial Intelligence (AI)
    • BigData
    • Cloud Computing
    • Machine Learning (ML)
  • Write for Us
    • Submit Article
    • Submit Source Code or Program
    • Share Your Interview Experience
  • Tools
    • IDE
    • CV Builder
    • Other Tools …
  • Jobs

Difference Between strlen and sizeof in C [Code Explained in Detail]

Aniruddha Chaudhari/17073/2
C / C++Code

Table of Contents

  • sterlen() in C
  • sizeof() in C
  • How to find the length of the string using sizeof() operator in C?
  • How to find number of elements in the array using sizeof Operator()?
  • Why is the size of the string one higher than the length of the same string?
  • strlen() Vs sizeof()
  • Difference Between strlen and sizeof in C Programming:

sterlen() in C

The strlen() is a predefined function in C programming languages. It returns the number of characters in the string. When we pass the pointer of the string to the function, it goes through the complete string, until it finds the NULL-terminated character.

  • It counts all the characters present in the string.
  • This function is defined in the string.h header file.
  • You need to include string.h header file before using this function in your C code.

Example Code for strlen():

#include <stdio.h>
#include <string.h>
int main()
{
  char str1[] = "CSEstack Programming";
  printf("Length of the string str1 is %d.", strlen(str1));
  return 0;
}

Output:

Length of the string str1 is 20.

From the above output of the program, you can see, the function also counts the space and other special characters in the string.

Use: The strlen() function is very useful for solving many of the coding questions. For example: check if the two given strings are anagram, where you have to find the length of both strings. If the two strings have different length, they can not be anagrams.

sizeof() in C

The sizeof() is one of the most useful operators in C programming. As it takes one input operand, it is the unary operator.

There are different data-types in C programming. You can pass any data type object as operand including primitive types (such as int, float, char…) and compound types objects (such as structure, union)

Return Type: sizeof() returns the total size assigned to the given operand. And the output data type is represented as size_t.

If we run the same program as above to print the size of the same string, here is an output.

#include <stdio.h>
int main()
{
  char str1[] = "CSEstack Programming";
  printf("Size of the string str1 is %d.", sizeof(str1));
  return 0;
}

Output:

Size of the string str1 is 21.

Note: Here we are considering the size of the character is one byte. It can be different based on the system and compiler designing.

You can also pass the data type to the sizeof operator.

#include <stdio.h>
int main()
{
  printf("Size of integere is %d.", sizeof(int));
  printf("Size of float is %d.", sizeof(float));
  printf("Size of character is %d.", sizeof(char));
  return 0;
}

Remember, the output of the above program may be different based on the system and compiler that you are using.

How to find the length of the string using sizeof() operator in C?

If you are not sure about the size of the character, you can simply divide the size of the string (or array) by character. You will get the length of the string.

#include <stdio.h>
#include <string.h>
int main()
{
  char str1[] = "CSEstack Programming";
  printf("Size of the string str1 is %d.", sizeof(str1)/sizeof(char));
  return 0;
}

How to find number of elements in the array using sizeof Operator()?

In the case of an integer array, you need to divide the size of the array by the size of integer to get the number of elements in the array.

#include <stdio.h>
#include <string.h>
int main()
{
  char arr[] = [24,5,6,7,3,54,7];
  printf("Number of elements in the array:", sizeof(arr)/sizeof(int));
  return 0;
}

Output:

Number of elements on the array: 7

Why is the size of the string one higher than the length of the same string?

When you declare a string, ‘\0’ NULL-terminated character will be added as the last character as a delimiter to represent the end of the string.

Comparing strlen and sizeof:

Function strlen() represents the number of characters in the string excluding delimiter '\0'. Whereas, operator sizeof() returns the memory size allocated to save the string. It also counts the memory size requires to save the character ‘\0'.

For an empty string, the length of the string is zero but the size of the string is one.

#include <stdio.h>
#include <string.h>
int main()
{
  char str1[] = "";
  printf("Length of the string str1 is %d.", strlen(str1));
  printf("\nSize of the string str1 is %d.", sizeof(str1));
  return 0;
}

Output:

Length of the string str1 is 0.
Size of the string str1 is 1.

So, the size of the string will be one higher than the length of the string.

Usage of sizeof() Operator in C Programming:

The sizeof() operator is mostly used for memory management calls like malloc(), memset(), memcpy().

strlen() Vs sizeof()

Let’s compare these two builtin functions in C programming.

Difference Between strlen and sizeof in C Programming:

  • Function/Operation/Evaluation:
    The strlen() returns you the length of the string stored in the array, however, sizeof() returns the total allocated size assigned to the array.
  • Difference in Return Value:
    If you pass the same string as input to both functions strlen() and sizeof(), the size of the string will be one higher than the length of the string. (Considering the size of the character in one byte.)
  • Dependency:
    Size of the string also depends on your system and design of your C compiler.
  • Data Type Support:
    Unlike strlen(), we can use the function sizeof() with any datatypes.
  • Return Type:
    The return type of the strlen() is the integer, whereas return type of the sizeof() operator is size_t.

This is all about the difference between strlen() and sizeof() in C / C++ programming. If you have any queries, let’s discuss this in the comment section.

sizeofstrlen
Aniruddha Chaudhari
I am complete Python Nut, love Linux and vim as an editor. I hold a Master of Computer Science from NIT Trichy. I dabble in C/C++, Java too. I keep sharing my coding knowledge and my own experience on CSEstack.org portal.

Your name can also be listed here. Got a tip? Submit it here to become an CSEstack author.

Comments

  • Reply
    Alan Corey
    February 21, 2020 at 6:05 am

    My search (Duckduckgo) led me to believe this page talked about sizeof() and strlen() relative to Unicode.

    If you want to malloc/calloc memory to store an array of char you want sizeof().

    But when you want to copy the data in you want strncpy() so you want a count of chars so strlen(). I think. It’s tricky, there’s a right way to do it but I don’t do it often so I forgot.

    • Reply
      Aniruddha Chaudhari
      February 21, 2020 at 7:18 pm

      Thanks, Alan for sharing your thought. This tutorial is to oppose misconception beginners have about strlen() and sizeof(). I hope they don’t think the same after reading this article.

Leave a Reply Cancel reply

C Programming

  1. C- Introduction
  2. C- Compile & Execute Program
  3. C- Data Types
  4. C- if-else statement
  5. C- While, do-while, for loop
  6. C- Array
  7. C- Function (Types/Call)
  8. C- strlen() vs sizeof()
  9. C- Nested Switch Statement
  10. C- Recursion
  11. C- Dynamic Programming
  12. C- Storage Classes
  13. C- Creating Header File
  14. C- Null Pointer
  15. C- Stack and Queue
  16. C- Implement Stack using Array
  17. C- Implement Linked List in C
  18. C- File Handling
  19. C- Makefile Tutorial

Object Oriented Concepts in C++

  • C++: C vs OOPs Language
  • C++: Introduction to OOPs Concepts
  • C++: Inheritance

Sorting Algorithms

  • Different Types of Sorting Algo
  • Selection Sort
  • Bubble Sort
  • Quick Sort

Programming for Practice

  1. Online C/C++ Compiler

String Handling:

  1. Remove White Spaces from String
  2. Implement strstr Function in C
  3. Convert String to Int – atoi()
  4. Check if String is Palindrome
  5. Check if Two Strings are Anagram
  6. Split String in using strtok_r()
  7. Undefined reference to strrev

Array:

  1. Check if Array is Sorted

Bit Manipulation:

  1. Count Number of 1’s in Binary

Linked List:

  1. Reverse a Linked List Elements

Number System:

  1. Program to Find 2’s Complement
  2. Convert Decimal to Binary in C

Tricky Questions:

  1. Add Two Numbers without Operator
  2. Find Next Greater Number
  3. Swap values without temp variable
  4. Print 1 to 100 without Loop

Interview Coding Questions

  • 50+ Interview Coding Questions

© 2022 – CSEstack.org. All Rights Reserved.

  • Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard