0% found this document useful (0 votes)
40 views39 pages

First Day of Program in C New Tobe Send

This document provides an introduction to variables in C++. It explains that variables are containers that store data values and lists some common variable types like int, double, char, and string. It also mentions assigning values to variables, printing variable values, getting input into variables, and creating a basic calculator function as examples of working with variables.

Uploaded by

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

First Day of Program in C New Tobe Send

This document provides an introduction to variables in C++. It explains that variables are containers that store data values and lists some common variable types like int, double, char, and string. It also mentions assigning values to variables, printing variable values, getting input into variables, and creating a basic calculator function as examples of working with variables.

Uploaded by

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

First day of Program in c+

+
HTTPS://WWW.TUTORIALSPOINT.COM/CPLUSPLUS/CPP_STL_TUTORIAL.HTM

HTTPS://WWW.W3SCHOOLS.COM/CPP/TRYCPP.ASP?FILENAME=DEMO_VARIABLES_INT
 #include <iostream>
 using namespace std;

 int main() {
 cout << "Hello World!" ;  Hello World!
 return 0;
 }
Example explained

Line 1: #include <iostream> is a header file library that lets us work with
input and output objects, such as cout (used in line 5). Header files add
functionality to C++ programs.
Line 2: using namespace std means that we can use names for objects and
variables from the standard library.
Don't worry if you don't understand how #include <iostream> and using
namespace std works. Just think of it as something that (almost) always
appears in your program.
Line 3: A blank line. C++ ignores white space. But we use it to make the
code more readable.
Line 4: Another thing that always appear in a C++ program, is int main().
This is called a function. Any code inside its curly brackets {} will be
executed.
Example explained

Line 5: cout (pronounced "see-out") is an object used together with


the insertion operator (<<) to output/print text. In our example it will output
"Hello World!".
Note: Every C++ statement ends with a semicolon ;.
Note: The body of int main() could also been written as:
int main () { cout << "Hello World! "; return 0; }
Remember: The compiler ignores white spaces. However, multiple lines
makes the code more readable.
Line 6: return 0 ends the main function.
Line 7: Do not forget to add the closing curly bracket } to actually end the
main function.
Example explained

This example uses a single-line comment before a line of code:

 #include <iostream>
 using namespace std;

 int main() {
 // This is a comment
 cout << "Hello World!";
 return 0;
 }
 #include <iostream>
 using namespace std; “ = means you declaring
TEXT
 int main() { endl = means end of the
 cout << "Hello World!" ; line.
 return 0;
No << endl only ; = it
 } will not go down to the next
line
 #include <iostream>
 using namespace std;

 int main() {
 Hello World! Hello World!
 cout << "Hello World!";
 cout << "Hello World!";
 return 0;
 }
 #include <iostream>
 using namespace std;

 int main() {  Hello World!


 cout << "Hello World!" << endl;  Hello World!
 cout << "Hello World!“;
 return 0;
 }
Example explained

 The cout object, together with the << operator, is used to output vales/print
text:
 You can add as many cout objects as you want. However, note that it does
not insert a new line at the end of the output:

Single-line Comments
Single-line comments start with two forward slashes (//).
Any text between // and the end of the line is ignored by the compiler (will
not be executed).
This example uses a single-line comment before a line of code:
C++ Multi-line Comments
Multi-line comments start with /* and ends with */.
Any text between /* and */ will be ignored by the compiler:

 #include <iostream>
 using namespace std;

 int main() {
 /* The code below will print the words Hello World!
 to the screen, and it is amazing */
 cout << "Hello World!";
 return 0;
 }
Example explained

This example uses a single-line comment before a line of code:

 #include <iostream>
 using namespace std;

 int main() {
 // This is a comment
 cout << "Hello World!";
 return 0;
 }
Type Typical Bit Width Typical Range

char 1byte -127 to 127 or 0 to 255

unsigned char 1byte 0 to 255

signed char 1byte -127 to 127

int 4bytes -2147483648 to 2147483647

The following
unsigned int 4bytes 0 to 4294967295 table shows the
signed int 4bytes -2147483648 to 2147483647 variable type,
how much
short int 2bytes -32768 to 32767
memory it takes
unsigned short int 2bytes 0 to 65,535
to store the
signed short int 2bytes -32768 to 32767
value in
long int 8bytes -9223372036854775808 to 9223372036854775807 memory, and
what is
signed long int 8bytes same as long int
maximum and
unsigned long int 8bytes 0 to 18446744073709551615
minimum value
which can be
long long int 8bytes -(2^63) to (2^63)-1 stored in such
unsigned long long int 8bytes 0 to 18,446,744,073,709,551,615
type of
variables.
float 4bytes

