Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Beginner- Floating point character conversion character

Beginner- Floating point character conversion character

Scheduled Pinned Locked Moved C / C++ / MFC
questionlearningregexjsonlounge
5 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    BeingGokul
    wrote on last edited by
    #1

    Hi, I am no expert in C and I have started learning it again after several years. Excuse me for stupid questions. My question is simple. When i give %f as conversion character inside printf, but pass some integer to it, what happens? It prints some garbage value? or is there any pattern? Consider the below program..

    #include <stdio.h>
    main()
    {
    float gift1, gift2;
    float total;
    printf("How much do you want to spend on 1 \n");
    scanf(" %f", &gift1);
    printf("How much do you want to spend on 2 \n");
    scanf(" %f", &gift2);
    total = gift1+gift2;
    printf("\nThe total you will be spending on gifts is $%.2f", total);
    printf("\n %s %d %f %c", "Gokul", 90, 100, 'X');
    return 0;
    }

    In the above program, i have added a final printf which has nothing to do with the rest of the program. I have simply added it. And i have passed the integer 100 to the %f conversion character. While executing the program, i give some random values for gift1,gift2 which accounts to, say 1958.9967. So, total is 1958.9967 But the value of total is also printed as part of my final line, like below Gokul 90 1958.9967 Here, X is not printed as well. So, what is happening. Instead of 100, when i tried with 100.19, it gives the desired result (like below) Gokul 90 100.19 X I tried another small program like below..

    #include<stdio.h>
    main()
    {
    printf("\n %f specifies floating number, but i will give an integer",1987);
    return 0;
    }

    Here, my output was like below 0.000000 specifies floating number, but i will give an integer. Here, where does this 0.000000 comes from? In case of previous program it printed the value of "total" - why? what is the behavior here? Btw, I am using Code Blocks compiler, if that's of any significance here. Thanks in advance.

    CPalliniC D M S 4 Replies Last reply
    0
    • B BeingGokul

      Hi, I am no expert in C and I have started learning it again after several years. Excuse me for stupid questions. My question is simple. When i give %f as conversion character inside printf, but pass some integer to it, what happens? It prints some garbage value? or is there any pattern? Consider the below program..

      #include <stdio.h>
      main()
      {
      float gift1, gift2;
      float total;
      printf("How much do you want to spend on 1 \n");
      scanf(" %f", &gift1);
      printf("How much do you want to spend on 2 \n");
      scanf(" %f", &gift2);
      total = gift1+gift2;
      printf("\nThe total you will be spending on gifts is $%.2f", total);
      printf("\n %s %d %f %c", "Gokul", 90, 100, 'X');
      return 0;
      }

      In the above program, i have added a final printf which has nothing to do with the rest of the program. I have simply added it. And i have passed the integer 100 to the %f conversion character. While executing the program, i give some random values for gift1,gift2 which accounts to, say 1958.9967. So, total is 1958.9967 But the value of total is also printed as part of my final line, like below Gokul 90 1958.9967 Here, X is not printed as well. So, what is happening. Instead of 100, when i tried with 100.19, it gives the desired result (like below) Gokul 90 100.19 X I tried another small program like below..

      #include<stdio.h>
      main()
      {
      printf("\n %f specifies floating number, but i will give an integer",1987);
      return 0;
      }

      Here, my output was like below 0.000000 specifies floating number, but i will give an integer. Here, where does this 0.000000 comes from? In case of previous program it printed the value of "total" - why? what is the behavior here? Btw, I am using Code Blocks compiler, if that's of any significance here. Thanks in advance.

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      Mandatory link: What Every Computer Scientist Should Know About Floating-Point Arithmetic[^]. Please note, that does not explain your first scenario (I really don't know why are you getting Gokul 90 1958.9967).

      THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite

      In testa che avete, signor di Ceprano?

      1 Reply Last reply
      0
      • B BeingGokul

        Hi, I am no expert in C and I have started learning it again after several years. Excuse me for stupid questions. My question is simple. When i give %f as conversion character inside printf, but pass some integer to it, what happens? It prints some garbage value? or is there any pattern? Consider the below program..

        #include <stdio.h>
        main()
        {
        float gift1, gift2;
        float total;
        printf("How much do you want to spend on 1 \n");
        scanf(" %f", &gift1);
        printf("How much do you want to spend on 2 \n");
        scanf(" %f", &gift2);
        total = gift1+gift2;
        printf("\nThe total you will be spending on gifts is $%.2f", total);
        printf("\n %s %d %f %c", "Gokul", 90, 100, 'X');
        return 0;
        }

        In the above program, i have added a final printf which has nothing to do with the rest of the program. I have simply added it. And i have passed the integer 100 to the %f conversion character. While executing the program, i give some random values for gift1,gift2 which accounts to, say 1958.9967. So, total is 1958.9967 But the value of total is also printed as part of my final line, like below Gokul 90 1958.9967 Here, X is not printed as well. So, what is happening. Instead of 100, when i tried with 100.19, it gives the desired result (like below) Gokul 90 100.19 X I tried another small program like below..

        #include<stdio.h>
        main()
        {
        printf("\n %f specifies floating number, but i will give an integer",1987);
        return 0;
        }

        Here, my output was like below 0.000000 specifies floating number, but i will give an integer. Here, where does this 0.000000 comes from? In case of previous program it printed the value of "total" - why? what is the behavior here? Btw, I am using Code Blocks compiler, if that's of any significance here. Thanks in advance.

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        When %f is encountered in the format string, the computer attempts to pop a floating point number off of the stack, what it finds instead is 100, an integer. When you changed it to 100.19, the computer found what it was expecting. My rule of thumb is always use a (double or float) cast, or append .0 to any floating point numbers. That way, these problems and others are avoided.

        "One man's wage rise is another man's price increase." - Harold Wilson

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

        1 Reply Last reply
        0
        • B BeingGokul

          Hi, I am no expert in C and I have started learning it again after several years. Excuse me for stupid questions. My question is simple. When i give %f as conversion character inside printf, but pass some integer to it, what happens? It prints some garbage value? or is there any pattern? Consider the below program..

          #include <stdio.h>
          main()
          {
          float gift1, gift2;
          float total;
          printf("How much do you want to spend on 1 \n");
          scanf(" %f", &gift1);
          printf("How much do you want to spend on 2 \n");
          scanf(" %f", &gift2);
          total = gift1+gift2;
          printf("\nThe total you will be spending on gifts is $%.2f", total);
          printf("\n %s %d %f %c", "Gokul", 90, 100, 'X');
          return 0;
          }

          In the above program, i have added a final printf which has nothing to do with the rest of the program. I have simply added it. And i have passed the integer 100 to the %f conversion character. While executing the program, i give some random values for gift1,gift2 which accounts to, say 1958.9967. So, total is 1958.9967 But the value of total is also printed as part of my final line, like below Gokul 90 1958.9967 Here, X is not printed as well. So, what is happening. Instead of 100, when i tried with 100.19, it gives the desired result (like below) Gokul 90 100.19 X I tried another small program like below..

          #include<stdio.h>
          main()
          {
          printf("\n %f specifies floating number, but i will give an integer",1987);
          return 0;
          }

          Here, my output was like below 0.000000 specifies floating number, but i will give an integer. Here, where does this 0.000000 comes from? In case of previous program it printed the value of "total" - why? what is the behavior here? Btw, I am using Code Blocks compiler, if that's of any significance here. Thanks in advance.

          M Offline
          M Offline
          M FarrukhFaizy
          wrote on last edited by
          #4

          Dear You are trying to convert an int value into float without using any of the coversion processing thats why you got wrng ans. everytime when you put a int value Similiarly when you are trying to put int value into float its also gave unrelevant answer

          Frk

          1 Reply Last reply
          0
          • B BeingGokul

            Hi, I am no expert in C and I have started learning it again after several years. Excuse me for stupid questions. My question is simple. When i give %f as conversion character inside printf, but pass some integer to it, what happens? It prints some garbage value? or is there any pattern? Consider the below program..

            #include <stdio.h>
            main()
            {
            float gift1, gift2;
            float total;
            printf("How much do you want to spend on 1 \n");
            scanf(" %f", &gift1);
            printf("How much do you want to spend on 2 \n");
            scanf(" %f", &gift2);
            total = gift1+gift2;
            printf("\nThe total you will be spending on gifts is $%.2f", total);
            printf("\n %s %d %f %c", "Gokul", 90, 100, 'X');
            return 0;
            }

            In the above program, i have added a final printf which has nothing to do with the rest of the program. I have simply added it. And i have passed the integer 100 to the %f conversion character. While executing the program, i give some random values for gift1,gift2 which accounts to, say 1958.9967. So, total is 1958.9967 But the value of total is also printed as part of my final line, like below Gokul 90 1958.9967 Here, X is not printed as well. So, what is happening. Instead of 100, when i tried with 100.19, it gives the desired result (like below) Gokul 90 100.19 X I tried another small program like below..

            #include<stdio.h>
            main()
            {
            printf("\n %f specifies floating number, but i will give an integer",1987);
            return 0;
            }

            Here, my output was like below 0.000000 specifies floating number, but i will give an integer. Here, where does this 0.000000 comes from? In case of previous program it printed the value of "total" - why? what is the behavior here? Btw, I am using Code Blocks compiler, if that's of any significance here. Thanks in advance.

            S Offline
            S Offline
            Satya Chamakuri
            wrote on last edited by
            #5

            Hi Gokul, I understood your doubt. Here i am trying to clarify why it is happening clearly. printf("\n %s %d %f %c", "Gokul", 90,100,'X'); in the above piece of code if you observe clearly, with the assumption of starting address will be 0x1000, the contents of your piece of code will occupy memory as follows. "Gokul"- at 0x1000 90 - at 0x1006 100 - at 0x1010 'X' - at 0x1015 Now as per your format specifiers first %s will fetch first 6 Bytes("Gokul"), %d will fetch next 4 bytes of data as integer, so fetches 90, now %f means it will fetch next 8 Bytes of data means from 0x1010 to 0x1017 which includes memory location of 'X' also, after fetching it will try to display float number but it won't display as no external coversion you are doing. So it will display 0.0000000 Then during this time we are at memory location 0x1018 in which some garbage value(Σ) will be there as we are not storing anything. Even though if you give some other character in place of 'X' also it will give same value(Σ) as garbage is there in 0x1018 location. if you change 100 to some float number or explicitly converted to float number((float)100), then it will occupy 8 bytes memory now. then no problems and no unwanted results you get. If any clarifications feel free to ask, you will get help definitely. Thank you.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups