fix(guide): simplify directory structure

This commit is contained in:
Mrugesh Mohapatra
2018-10-16 21:26:13 +05:30
parent f989c28c52
commit da0df12ab7
35752 changed files with 0 additions and 317652 deletions

View File

@ -0,0 +1,225 @@
---
title: Tokens Part 1
---
### What are tokens ?
Tokens are the smallest units of a program which are important to the compiler. There are different kinds of tokens:
- Keywords
- Operators
- Punctuators
- Literals
- Identifiers
* **Combination of tokens form an expression**
### What are Variables ?
* Textbook definition : Variables are named memory locations whoose data can be altered.
* But I would like you to think of a variable to be something like a box, something like this :
![Img](https://i.imgur.com/YdbgWHL.png)
So, for example :
I'm shifting to a new place and I need to arrange my stuff in boxes . Thus there come 2 things to my mind **What kind of stuff will be stored in the box, so that the size off the box is known (the data type)** and **How do I identify the box ?(Naming the variable)**
Hence , we know that a variable in C++ needs a *name* and a *data type* and that the value stored in them can be changed.
### Data Types in C++ :
When declaring variables in c++ they must have a name to which you will reffer later on, a value (constant or not) and a type.
The type will tell the compiler the values that the variable can use, the possible operations and will save a certain space in memmory.
In c++ there are two types of data:
* Simple type
* Struct type
### Simple data types
* Boolean -- bool
Works like a switch, can be on or off.
* Character -- char
Stores a single character.
* Integer -- int
Stores an [integer](https://en.wikipedia.org/wiki/Integer).
* Floating point -- float
They can use decimals.
* Double floating point -- double
Double precision of the float type.
Here you can see some examples:
```cpp
bool GameRunning = true;
char a;
int x = 2;
```
#### These types can also be modified with modifiers such as:
signed
unsigned
short
long
### Struct data type
#### Identifiers.
- Identifiers are the names given to a variable or a class or a function or any user defined function.
## Rules for naming a variable : ##
- Start naming with a letter from A-Z or a-z .
- Numbers can follow thee first letter but we cannot start naming with numbers.
- NO use of spaces or special characters are allowed, instead, use an UNDERSCORE _ .
#### Declaring a variabe :
The syntax is as follows
<*data type*> <*variable name*>;
or
<*data type*> <*variable name*> = <*value*>; if we also want to initialize the variable.
For example :
```cpp
int a ; //declaring a variable named 'a' of type integer.
a=4; //initializing a variable
int b = 5 ; //declaring and initializing a variable 'b' of type integer.
```
**Examples of declaring a variable:**
```cpp
int a9;
char A;
double area_circle;
long l;
```
**Wrong ways to declare variables**-
```cpp
int 9a;
char -a;
double area of circle;
long l!!;
```
- Variable names cannot start with a number
- Special characters are not allowed
- Spaces are not allowed
You can imagine different boxes of different sizes and storing different things as different variables.
**NOTES :**
1. **The C++ compiler ignores whitespaces and they are generally used for beautification of the code so that it is eassy for any programmer to debug or understand the code.**
2. **If a variable is not initialized , it contains a garbage value. Let me give an example:**
### Scope of Variables
All the variables have their area of functioning, and out of that boundary they don't hold their value, this boundary is called scope of the variable. For most of the cases its between the curly braces,in which variable is declared that a variable exists, not outside it. We will study the storage classes later, but as of now, we can broadly divide variables into two main types,
*Global Variables.
*Local variables.
#### Global variables
Global variables are those, which ar once declared and can be used throughout the lifetime of the program by any class or any function. They must be declared outside the main() function. If only declared, they can be assigned different values at different time in program lifetime. But even if they are declared and initialized at the same time outside the main() function, then also they can be assigned any value at any point in the program.
Example : Only declared, not initialized.
```cpp
#include <iostream>
using namespace std;
int x; // Global variable declared
int main()
{
x=10; // Initialized once
cout <<"first value of x = "<< x;
x=20; // Initialized again
cout <<"Initialized again with value = "<< x;
}
```
#### Local Variables
Local variables are the variables which exist only between the curly braces, in which its declared. Outside that they are unavailable and leads to compile time error.
Example :
```cpp
#include <iostream>
using namespace std;
int main()
{
int i=10;
if(i<20) // if condition scope starts
{
int n=100; // Local variable declared and initialized
} // if condition scope ends
cout << n; // Compile time error, n not available here
}
```
### Constant Variables
Constant variable are the variables which cannot be changed. For example, if you needed "pi" in your code, you would not want to change it after initialization.
Example :
```cpp
#include <iostream>
using namespace std;
const double PI = 3.14159253;
int main()
{
//Calculating the area of a circle, using user provided radius
double radius;
//input and output explained in other guide
cin>>radius;
//pi*r^2
double area = PI*radius*radius;
cout<<area<<endl;
}
```
### Garbage Values in a Variable
If a variable is not initialized , it contains a garbage value. For example:
So in terms of boxes, you can imagine this as -
![Img](https://i.imgur.com/YdbgWHL.png)
```cpp
#include<iostream>
using namespace std;
int main()
{
int a ;
cout<<"Garbage value in a : "<<a<<endl; //declaring the variable named 'a' of type integer
a=5; //initializing variable.
cout<<"New value in a "<<a<<endl;
}
```
### The output is :
```
Garbage value in a : 0
New value in a : 5
```
As you can see, there is already a value stored in 'a' before we give it a value(here , it is 0 ). This should remain in the mind of every programmer so that when the variables are used they do not create a logical error and print garbage values.
<a href='https://repl.it/Mg7j' target='_blank' rel='nofollow'>Try the code yourself ! :) </a>
#### Keywords :
*Keywords are reserved words that convey a special meaning to the compiler. They **CANNOT** be used for naming in c++.*
Examples of Keywords :
inline , operator, private int, double ,void , char, template ,using , virtual , break , case , switch , friend, etc.
**Each of these keywords is used for a special function in C++.**
_Tokens part 1 is over. See you campers at [Part 2](https://guide.freecodecamp.org/cplusplus/tokens-part-II) of Tokens :)_
**Good Luck to all of you**
**Happy Coding ! :)**
**Feel free to ask any queries on FreeCodeCamp's GitHub page or [FreeCodeCamp's Forum .](https://forum.freecodecamp.org/)**

View File

@ -0,0 +1,125 @@
---
title: Variables
---
Let's discuss something know as variables. Variables are like a bucket. You can put something in it and then change it
afterwards when needed.
In C++, there are many types of variables like Integers, Strings, Booleans and many other.
Let's look at a simple program using integer variables. Integers store whole numbers that are positive, negative or zero. Whole numbers are not fractional numbers for example 1/2, 1/4, and 1/5. Lets look at a simple program which uses an integer
variable.
```cpp
#include <iostream>
using namespace std ;
int main()
{
int a; // Declare an integer variable a
a = 5; // Assign value of 5 to variable a
cout << a; // Display the value of variable a which contains 5
return 0;
}
```
When you execute this program, you will see 5 displayed on the screen
* Note that in the above program // is placed after the lines. The symbol "//" is for commenting our code. Code after the symbol
"//" is not execueted in the line where its placed.
* On line 5 n simple integer variable is declared.
* On line 6 the value 5 is assigned to the variable a. Now whenever we use the variable a in our program its value will be 5
unless we change it.
* On line 7 we display the value of variable a and 5 is printed on the screen.
### Scope of Variables
All the variables have their area of functioning, and out of that boundary they don't hold their value, this boundary is called scope of the variable. For most of the cases its between the curly braces,in which variable is declared that a variable exists, not outside it. We will study the storage classes later, but as of now, we can broadly divide variables into two main types,
*Global Variables.
*Local variables.
#### Global variables
Global variables are those, which ar once declared and can be used throughout the lifetime of the program by any class or any function. They must be declared outside the main() function. If only declared, they can be assigned different values at different time in program lifetime. But even if they are declared and initialized at the same time outside the main() function, then also they can be assigned any value at any point in the program.
Example : Only declared, not initialized.
```cpp
include <iostream>
using namespace std;
int x; // Global variable declared
int main()
{
x=10; // Initialized once
cout <<"first value of x = "<< x;
x=20; // Initialized again
cout <<"Initialized again with value = "<< x;`
}
```
#### Local Variables
Local variables are the variables which exist only between the curly braces, in which its declared. Outside that they are unavailable and leads to compile time error.
Example :
```cpp
include <iostream>
using namespace std;
int main()
{
int i=10;
if(i<20) // if condition scope starts
{
int n=100; // Local variable declared and initialized
} // if condition scope ends
cout << n; // Compile time error, n not available here
}
```
Now let's read about a new type of variable-
#### Static variable
Static variables : When a variable is declared as static, space for it gets allocated for the lifetime of the program. Even if the function is called multiple times, space for the static variable is allocated only once and the value of variable in the previous call gets carried through the next function call. This is useful for implementing coroutines in C/C++ or any other application where previous state of function needs to be stored.
In layman terms , it means that a normal variable when goes out of scope looses it's identity (value) , but a static variable has a global scope and retain it's value till end of program , but unlike global variable it is not necessary to declare it at start of program.
#### EXTRA-
Static is a keyword in C++ used to give special characteristics to an element. Static elements are allocated storage only once in a program lifetime in static storage area. And they have a scope till the program lifetime.
#### CODE-
```
#include <iostream>
#include <string>
using namespace std;
void howstaticworks()
{
static int count = 0; // static variable
cout << count << " ";
/* value is updated and
will be carried to next
function calls*/
count++;
}
int main()
{
for (int i=0; i<5; i++)
howstaticworks();
return 0;
}
```
#### Try yourself
just copy the code and paste it in link given.
Run on IDE- https://ideone.com/
Output:
0 1 2 3 4
You can see in the above program that the variable count is declared as static. So, its value is carried through the function calls. The variable count is not getting initialized for every time the function is called.
Let's give the same code a try by removing "static" keyword and guess the output and compare it with one on IDE.
The static is now converted into normal variable