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

  • SEARCH
  • Home
  • 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 8637127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:24:00+00:00 2026-06-12T10:24:00+00:00

So I’ve just started learning C for a class of mine, and I have

  • 0

So I’ve just started learning C for a class of mine, and I have been working on this assignment, but this part has me perplexed. I have some code that essentially looks like:

#include <stdio.h>
#include <string.h>

int main(void)
{
    int one = 0;
    int two = 0;
    char oneWord[100]; 
    char twoWord[100];

    printf("Enter a two digit number: ");
    scanf("%d%d", &one, &two);

    strcpy(firstWord, "Test");
    strcpy(secondWord, "Two");

    printf("%s", firstWord);

    return 0;
}

Now, logic/purpose of the program aside, what I cannot figure out is why that scanf statement is resulting in an infinite loop? I determined that was that cause of the problem when I commented it out and the final printf statement worked just fine.

I tried changing it to scanf("%d,%d", %one, %two) and that seemed to work fine when I added a comma to the input. But I want to just be able to take a number like 55 and break it into 2 digits. What exactly am I doing wrong here?

  • 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-06-12T10:24:02+00:00Added an answer on June 12, 2026 at 10:24 am

    What you run into is not an infinite loop, but rather scanf waiting for more input.

    The d-format-specifier means a decimal number, so anything like “0”, “41”, or “2342345”.

    After you have entered the first number, you expect the program to continue, but scanf has just completed the first %d, so scanf is waiting for you to fulfill the second %d. This is what seems to you an infinite loop, but really is scanf doing what you ordered it to.

    scanf cannot guess how many digits for each number you desire. But you can tell it how you want it, with a width-specifier that tells how many characters each information is composed of:

    #include <stdio.h>
    
    int main () {
        int a, b;
        scanf("%2d%1d", &a, &b);
        printf("%d, %d", a, b);
    } 
    

    with error handling:

    #include <stdio.h>
    
    int main () {
        int a, b;
        if (scanf("%2d%1d", &a, &b) < 2)
            fprintf(stderr, "not enough digits");
        else
            printf("%d, %d", a, b);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.