Search code examples
cccminix

"char not expected" error when compiling in MINIX


I need to print a line to the screen and then get user input, but the printf("blah") statements cause my code to not compile. The error message says 'char not expected" but when I comment the printf() statements out, then the code compile.

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

int main(void) 
{ 
    printf("Welcome to the shell!\n"); 
    printf("shell:>");
    char* inp = (char*)malloc(20); // error at this line
}

I am using the cc compiler in MINIX 3.1.0


Solution

  • The MINIX C compiler is not following modern standards, which means that local variables can only be declared at the start of functions.

    You need to do e.g.

    char *inp;
    
    printf("Welcome to the shell!\n"); 
    printf("shell:>");
    
    inp = malloc(20);
    

    When I say "modern" I mean the C99 standard. The older C89 standard, which the MINIX compiler seems to follow, and also the Visual Studio C compiler until recently (much of C99 wasn't supported until VS2013 and later), only allowed declarations at the beginning of blocks.