The Big Master

Defining variable at the time of use

In C++, you can also define variables at the time of use,...

In C++, you can also define variables at the time of use, which is known as “inline variable declaration” or “deferred variable definition.” This allows you to declare and define a variable right where it’s needed, instead of declaring it at the beginning of a block or function.

To define a variable at the time of use, you simply provide the variable declaration and initialization within the scope where you want to use the variable. Here’s an example:

#include <iostream>
Using namespace std ;
int main() 
{
    int x = 10 , y = 20 ;
   Int z = x + y;                     // Variable defined and initialized at the time of use
    cout << "The value of z is: " << z << endl;
    return 0;
}


PROGRAM 2
#include <iostream>
using namespace std;
int main()
{
    int	x = 10.1 ;
    int y = 20.1;	
    cout << x + y  << endl ;	    // output is 30 not 30.2 , int will truncate decimal values.
    return 0;
}