double 8bytes

long double 12bytes

wchar_t 2 or 4 bytes 1 wide character


 #include <iostream>
 using namespace std;

 int main() {
 cout << "Size of char : " << sizeof(char) << endl;  Size of char : 1
 cout << "Size of int : " << sizeof(int) << endl;  Size of int : 4
 cout << "Size of short int : " << sizeof(short int) <<  Size of short int : 2
endl;
 cout << "Size of long int : " << sizeof(long int) <<  Size of long int : 4
endl;
 Size of float : 4
 cout << "Size of float : " << sizeof(float) << endl;
 Size of double : 8
 cout << "Size of double : " << sizeof(double) << endl;
 cout << "Size of wchar_t : " << sizeof(wchar_t) <<  Size of wchar_t : 4
endl;

 return 0;
 }
 Int Min -2147483648
 #include <iostream>
 Int Max 2147483647
 #include <limits>
 Unsigned Int Min 0
 using namespace std;
 Unsigned Int Max 4294967295

  Long Int Min -9223372036854775808


int main() {
 Long Int Max 9223372036854775807

 std::cout << "Int Min " << std::numeric_limits<int>::min() << endl;  Unsigned Long Int Min 0

 std::cout << "Int Max " << std::numeric_limits<int>::max() << endl;  Unsigned Long Int Max 18446744073709551615

 std::cout << "Unsigned Int Min " << std::numeric_limits<unsigned int>::min() << endl;
 std::cout << "Unsigned Int Max " << std::numeric_limits<unsigned int>::max() << endl;
 std::cout << "Long Int Min " << std::numeric_limits<long int>::min() << endl;
 std::cout << "Long Int Max " << std::numeric_limits<long int>::max() << endl;

 std::cout << "Unsigned Long Int Min " << std::numeric_limits<unsigned long int>::min() <<endl;
 std::cout << "Unsigned Long Int Max " << std::numeric_limits<unsigned long int>::max() << endl;

 }
Variables
Assigning data in variables
Printing out variables
Variables data type
Input values to variables
Make a one function calculator
C++ Variables
Variables are containers for storing data values.
In C++, there are different types of variables (defined with different keywords), for example:

•int - stores integers (whole numbers), without decimals, such as 123 or -123

•double - stores floating point numbers, with decimals, such as 19.99 or -19.99

•char - stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotes

•string - stores text, such as "Hello World". String values are surrounded by double quotes

•bool - stores values with two states: true or false

 Declaring (Creating) Variables


 To create a variable, specify the type and assign it a value:

#include <iostream>
using namespace std;

int main() {
int myNum = 15;
cout << myNum;
return 0;
}
C++ Variables
Note that if you assign a new value to an existing variable, it
will overwrite the previous value:

#include <iostream>
using namespace std;

int main() {
int myNum = 15; // Now myNum is 15
myNum = 10; // Now myNum is 10
cout << myNum;
return 0;
}
C++ Variables
Note that if you assign a new value to an existing variable, it
will overwrite the previous value:

#include <iostream>
using namespace std;

int main() {
int x = 5, y = 6, z = 50;
cout << x + y + z;
return 0;
}
Variables = in math what so ever assign in X that is
variables
 = in Programming Variables are use to store up
some kind of data for later use. To understand this we will
apply this to our c++ program.
 Keep in my always to start your code in between “int main to the last }”

 #include <iostream>
 using namespace std;

 int main() {
 string x = “Welcome to ZPPSU”;  Welcome to ZPPSU
 cout << x;
 return 0;
 }

 Did you guess what might be the printout?


 #include <iostream>
 using namespace std;

 int main() {
 cout << “Nicolas is a programmer!" << endl;
 cout << “Elderick is a programmer!" << endl;
 cout << “Nicolas and Elderick are partners!" << endl;
 return 0;
 }
 #include <iostream>
 using namespace std;

 int main() {
 String nameOne = “Nicolas”;
 String nameTwo = “Elderick”;
 String occupation = “Programmer”;

 cout << nameOne << “ is a “ << occupation! << endl;


 cout << nameTwo << “ is a “ << occupation!" << endl;
 cout << nameOne << “ and “ << nameTwo << “ are partners! " << endl;
 return 0;
 }
 Nicolas is a programmer!
 Elderick is a programmer!
 Nicolas and Elderick are partners!
