Home > Enterprise >  Is this a syntax error or an error with the compiler?
Is this a syntax error or an error with the compiler?

Time:04-04

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

int main(void)
{
    char charName[] = "John";
    int charAge[] = 35;

    printf("There once was a man named %s\n", charName);
    printf("He was %s\n", charAge); 

    return 0;
}

First error: array initializer must be an initializer list or wide string literal int charAge[] = 35; Second error: make: *** [<builtin>: main.o] Error 1 exit status 2

I did everything I could to fix this, but nothing worked.

CodePudding user response:

If it was an error of the compiler then it would be already resolved.

If you declare an array then the initializer must be a braced list like

int charAge[] = { 35 }; 

The only exception is initialization of character arrays with string literals like

char charName[] = "John";

that may be also rewritten like

char charName[] = { "John" };

From the C Standard (6.7.9 Initialization)

14 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.

15 An array with element type compatible with a qualified or unqualified version of wchar_t may be initialized by a wide string literal, optionally enclosed in braces. Successive wide characters of the wide string literal (including the terminating null wide character if there is room or if the array is of unknown size) initialize the elements of the array.

16 Otherwise, the initializer for an object that has aggregate or union type shall be a brace-enclosed list of initializers for the elements or named members.

Pay attention to that this call of printf

printf("He was %s\n", charAge);

invokes undefined behavior.

Taking into account the name of the array it seems you was going to declare it with the element type char like

char charAge[] = "35";

Otherwise you need to write

printf("He was %d\n", *charAge);

In general elements of an array of an integral type (except arrays that contain strings) should be outputted in a loop element by element.

Though in this concrete case there is no sense to declare an array. You could declare just a scalar object

int charAge = 35;

In this case the call or printf will look like

printf("He was %d\n", charAge);

CodePudding user response:

The error comes from the fact that you're declaring an array of integers, int charAge[] and yet you're assigning not an array but an individual value, 35, to it.

Based on your usage, you don't want an array. So, you can just do

int charAge = 35;

Furthermore, when you're printing charAge, you need to use %i (or %d; that's also acceptable for integers) instead of %s. %s is for char arrays like charName. That is, you should do

printf("He was %i\n", charAge);
  •  Tags:  
  • c
  • Related