C Programming - Declarations and Initializations - Discussion

1. 

Which of the following statements should be used to obtain a remainder after dividing 3.14 by 2.1 ?

[A]. rem = 3.14 % 2.1;
[B]. rem = modf(3.14, 2.1);
[C]. rem = fmod(3.14, 2.1);
[D]. Remainder cannot be obtain in floating point division.

Answer: Option C

Explanation:

fmod(x,y) - Calculates x modulo y, the remainder of x/y.
This function is the same as the modulus operator. But fmod() performs floating point divisions.

Example:


#include <stdio.h>
#include <math.h>

int main ()
{
  printf ("fmod of 3.14/2.1 is %lf\n", fmod (3.14,2.1) );
  return 0;
}

Output:
fmod of 3.14/2.1 is 1.040000


Shan said: (Sep 16, 2010)  
What is the work of the fmod?

Nidhi said: (Nov 11, 2010)  
The answer could be option A (i.e) rem = 3.14 % 2.1 ?

Chandra said: (Dec 3, 2010)  
% operator represents Reminder in C Language
But it is not possible for floating point operations.So,the Answer would be option (d).

(d)Remainder cannot be obtain in floating point division.

Nagendran said: (Dec 8, 2010)  
Good explanation

Jay said: (Dec 22, 2010)  
I think chandra also correct in the sence of maths.

Arvind said: (Jan 17, 2011)  
%(modulo) operator is in arithmatic
fmod is the c function

Swathy said: (Jan 20, 2011)  
Yes. For int only we can use mod for float we should use fmod the answer should be c only for this problem.

Sandeep said: (Jan 25, 2011)  
What Swathy said would be the exact explanation.

Renuka said: (Feb 15, 2011)  
Yes fmod is the correct ans as in for floating division.

Mahesh said: (Feb 21, 2011)  
What is use of fmod() function ?

Deepak Sharma said: (Mar 8, 2011)  
That is right answer because the given expression is in the floating type formate so witought fmod() is use that not give right answer so fmod() is used.

V!Nu said: (Mar 18, 2011)  
Nice explanation. I agree with that answer.

Pappu said: (Apr 4, 2011)  
Ya, swathi answer is correct.

Ram Krishna said: (Apr 20, 2011)  
Ya. Its nice ans we use % and mod () , fun for int, type but not for float hence fmode is right answer.

Kavita.C.Karjagar said: (Apr 26, 2011)  
What is the function of fmod and were we can use fmod?

Kavita.C.K said: (Apr 26, 2011)  
What is the difference between %, mod(), fmod()and modf()?

Prathamesh M. Patkar said: (Apr 29, 2011)  
Yes, because the given values are in floating type so we also want the ans in floating type therefore we use fmod().

Sivakumar said: (Jun 14, 2011)  
I want to know clear difference between fmod() and modf().

Vijay said: (Jul 3, 2011)  
What is the difference between %, mod(), fmod() and modf()?

Ratnaprakash said: (Jul 7, 2011)  
Me too want to know about fmod&modf.

Prem Kumar said: (Jul 15, 2011)  
modf->Breaks x into two parts: the integer part (stored in the object pointed by intpart) and the fractional part (returned by the function).

Each part has the same sign as x.

Example
1
2
3
4
5
6
7
8
9
10
11
12
13
/* modf example */
#include <stdio.h>
#include <math.h>

int main ()
{
double param, fractpart, intpart;

param = 3.14159265;
fractpart = modf (param , &intpart);
printf ("%lf = %lf + %lf \n", param, intpart, fractpart);
return 0;
}


Output:

3.141593 = 3.000000 + 0.141593

Velmani said: (Jul 17, 2011)  
% (%d)is used for only integer datatype. % is does not used for float datatype. So if you need mod in deciamal number then you can use fmod(%f) function.

Saikiran said: (Jul 18, 2011)  
#include<stdio.h>
#include<math.h>

int main()
{
float i,j;
i=3.14;j=2.1;
printf("\ni modulo j is %f",fmod(i,j));
return 0;
}

O/P:
i modulo j is 1.040000

Saikiran said: (Jul 18, 2011)  
What Prem Kumar comented is correct.

Mounika said: (Jul 19, 2011)  
Yes renuka is right.

Hemanth Kumar K said: (Jul 23, 2011)  
fmod() function with values of floating type is the only operator which can be used to obtain the remainder for floating numbers.

