0% found this document useful (0 votes)
7 views6 pages

C++ Programming Basics and Data Types

C++ is a general-purpose programming language that enhances C with object-oriented features. The document covers key concepts such as header files, data types, identifiers, and operators, providing examples and rules for each. It highlights the importance of data types in memory allocation and the rules for naming identifiers in C++.

Uploaded by

alveena0504
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)
7 views6 pages

C++ Programming Basics and Data Types

C++ is a general-purpose programming language that enhances C with object-oriented features. The document covers key concepts such as header files, data types, identifiers, and operators, providing examples and rules for each. It highlights the importance of data types in memory allocation and the rules for naming identifiers in C++.

Uploaded by

alveena0504
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

C++ Programming

"C++ is a general-purpose programming language that was developed by Bjarne Stroustrup


as an enhancement of the C language to add object-oriented paradigm."

Header Files
"Header files are files in programming (especially in C and C++) that contain declarations of
functions, macros, constants, classes, and variables which can be shared between multiple
source (.c or .cpp) files."

They usually have the extension .h.

They are included in your program using the #include directive.

They allow code reusability and modularity by keeping declarations separate from
definitions.

Data Types
"Data types specify the type of data that a variable can store. Whenever a variable is
defined in C++, the compiler allocates some memory for that variable based on the data
type with which it is declared as every data type requires a different amount of memory."

Types of Data Types


1. Character Data Type (char)

The character data type is used to store a single character. The keyword used to define a
character is char. Its size is 1 byte, and it stores characters enclosed in single quotes (' ').

2. Integer Data Type (int)

Integer data type denotes that the given variable can store the integer numbers. The
keyword used to define integers is int. Its size is 4-bytes (for 64-bit) systems.

3. Boolean Data Type (Bool)

The boolean data type is used to store logical values: true(1) or false(0). The keyword used to
define a boolean variable is bool. Its size is 1 byte.

4. Floating Data Type (float)

Floating-point data type is used to store numbers with decimal points. The keyword used to
define floating-point numbers is float. Its size is 4 bytes (on 64-bit systems) .

5. Double Data Type (double)

The double data type is used to store decimal numbers with higher precision. The keyword
used to define double-precision floating-point numbers is double. Its size is 8 bytes (on 64-bit
systems).

Data Type Description Example


int Stores whole numbers (no int age = 20;
decimals)
float Stores decimal numbers (single float price = 99.5;
precision)
double Stores decimal numbers double pi = 3.14159;
(double precision, more
accurate)
char Stores a single character char grade = 'A';
bool Stores true or false value bool isPassed = true;
void Represents “nothing” (used for void greet() {}
functions that don’t return
anything)

Example Code
#include <iostream>
using namespace std;

int main() {
int age = 20;
float price = 99.5;
double pi = 3.14159;
char grade = 'A';
bool isPassed = true;

cout << "Age: " << age << endl;


cout << "Price: " << price << endl;
cout << "Pi: " << pi << endl;
cout << "Grade: " << grade << endl;
cout << "Passed: " << isPassed << endl;

return 0;
}

Output:

Identifiers
Based on the information from GeeksforGeeks, a C++ identifier is a unique name assigned by
the programmer to identify entities in a program, such as variables, functions, classes, and
structs.

Rules for naming identifiers

Allowed characters: An identifier can consist of letters (A-Z or a-z), digits (0-9), and
underscores (_).

Starting character: It must begin with a letter or an underscore. It cannot start with a digit.

Reserved keywords: C++ has a list of reserved keywords (e.g., int, if, for) that have
predefined meanings and cannot be used as identifiers.

Case sensitivity: C++ is a case-sensitive language. This means that num and Num are treated
as different identifiers.

Uniqueness: An identifier must be unique within its specific namespace.

Valid identifiers

_sum
student_Name

getSum

Car

studentAge

accountBalance

Invalid identifiers

123geeks: Starts with a digit.

test@variable: Contains a special character (@).

int: Is a reserved keyword.

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

Arithmetic Operators
Arithmetic operators are used to perform common mathematical operations.

Assignment operators are used to assign values to variables.


Comparison Operators
Comparison operators are used to compare two values (or variables). This is important in
programming, because it helps us to find answers and make decisions.

Logical Operators
As with comparison operators, you can also test for true (1) or false (0) values with logical
operators.

Assignment Operators
Assignment operators are used to assign values to variables.

You might also like