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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T14:17:48+00:00 2026-06-16T14:17:48+00:00

Why this code doesn’t compile #include <stdio.h> int x=5; int a[x]; int main() {

  • 0

Why this code doesn’t compile

#include <stdio.h>
int x=5;
int a[x];
int main()
{
     a[0]=5;
     printf("%d\n",a[0]);
     return 0;
}

When compiled with gcc filename.c -Wall -ansi -pedantic it produces a error

error: variably modified ‘a’ at file scope

However this code compiles although giving warnings but gives correct output:

#include <stdio.h>
int main()
{
     int x=5;
     int a[x];
     a[0]=5;
     printf("%d\n",a[0]);
     return 0;
}

warning: ISO C90 forbids variable length array ‘a’ [-Wvla]

However if i try to compile this using g++ filename.c -Wall -ansi -pedantic it produces no warnings and gives correct output too

#include <stdio.h>
const int x=5;
int a[x];
int main()
{
    a[0]=5;
    printf("%d\n",a[0]);
    return 0;
}

I am using gcc version 4.7.0

Please explain in detail what’s happening ?

  • 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-16T14:17:49+00:00Added an answer on June 16, 2026 at 2:17 pm

    In C(unlike C++) const declarations do not produce constant expressions, i.e. in C you can’t use a const int object as array size in a non-VLA array declaration.

    So,

    const int max_foos = 10;
    int foos[max_foos];
    

    is valid in C++ but invalid in C. Equivalent C code will be:

    #define MAX_FOOS 10
    int foos[MAX_FOOS];
    

    Note that:
    const in c doesn’t mean constant. It means “read only”.

    Note variable length arrays became a part of the C standard only in C99 prior to that the standard did not allow them though most compilers allowed them as a extension.


    The first code snippet doesn’t compile because the array subscript needs be a constant which it is not. Also, variable length arrays cannot be declared at global scope.

    The second code snippet doesn’t compile because variable length arrays were not part of standard prior to c99.

    The third snippet compiles because const declarations produce constant expressions in C++ unlike C.

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

Sidebar

Related Questions

This code doesn't work: return this.Context.StockTakeFacts.Select(stf => ((stf.StockTakeId == stocktakeid) && (stf.FactKindId == ((int)kind)))).ToList<IStockTakeFact>();
Possible Duplicate: problem with template inheritance This code doesn't compile in GCC: template <typename
Why this code doesn't work? wchar_t path[300]; wchar_t *PathCreator(wchar_t *dir, wchar_t *fileName) { int
This code doesn't compile: const int x = 123; const int y = x;
This code doesn't work but when I put print(7) this works fine: #include<stdio.h> #define
Got any ideas why this code doesn't work? #include <stdio.h> char* get_name() { char
this code doesn't compile. I'm wondering what I am doing wrong: private static Importable
This code doesn't return data from table: var pom = from k in dataContext.student_gods
This code doesn't compile (gives errors C2059, C2065, C2072, C2143, C2146, C2447, C2470, C4430)
It looks like this snippet of code doesn't work unless I include the first

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.