Riyaz said: (Jul 31, 2011)  
#include<stdio.h>
#include<conio.h>
void main()
{
float a,b,c;
c=a%b;
printf("%f",c);
}

Harshada said: (Aug 8, 2011)  
Yes Prem Kumar is right.

Nagesh said: (Aug 25, 2011)  
Your answer may be depends on the Statement i.e. both % operator as well as fmod() used for taking Reminder %op for integer and fmod() for float.

Jcoder said: (Sep 5, 2011)  
fmode() is the lib function, returns the reminder of floating point.

Keerthi said: (Sep 12, 2011)  
Nice explanation given by premkumar now its very clear.

Anitha said: (Sep 27, 2011)  
Hai friends, mod() is used for the integer division to get remainder. Where as fmod() is used to get floating point remainder. We find both mod() and fmod()in math.h header file check it once.

Sagar said: (Oct 5, 2011)  
What is the difference between %, mod(), fmod() and modf()?

Abhishek said: (Nov 11, 2011)  
Thanks Anitha for your Answer.

Narayana Reddy said: (Nov 20, 2011)  
Is it possible take this in to EXE file to run other than c compiler please tell me.

Raku said: (Jan 2, 2012)  
@Nayarana it is not possible.

Ajinkya said: (Jan 14, 2012)  
What is the difference between

rem = modf(3.14, 2.1);
rem = fmod(3.14, 2.1);

?

Rajasekar said: (Jan 26, 2012)  
Its very nice.

Dipak said: (Jan 28, 2012)  
I think, an % sign also can done it !

Rohini Palanichamy said: (Jan 29, 2012)  
@Ajinkya , see @pramkumar's explanation , you will clear.

Sreedhar said: (Feb 1, 2012)  
Hi friends, please help me how give below program output -80 ?

#include<stdio.h>

#define ABC 20
#define XYZ 10
#define XXX ABC - XYZ

void main()
{

int a;

a = XXX * 10;

printf("%d\n", a);

}

Sundar said: (Feb 1, 2012)  
@Sreedhar

Step by step process:

a = XXX * 10
a = ABC - XYZ * 10
a = 20 - 10* 10
a = 20 - 100
a = -80

Amar said: (Feb 13, 2012)  
Thanks for your suggestions.

Krishna said: (Jun 14, 2012)  
modf(x, y) is used to split the float point in to the intpart and factpart.

Ex: 3.13=3.00+0.13

fmod(x,y) is used to find the remainder in floating points.

Ex: fmod(3.14,2.1)

Bharadwaj said: (Jun 14, 2012)  
Please clarify my doubt.

What is the meaning of "External declaration and Where is use" ?

Manoj Kumar said: (Jul 25, 2012)  
External declaration means Declaring any variable before main() function or after main() function but not in main() function.

Example :

#include<stdio.h>

int S_no ;

main()
{
S_no=10;
printf ("%d\n",S_no);
}
Here I declared S_no Externally

Jagadeesh Kumar said: (Aug 19, 2012)  
It's nothing but difference between numerator and denominator.

Vinod Kumar said: (Aug 21, 2012)  
fmod(x,y) is floating point remainder of x/y,with the same sign as x.If y=0 ,the result is implementation-defined
e.g.

#include <stdio.h>
#include <math.h>

int main ()
{
printf ("fmod of 3.14/2.1 is %lf\n", fmod (3.14,2.1) );
return 0;
}
ans output:fmod of 3.14/2.1 is 1.040000

Pooja Papule said: (Aug 24, 2012)  
But % operator also gives remainder in output.... So, we can also use %....

Manikandan said: (Sep 20, 2012)  
What is the differnce b/w fmod and modf ?

Vishnu said: (Dec 10, 2012)  
Please any one can help me how to execute program using gcc compiler ?

