Simple version of a hello world in c include iostream

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: simple version of a “Hello World” in C++: #include <iostream> int main() { std::cout << "Hello World!" << std::endl; return 0; } Scott B. Baden / CSE 100, Lec 2 / Spring 2013 40 Compiling and running “Hello World” in C++ •  In C++ , code can appear outside a class definition (as in that example) •  There is no requirement about the name of a source code file, but it is conventional to use a .cpp extension; for example, hw.cpp •  A C++ program must contain exactly one function named main which returns an int •  That main function is the entry point of the program •  From the command line, compile a C++ source code file using the g++ compiler: g++ hw.cpp •  ... which will produce a file a.out of executable native machine code •  Run the program: a.out •  ... which will print to standard output: Hello World! #include <iostream> int main() { std::cout << "Hello World!" << std::endl; return 0; } Scott B. Baden / CSE 100, Lec 2 / Spring 2013 41 Parts of the hello world program in C++ #include <iostream> •  This #include directive tells the compiler to read in the contents of the system header file iostream at this point. This file contains declarations of variables and functions necessary for doing I/O in C++| int main () { #include <iostream> int main() { std::cout << "Hello World!" << std::endl; return 0; } •  To run, every C++ program must contain a definition of a function named “main”. When the program starts, the operating system calls this function. main() is declared to return an int value. Here, we aren’t using any command-line arguments to main, but we std::cout << "Hello World!" << std::endl; •  Send the string “Hello World!” to standard output, followed by end-of-line. << is the bitwise left-shift operator, overloaded to implement stream I/O. The std:: is a namespace qualifier (put using namespace std; near the top of the file to avoid it). return 0; •  Since main is declared to return an int, we better do so. Returning from main ends your program; it is traditional to return 0 if it is terminating normally, and a nonzero value if terminating because of some error condition Scott B. Baden / CSE 100, Lec 2 / Spring 2013 42 The C++ compiler •  We will use the GNU C++ compiler, g++ •  We are using 4.7.2 AKA “C++11” g++ -v ...lots of output gcc version 4.7.2 (GCC) •  This is a very good C/C++ compiler, freely available for many platforms •  (Other C++ compilers may differ in some features) •  If you have a .cpp file that contains a definition of main(), and that uses only functions defined in it or included from the standard libraries, you can just compile it as was shown, creating an executable file named a.out •  If you want another name for the resulting executable, you can use the -o flag, with the name you want. This will compile hw....
View Full Document

This note was uploaded on 09/11/2013 for the course CSE 100 taught by Professor Staff during the Spring '08 term at UCSD.

Ask a homework question - tutors are online