Learning c
Learning c
1|Page
1. Introduction To C++:
C++ is a general-purpose programming language created by
Bjarne Stroustrup in 1979 as an extension of the C programming
language. It is a statically typed, compiled, multi-paradigm, and
object-oriented language. C++ is widely used for developing
systems software, application software, device drivers, embedded
software, high-performance server and client applications, and
entertainment software such as video games.
#include <iostream>
1 using namespace std;
2
3 int main()
4{
5 cout << "Hello World!"; // prints "Hello World!" to the
6 console
7 return 0; // indicates that the program ran successfully and
8 returns 0
}
2|Page
Maybe you will not understand some of lines from this code,
that’s normal , do not worry we will learn it later.
3. Variables:
In C++, a variable is a storage location that holds a value. The
value of a variable can change during the execution of a program.
The type of a variable determines how much storage it occupies in
the computer's memory, and how the value is interpreted.
3.1 Integers:
An integer is a whole number (not a fraction) that can be
positive, negative, or zero. In C++, there are several different
types of integers that you can use, depending on the size and
range of values that you need to store.
3|Page
• char: A character type that can store a single byte (8 bits).
• short: A short integer type that can store a small number
(usually at least 16 bits).
• int: A standard integer type that is usually the same size
as a short on most systems (at least 16 bits).
• long: A long integer type that is usually the same size as
an int on most systems (at least 32 bits).
• long long: A very long integer type that is usually at least
64 bits.
Example:
#include <iostream>
1 int main() {
2 // Declare an integer variable called "x" and
3 initialize it to the value 10
4 int x = 10;
5
6 // Print the value of x to the console
7 std::cout << "x is " << x << std::endl;
8
9 // Modify the value of x
10 x = 20;
11
12 // Print the new value of x
13 std::cout << "x is now " << x << std::endl;
14
15 return 0;
16 }
17 Output :
18 x is 10
x is now 20
4|Page
Note : We used std:: before the cout function because we don’t typed this line
at the beginning of our program.
3.2 Strings:
A string is a sequence of characters in C++. Strings can be used
to store text-based data such as names, addresses, and other
pieces of information. In C++, there are several ways to work
with strings.
Here are a few common string types in C++:
• char*: A pointer to a null-terminated array of characters.
• string: A class that represents a string.
We should import “string” library to get more functions
about strings :
#include <iostream>
1
#include <string>
2
3
int main() {
4
// Declare a string variable called "name" and
5
initialize it to the value "John"
6
std::string name = "John";
7
8
// Print the value of name to the console
9
std::cout << "My name is " << name << std::endl;
10
11
// Modify the value of name
12
name = "Jane";
13
14
// Print the new value of name
15
std::cout << "My name is now " << name <<
16
std::endl;
17
18
return 0;}
5|Page
This would output the following:
My name is John
My name is now Jane
6|Page