unable to remember

This is a discussion on unable to remember within the C Programming forums, part of the General Programming Boards category; can anyone please tell me what is the specifier for float and double datatypes? i know its %d for int ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    26

    unable to remember

    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!!!

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    %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.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    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.

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    26

    the actual problem

    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;
    }

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Probably because you are dividing an int by an int here:
    Code:
    frequency=(constant/length)
    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.

    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

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    81
    <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!

  7. #7
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by nullifyed View Post
    <code>

    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 01: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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. does anyone remember computer shows?
    By doubleanti in forum General Discussions
    Replies: 7
    Last Post: 06-26-2009, 05:55 PM
  2. Replies: 4
    Last Post: 04-09-2009, 11:11 AM
  3. Remember Turbo? It is comming back
    By Mario F. in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2006, 02:26 PM
  4. Replies: 10
    Last Post: 03-19-2003, 02:15 PM
  5. Unable to start pppd
    By taps in forum Linux Programming
    Replies: 5
    Last Post: 10-11-2001, 12:26 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21