Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8437367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:24:37+00:00 2026-06-10T07:24:37+00:00

In C++ I am trying to get size of a string say gibbs; When

  • 0

In C++ I am trying to get size of a string say “gibbs”;

When I am using sizeof function it is returning me size less then one of actual size.

I have below code :

 string s = "gibbs";
 cout << sizeof(s) <<endl;

and output is : 4. I guess it should be 5.

How to fix this issue. We can add +1 always to return of sizeof but will that be perfect solution ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T07:24:38+00:00Added an answer on June 10, 2026 at 7:24 am

    sizeof(s) gives you the size of the object s, not the length of the string stored in the object s.

    You need to write this:

    cout << s.size() << endl;
    

    Note that std::basic_string (and by extension std::string) has a size() member function. std::basic_string also has a length member function which returns same value as size(). So you could write this as well:

    cout << s.length() << endl;
    

    I personally prefer the size() member function, because the other containers from the standard library such as std::vector, std::list, std::map, and so on, have size() member functions but not length(). That is, size() is a uniform interface for the standard library container class templates. I don’t need to remember it specifically for std::string (or any other container class template). The member function std::string::length() is a deviation in that sense.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry beginner here. I'm trying to get the string size from the cin function,
I'm trying to get the size on disk of a file in iOS using
I am trying to use my own function to get the file size from
I have a (hopefully silly) iPhone programming question. I'm trying to get a string
I'm trying to get the path of a device by using the SetupDiGetDeviceInterfaceDetail() function,
I'm trying to get the size of my UIView after the orientation changes. In
I am trying to get the font size of selected range in a html
I'm trying to get the image.size.h and image.size.w of the UIImage inside of an
I am trying to get the maximum texture size limit in Android for OpenGL
I'm trying to get every widget to scale with change in window size. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.