Values and Variables in c++

Values and Variables in c++

In this chapter we explore some building blocks that are used to develop C++ programs.

Integer Values

The number four (4) is an example of a numeric value. In mathematics, 4 is an integer value. Integers are whole numbers, which means they have no fractional parts, and an integer can be positive, negative, or zero. Examples of integers include 4,−19, 0, and−1005.

Code Example

#include <iostream>
int main() 
{ 
    std::cout << 4 << '\n'; 
}
std::cout << "4\n";

sends one thing to the output stream, the string”4\n”. The statement

std::cout << 4 << ‘\n’;

sends two things to the output stream, the integer value 4 and the newline character’\n’.

#include <iostream>
int main() 
{ 
     std::cout << -3000000000 << '\n';
}
output  1294967296

Variables and Assignment

In algebra, variables are used to represent numbers.

#include <iostream>
int main() 
{ 
    int x; x = 10; std::cout << x << '\n'; 
}
int x; 

This is a declaration statement. All variables in a C++ program must be declared. A declaration specifies the type of a variable. The word int indicates that the variable is an integer. The name of the integer variable is x. We say that variable x has type int.

#include <iostream>
int main() 
{ 
    int x; 
    x = 10; 
    std::cout << x << '\n'; 
    x = 20; 
    std::cout << x << '\n'; 
    x = 30; 
    std::cout << x << '\n'; 
}
int x = 0; 
int y; 
int z = 5;

In the case of multiple declaration statements the type name (hereint) must appear in each statement. The compiler maps a variable to a location in the computer’s memory. We can visualize a variable and its corresponding memory location as a box.

define a variable in c++ diffarent type
define a variable in c++ diffarent type AndroWep-Tutorials

Identifiers

While mathematicians are content with giving their variables one-letter names likex, programmers should use longer, more descriptive variable names. Names such as altitude, sum, and user_name are much better than theequally permissible a, s, andu. Avariable’s name should be related to its purpose within the program. Good variable names make programs more readable by humans. Since programs often contain many variables, well-chosen variable names can render an otherwise obscure collection of symbols more understandable.

Identifiers in c++ language AndroWep-Tutorials
Identifiers in c++ language AndroWep-Tutorials