Using types and variables.
So types will be covered in more detail in the next lectures, but it's useful to give a basic information
here.
So Cplusplus is a strong type language as we mentioned earlier.
So which means that you have to declare the type of the variable that you use.
So the reason for this is that the compiler needs to know how much memory to allocate for the variable,
and it can determine this by the type of the variable.
So in addition to the compiler needs to know how to initialize a variable if it has not been explicitly
initialized.
And to perform this initialization, the compiler needs to know the type of the variable so the cplusplus
11 provides with the auto keyword which you will learn.
So which relaxes this concept of strong typing and it will be covered in the next chapters.
So however, the type checking for the compiler is so important that you should use type checking as
much as possible so cplusplus variables can be declared anywhere in your code as long as they are declared
before they are used.
So where you declare a variable determines how you use it.
So this is called the scope of variable.
So in general, it is best to declare the variable as close as possible to where you will use it.
And within the most restrictive scope, this prevents name clashes where you will have to add additional
information to disambiguate two or more variables.
So you may and should give your variables descriptive names.
So this makes your code much more readable and easier to understand.
So Cplusplus must start with the alphabetic character or an underscore so you can start.
So for example, Cplusplus starts.
I have to start with alphabetic character or an underscore so you can write variables like this.
INT for example.
Integer my variable.
And here, as you can see, there is no error at all.
And you can write your variables by.
Mhm.
To upper score variable.
My variable.
To you can underscore my very well.
Two and integer my.
My variable.
You're here.
So as you can see here, these are names are the almost same, except there's a underscore here.
But these three names are same.
But as you can see, we didn't get an error If you write this exactly like this underscores and underscores
like this, for example, you will get an error because of this redefinition.
So we have to remove the previous declaration of a local variable, as Cplusplus suggests.
So let's compile it and see what happens.
So the declaration of an integer variable note here, integer variable previously declared here.
When we click on this here, as you can see, we can not declare the same names, but if we change this
here, this alphabet here with make it and make it underscore they are no longer the same variable here
as let's try it.
So.
Here.
And the C plus plus names are case sensitive.
As I said earlier, and the first
2084, 2048 characters are significant.
So you can start variable name with an underscore, but you cannot use two underscores here, as you
can see here.
Variable here.
So now you can use an underscore followed by a capital letter here.
As you can see, local variable is only assigned but never accessed.
Here.
We got no error here.
So and clearly you cannot use type name as variable names either built in type names, for example,
integer.
So we can use the long as a variable name because it's a keyword and it's reserved for it's reserved
keyword.
So or we cannot use while or other keywords in our language here, as you can see here.
So you declare a variable in a statement ending with a semicolon here, as you can see here.
So the basic syntax of declaring a variable is that you specify the type, then the name and optionally
an initialization variable.
Here, this is a type name and initialization variable.
So built in types must be initialized before you use them.
As you can see here, Integer E here, let's increment E by one and here STD see out and E here.
And let's actually let's make a return statement here and let's run this program here.
As you can see, we got an error.
We got not error.
This is a correct program.
This has this program compiled successfully.
And as you can see here, there is a output one.
So this gave us output one, this is the zero by default, as you know earlier.
And we incremented increment by one here, as you can see here.
And we and so zero plus one equals one.
And we got this one here.
So there are essentially three ways to initialize variables.
You can assign a value, you can call a type, you can call the type constructor, or you can initialize
a variable using function syntax.
By the way, you will learn constructors for classes in next lectures.
So let's create some program here, some syntax here, The code here A equals one and assign it one
integer be integer two and integer C here three.
So now I want to explain what these are.
So obviously they are almost they they we can compile our program without a problem and actually E equals
to one, B equals to two and C equals to three.
I will show you what these are here.
A.
So firstly, a.
Then the AB.
Okay, then.
See?
And and yeah that's it.
And new line here.
So let's A, B, C and let's compile our program.
UPS.
Yeah.
Let's delete this.
C.
A.
B.
C.
Here.
As you can see here, we got this and there are three legal cplusplus.
But statistically, the first is the better because it is more obvious.
So the variable is an integer.
It's called a and it's assigned to value one.
The third look confusing.
As you can see here, it looks like the declaration of function when it is actually declaring variable.
The next lecture will have a a variation of assigning a value using the initialization list syntax.
So is the reason why you will want to do this will be you will learn that in the next sections.
So.
And and also you will learn classes will cover classes, your own custom types.
So a custom type may be defined to have a default value, which means that you have you may decide not
to initialize a variable of a custom type before using it.
So however, this will result in a poorer performance because the compiler will initialize the variable
with a default value and subsequently your code will sign a value.
So resulting in a sign being performed twice here.
So let's let's print this with here.
Let's print this.
So let's print this with.
Uh, see our function here.
See out.
Stdcout.
Stdcout here.
And C here.
Oops.
As you can see here, if we delete this, see, as you can see, we got an error format specifier w.r.t.
The argument has a type init here.
As you can see here, our program is.
Fixed here.
And let's delete this also here.
Actually, no.
Here.
As you can see the container.
So I want to say why we got this hurt and this strange number.
It's because of we need to.
And as you can see there, a C here.
Let's delete this and you will see our program has fixed the error here.
We used C format specifier.
So if you put this operator before in printf function before any alphabet here you this C will turn
into just some format specifier here.
That's because you have to do the space here and our code will fix here as you can see here.
Let's run it again.
And as you can see, we got an A one, two, three and there is a three times three assignment ways
in C plus plus.
So I'm waiting you in next lecture.