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 8476919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:18:41+00:00 2026-06-10T18:18:41+00:00

I have this code say: std::string str(ashish); str.append(\0\0); printf(%d, str.length()); It is printing 6

  • 0

I have this code say:

 std::string str("ashish");  
 str.append("\0\0");  
 printf("%d", str.length());  

It is printing 6 but if I have this code

 std::string str("ashish");  
 str.append("\0\0",2);  
 printf("%d", str.length());  

it is printing 8 ! Why?

  • 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-10T18:18:43+00:00Added an answer on June 10, 2026 at 6:18 pm

    It’s because str.append("\0\0") uses the null character to determine the end of the string. So “\0\0” is length zero. The other overload, str.append("\0\0",2), just takes the length you give it, so it appends two characters.

    From the standard:

      basic_string&
       append(const charT* s, size_type n);
    

    7         Requires: s points to an array of at least n elements of charT.

    8         Throws: length_error if size() + n > max_size().

    9         Effects: The function replaces the string controlled by *this with a string of length size() + n whose first size() elements are a copy of the original string controlled by *this and whose remaining elements are a copy of the initial n elements of s.

    10         Returns: *this.

      basic_string& append(const charT* s);
    

    11         Requires: s points to an array of at least traits::length(s) + 1 elements of charT.

    12         Effects: Calls append(s, traits::length(s)).

    13         Returns: *this.

                                                                   — [string::append] 21.4.6.2 p7-13

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

Sidebar

Related Questions

Say I have the code: std::string str = random; function(str); void function (std::string str)
Lets say I have this code: public void MyMethod(string Data, List<string> InputData) { //I
I have code like this: class MapIndex { private: typedef std::map<std::string, MapIndex*> Container; Container
Say I have the following code: #include <string> using namespace std; string GetPath(const string
Lets say I have this code: <?php class hello { var $greeting = hello;
Say I have this code - public interface ParentInterface1 { public List<? extends ChildInterface1>
Say I have this code: #import <UIKit/UIKit.h> @interface MyView : UIView @end @implementation MyView
Lets say we have this code: bool KeepGoing = true; DataInThread = new Thread(new
Say I have this code in my page: <script language=javascript> $(document).ready(function() { $(.test).click(function() {
Say I have this sample code I'm writing for a Python lesson I'm holding:

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.