In general most of codes in programming are text lines, that means you can read codes my any editors i.e. with Notepad, Word, etc. on Windows. C and C++ Compilers, compiles (we can say converts) these lines to machine codes that directly runs in that operating system and links as a executable (*.exe) file. A Compiler (C or C++ Compiler, etc.) is a computer program that converts one programming language (i.e. C/C++ codes) written with text into executable machine code with a linker. Such code may not be as fast as assembler code, but the difference in speed is very small because both machine code and compiler-based code in text form are much more compatible with other CPU/GPUs and/or with other Operating Systems when you compile them on a machine. This is one reason why C++ is the fastest and most powerful programming language. Interpreted programming languages run inside executable applications like Java, Python or Visual Basic. This is why they are slower when executing operations, as they need to use compiled libraries for faster operations. Again, they mostly they use C/C++ compilers to build these libraries. Using an interpreted programming language is like being carried by a runner, while a compiled (non-interpreted) programming language is like running itself. This subtle difference turns into a huge gap when you run the same routine (i.e. for face recognition) millions of times a millisecond.
In this post, we want to show you Structure of C++ Programming Language, it is very important for beginners to understand how code lines work in C++. Dev-C++ is a notable IDE for programming C++ applications on Windows. In C+ Programming Language, structure of simple applications are like this,
Structure of a Simple C or C++ Programming Language;
1 2 3 4 5 |
< Library Headers > < Main Program Function > |
Basically C++ has two part includes and other definitions outside of main() function and main() function.
In professional coding, applications needs many functions. For example one function may save configuration of your game application and another function may load that configuration. There are many functions that means you need to include many library headers, you also need your Custom Header Files. You also need sub functions may be sub functions of these function, you can also call some functions recursively. So all these should be outside of the main() function.
Structure of an Advanced C++ Programming Language is,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
< Library Headers > < Custom Header Files > < Constants > < Global Variables> < Classes > < Sub Functions > < Main Program Function < Local Variables > < Calling Sub Functions > > |
Commands in Libraries are included with #include <…> command and these libraries contain commands, variables, classes. We can say they are like sub programs that has basic commands used in C++. So it is good to know if your command needs a library or it is also same library. For example cout command used to print out texts and variables to screen or to an output like file, printer etc. This command is defined in iostream library, so you must add this library header as given here,
1 2 3 |
#include <iostream> |
Generally C headers has *.h extension and C++ headers has *.hpp extension. Some standard libraries has no extension. In this line <….> these brackets means it is in default include path of C++ compiler that you are using. Most of include files can be found in C:\Program Files (x86)\Embarcadero\Dev-Cpp\TDM-GCC-64\x86_64-w64-mingw32\include folder.
Main functions are defined as main() here, note that a function in C++, has parentheses and it’s parameters are used in this parentheses. Code lines of these functions are defined in { and } brackets, so a simple main function should be as shown below here,
1 2 3 4 5 6 |
main() { // main program codes here } |
Here // is used to commenting in lines, that means all text after this is not a C++ code, just a text in code as a reminder. You can use this to note or to remember code lines. As a standard, In Dev-C++ and in most of other C++ compilers main() function has two arguments and it returns an integer number, as given here below,
1 2 3 4 5 6 7 |
int main(int argc, char** argv) { // main program codes here return 0; } |
Dev-C++ Hello World C++ Example
If you are a beginner “Hello World” examples are good to understand feature of that programming language. It is good to understand how to edit text, how to write in its format, how to compile and link, how to debug and execute, how to deploy or release. Hello World example print outs “Hello World” sentence to the screen. Here below is the most common “Hello World” C++ example, the best way to learn a programming language is writing programs. Typically, the first program beginners write is a program called “Hello World”, which simply prints “Hello World” to your computer screen
1 2 3 4 5 6 7 8 9 10 |
#include <iostream> /* run this program using the console pauser or add your own getch, system("pause") or input loop */ int main(int argc, char** argv) { std::cout << "Hello Dev-C++ World\n"; return 0; } |
These examples are also good to understand Structure of C++ Programming Language. Here we have iostream library library and we used cout function to write “Hello World” . If you write this same code, compile and run with F11 in Dev-C++ ( or in many other C++ compilers) this code will print out “Hello Dev-C++ World” to the screen.
1 2 3 |
Hello Dev-C++ World |
Dev-C++ Hello World C Example
If you would like to code same in C format with Dev-C++ you must chose Console – C Language, And code should be like this,
1 2 3 4 5 6 7 8 9 10 11 |
#include <stdio.h> #include <stdlib.h> /* run this program using the console pauser or add your own getch, system("pause") or input loop */ int main(int argc, char *argv[]) { printf("Hello Dev-C World\n"); return 0; } |
C++ includes all C features and it has many great features and modern ways. In C format, here we have two standard libraries and we used printf() function to write “Hello World”, and this will print out,
1 2 3 |
Hello Dev-C World |