C++ Variables
Variables are containers for storing data values.
In C++, there are different types of variables (defined with
different keywords), for example:
•int - stores integers (whole numbers), without decimals,
such as 123 or -123
•double - stores floating point numbers, with decimals, such
as 19.99 or -19.99
•char - stores single characters, such as 'a' or 'B'. Char
values are surrounded by single quotes
•string - stores text, such as "Hello World". String values are
surrounded by double quotes
•bool - stores values with two states: true or false
C++ Variables
int myNum = 5; // Integer (whole number without decimals)
double myFloatNum = 5.99; // Floating point number (with
decimals)
char myLetter = 'D'; // Character
string myText = "Hello"; // String (text)
bool myBoolean = true; // Boolean (true or false)
C++ Variables
int myNum = 5; // Integer (whole number without decimals)
double myFloatNum = 5.99; // Floating point number (with
decimals)
char myLetter = 'D'; // Character
string myText = "Hello"; // String (text)
bool myBoolean = true; // Boolean (true or false)
One Value to Multiple Variables
 You can also assign the same value to multiple variables in one line:

 include <iostream>
 using namespace std;

 int main() {
 int x, y, z;
 x = y = z = 50;
 cout << x + y + z;
 return 0;
 }
Local variable declaration
 #include <iostream>
 using namespace std;

 int main () {
 // Local variable declaration:
 int a, b;
 int c;

 // actual initialization
 a = 10;
 b = 20;
 c = a + b;

 cout << c;

 return 0;

Global variable declaration
 #include <iostream>
 using namespace std;

 // Global variable declaration:


 int g;

 int main () {
 // Local variable declaration:
 int a, b;

 // actual initialization
 a = 10;
 b = 20;
 g = a + b;

 cout << g;

 return 0;
A program can have same name for local and global variables but value of local variable
inside a function will take preference. For example

 #include <iostream>
 using namespace std;

 // Global variable declaration:


 int g = 20;

 int main () {
 // Local variable declaration:
 int g = 10;

 cout << g;

 return 0;
 }
The general rules for naming variables
are:
•Names can contain letters, digits and underscores
•Names must begin with a letter or an underscore (_)
•Names are case sensitive (myVar and myvar are
different variables)
•Names cannot contain whitespaces or special
characters like !, #, %, etc.
•Reserved words (like C++ keywords, such as int)
cannot be used as names
C++ Identifiers
 All C++ variables must be identified with unique names.
 These unique names are called identifiers.
 Identifiers
can be short names (like x and y) or more descriptive
names (age, sum, totalVolume).
 Note: It is recommended to use descriptive names in order to create
understandable and maintainable code:
C++ Identifiers
 // Good
int minutesPerHour = 60;

// OK, but not so easy to understand what m actually is


int m = 60;
C++ Constants
 When you do not want others (or yourself) to change existing variable values, use the const
keyword (this will declare the variable as "constant", which means unchangeable and read-only):

 #include <iostream>
 using namespace std;

 int main() {
 const int myNum = 15;
 myNum = 10;
 cout << myNum;
 return 0;
 }
C++ Constants
 You should always declare the variable as constant when you have values that are unlikely to change:

 #include <iostream>
 using namespace std;

 int main() {
 const int minutesPerHour = 60;
 const float PI = 3.14;
 cout << minutesPerHour << "\n";
 cout << PI;
 return 0;
 }
Quiz Quiz:

1. Write a c++ program that display Hello world.

2. Hello world hello world


3. Hello world
Hello world
4.
 #include <iostream>

 using namespace std;

 int main()
 {
 string name;
 cout << "What's your name?: ";
 cin >> name;
 cout<< "Hello! " << name;
 return 0;
 }
 #include <iostream>

 using namespace std;

 int main()
 {
 string name;
 int age;

 cout << "What's your name?: " ;


 cin >> name ;
 cout << "What's your age?: " ;
 cin >> age ;
 cout<< "Hello! " << name ;
 cout << "You are " << age << "years old ";
 return 0;
 }
 #include <iostream>

 using namespace std;

 int main()
 {
 string name;
 int age;

 cout << "What's your name?: " ;


 cin >> name ;
 cout << "What's your age?: " ;
 cin >> age ;
 cout<< "Hello! " << name << ‘\n’;
 cout << "You are " << age << "years old ";
 return 0;
C++ full course for free
 }

You might also like