-4

I have this basic code in C. A program to say 'Hello World'. And the first line That say #include <stdio.h> is underlined in red as though an error is occurred.

#include<stdio.h>

main()
{
    printf("hello, world\n")
}

I'm using vs code to run the program, is it a bug in vs code? or maybe a problem in the installation of C? please help.

I don't understand where the problem originates so no actions have been pursued.

  • 5
    Is your source file a C file or a C++ file? What is its file extension? Is Visual Studio Code configured to compile it as C or as C++? If you put `#include ` in the file, is it in red too? – Eric Postpischil Dec 31 '22 at 14:23
  • 3
    maybe missing space after include, also a missing `;` after `printf` – Axeltherabbit Dec 31 '22 at 14:24
  • 3
    @Axeltherabbit Neither of those things would cause the reported problem. – Steve Summit Dec 31 '22 at 14:28
  • 1
    @SteveSummit oh yeah, also missing the return type of `main` I guess it might be that – Axeltherabbit Dec 31 '22 at 14:31
  • @Axeltherabbit has a very plausible answer. Because of the missing return type, compiler might consider that the last thing from `stdio.h` is intended to be the return type, and is illegal at that. Hence the underlining of the faulty return type, that is the `#include` – chrslg Dec 31 '22 at 14:34
  • 2
    @Axeltherabbit The problem is clearly that vscode is misconfigured, or that the OP is using it incorrectly, perhaps by trying to compile a C program as C++. If the compiler were trying to complain about the missing return type, it would put the red underline there, don't you think? – Steve Summit Dec 31 '22 at 14:34
  • @SteveSummit But that might be exactly what it does: underlying the faulty return type. – chrslg Dec 31 '22 at 14:35
  • mh, well depends on the IDE+compiler I guess, I had many instances of the error being in the previous line due to a missing `;` or similar things – Axeltherabbit Dec 31 '22 at 14:35
  • 2
    @chrslg I believe that you and Axeltherabbit are sending the OP on a wild goose chase with your wild guesses. – Steve Summit Dec 31 '22 at 14:36
  • 1
    Please, no need to make an argument about this (I for one, am too old for internet fights). I am just saying that @Axeltherabbit hypothesis is not that impossible. But I also do believe that a faulty configured IDE is the most probable cause (compiling C code as C++, for example was my 1st thought). But your positive dismissal of Axeltherabbit hypothesis might also be subjective (I guess that is what you meant by "wild guesses". But nobody has nothing more to offer here). Although I don't believe that hypothesis the most probable, I don't believe it should be dismissed as impossible neither. – chrslg Dec 31 '22 at 14:40

1 Answers1

-1

Add space between #include and <stdio.h> Also, specify the return type of your main function. Also at the end of printf("hello, world\n"), there should be semi colon

#include <stdio.h>

void main(){
    printf("hello, world\n");
}

it is void since it is not returning anything.

  • 1
    Note that in C99 or later, if you have `int main(void)` and execution 'falls off the end of the `main()` function', it is equivalent to [`return 0;`](http://port70.net/~nsz/c/c11/n1570.html#5.1.2.2.3) (aka 'success'). With a `void main()`, you'd better be working on Windows; otherwise, it is [undefined behaviour](https://stackoverflow.com/q/204476/15168). – Jonathan Leffler Jan 01 '23 at 23:53