Vani said: (Dec 18, 2012)  
#include<stdio.h>
main()
{
printf("%d",printf("ABC\"));
}

Can anyone please explain how it gives o/p as ABC4 ?

Siva Krishna said: (Dec 26, 2012)  
@Vani :

In the inner printf statement, there are 4 characters.
The output of a printf stmt is a integer. that number is equal to the number of Characters that the printf statement printed.

For example:

printf("%d",printf("ABC"));
o/p= ABC4
-----------
printf("%d",printf("A"));
o/p= A1
-----------
printf("%d",printf("AB"));
o/p= AB2
-----------
printf("%d",printf("ABC10"));
o/p= ABC105
(here 10 is taken as two different characters.)
-----------

Elakkiah said: (Jan 6, 2013)  
#include <stdio.h>
#include <math.h>

int main ()
{
float 3.13.2.1;
float R;
R=fmod(3.14,2.1);
printf ("The remainder is:",R);
scanf("%f",R);
return 0;
}

Bdp said: (Jan 7, 2013)  
% operator is not use for floating operations it is only used for integers.

Gautam said: (Jan 7, 2013)  
Please explain fmod() and modf ().

Pooja said: (Apr 5, 2013)  
Difference between fmode(), modef() and mode()?

Shubham Choudhary said: (Apr 19, 2013)  
Why did ansi form fmod() operator when % already was. It could be used for both the purpose. Integers as well as float?

Shakir Baba said: (Apr 29, 2013)  
Why array's base address is initialized to 0 and it terminates on -1?

Janani said: (Jun 18, 2013)  
What is the difference between an identifier and variable?

Ayapan said: (Aug 12, 2013)  
#include <stdio.h>
#include <math.h>

int main ()
{
printf ("fmod of 3.14/2.1 is %d\n", fmod(3,2) );
return 0;
}

I am getting output as 0. Anyone can explain it why?

Shambhu Kumar said: (Oct 10, 2013)  
What is wrong in this code why it will give segmentation fault?

#include <stdio.h>
void main()
{
register int x = 0;
if (x < 2)
{
x++;
main();
}
}

Dhara said: (Oct 17, 2013)  
What different between fmod & modf?

Sujitha said: (Oct 24, 2013)  
@Ayapan.

You have declared the fun as fmod(3, 2).

When 3 gets divided by 2 it gives 1.5 with remainder 0.

Pooja said: (Oct 24, 2013)  
@Vani.

printf inside the printf....firstly there is a abc and also a slace is a character literal so there it work as a character...and 1 char is 1 byte so 4 character print 4...on place of printf("%d",4 );

Due to %d it'll print 4.

Ragu said: (Feb 13, 2014)  
What is the difference between modf and fmod?

Sri said: (Feb 17, 2014)  
Why we can use the fmod? instead of that what word is apt for the execution.

Sugandha Singh said: (Apr 16, 2014)  
It means % operator is use when we are doing integer division to get the remainder?

Chillu said: (May 24, 2014)  
What is the difference between modf and fmod?

Dinesh Kumar I said: (Jun 19, 2014)  
The output of following program is:

#include<stdio.h>
int main()
{
int a=1;
printf("%d %d %d",a,a++,++a);
return 0;
}
Output : 3 2 3.
But logically 3 2 2.

Can any explain how 3 2 3 comes?

Asif Sadrul said: (Jun 21, 2014)  
@Ayapan.

Use %f instead of %d.

Grub said: (Jun 25, 2014)  
fmod() outdated and is not supported by gcc compilers hence answer is(D).

Prashant said: (Jun 26, 2014)  
% in c language represents remainder after division,

The number can be of integer or float but in global declaration reminder should be declared in float irrespective of declared variables.

Hamir Bharwad said: (Jul 5, 2014)  
fmod(x,y) function is used to get remainder of floating point divisions.

Vignesh said: (Jul 8, 2014)  
What is the work of fmod?

Manjunath K said: (Jul 11, 2014)  
Hai friends, mod() is used for the integer division to get remainder. Where as fmod() is used to get floating point remainder. We find both mod() and fmod()in math.h header file check it once.

Vinay said: (Jul 14, 2014)  
What is the difference between modf and fmod?

Sangee said: (Jul 19, 2014)  
Where we want to use #include<math.h>?

Aafreen said: (Jul 24, 2014)  
What if 3.14 % 2? What to use?

Prithvi said: (Oct 20, 2014)  
#include <stdio.h>
#include <math.h>

int main ()
{
int rem;
rem=3.14/2.1;

printf ("rem= %d\n", rem);
return 0;
}

0p:1.

Vikas said: (Oct 28, 2014)  
What is the use of return 0 there?

Prasann said: (Dec 26, 2014)  
For floating point operator % cannot be used. Only operator/can be used.

Ajay said: (Dec 29, 2014)  
Do we have both modf() and fmod() in C language?

Sivaraman said: (Jan 1, 2015)  
It's really not understand me, please briefly explain it for modf() and fmod().

Ajith said: (Jan 23, 2015)  
What is REM?

Prtk15 said: (Jan 30, 2015)  
There is a new data-type which can take as values natural numbers between (and including) 0 and 25. How many minimum bits are required to store this data-type?

What will be its answer? Can anybody help me?

Anjali said: (Feb 12, 2015)  
What is return 0?

Md Imran Khan said: (Feb 27, 2015)  
Which function is called global and local function? Can anybody help me?

Md Imran Khan said: (Feb 27, 2015)  
Why array index is always start with zero? Please reply any body?

Mani said: (Mar 10, 2015)  
2)16(8
16
------------
0

Rem = 0.

Trupti Patil said: (Aug 1, 2015)  
Function written inside block is called local function & used only within that block. Function declared before main program starts below the header files is called global function.

Scope of local function is only within that part of the program it can not be access in other function.

Rajashekhar said: (Aug 6, 2015)  
int main()? What's the function it is?

Shalivahana said: (Aug 6, 2015)  
fmod() function or keyword is find used to find out.

Gayathridhiravidamani said: (Aug 15, 2015)  
#include<stdio.h>
main()
{
float a=3.14,b=2.1,c;
c=a%b;
pf("%d",c);
output: invalid operand to binary%
BUT
int a;
a=3.14%2.1;
pf("%d",a);

Output 1:

I think so, comma act as a operator. Please clear explanation.

Rey said: (Aug 27, 2015)  
What is REM?

And I also need some information about what is the difference between.

Rem = 3.14% 2.1; and rem = fmod (3.14, 2.1);

Amer Patel said: (Sep 16, 2015)  
If you use % for fining remainder it will give you an integer value.

But the fmmod() function gives you remainder in floating point value.

Madhuri said: (Sep 30, 2015)  
Nice explanation but I am not clear about increment.

Kalpanadevi said: (Oct 17, 2015)  
What is the maximum numbers of parameters that can be passed in printf and scanf?

Deepika said: (Nov 2, 2015)  
What is difference between fmod and modf?

Vela said: (Dec 25, 2015)  
Given explain is well.

Priyanka said: (Dec 26, 2015)  
Modulus support only integers. For ex: If we take 16.25/2.5 then in modulus it take only 16 from 16.25 and from 2.5 it takes 2 and gives answer 4. It performs on integers only.

fmod gives reminder in float.

Hope it clear.

Karri Jagapathibabu said: (Mar 11, 2016)  
Please tell me what is the use of fmod?

Shikha said: (May 22, 2016)  
Is fmod used in PHP?

Bhanu said: (May 30, 2016)  
Nice explanation @Prem Kumar. Thank you.

Angela said: (Jun 7, 2016)  
This is the synopsis for fmod and fmodf:

#include <math.h>
double fmod(double x, double y)
float fmodf(float x, float y)

//The fmod() function computes the floating-point remainder of x/y.

An application wishing to check for error situations should set error number to 0 before calling fmod(). If an error is non-zero in return, or the return value is NaN, an error has occurred.

The fmodf() function is a single-precision version of fmod().

Ron said: (Jul 21, 2016)  
@Gayathridhiravidamani.

In your first program, you have declared the variable a, b & c as float but you have used the format specifier as %d. You cannot use %d for floating point numbers. You have to use %f for float. That is why your program is showing error.

In your second program, you have done it rite, but you took % for finding the remainder of 3.14% 2.1% in for int, for float you have to use fmod () and you have use %d there. So, the compiler will take 3 from 3.14 and 2 from 2.1. So, 3%2 is 1.

Ron said: (Jul 21, 2016)  
@Ajith and @Rey

rem is just the variable name for Remainder.It is not a function or anything and it's not compulsory to write rem. You can use any variable name. Like R, A, Remain, B etc like literally anything expect keywords.

@Anjali

return 0 is used to denote that you are not returning any arguments from that function.
Eg; you declare int main it means that your main is of int datatype so it must return some value.
but if you declare return 0 it says you are not returning any value. You can use void instead of writing return 0.

Example 1:

int main()
{
printf("My name is Ron\n");
return 0;
}

Example 2:

void main()
{
printf("My name is Ron\n");
}

Gaurav said: (Aug 3, 2016)  
fmod() function is used to find out remainder of float numbers, not integer and normal operation like rem=12%7, it gives remainder only integer not float.

So option 1 is wrong and the correct is c.

I think you understand.

Karthi said: (Oct 1, 2016)  
What does the fmod represent here?

How could the answer be c but not a?

Vishal said: (Nov 22, 2016)  
C is the correct answer as % operator don't work on floating point numbers so A is incorrect.

Vikas said: (Nov 23, 2016)  
#include<stdio.h>
#includ<conio.h>
void main()
clrscr();
{
float a,b,c;
c=a%b;
getch();
}

Please explain the program with its output.

Vidya said: (Feb 5, 2017)  
Usually % (modules) operator not for floating point numbers, so in c fmod is inbuilt function that will use to get the reminder of the floating point number. Answer is C.

Keerthana said: (Mar 17, 2017)  
% - it doesn't support real numbers that is a float and double.

fmod is an inbuilt function that finds the remainder of real number.

Renu said: (Apr 3, 2017)  
Modulus & division are different.

Kennedy Muriuki said: (Apr 25, 2017)  
I support answer C, fmod should be used in float where we need decimal points.

Amarjeet said: (Jun 19, 2017)  
I think the answer A is correct.

Bhargav Dave said: (Jul 27, 2017)  
Hello, I am new on this.

Can anyone explain to me how this program work?

Afsia said: (Aug 10, 2017)  
Difference between modf and fmod is fmod gives remainder when x divided y. Fmod stands for floating modulus while in modf the parts spilt in two modfvalue into an integer and a fractional part. The fraction is returned by the modf function and the integer part is stored in the iptr variable. Iptr means pointer to the variable stored in an integer.

Banu said: (Aug 17, 2017)  
What is the correct answer for this question?

Tanushi said: (Sep 16, 2017)  
Please explain how this work?

Subrat said: (Oct 30, 2017)  
#include <stdio.h>
#include <math.h>

int main ()
{
float 3.13.2.1;
float R;
R=fmod(3.14,2.1);
printf ("The remainder is:",R);
scanf("%f",R);
return 0;
}

It will show error, because you write
float 3.13.2.

Subrat said: (Oct 30, 2017)  
@Dinesh Kumar.

#include<stdio.h>
int main()
{
int a=1;
printf("%d %d %d",a,a++,++a);
return 0;
}
Output : 3 2 3.
But logically 3 2 2.

Can any explain how 3 2 3 comes?

Your logic is true,function data passing is from right to left. So first ++a execute then a++,then a.
But the current value of a will be substitute in pre-increment operator always.
So the output will be 3 2 3.

Nayan said: (Dec 16, 2017)  
What is the meaning of %lf ? Or why not use %f only?

Venkat said: (Jan 3, 2018)  
#include<stdio.h>
#include<math.h>
main()
{
float a=1.2,b=2.3,c;
c=a%b;
printf("%f\n",fmod(a,b));
}
~

Test1.c:6:4: error: invalid operands to binary % (have \'float\' and \'float\')
user@user:~/c/test$

I have tested in GCC it gives error so finally conclusion is % this operator not possible for floating points

Shahi said: (Apr 27, 2018)  
What is the function %lf here?

Piyush said: (Jun 23, 2018)  
Why option A is incorrect?

S.Sasindra said: (Aug 14, 2018)  
Option A is also correct becuase;

int rem=3.14/2.1;
printf("%d",rem);
return();

Prerna said: (Aug 22, 2018)  
fmod is not working in GCC compiler. How can I use this in GCC compiler?

Please suggest me to get the output of this.

Varadaraj said: (Aug 30, 2018)  
As per my knowledge, it is option A.

Keerthi said: (Oct 31, 2018)  
Option A is correct.

Silambu said: (Feb 15, 2019)  
No @Keerthi.

The floating point remainder cannot be obtained by modulo operator.

Bhuvana said: (Jun 14, 2019)  
Option C is correct. Thanks

Krishna said: (Sep 19, 2019)  
Why option C is more relevant?

Bibhas Kumar Sahu said: (Oct 8, 2019)  
I don't understand this answer please explain me simply.

Renu said: (Oct 22, 2019)  
I don't understand this answer please explain me simply.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.