Classic Hello World

Hello, I am having an issue with the classic hello world program. I seem to have an issue when inserting the line "system("pause");" . Could someone direct me to what I am doing wrong?

/* Ewilkz
My Attempt at writing a program
8/23/14
oh hey, didn't see you there!!! */

#include <stdio.h>

int main(void) {

printf("Hello world!!!\n");
system("pause");
return 0;
}

These are the "errors" :
C:\Users\Eric\Desktop\Programming\My Programs\Hello World.cpp In function `int main()':
11 C:\Users\Eric\Desktop\Programming\My Programs\Hello World.cpp `system' undeclared (first use this function)
(Each undeclared identifier is reported only once for each function it appears in.)
you may want to add stdlib.h
and also change system("pause") to getchar()
Last edited on
Thanks for your comment. I found it, I guess? The file was saved as a C++ file and when I saved it as a .c file and compiled, I didn't come across any errors and it executed fine. Any reason/explanation/advice other than "just save your programs as a .c"?
you dont need system pause or getchar thats only if you are looking to pause the program for any reason the classic hello world is much simpler.

here is my hello world C++ code.

1
2
3
4
5
6
7
8
9
#include <iostream>

using namespace std;

int main()
{
	cout << "Hello World" << endl;
return 0;
}
Last edited on
Some compilers might deduce the language by the filename extension. A compiler in C-mode behaves differently from a compiler in C++-mode. That includes the implicitly linked libraries.
To use system() put
 
#include <stdlib.h> 
or in C++
 
#include <cstdlib> 


Suggestion: if you're not sure which #include is necessary, type the function name in the search box at the top of this page. That should lead you to the reference page where it will state which header is needed (and give sample code).
Topic archived. No new replies allowed.