Basic Syntax of C++

Share on facebook
Share on twitter
Share on linkedin
Share on twitter
Share on tumblr

In this section of C++ tutorial, we will have a look at the basic syntax of C++. From printing “hello world” to writing the functions in C++, we will cover all the basic syntax that you need to learn before getting your hands dirty with the C++ programming language.

Basic Syntax of C++

C++ Program Structure to Print “Hello World”

#include <iostream> 
using namespace std;

int main() {
   cout << "Hello World"; 
   return 0;
}

C++ Program Structure to use Function

#include <iostream>  
void myFirstFunction() {
  cout << "Got triggered!\n";
}

int main() {
  myFunction();
  return 0;
}

Syntax to Declare the Variable in C++

type variable = value;

Syntax to Comment in C++

/* Single line comment*/

/* C++ example
   * of multiline comment
*/

Syntax to Open a File in C++

void open(const char *filename, ios::openmode mode);

Syntax to Close a File in C++

void close();

Syntax for Dynamic Memory Allocation for Arrays in C++

char* pvalue  = NULL;         
pvalue  = new char[10];

Leave a Comment

Your email address will not be published. Required fields are marked *