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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:28:17+00:00 2026-06-14T18:28:17+00:00

int main() { char a[7] = Network; return 0; } A string literal in

  • 0
int main()
{
    char a[7] = "Network";
    return 0;
}

A string literal in C is terminated internally with a nul character. So, the above code should give a compilation error since the actual length of the string literal Network is 8 and it cannot fit in a char[7] array.

However, gcc (even with -Wall) on Ubuntu compiles this code without any error or warning.
Why does gcc allow this and not flag it as compilation error?

gcc only gives a warning (still no error!) when the char array size is smaller than the string literal. For example, it warns on:

char a[6] = "Network";

[Related] Visual C++ 2012 gives a compilation error for char a[7]:

1>d:\main.cpp(3): error C2117: 'a' : array bounds overflow
1> d:\main.cpp(3) : see declaration of 'a'
  • 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-14T18:28:18+00:00Added an answer on June 14, 2026 at 6:28 pm

    Initializing a char array with a string literal that is larger than it is fine in C, but wrong in C++. That explains the difference in behavior between gcc and VC++.

    You would get no error if you compiled the same as a C file with VC++. And you would get an error if you compiled it as a C++ file with g++.

    The C standard says:

    An array of character type may be initialized by a character string
    literal or UTF−8 string literal, optionally enclosed in braces.
    Successive bytes of the string literal (including the terminating null
    character if there is room or if the array is of unknown size)
    initialize the elements of the array.

    […]

    EXAMPLE 8

    The declaration

    char s[] = "abc", t[3] = "abc";
    

    defines ‘‘plain’’ char array objects s and t whose elements are initialized
    with character string literals.
    This declaration is identical to

    char s[] = { 'a', 'b', 'c', '\0' },
         t[] = { 'a', 'b', 'c' };
    

    (Section 6.7.9 of the C11 draft standard, actual wording in final standard might be different.)

    This means that it’s perfectly correct to drop the termination character if the array doesn’t have room for it. It’s maybe unexpected, but it’s exactly how the language is supposed to work, and a (at least to me) well-known feature.

    On the contrary, the C++ standard says:

    There shall not be more initializers than there are array elements.

    Example:

     char cv[4] = "asdf"; // error
    

    is ill-formed since there is no space for the implied trailing ‘\0’.

    (8.5.2 of the C++ 2011 draft n3242.)

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

Sidebar

Related Questions

I have this code: int main() { char ch[15]; cout<<strlen(ch)<<endl; //7 cout<<sizeof(ch)<<endl; //15 return
I have this code: int main() { char buffer[10]; if( buffer ) { return
int main() { char *p,c; for(p=Hello World;c=*p;++p) { printf(%c,c); } } In the above
This code causes a Segmentation Fault: int main(){ char *p; char a[50] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;
I am writing some code in C: int main(){ char guess[15]; guess = helloValidation(*guess);
Here's my program so far: int main() { char choice = 'D'; string inputString;
Why does the following code seg fault before returning: int main() { char iD[20];
Why this below code gives segmentation fault? int main() { char *t = Working
#include <stdio.h> int main() { char string[] = my name is geany; int length
I have this code int main() { char A = 'E'; const char *

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.