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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:19:09+00:00 2026-06-03T00:19:09+00:00

//SECTION I: void main() { char str[5] = 12345; //—a) char str[5] = 1234;

  • 0
//SECTION I:
void main()
{
    char str[5] = "12345";  //---a)
    char str[5] = "1234";   //---b)
    cout<<"String is: "<<str<<endl;
}
Output: a) Error: Array bounds Overflow.
        b) 1234

//SECTION II:
void main()
{
    char str[5];
    cout<<"Enter String: ";
    cin>>str;
    cout<<"String is: "<<str<<endl;
}

I tried with many different input strings, and to my surprise, I got strange result:

Case I: Input String: 1234, Output: 1234 (No issue, as this is expected behavior)

Case II: Input String: 12345, Output: 12345 (NO error reported by compiler But I was expecting an Error: Array bounds Overflow.)

Case III: Input String: 123456, Output: 123456 (NO error reported by compiler But I was expecting an Error: Array bounds Overflow.)

………………………………………….

………………………………………….

Case VI: Input String: 123456789, Output: 123456789(Error: unhandeled exception. Access Violation.)

My doubt is, When I assigned more characters than its capacity in SECTION I, compiler reported ERROR: Array bounds Overflow.

But, when I am trying the same thing in SECTION II, I am not geting any errors. WHY it is so ?? Please note: I executed this on Visual Studio

  • 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-03T00:19:12+00:00Added an answer on June 3, 2026 at 12:19 am
    char str[5] = "12345";
    

    This is a compiletime error. You assign a string of length 6 (mind the appended null-termination) to an array of size 5.


    char str[5];
    cin>>str; 
    

    This may yield a runtime error. Depending on how long the string is you enter, the buffer you provide (size 5) may be too small (again mind the null-termination).


    The compiler of course can’t check your user input during runtime. If you’re lucky, you’re notified for an access violation like this by Segmentation Faults. Truly, anything can happen.

    Throwing exceptions on access violations is not mandatory. To address this, you can implement array boundary checking yourself, or alternatively (probably better) there are container classes that adapt their size as necessary (std::string):

    std::string str;
    cin >> str;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In this section of a function (.NET 2.0): public void AttachInput<T>(T input) where T
I have a section of code that can be summarised as follows; void MyFunc()
Consider this code: class Foo { public void doIt(String... strs) { System.out.println(this is varargs);
I'm trying to output a CDATA section in the result of XSLT using Xalan
i have defiened a structure here struct Owner{ char* ownerName;char* fatherName;char* address;}; void registerV(Owner
The C# 3.0 spec has the following code example in section 10.6.1.3 Output parameters:
.section .data astring: .asciz 11010101 format: .asciz %d\n .section .text .globl _start _start: xorl
.section .data msgI: .ascii x = y\n msgI_end: msgM: .ascii x > y\n msgM_end:
Section 7.1.6 of the Modeshape docs say 'Your application can now create and remove
The section Last links in the chain: Stashing and the reflog in http://ftp.newartisans.com/pub/git.from.bottom.up.pdf recommends

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.