Dev C Basic Codes
The left panel above shows the C++ code for this program. The right panel shows the result when the program is executed by a computer. The grey numbers to the left of the panels are line numbers to make discussing programs and researching errors easier. They are not part of the program.
Let's examine this program line by line:
- Line 1:
// my first program in C++
- Two slash signs indicate that the rest of the line is a comment inserted by the programmer but which has no effect on the behavior of the program. Programmers use them to include short explanations or observations concerning the code or program. In this case, it is a brief introductory description of the program.
- Line 2:
#include <iostream>
- Lines beginning with a hash sign (
#
) are directives read and interpreted by what is known as the preprocessor. They are special lines interpreted before the compilation of the program itself begins. In this case, the directive#include <iostream>
, instructs the preprocessor to include a section of standard C++ code, known as header iostream, that allows to perform standard input and output operations, such as writing the output of this program (Hello World) to the screen. - Line 3: A blank line.
- Blank lines have no effect on a program. They simply improve readability of the code.
- Line 4:
int main ()
- This line initiates the declaration of a function. Essentially, a function is a group of code statements which are given a name: in this case, this gives the name 'main' to the group of code statements that follow. Functions will be discussed in detail in a later chapter, but essentially, their definition is introduced with a succession of a type (
int
), a name (main
) and a pair of parentheses (()
), optionally including parameters.
The function namedmain
is a special function in all C++ programs; it is the function called when the program is run. The execution of all C++ programs begins with themain
function, regardless of where the function is actually located within the code. - Lines 5 and 7:
{
and}
- The open brace (
{
) at line 5 indicates the beginning ofmain
's function definition, and the closing brace (}
) at line 7, indicates its end. Everything between these braces is the function's body that defines what happens whenmain
is called. All functions use braces to indicate the beginning and end of their definitions. - Line 6:
std::cout << 'Hello World!';
- This line is a C++ statement. A statement is an expression that can actually produce some effect. It is the meat of a program, specifying its actual behavior. Statements are executed in the same order that they appear within a function's body.
This statement has three parts: First,std::cout
, which identifies the standardcharacter output device (usually, this is the computer screen). Second, the insertion operator (<<
), which indicates that what follows is inserted intostd::cout
. Finally, a sentence within quotes ('Hello world!'), is the content inserted into the standard output.
Notice that the statement ends with a semicolon (;
). This character marks the end of the statement, just as the period ends a sentence in English. All C++ statements must end with a semicolon character. One of the most common syntax errors in C++ is forgetting to end a statement with a semicolon.
You may have noticed that not all the lines of this program perform actions when the code is executed. There is a line containing a comment (beginning with
//
). There is a line with a directive for the preprocessor (beginning with #
). There is a line that defines a function (in this case, the main
function). And, finally, a line with a statements ending with a semicolon (the insertion into cout
), which was within the block delimited by the braces ( { }
) of the main
function. The program has been structured in different lines and properly indented, in order to make it easier to understand for the humans reading it. But C++ does not have strict rules on indentation or on how to split instructions in different lines. For example, instead of
We could have written:
all in a single line, and this would have had exactly the same meaning as the preceding code.
In C++, the separation between statements is specified with an ending semicolon (
;
), with the separation into different lines not mattering at all for this purpose. Many statements can be written in a single line, or each statement can be in its own line. The division of code in different lines serves only to make it more legible and schematic for the humans that may read it, but has no effect on the actual behavior of the program.Now, let's add an additional statement to our first program:
In this case, the program performed two insertions into
std::cout
in two different statements. Once again, the separation in different lines of code simply gives greater readability to the program, since main
could have been perfectly valid defined in this way:The source code could have also been divided into more code lines instead:
And the result would again have been exactly the same as in the previous examples.
Preprocessor directives (those that begin by
#
) are out of this general rule since they are not statements. They are lines read and processed by the preprocessor before proper compilation begins. Preprocessor directives must be specified in their own line and, because they are not statements, do not have to end with a semicolon (;
).Code with C is a comprehensive compilation of Free projects, source codes, books, and tutorials in Java, PHP.NET, Python, C, C, and more. Our main mission is to help out programmers and coders, students and learners in general, with relevant resources and materials in the field of computer programming.
Format Source Code within Dev-C. The following instructions allow you to format source code files in Dev-C using FormatCode command line tool. Add a Tool Menu that will invoke FormatCode. Click 'Tools' - 'Configure Tools.' Click the 'Add' button to add a new tool. Compile and Execute C Program. Let's look at how to save the file, compile and run the program. Please follow the steps given below − Open a text editor and add the code as above. Save the file as: hello.cpp. Open a command prompt and go to the directory where you saved the file. Type 'g hello.cpp' and press enter to compile your code. The keyword class is used to denote a class in C+. Every class in C have a constructor and a destructor. This sample code provides the code for a simple C class and associated constructor, destructor a function. All the functions defined in this basic c class are public. Nov 29, 2016 Delphi is the ultimate IDE for creating cross-platform, natively compiled apps. Are you ready to design the best UIs of your life? Our award winning VCL framework for Windows and FireMonkey (FMX) visual framework for cross-platform UIs provide you with the foundation for intuitive, beautiful.
Using namespace std
If you have seen C++ code before, you may have seencout
being used instead of std::cout
. Both name the same object: the first one uses its unqualified name (cout
), while the second qualifies it directly within the namespacestd
(as std::cout
).cout
is part of the standard library, and all the elements in the standard C++ library are declared within what is called a namespace: the namespace std
.In order to refer to the elements in the
std
namespace a program shall either qualify each and every use of elements of the library (as we have done by prefixing cout
with std::
), or introduce visibility of its components. The most typical way to introduce visibility of these components is by means of using declarations:The above declaration allows all elements in the
std
namespace to be accessed in an unqualified manner (without the std::
prefix).With this in mind, the last example can be rewritten to make unqualified uses of
cout
as:Both ways of accessing the elements of the
std
namespace (explicit qualification and using declarations) are valid in C++ and produce the exact same behavior. For simplicity, and to improve readability, the examples in these tutorials will more often use this latter approach with using declarations, although note that explicit qualification is the only way to guarantee that name collisions never happen.Namespaces are explained in more detail in a later chapter.
Dev C Programming
Previous: Compilers | Index | Next: Variables and types |
- C++ Basics
- C++ Object Oriented
- C++ Advanced
- C++ Useful Resources
- Selected Reading
Dev C Basic Codes 2017
When we consider a C++ program, it can be defined as a collection of objects that communicate via invoking each other's methods. Let us now briefly look into what a class, object, methods, and instant variables mean.
Object − Objects have states and behaviors. Example: A dog has states - color, name, breed as well as behaviors - wagging, barking, eating. An object is an instance of a class.
Class − A class can be defined as a template/blueprint that describes the behaviors/states that object of its type support.
Methods − A method is basically a behavior. A class can contain many methods. It is in methods where the logics are written, data is manipulated and all the actions are executed.
Instance Variables − Each object has its unique set of instance variables. An object's state is created by the values assigned to these instance variables.
C++ Program Structure
Let us look at a simple code that would print the words Hello World.
Let us look at the various parts of the above program −
Auto tune 8 free download for windows. Auto-Tune Pro is the most complete and advanced edition of Auto Tune for Windows PC.It includes both Auto Mode, for real-time pitch correction and effects, and Graph Mode, for detailed pitch and time editing.For twenty years, the tool has been the professional standard for pitch correction, and the tool of choice for the most iconic vocal effect in popular music.
The C++ language defines several headers, which contain information that is either necessary or useful to your program. For this program, the header <iostream> is needed.
The line using namespace std; tells the compiler to use the std namespace. Namespaces are a relatively recent addition to C++.
The next line '// main() is where program execution begins.' is a single-line comment available in C++. Single-line comments begin with // and stop at the end of the line.
The line int main() is the main function where program execution begins.
The next line cout << 'Hello World'; causes the message 'Hello World' to be displayed on the screen.
The next line return 0; terminates main( )function and causes it to return the value 0 to the calling process.
Compile and Execute C++ Program
Let's look at how to save the file, compile and run the program. Please follow the steps given below −
Open a text editor and add the code as above.
Save the file as: hello.cpp
Open a command prompt and go to the directory where you saved the file.
Type 'g++ hello.cpp' and press enter to compile your code. If there are no errors in your code the command prompt will take you to the next line and would generate a.out executable file.
Now, type 'a.out' to run your program.
You will be able to see ' Hello World ' printed on the window.
If that's the case, then you have some kind of incompatibility / misconfiguration between your C compiler and the C compiler that Open MPI was compiled/installed with.Looking at your mpic -showme output, it looks like you have some kind of package distribution of Open MPI - -R is not put in the flags by default, for example. Where did you get this Open MPI installation? It's quite possible that it is not (fully) compatible with your g installation (e.g., if it was compiled with a different version of g).That being said, your mpic -showme output is also weird in that it lists -lmpicxx at the end of the line. It should be to the left of -lmpi, not to the right of it. However, if you are using this small C hello world program as a simple example and your actual target is to compile a C MPI program, then mpic is the correct wrapper to try (even with a simple C program). Hello world program in c using dev c++.
Make sure that g++ is in your path and that you are running it in the directory containing file hello.cpp.
You can compile C/C++ programs using makefile. For more details, you can check our 'Makefile Tutorial'.
Semicolons and Blocks in C++
In C++, the semicolon is a statement terminator. That is, each individual statement must be ended with a semicolon. It indicates the end of one logical entity.
For example, following are three different statements −
A block is a set of logically connected statements that are surrounded by opening and closing braces. For example −
C++ does not recognize the end of the line as a terminator. For this reason, it does not matter where you put a statement in a line. For example −
is the same as
C++ Identifiers
A C++ identifier is a name used to identify a variable, function, class, module, or any other user-defined item. An identifier starts with a letter A to Z or a to z or an underscore (_) followed by zero or more letters, underscores, and digits (0 to 9).
C++ does not allow punctuation characters such as @, $, and % within identifiers. C++ is a case-sensitive programming language. Thus, Manpower and manpower are two different identifiers in C++.
Here are some examples of acceptable identifiers −
C++ Keywords
The following list shows the reserved words in C++. These reserved words may not be used as constant or variable or any other identifier names.
asm | else | new | this |
auto | enum | operator | throw |
bool | explicit | private | true |
break | export | protected | try |
case | extern | public | typedef |
catch | false | register | typeid |
char | float | reinterpret_cast | typename |
class | for | return | union |
const | friend | short | unsigned |
const_cast | goto | signed | using |
continue | if | sizeof | virtual |
default | inline | static | void |
delete | int | static_cast | volatile |
do | long | struct | wchar_t |
double | mutable | switch | while |
dynamic_cast | namespace | template |
Dev C Programs
Trigraphs
Dev C Codes
A few characters have an alternative representation, called a trigraph sequence. A trigraph is a three-character sequence that represents a single character and the sequence always starts with two question marks.
Dev C Programming Examples
Trigraphs are expanded anywhere they appear, including within string literals and character literals, in comments, and in preprocessor directives.
Following are most frequently used trigraph sequences −
Trigraph | Replacement |
---|---|
??= | # |
??/ | |
??' | ^ |
??( | [ |
??) | ] |
??! | |
??< | { |
??> | } |
??- | ~ |
All the compilers do not support trigraphs and they are not advised to be used because of their confusing nature.
Whitespace in C++
A line containing only whitespace, possibly with a comment, is known as a blank line, and C++ compiler totally ignores it.
Whitespace is the term used in C++ to describe blanks, tabs, newline characters and comments. Whitespace separates one part of a statement from another and enables the compiler to identify where one element in a statement, such as int, ends and the next element begins.
Statement 1
In the above statement there must be at least one whitespace character (usually a space) between int and age for the compiler to be able to distinguish them.
Statement 2
In the above statement 2, no whitespace characters are necessary between fruit and =, or between = and apples, although you are free to include some if you wish for readability purpose.