Is this C program code correct?

main()

{

printf("\nhello");

}

I cant run the program. I use Turbo C to compile my code. When I run the exe file nothing happens only a blank screen comes for a second.

Comments

  • 1. the program opens the window

    2. the program prints

    3. the window closes

    The whole thing takes about .06 seconds, so it looks like a flash. Put a cin statement to pause it.

  • Hi, this is just as a guess but it seems that the program is starting up in a DOS box, exiting immediately and shutting down the DOS Box. You need to put a pause in there when the program is completed. The best way to do this is by adding in a call to getch() before the program exits. getch() will read a single character directly from the keyboard, without echoing it to the screen so its ideal for adding a pause at the end of your program.

    For example compile and run the following 2 programs. One is with getch() and the other without and see the difference.

    // Program 1 Without getch()

    #include <stdio.h>

    #include <conio.h>

    void main(void)

    {

    printf("\nhello without a getch()\n");

    // This line will not be seen as the DOS box closes

    // immediately

    }

    // Program 2 With getch()

    #include <stdio.h>

    #include <conio.h>

    void main(void)

    {

    printf("\nhello with a getch()\n");

    // This line will be seen as the DOS box stays open until

    // the user presses a key

    // wait for user to press a key

    getch();

    }

  • MAIN is a function. Functions have types (like data types, INT, CHAR, etc).

    In C, MAIN cannot be VOID. It needs to return a INT. Just place "int" before the word main, and put a "return 0;" before your final close bracket. That ought to do it (assuming you have included the right header files).

    Edit: Oh, I guess Turbo allows VOID main functions, even though this isn't strictly legal C. The above poster^ is right, the program executes so quickly that when it closes after finishing, you don't see much of anything.

    You can either put in something to make the program stall until the user presses any key, or run the EXE from the command prompt. THe output ofthis program will be written in there, but once the program is done, command prompt will remain open so you can see it.

  • the main() function returns an int. If the program executes without error, main() will return a 0. Also make sure you've included the stdio.h header file. Otherwise printf will not be defined.

    #include <stdio.h>

    int main()

    {

    printf("\nHello");

    return 0;

    }

    If the program exits immediately after execution you can use the system("PAUSE") function defined in stdlib.h

    #include <stdio.h>

    #include <stdlib.h>

    int main()

    {

    printf("\nhello");

    system("PAUSE");

    return 0;

    }

  • When you run the exe file directly by clicking on it(from windows environment), that window will close immediately after execution, your program is coded only to print the message "hello", so as soon as it finishes printing it on the screen it'll close by itself, so if you want the window to stay ther after execution , include this line after printf statement, getch(); ,you may hav to include<conio.h> header file .

  • #include <stdio.h>

    int main()

    {

    printf("Hello World/n");

    return (0);

    }

    Personally i would use http://www.bloodshed.net/devcpp.html for a Windows enviroment, but even more preferably the GCJ package which includes command line based compilers GCC (for C) and G++(C++) and others. Its much lighter on system resources and although the command-line may seem intimidating the process is relatively simple e.g. $> gcc inputfilename.c -o outputfilename(.run or .exe) goodluck.

  • You code is correct. void main or int main all are ok. But I got something. Why you ask the person to enter the number 1, 2 or 3. That’s ok to use scanf with %d and keep value in choice variable (int). Then, you use switch…case. You have to change the case from yours to be mine for working well. See the following code. #include<stdio.h> void main() { int choice; printf("Enter 1,2,3\n"); scanf("%d",&choice); switch(choice) { case 1: printf("You selected choice 1\n"); break; case 2: printf("You selected choice2\n"); break; case 3: printf("You selected choice 3\n"); break; default: printf("ERROR\n"); break; } getchar(); } Because you keep value as integer variable, you must use the case like this. By the way, if you keep the user input value into char, you can use case with ‘1’ or ‘2’ or ‘3’. See the following code. #include<stdio.h> void main() { char choice; printf("Enter 1,2,3\n"); scanf("%c",&choice); switch(choice) { case '1': printf("You selected choice 1\n"); break; case '2': printf("You selected choice2\n"); break; case '3': printf("You selected choice 3\n"); break; default: printf("ERROR\n"); break; } getchar(); } Astalavista :)

Sign In or Register to comment.