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 * |
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 z = x y;// addition driver x = 5;// emulsion assignment driver bool result = x> y;// comparison 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){ switch( x){ for( int i = 0; i< 10; i){ while( x> 0){ |
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){ 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{ void sayHello(){ ; Person person; |
7. Namespaces:
Namespaces are used to group related law together and avoid naming conflicts. They’re declared using the namespace keyword.
Code
namespace myNamespace{
myNamespacex = 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;
|
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}; struct Person{ union Data{ |
3. stoner- Defined Data Types
stoner- defined data types are created by the programmer. They can be created using classes or typedefs.
Code
typedef Auto Vehicle; Vehicle auto; |