C Programming: Output of Float Calculation

Click For Summary
SUMMARY

The output of the provided C program is 1.0 due to integer division. In the expression 6/4, both operands are integers, resulting in an integer output of 1 before being converted to a float. This behavior is a fundamental aspect of C programming, where integer operations yield integer results. Understanding this concept is crucial for accurate float calculations in C.

PREREQUISITES
  • Understanding of C programming syntax and structure
  • Knowledge of data types in C, specifically integers and floats
  • Familiarity with the concept of integer division
  • Experience with the printf function for output formatting
NEXT STEPS
  • Research the rules of type conversion in C programming
  • Learn about floating-point arithmetic and precision in C
  • Explore the implications of integer division in mathematical operations
  • Practice using the printf function with various format specifiers
USEFUL FOR

C programmers, computer science students, and anyone looking to deepen their understanding of data types and arithmetic operations in C.

ubuntu2
Messages
7
Reaction score
0
The question is
What will be the output?
#include<stdio.h>
int main(void)
{float y=6/4;
printf("%.1f\n",y);}
The answer is
1.0
I got that right by trusting my senses:biggrin:; but I want to understand why.
 
Last edited:
Physics news on Phys.org
C does maths in the 'simplest' way possible, so 6/4 is done as an integer division = the answer is an integer which is then converted to a float.
 
right! integer operated by integer results in integer!
thanks a lot.
 

Similar threads

  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
7
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 1 ·
Replies
1
Views
11K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K