can anyone please tell me what is the specifier for float and double datatypes? i know its %d for int datatypes and %c for char datatypes. unable to find it anywhere as well. thanks in advance!!!
can anyone please tell me what is the specifier for float and double datatypes? i know its %d for int datatypes and %c for char datatypes. unable to find it anywhere as well. thanks in advance!!!
%f and %lf
1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
3. Get rid of conio.h and other antiquated DOS crap headers.
4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.
printf family: %f for both float and double.
scanf family: %f for float and %lf for double.
google for "scanf man" or "printf man" depending on what type of function you want to use.
i am facing this problem actually which i thought was due to a mistake in my %specifier but after making the correction also the problem persisted. the problem is whatever the value of the 4 variables i give the answer always comes out to be 0.0000. here is the code and everything:
Code:/*very basic program. Just revising for fun*/ /* c program to make the code for the following question: the frequency 'n' of a vibrating string which depends on length 'l', tension'T',and linear density 'm' is given by n=(k/l)sqrt(T/m) where k is a constant.write a c program to find n when l=50,60,70,80 cm. i might be programming for any value of l*/ #include<stdio.h> #include<conio.h> #include<stdlib.h> #include<math.h>/*the maths library*/ int main() { /*i will be writting this for integer values. since it is a program to be used for scientific calculations, it would be better to use floats or doubles but for the sake of simplicity, the compensation.*/ int length,tension,linear_density,constant; float frequency=0;/*i am forced to make the frequency as float since the answer will be comming in decimals*/ char ch; printf("\n Enter value of tension:"); scanf("%d",&tension); printf("\n Enter value of linear density:"); scanf("%d",&linear_density); printf("\n Enter value of constant:"); scanf("%d",&constant); while(1) { printf("\n Enter value of length:"); scanf("%d",&length); frequency=(constant/length)*sqrt(tension/linear_density); printf("\n Frequency is given by:%f",frequency); fflush(stdin); printf("\n Continue:"); scanf("%c",&ch); if(ch=='n')break; } getch(); return 0; }
Probably because you are dividing an int by an int here:
This will NOT yield a decimal number, it will yield a whole number in floating point form. Eg, if constant is 5 and length is 3, frequency will be 0.0f.Code:frequency=(constant/length)
You need to cast the ints to get the arithmetic to work properly:
Code:frequency=(float)constant/(float)length
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
<code>
/*very basic program. Just revising for fun*/
/* c program to make the code for the following question:
the frequency 'n' of a vibrating string which depends on length 'l',
tension'T',and linear density 'm' is given by n=(k/l)sqrt(T/m) where
k is a constant.write a c program to find n when l=50,60,70,80 cm. i might
be programming for any value of l*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>/*the maths library*/
int main()
{
/*i will be writting this for integer values. since it is a program
to be used for scientific calculations, it would be better to use floats
or doubles but for the sake of simplicity, the compensation.*/
int length,tension,linear_density,constant;
float frequency=0;/*i am forced to make the frequency as float since the
answer will be comming in decimals*/
char ch;
printf("\n Enter value of tension:");
scanf("%d",&tension);
printf("\n Enter value of linear density:");
scanf("%d",&linear_density);
printf("\n Enter value of constant:");
scanf("%d",&constant);
system("pause");
while(1)
{
printf("\n Enter value of length:");
scanf("%d",&length);
// This is what i added...
printf("--> %d\n", constant);
printf("--> %d\n", length);
printf("%f", sqrt(tension/linear_density));
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
frequency=(constant/length)*sqrt(tension/linear_density);
printf("\n Frequency is given by:%f",frequency);
fflush(stdin);
printf("\n Continue:");
scanf("%c",&ch);
if(ch=='n')break;
}
getch();
return 0;
}
</code>
check this!
Maybe this helps others read it. I've learnt this from someone .
You are doing integer division instead of floating point division
1) Change this:
printf("%f", sqrt(tension/linear_density));
to this:
printf("%f", sqrt((tension + 0.0)/linear_density));
2) Change this:
frequency=(constant/length)*sqrt(tension/linear_density);
to this:
frequency=((constant+0.0)/length)*sqrt((tension+0.0)/linear_density);
3) Remove this:
fflush(stdin);
It has undefined behavior.
Last edited by claudiu; 05-08-2010 at 12:01 PM.
1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
3. Get rid of conio.h and other antiquated DOS crap headers.
4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.