Best Top 10 Basic Syntax and Data Types in C++

Basic Syntax and Data Types in C++

 

Basic Syntax and Data Types is an extension of the C programming language and provides important features for erecting complex software operations. In this composition, we will explore the basics of C syntax and data types.

 

Basic Syntax

 

1. commentary:

commentary: are used to give fresh information about the law. They’re ignored by the compiler and don’t affect the prosecution of the program. There are two types of commentary in C

1.1 – Single- line commentary start with// and continue to the end of the line.
1.2 – Multi-line commentary start with/ * and end with */.

                       code

/ This is a single- line comment

*
This is a
multi-line
comment
*/

 

 

2. Variables:

Data is stored in memory via variables. A variable must first be declared in C before it may be used.
The protestation specifies the type of the variable and its name.

         Code

int x;// declare an integer variable named x

 

 

3. Drivers:

Drivers are used to perform operations on variables and values. C provides a wide range of drivers, including computation, assignment, comparison, and logical drivers.

        Code

int x = 10;
int y = 20;

int z = x y;// addition driver
int w = x * y;// addition driver

x = 5;// emulsion assignment driver

bool result = x> y;// comparison driver
bool result2 = x & & y;// logical driver

 

 

4. Control Structures:

Control structures are used to control the inflow of prosecution in a program. C provides several control structures, including if statements, switch statements, circles, and goto statements.

         Code

int x = 10;

if( x> 5){
/ do commodity
differently{
/ do commodity additional

switch( x){
case 1
/ do commodity
break;
case 2
/ do commodity additional
break;
dereliction
/ do commodity if none of the below cases match
break;

for( int i = 0; i< 10; i){
/ do commodity 10 times

while( x> 0){
/ do commodity while x is lesser than 0
x–;

 

 

5. Functions:

Functions are a set of statements that perform a specific task. They’re used to break down a large program into lower, more manageable pieces.

                          Code

int add( int x, int y){
return x y;

int result = add( 5, 10);// result = 15

 

 

6. Classes:

Classes are stoner- defined data types that synopsize data and styles. They give a way to model real- world objects and relations between them. In C, a class is defined using the class keyword.

                        Code

class Person{
public
stdstring name;
int age;

void sayHello(){
stdcout<<” Hello, my name is”<< name<<” and I’m”<< age<<” times old.”<< stdendl;

;

Person person;
= ” John”;
= 30;
();// affair Hello, my name is John and I’m 30 times old.

 

 

7. Namespaces:

Namespaces are used to group related law together and avoid naming conflicts. They’re declared using the namespace keyword.

                           Code

namespace myNamespace{
int x;
void printX(){
stdcout<<” x = “<< x<< stdendl;

 

myNamespacex = 10;
myNamespaceprintX();// affair x = 10

 

 

Data Types

In C, data types are used to define the type of data that a variable can hold. There are three orders of data types abecedarian data types, deduced data types, and stoner- defined data types.

1. Fundamental Data Types:

Fundamental data types are erected- in data types that are handed by the C language. They include

1.1 – bool : boolean( true/ false) value
1.2 – char : single character
1.3 – int : integer
1.4 – pier : floating- point number
1.5 -double : double- perfection floating- point number
1.5 – void : absence of type( used for functions that don’t return a value)

         Code

bool b = true;
housekeeper c = ‘ a’;
int i = 10;
pier f = 3.14;
double d = 3.14159;
void doSomething(){/ * do commodity */}

 

 

 

2. Derived Data Types

derived data types are erected from abecedarian data types. They include:-

2.1 – Arrays- a collection of rudiments of the same data type
2.2 – Pointers- a variable that stores the memory address of another variable
2.3 – References an alias for another variable
2.4 – Structures- a collection of affiliated data members
2.5 – Unions- a collection of data members that partake the same memory position

          Code

int arr( 5) = { 1, 2, 3, 4, 5};
int * ptr = & i;
int & ref = i;

struct Person{
stdstring name;
int age;
;

union Data{
int i;
pier f;

 

 

3. stoner- Defined Data Types

stoner- defined data types are created by the programmer. They can be created using classes or typedefs.

         Code

 
class Auto{
public
stdstring make;
stdstring model;
int time;
;

typedef Auto Vehicle;

Vehicle auto;
= ” Toyota”;
= ” Camry”;
= 2022;

 

 

 

Leave a Comment