0

I'm learning C and I wrote a simple program displaying first n numbers of fibonacci sequence, where n is user input. VS Code shows error when I declare an array with length n ("expression must have a constant value"). The program compiles and runs just fine, but the line and file highlighted in red is quite annoying. Am i doing something wrong, or is it a problem with VS Code? Program:

#include<stdio.h>
#include<stdlib.h>

int main() {
  printf("Ile liczb fibonacciego: ");
  int n;
  scanf("%d", &n);
  int fibo[n];
  
  for (int i = 0; i < n && i < 2; i++)
  fibo[i] = i;

  for (int i = 2; i < n; i++)
  fibo[i] = fibo[i-1] + fibo[i-2];

  for (int i = 0; i < n; i++)
  printf("%d ", fibo[i]);

  return 0;
}
  • `int fibo[n];`: (VLAs = variable length arrays) are not supported by the Microsoft C compiler. The only thing you're doing wrong is using the wrong compiler. – Jabberwocky Apr 17 '22 at 12:22
  • VS Code is an IDE, not compiler. Which compiler are you using? – phuclv Apr 17 '22 at 12:25
  • @Jabberwocky the OP said that `The program compiles and runs just fine` so it's likely that they only want to change VS Code's highlighting feature – phuclv Apr 17 '22 at 12:32
  • @Adrian the question is about VS Code, not VS – phuclv Apr 17 '22 at 12:32
  • @phuclv I'm using gcc mingw compiler. The code runs alright but vs code shows error regardless. Do you know a way to make your IDE more "compatible" with the compiler? – AgresywnyToster Apr 17 '22 at 17:19
  • no, but you should avoid VLA because it'll be optional again in C11 and many compilers won't supported. Allocate memory yourself instead – phuclv Apr 17 '22 at 17:23

0 Answers0