Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6204805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:09:42+00:00 2026-05-24T05:09:42+00:00

I have a program, but when I input float numbers whenever the program asks

  • 0

I have a program, but when I input float numbers whenever the program asks for inputs, the program abruptly skips a step and moves onto the end output. The program is below:

#include <stdio.h>
#include <stdlib.h>

int main()
{
  int a,b,c;
  int i;

  printf("Please enter a number: ");
  scanf("%d", &a);
  printf("Please enter a number: ");
  scanf("%d", &b);

  c = 0; 
  for(i=0; i < b; i++)
    {
    c = c + a;
    } 

  printf("%d x %d = %d\n", a, b, c);

  return 0;
}

When I input an int for a, and a float for b, the program will output the product as expected if the numbers after the decimal point for b is truncated. However when I input a float for a, the program doesn’t take the value for the second number b and instead skips that step and outputs the integer version of a x -858993460 = 0.

For example:

a = int, b = float

Please enter a number: 3
Please enter a number: 5.6
3 x 5 = 15

a = float, b = skipped

Please enter a number 3.9
Please enter a number: 3 x -858993460 = 0

All the flaws in the code are deliberate, but I just wanted to know why it behaves the way I explained above. I know it’s because of something to do with trying to input a float into a signed integer but I’m not sure what exactly is causing it to skip the second scanf("%d", &b). Can anyone explain why this happens?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T05:09:42+00:00Added an answer on May 24, 2026 at 5:09 am

    It looks like scanf() is reading your “3” in the second case, and ignoring the “9”.

    Then when the second scanf() is called, there is already text in the input buffer (the “.9”).

    I can’t tell exactly what it’s doing with the “.9”. It may have found the dot and just aborted there with b uninitialized. It should be a simple matter to determine what is happening by stepping through with the debugger.

    But, basically, not all the input is being processed by the first call to scanf() and so that’s what the second call is trying to read. And that’s why it’s not waiting for you to input any data for the second call.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this program, but cin in randomly skips.. I mean sometimes it does,
I am working on kind of drawing program but I have a problem with
We have a program with a main() that parses certain CLPs but does not
I have a program which i have myself written in java, but I want
Imagine we have a program trying to write to a particular file, but failing.
I have an OpenGL program that works on all of my computers but one.
I have an MPI program which compiles and runs, but I would like to
I have an RMI program which runs fine on my computer, but when I
I have a simple program to check if a port is open, but I
I have NUnit installed on my machine in C:\Program Files\NUnit 2.4.8\ but on my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.