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

  • Home
  • SEARCH
  • 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 8990879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:36:03+00:00 2026-06-15T22:36:03+00:00

If I declare a character array: char arr[200] and then I subsequently use the

  • 0

If I declare a character array: char arr[200]
and then I subsequently use the function cin to read values into arr[200]
and I type into the command window line: abcd

Is there a null terminated: \0 automatically added to the array at the end of the input?

(I don’t think so because I tested it using the cin function: cin>>abcd )

Can somebody explain it to me why?

Below is a snippet of my code I use to test

    char arr[200]
    int count=0;
    int i=0;
    cin>>arr // i type into command window:abcd

    while (arr[i] != '\0')
    {
       count++;
        i++




    }

My count value will not be 4 but like 43 hence I concluded that the character array is not null terminated after the cin function

  • 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-15T22:36:04+00:00Added an answer on June 15, 2026 at 10:36 pm

    Formatted input from a std::istream into a character array will null-terminate the input, as specified in C++11 27.7.2.2.3/9:

    operator>> then stores a null byte (charT()) in the next position

    The code you’ve posted gives the expected result once the obvious syntax errors are fixed. But beware that this is very dangerous; there is no check on the length of the array, so too much input will overflow it. I strongly recommend you use the std::string class, rather than plain character arrays, for managing strings.

    The code you posted in a comment via a link looks like this:

    char array[20];
    int length=getlength(array);
    cin>>array;
    

    reading into the array after attempting to measure the string length of the uninitialised array. This could give any result, or crash, or cause any other example of undefined behaviour.

    In future, you should make sure that the code you post in your question is the same code that exhibits the behaviour you’re asking about; otherwise, it’s impossible to answer the question.

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

Sidebar

Related Questions

I am trying to read a text file into a two dimensional character array.
If I declare a vector as such: vector<char *> charStarVec(4); and then add values
How do I declare a pointer to a character array in C?
In fortran, I can declare a character string as: character*80 mystring and then send
I have a question about char array. How do I declare an array of
I want to declare character array and later want to fill it. But getting
I have a character array defined in a header //header.h const char* temp[] =
How to declare array of strings in C. Is it like char str[100][100] ={this,that,those};
I am trying right now to declare a large character array. I am using
DECLARE @table table(XYZ VARCHAR(8) , id int) INSERT INTO @table SELECT '4000', 1 UNION

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.