1

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

Armand Maree
  • 488
  • 2
  • 6
  • 21
  • 2
    Please [see why not to cast](http://stackoverflow.com/q/605845/2173917) the return value of `malloc()` and family in `C`. – Sourav Ghosh Aug 10 '15 at 13:14
  • And include `stdlib.h` and don't forget to free the allocated memory. – Spikatrix Aug 10 '15 at 13:15
  • It was included in my original program, I added it here just now. Tfree the memory later in the program. :-) I will check the malloc() link now. Thanks. – Armand Maree Aug 10 '15 at 13:17

1 Answers1

5

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.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621