First Day of Program in C New Tobe Send
First Day of Program in C New Tobe Send
+
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
#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;
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
#include <iostream>
using namespace std;
int main() {
// This is a comment
cout << "Hello World!";
return 0;
}
Type Typical Bit Width Typical Range
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
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
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
#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;
}
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”;
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;
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;
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;
#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:
int main()
{
string name;
cout << "What's your name?: ";
cin >> name;
cout<< "Hello! " << name;
return 0;
}
#include <iostream>
int main()
{
string name;
int age;
int main()
{
string name;
int age;