C++, question on double variable

  • C/C++
  • Thread starter loli12
  • Start date
  • Tags
    Variable
In summary, C++ is a programming language commonly used for developing efficient and high-performance software. It supports various data types, including the double variable, which is used to store large decimal numbers. The double variable has a higher precision than the float data type, allowing for more accurate calculations and storage of larger values. However, it also requires more memory to store these values.
  • #1
loli12
I want to know if i use double variable and the input value is an integer, will that variable be stored as a decimal? How about for float variable?

ex. double x, and cin 3 for x, will x be stored as 3 or 3.0?

Thanks
 
Technology news on Phys.org
  • #2
C++ will used the declared variable type when assigning values to said variable. So, if you had int x and you tried to store 3.14159 x would end up holding 3; likewise, float x would store all values as floats.
 
  • #3
thanks a lot!
 

1. What is a double variable in C++?

A double variable in C++ is a data type that is used to store floating-point numbers with a higher precision compared to the float data type. It can hold larger values and has a higher range of decimal places.

2. How do you declare a double variable in C++?

In C++, a double variable can be declared by using the keyword "double" followed by the variable name. For example, "double myDouble = 3.14159;".

3. What is the difference between a double and float variable in C++?

The main difference between a double and float variable in C++ is the precision. A double variable has a higher precision and can store larger values and more decimal places compared to a float variable.

4. Can a double variable store integer values?

Yes, a double variable in C++ can store both integer and floating-point values. However, when storing an integer value, it will still be stored as a floating-point number with a decimal point of ".0".

5. How do you perform calculations with double variables in C++?

To perform calculations with double variables in C++, you can use the standard arithmetic operators such as +, -, *, and /. For more complex calculations, you can also use built-in math functions from the cmath library.

Similar threads

  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
10
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
3
Views
727
  • Programming and Computer Science
Replies
10
Views
4K
  • Programming and Computer Science
Replies
1
Views
646
  • Programming and Computer Science
2
Replies
47
Views
3K
Replies
63
Views
3K
  • Programming and Computer Science
Replies
16
Views
4K
Back
Top