0% found this document useful (0 votes)
2 views

Learning c

Uploaded by

hjwzahmd
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Learning c

Uploaded by

hjwzahmd
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Learning C++ For Beginners

By Ahmad Hajjouz & Chatgpt

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.

2. First C++ Program:


This is a basic C++ program. It starts with the #include statement,
which tells the compiler to include the iostream library. This
library contains functions for input/output operations such as
printing text to the console.
The next line is using namespace std, which allows us to use
standard library functions without having to specify their full
name each time. Then we have the main function, which is where
our program starts executing from. The cout statement prints
"Hello World!" to the console, and finally we return 0 from main,
indicating that our program ran successfully and returning 0 (zero)
as an exit code.

#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.

For example, a char variable is typically used to store a single


character, such as the letter 'A', and it occupies 1 byte of memory.

An int variable, on the other hand, is used to store an integer


value such as 42, and it occupies 4 bytes of memory.
In order to use a variable in a C++ program, you must first declare
it by specifying its type and giving it a name.
For example:
1 int x;
2 char ch;

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.

Here are the most common integer types in C++:

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.

1 using namespace std;

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

You might also like