
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Assigning an integer to float and comparison in C/C++
The integer is a data type used to define a number that contains all positive, negative or zero non-fractional values. These cannot have decimals.
Float is a data type used to define a number that has a fractional value. These can have decimals also.
Now, we will check what will be the value of float and integer return by the compiler when we input the same value for both.
Example
#include <iostream> using namespace std; int main(){ float f = 23; unsigned int x = 23; cout<<"Float f = "<<f<<endl; cout<<"Integer x = "<<x<<endl; f = 0xffffffff; x = 0xffffffff; cout << "f = " << f << endl; cout << "x = " << x << endl; return 0; }
Output
Float f = 23 Integer x = 23 f = 4.29497e+09 x = 4294967295
Here in this code, we can see that if we pass an integer value to a float then it will act as an integer and returns an integer value as output. But the max values for both of them come to be different.
Now, let’s see what if we initialize integer variable with float value.
Example
#include <iostream> using namespace std; int main(){ float f = 23.768; unsigned int x = 23.768; cout<<"Float f = "<<f<<endl; cout<<"Integer x = "<<x<<endl; return 0; }
Output
Float f = 23.768 Integer x = 23
In this condition too the program compiled and run. The integer variable discarded the decimal point values of the initialize float value and gets initialized with the integer value of it.
Now, let's compare these values −
Example
#include <iostream> using namespace std; int main(){ float f = 0xffffffff; unsigned int x = 0xffffffff; if(f == x ){ cout<<"TRUE"; } else cout<<"FALSE"; return 0; }
Output
TRUE
- Related Articles
- Comparison of double and float primitive types in C#
- What is the most effective way for float and double comparison in C/C++?
- C++ program to take integer and float as input and return their sum
- Assigning multiple characters in an int in C language
- Comparison of Float in Java
- Most Profit Assigning Work in C++
- Float and Double in C
- Comparison of double and float primitive types in Java\n
- Convert string to integer/ float in Arduino
- How to Read and Print an Integer value in C++
- Relational and comparison operators in C++
- C/C++ Program to Count set bits in an integer?
- How to convert float to integer in Python?
- Difference between float and double in C/C++
- How to compare float and double in C++?
