{"id":3555,"date":"2024-05-28T05:30:39","date_gmt":"2024-05-28T05:30:39","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=3555"},"modified":"2024-05-28T05:30:40","modified_gmt":"2024-05-28T05:30:40","slug":"c-basic-syntax","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/05\/28\/c-basic-syntax\/","title":{"rendered":"C++ Basic Syntax"},"content":{"rendered":"\n<p>When we consider a C++ program, it can be defined as a collection of objects that communicate via invoking each other&#8217;s methods. Let us now briefly look into what a class, object, methods, and instant variables mean.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Object<\/strong>\u00a0\u2212 Objects have states and behaviors. Example: A dog has states &#8211; color, name, breed as well as behaviors &#8211; wagging, barking, eating. An object is an instance of a class.<\/li>\n\n\n\n<li><strong>Class<\/strong>\u00a0\u2212 A class can be defined as a template\/blueprint that describes the behaviors\/states that object of its type support.<\/li>\n\n\n\n<li><strong>Methods<\/strong>\u00a0\u2212 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.<\/li>\n\n\n\n<li><strong>Instance Variables<\/strong>\u00a0\u2212 Each object has its unique set of instance variables. An object&#8217;s state is created by the values assigned to these instance variables.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">C++ Program Structure<\/h2>\n\n\n\n<p>Let us look at a simple code that would print the words&nbsp;<em>Hello World<\/em>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\n\n\/\/ main() is where program execution begins.\nint main() {\n   cout &lt;&lt; \"Hello World\"; \/\/ prints Hello World\n   return 0;\n}<\/code><\/pre>\n\n\n\n<p>Let us look at the various parts of the above program \u2212<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The C++ language defines several headers, which contain information that is either necessary or useful to your program. For this program, the header\u00a0<strong>&lt;iostream><\/strong>\u00a0is needed.<\/li>\n\n\n\n<li>The line\u00a0<strong>using namespace std;<\/strong>\u00a0tells the compiler to use the std namespace. Namespaces are a relatively recent addition to C++.<\/li>\n\n\n\n<li>The next line &#8216;<strong>\/\/ main() is where program execution begins.<\/strong>&#8216; is a single-line comment available in C++. Single-line comments begin with \/\/ and stop at the end of the line.<\/li>\n\n\n\n<li>The line\u00a0<strong>int main()<\/strong>\u00a0is the main function where program execution begins.<\/li>\n\n\n\n<li>The next line\u00a0<strong>cout &lt;&lt; &#8220;Hello World&#8221;;<\/strong>\u00a0causes the message &#8220;Hello World&#8221; to be displayed on the screen.<\/li>\n\n\n\n<li>The next line\u00a0<strong>return 0;<\/strong>\u00a0terminates main( )function and causes it to return the value 0 to the calling process.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Compile and Execute C++ Program<\/h2>\n\n\n\n<p>Let&#8217;s look at how to save the file, compile and run the program. Please follow the steps given below \u2212<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Open a text editor and add the code as above.<\/li>\n\n\n\n<li>Save the file as: hello.cpp<\/li>\n\n\n\n<li>Open a command prompt and go to the directory where you saved the file.<\/li>\n\n\n\n<li>Type &#8216;g++ hello.cpp&#8217; 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.<\/li>\n\n\n\n<li>Now, type &#8216;a.out&#8217; to run your program.<\/li>\n\n\n\n<li>You will be able to see &#8216; Hello World &#8216; printed on the window.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>$ g++ hello.cpp\n$ .\/a.out\nHello World\n<\/code><\/pre>\n\n\n\n<p>Make sure that g++ is in your path and that you are running it in the directory containing file hello.cpp.<\/p>\n\n\n\n<p>You can compile C\/C++ programs using makefile. For more details, you can check our\u00a0&#8216;Makefile Tutorial&#8217;.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Semicolons and Blocks in C++<\/h2>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>For example, following are three different statements \u2212<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>x = y;\ny = y + 1;\nadd(x, y);\n<\/code><\/pre>\n\n\n\n<p>A block is a set of logically connected statements that are surrounded by opening and closing braces. For example \u2212<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n   cout &lt;&lt; \"Hello World\"; \/\/ prints Hello World\n   return 0;\n}\n<\/code><\/pre>\n\n\n\n<p>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 \u2212<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>x = y;\ny = y + 1;\nadd(x, y);\n<\/code><\/pre>\n\n\n\n<p>is the same as<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>x = y; y = y + 1; add(x, y);\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">C++ Identifiers<\/h2>\n\n\n\n<p>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).<\/p>\n\n\n\n<p>C++ does not allow punctuation characters such as @, $, and % within identifiers. C++ is a case-sensitive programming language. Thus,&nbsp;<strong>Manpower<\/strong>&nbsp;and&nbsp;<strong>manpower<\/strong>&nbsp;are two different identifiers in C++.<\/p>\n\n\n\n<p>Here are some examples of acceptable identifiers \u2212<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mohd       zara    abc   move_name  a_123\nmyname50   _temp   j     a23b9      retVal\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">C++ Keywords<\/h2>\n\n\n\n<p>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.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>asm<\/td><td>else<\/td><td>new<\/td><td>this<\/td><\/tr><tr><td>auto<\/td><td>enum<\/td><td>operator<\/td><td>throw<\/td><\/tr><tr><td>bool<\/td><td>explicit<\/td><td>private<\/td><td>true<\/td><\/tr><tr><td>break<\/td><td>export<\/td><td>protected<\/td><td>try<\/td><\/tr><tr><td>case<\/td><td>extern<\/td><td>public<\/td><td>typedef<\/td><\/tr><tr><td>catch<\/td><td>false<\/td><td>register<\/td><td>typeid<\/td><\/tr><tr><td>char<\/td><td>float<\/td><td>reinterpret_cast<\/td><td>typename<\/td><\/tr><tr><td>class<\/td><td>for<\/td><td>return<\/td><td>union<\/td><\/tr><tr><td>const<\/td><td>friend<\/td><td>short<\/td><td>unsigned<\/td><\/tr><tr><td>const_cast<\/td><td>goto<\/td><td>signed<\/td><td>using<\/td><\/tr><tr><td>continue<\/td><td>if<\/td><td>sizeof<\/td><td>virtual<\/td><\/tr><tr><td>default<\/td><td>inline<\/td><td>static<\/td><td>void<\/td><\/tr><tr><td>delete<\/td><td>int<\/td><td>static_cast<\/td><td>volatile<\/td><\/tr><tr><td>do<\/td><td>long<\/td><td>struct<\/td><td>wchar_t<\/td><\/tr><tr><td>double<\/td><td>mutable<\/td><td>switch<\/td><td>while<\/td><\/tr><tr><td>dynamic_cast<\/td><td>namespace<\/td><td>template<\/td><td>&nbsp;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Trigraphs<\/h2>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>Trigraphs are expanded anywhere they appear, including within string literals and character literals, in comments, and in preprocessor directives.<\/p>\n\n\n\n<p>Following are most frequently used trigraph sequences \u2212<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><th>Trigraph<\/th><th>Replacement<\/th><\/tr><tr><td>??=<\/td><td>#<\/td><\/tr><tr><td>??\/<\/td><td>\\<\/td><\/tr><tr><td>??&#8217;<\/td><td>^<\/td><\/tr><tr><td>??(<\/td><td>[<\/td><\/tr><tr><td>??)<\/td><td>]<\/td><\/tr><tr><td>??!<\/td><td>|<\/td><\/tr><tr><td>??&lt;<\/td><td>{<\/td><\/tr><tr><td>??&gt;<\/td><td>}<\/td><\/tr><tr><td>??-<\/td><td>~<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>All the compilers do not support trigraphs and they are not advised to be used because of their confusing nature.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Whitespace in C++<\/h2>\n\n\n\n<p>A line containing only whitespace, possibly with a comment, is known as a blank line, and C++ compiler totally ignores it.<\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Statement 1<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>int age;\n<\/code><\/pre>\n\n\n\n<p>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.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Statement 2<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>fruit = apples + oranges;   \/\/ Get the total fruit\n<\/code><\/pre>\n\n\n\n<p>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.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When we consider a C++ program, it can be defined as a collection of objects that communicate via invoking each other&#8217;s methods. Let us now briefly look into what a class, object, methods, and instant variables mean. C++ Program Structure Let us look at a simple code that would print the words&nbsp;Hello World. Let us [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[250],"tags":[],"class_list":["post-3555","post","type-post","status-publish","format-standard","hentry","category-03-c"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/3555","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/comments?post=3555"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/3555\/revisions"}],"predecessor-version":[{"id":3556,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/3555\/revisions\/3556"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=3555"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=3555"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=3555"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}