-5

I wrote little program to print "Hello world" in C. I'm not a C programmer, but I liked to try it. In my program there is an error. Please tell me what is it? This is my program:

int main(){
    printf("Hello World");
}

I wrote this with my Java Experiences. I can't find what is wrong.

javathunderman
  • 1,074
  • 2
  • 13
  • 31
Climax
  • 27
  • 1
  • 1
  • 2
  • 2
    You are missing a `return 0;` at the end of `main()`. – Lyubomir Vasilev Aug 23 '12 at 05:31
  • 1
    Or `return 42` for that matter. main declares a return type, so you have to return something. – Eric J. Aug 23 '12 at 05:32
  • There will be no errors specific to this code. You may get warning message like 1) to include the header file #include 2) To add return 0 at the end of function. There is nothing wrong with your program so you won't get error unless until if you using right compiler to compile it :) – Viswesn Aug 23 '12 at 05:37
  • 2
    @EricJ.: Actually, you don't have to, not for `main()`, which is special. See n1516 §5.1.2.2.3 Program Termination: "... reaching the `}` that terminates the `main` function returns a value of 0." – Dietrich Epp Aug 23 '12 at 05:48
  • 1
    @DietrichEpp You are assuming C99/C11, however. The need for return 0 in main() was removed/clarified in C99. – Lundin Aug 23 '12 at 06:35

12 Answers12

10

You can't directly use printf() function as in Java. You should tell the compiler that you are going to use the input/output stream. You can tell it in this line:

#include <stdio.h>

and also you should enter this line at the end of the source code:

return 0;

this will tell the compiler :

"If the program succeed It will return 0 otherwise It will return any other number"

This means if your program is successful main() function will return 0. Then the compile know the program is Ok.

Then at last your complete code is:

#include <stdio.h>

int main() {
    printf("Hello world");
    return 0;
}

To compile this and see the word "Hello World", just save this file as a .c file and Open cmd in your program directory and type

gcc hello.c -o hello && hello

(Replace the 'hello.c' with your filename, and 'hello' with the name you want to put with your .exe file)

Remember My computer is Windows. And this compile code is for windows. If your OS is UNIX like OS. then use this code to compile:

gcc hello.c -o hello
./hello
0xEDD1E
  • 1,172
  • 1
  • 13
  • 28
  • just wondering: your profile is full of "What's wrong with this C code" questions. Did you just answer your own question? – rein Aug 23 '12 at 05:56
  • @rein Since this site is a public community thing, with the aim to gather as much knowledge as possible in one place, answering your own questions is actually encouraged, as long as you give others a fair chance to answer as well. Though of course it would be more honest doing so, by using with the same account for the question and answer both. – Lundin Aug 23 '12 at 06:22
8

A full hello world program in C:

#include <stdio.h>

int main(void) {
    printf("Hello World\n");
    return 0;
}

Then compile (assuming gcc) and execute it:

gcc -o test test.c
./test
rein
  • 32,967
  • 23
  • 82
  • 106
  • 2
    I think the `int main(void)` is the most important part of the answer and should be noted. – Marlon Aug 23 '12 at 06:25
3

First, you have to use a header file.

#include <stdio.h>

What that does is bring up a header file with a bunch of commands in them. That will make it recognize the "printf" piece of code. Next, you have to close your program. By not having a closing statement, the program will not compile because it doesn't know if that is the end of the code. Use this at the end of your program...

return 0;

That closes the program, meaning that the compiler can stop looking for other code. You may also want to look at some programming manuals (there are dozens of free online ones) to learn about the syntax.

One last thing: Most pieces of C code require a semicolon at the end. This is not true for the "int main" statement, nor is it for the header file which I have defined above. The "return" function that closes the program, does however, need a semicolon.

Hoped this helped.

Mohit Jain
  • 30,259
  • 8
  • 73
  • 100
javathunderman
  • 1,074
  • 2
  • 13
  • 31
2

Should also include a pause at the end:

#include <stdio.h>

int main(void) {
    printf("Hello World\n");

    //Read a character from the console
    getchar();

    return 0;
}
mcottingham
  • 1,926
  • 2
  • 18
  • 28
2

Just like import in Java programs, in here you have to include libraries you're using in your program. You have used library function printf, but not included stdio.h.

Rsh
  • 7,214
  • 5
  • 36
  • 45
2

I agree there are many ways to write one of the simplest way is

#include<stdio.h>

int main(void){
  printf("Hello World\n");
  return 0;
}

You can even use different ways as suggested above.

Mohit Jain
  • 30,259
  • 8
  • 73
  • 100
1

You should first look at the structure of "main". Try to understand the various parts as already explained so well in the above answers.

"#include" : The preprocessing directives to be included in the program. But why? Because you are trying to use the functions defined inside them.

int : The return type of "main" program. But why? Because the function calling "main" needs to know if the "main" program has functioned correctly.

main : The entry point of your code. Dont ask why here :-)

main( void ) : To tell the compiler that we are not passing any arguments to program "main"

return 0 : Beacuse you promised "main" that you will return something if "main" will function properly.

Finally the code:

#include <stdio.h>
int main( void )
{
    printf( "Hello World\n" ) ; //Notice the '\n' here. Good coding practice.
    return 0 ;
}
Community
  • 1
  • 1
Abhineet
  • 5,320
  • 1
  • 25
  • 43
1
 #include <stdio.h>                   //Pre-processor commands<br/>
 void main()                          //Starting point of the program<br/>{ //Opening Braces            
     printf("Hello World\n");         //Print Hello World on the screen<br/> 
     return 0;
 }                                    //Ending Braces
theoden8
  • 773
  • 7
  • 16
0

Check it once it will work, I have written it with comments:

#include<stdio.h>               //Pre-processor commands

void main() {
    printf("Hello World\n");    //Print Hello World on the screen
}
theoden8
  • 773
  • 7
  • 16
0

A full hello world program in C:

#include <stdio.h>

int main(void) {
    printf("Hello World\n");
    return 0;
}
Then compile (assuming gcc) and execute it:

gcc -o test test.c
./test
0

You can't use printf() function as in Java. You have to tell the compiler what you are going to use. You can tell this as follows:-

#include <stdio.h>

You must enter this line in last:-

return 0;

Then Your complete code is:-

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

For compiling this and see the word "Hello World", just save this file as a .c file and Open cmd in your program directory and type:-

gcc hello.c -o hello && hello

(Replace the 'hello.c' with your filename, and 'hello' with the name you want to put with your .exe file)

Remember My computer is Windows. So I can compile only for Windows OS.

Julies Dein
  • 5
  • 1
  • 4
-1
#include <stdio.h>
int main() {

    // printf, used to print (display) Hello World
    printf("Hello World ! ");

    // return 0, as the main function is of type int so it must return an integer value
    return 0;
}
kesarling He-Him
  • 1,944
  • 3
  • 14
  • 39