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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:48:20+00:00 2026-06-04T10:48:20+00:00

I should know this, but I don’t and I think its probably a major

  • 0

I should know this, but I don’t and I think its probably a major gap in my foundation knowledge so I thought I should ask the experts.

Given:

 char s1[] = { 'M', 'y', 'W', 'o', 'r', 'd' };
 char s2[] = "MyWord";

cout << strlen(s1)<<endl;
cout << strlen(s2)<<endl;

cout << sizeof(s1)<<endl;
cout << sizeof(s2)<<endl;

Why when declared as s1 is the strlen 9 but when declared as s2 is is 6? Where does the extra 3 come from, it it the lack of a null terminating character?

And I understand that sizeof(s2) is 1 byte larger than sizeof(s2) because s2 will have the null character automatically added?

Please be gentle, TIA!

  • 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-04T10:48:21+00:00Added an answer on June 4, 2026 at 10:48 am

    char s2[] = "MyWord"; Auto adds the null terminator because of the “” declaration.

    s1 declaration does not. When you do a strlen on s1 and it comes out to 9 it is because it eventually ran into a \0 and stopped. s1 shouldn’t be used with strlen since it is not null terminated. strlen on s1 could have been 1,000. If you turn on memory violation detection strlen of s1 would crash.

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

Sidebar

Related Questions

This is one of those I probably should know this, but I don't questions.
I think this should be releated to IIS settings but don't know exactly what
I should probably know this already, but I'm not sure and I don't see
This should be very easy but I don't know how to do it. I
This is something stupid I probably should know, but Googling fails me: When compiling
I know this should be fairly simple but I don't get it anyhow. I've
I feel like I should know the answer to this, but I don't. What
I have this flow but don't know how to deal with it. I think
sorry - I should know this but I don't. I have computed the position
This is the kind of thing I feel I should already know, but don't...

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.