C++, question on double variable

  • Context: C/C++ 
  • Thread starter Thread starter loli12
  • Start date Start date
  • Tags Tags
    Variable
Click For Summary
SUMMARY

In C++, when a variable of type double is assigned an integer value, such as 3, it is stored as 3.0. The C++ language utilizes the declared variable type for value assignment, ensuring that a double retains its decimal representation. Similarly, when a float variable is used, it will also store values as floats, preserving any decimal points in the input. This behavior is consistent across different variable types in C++.

PREREQUISITES
  • Understanding of C++ variable types (int, float, double)
  • Familiarity with C++ input/output operations (cin, cout)
  • Basic knowledge of data type conversions in C++
  • Awareness of how C++ handles floating-point arithmetic
NEXT STEPS
  • Explore C++ data type conversions and implicit casting
  • Learn about floating-point precision and representation in C++
  • Investigate the differences between float and double in C++
  • Study best practices for handling user input in C++ applications
USEFUL FOR

C++ developers, programming students, and anyone interested in understanding variable types and data handling in C++.

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
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.
 
thanks a lot!
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 8 ·
Replies
8
Views
5K
  • · Replies 25 ·
Replies
25
Views
4K
  • · Replies 118 ·
4
Replies
118
Views
10K
  • · Replies 6 ·
Replies
6
Views
13K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 16 ·
Replies
16
Views
7K