0% found this document useful (0 votes)
3 views4 pages

C++_Basics

C++

Uploaded by

M Khalid Mahmood
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

C++_Basics

C++

Uploaded by

M Khalid Mahmood
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

C++ Programming Basics

1. Basic Elements of a C++ Program

A C++ program consists of the following basic elements:

Structure of a C++ Program:

#include <iostream> // Header file inclusion

using namespace std; // Namespace declaration

int main() { // Main function

cout << "Hello, World!" << endl; // Statement

return 0; // Return statement

Explanation of Elements:

1. Preprocessor Directives: Statements starting with #, such as #include <iostream>, are used to

include libraries.

2. Namespace: Declares a scope to avoid naming conflicts (using namespace std).

3. Main Function: The entry point of the program (int main()).

4. Statements: Instructions inside {} that perform actions, e.g., cout.

5. Comments: Documentation, written as // single-line or /* multi-line */.

2. Compiled vs. Interpreted Languages

Differences:
| Aspect | Compiled Languages | Interpreted Languages |

|--------------|---------------------------------------------|---------------------------------------|

| Execution | Translated to machine code before execution.| Executed line-by-line by an

interpreter.|

| Speed | Faster (precompiled). | Slower due to runtime interpretation.|

| Errors | Detected during compilation. | Detected at runtime. |

C++ Classification:

C++ is a compiled language. A C++ compiler (e.g., GCC, Clang) translates source code into an

executable file.

3. Variables in C++

A variable is a named memory location used to store data.

Declaration, Definition, and Initialization:

1. Declaration: Declares a variable's type and name, but does not allocate memory.

extern int x; // Declares 'x' but does not define it.

2. Definition: Allocates memory for the variable.

int x; // Defines 'x' (default initialized to 0 for global variables).

3. Initialization: Assigns a value during or after definition.

int x = 10; // Defines and initializes 'x'.


4. Data Types in C++

Categories:

1. Fundamental Data Types: Basic types provided by the language (e.g., int, float, char, bool).

2. Derived Data Types: Derived from fundamental types (e.g., array, pointer, function).

3. User-Defined Types: Created by the programmer (e.g., struct, class, enum).

Fundamental Data Types:

| Type | Size (bytes) | Range | Example |

|-------|--------------|--------------------------------|------------------|

| int | 4 | -2,147,483,648 to 2,147,483,647| int a = 10; |

| float | 4 | ±3.4e-38 to ±3.4e+38 | float b = 5.5; |

| char | 1 | -128 to 127 or 0 to 255 | char c = 'A'; |

| bool | 1 | true or false | bool d = true; |

5. Purpose of Operators in C++

Operators are symbols used to perform operations on variables and values.

Types of Operators:

1. Arithmetic Operators:

int a = 10, b = 3;

cout << a + b; // Output: 13

cout << a - b; // Output: 7

cout << a * b; // Output: 30


cout << a / b; // Output: 3

cout << a % b; // Output: 1

2. Assignment Operators:

int x = 10; // Simple assignment

x += 5; // x = x + 5; Result: x = 15

x *= 2; // x = x * 2; Result: x = 30

3. Comparison Operators:

int a = 5, b = 10;

cout << (a == b); // Output: 0 (false)

cout << (a < b); // Output: 1 (true)

4. Logical Operators:

bool x = true, y = false;

cout << (x && y); // Output: 0 (false)

cout << (x || y); // Output: 1 (true)

cout << (!x); // Output: 0 (false)

You might also like