C++ syntax refers to the set of rules and guidelines that govern how C++ code is written and structured. It includes rules for naming variables, defining functions, using control statements like if-else and loops, and more.
Some basic syntax rules in C++ include:
- Statements must end with a semicolon (;)
- Code is organized into functions, which are defined with a return type, function name, and parameters (if any)
- Variables must be declared with a data type before they can be used
- Comments can be added to code using // for single-line comments or /* */ for multi-line comments
- Control statements like if-else and loops are used to control the flow of code execution.
Here are some examples of C++ syntax with explanations:
Keywords
#include <iostream> //include a standard library
using namespace std; //using the standard namespace
int main() { //main function, entry point of the program
int x = 5; //integer variable declaration and assignment
float y = 3.14; //float variable declaration and assignment
char z = 'a'; //char variable declaration and assignment
return 0; //return 0 to indicate successful program termination
}
Variables
int age; //integer variable declaration age = 25; //assign a value to the variable float weight = 68.5; //float variable declaration and initialization char initial = 'J'; //char variable declaration and initialization
Operators
int a = 10; int b = 3; int sum = a + b; //addition operator int diff = a - b; //subtraction operator int product = a * b; //multiplication operator float quotient = a / b; //division operator int remainder = a % b; //modulus operator
Functions
int add(int x, int y) { //function declaration with two integer parameters
int sum = x + y; //add the two parameters
return sum; //return the result
}
int main() {
int a = 5;
int b = 3;
int result = add(a, b); //call the add function with arguments a and b
cout << "The sum of " << a << " and " << b << " is " << result << endl;
return 0;
}
Classes
class Rectangle { //class declaration
public: //access specifier
int width; //data member
int height; //data member
int area() { //member function to calculate area
return width * height;
}
};
int main() {
Rectangle r; //create an instance of the Rectangle class
r.width = 5; //assign a value to the width data member
r.height = 3; //assign a value to the height data member
int a = r.area(); //call the area() member function and assign the result to a
cout << "The area of the rectangle is " << a << endl;
return 0;
}
Control Structures
int main() {
int age = 18;
if (age >= 18) { //if statement to check if age is greater than or equal to 18
cout << "You are an adult." << endl;
} else { //else statement for when age is less than 18
cout << "You are not an adult yet." << endl;
}
for (int i = 1; i <= 10; i++) { //for loop to print numbers from 1 to 10
cout << i << " ";
}
cout << endl;
while (age < 21) { //while loop to print ages until age is greater than or equal to 21
cout << "You are " << age << " years old." << endl;
age++;
}
return 0;
}
Comments
int main() {
int x = 5; //assign 5 to x
int y = 3; //assign 3 to y
//int z = x + y; //this line is
Overall, following proper syntax rules in C++ is essential for writing readable and maintainable code.