Search code examples
cwindowsvisual-studio-codegccterminal

C compiling and running


I'm new in C and I'm trying to run a simple helloworld, but the terminal is not displaying the output. Here is the code:

int main(int argc, char *argv[])
{
    printf("Hello");
    return 0;
}

When I'm trying to compile, I run the command

gcc hello.c -o hello.exe

But it is not displaying the phrase. Am I missing something? I have installed the ucrt64 environment and the gcc compiler in vscode.


Solution

  • The command line gcc hello.c -o hello.exe constructs the executable file hello.exe. You must execute this program to see its output by typing hello, ./hello.exe or .\hello.exe at the shell prompt, depending on the operating system and command line interpreter used for your tests.

    Note also these remarks:

    • you must include the <stdio.h> header
    • you should add a trailing newline to ensure proper output on all systems.

    Here is a modified version:

    #include <stdio.h>
    
    int main(int argc, char *argv[]) {
        printf("Hello\n");
        return 0;
    }
    

    And since you do not use the command line arguments, you could simplify the code as:

    #include <stdio.h>
    
    int main() {
        printf("Hello\n");
        return 0;
    }
    

    If the gcc command does not create the executable file without even complaining about any errors doing so, I'm afraid the compiler setup seems to be broken. Try and reinstall the compiler or use a different system if possible.