15.04.2018 Views

programming-for-dummies

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

524<br />

The Structure of a C/C++ Program<br />

The Structure of a C/C++ Program<br />

A C program is divided into multiple functions where each function acts like<br />

a separate building block. To identify the starting point of a C program, one<br />

function is always designated as the main function. A simple C program<br />

looks like this:<br />

main()<br />

{<br />

printf(“This is a simple C program.\n”);<br />

}<br />

Large C programs consist of multiple functions where each additional function<br />

appears defined separate from the main function. In the following example,<br />

the separate function is printme as follows:<br />

main()<br />

{<br />

printme;<br />

}<br />

printme()<br />

{<br />

printf(“This is a simple C program.\n”);<br />

}<br />

In many <strong>programming</strong> languages, you can define a function inside another<br />

function. However in C/C++, you can’t do this.<br />

Oftentimes a C program needs to use a feature defined in a separate library,<br />

so you may see programs that define a library to add, such as<br />

#include <br />

main()<br />

{<br />

printf(“This is a simple C program.\n”);<br />

}<br />

The structure of a typical C++ program looks similar. The following C++<br />

example includes a C++ library called iostream and uses elements defined<br />

in a standard C++ library called std:<br />

#include <br />

using namespace std;<br />

int main ()<br />

{<br />

cout

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!