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 8664357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:13:19+00:00 2026-06-12T17:13:19+00:00

So I can’t really figure out why scanf is messing up a[0]. When I

  • 0

So I can’t really figure out why scanf is messing up a[0]. When I print out the array, a[0] is empty and where I want a[0] it is stored in a[1].

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

int main(int argc, char *argv[])
{
    char *a[128];
    int i;
    int num;

    scanf("%d", &num);

    for (i = 0; i < 2; i++) {
        a[i] = malloc(50*sizeof(char));
        fgets(a[i], 100, stdin);
    }

    printf("[0] = %s [1] = %s", a[0], a[1]);

    return 0;
}
  • 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-12T17:13:21+00:00Added an answer on June 12, 2026 at 5:13 pm

    Your scanf() is not messing up your a[0], at least not in the way you seem to think, by somehow corrupting its storage area. This is very much an issue with mixing different "methods" of input.

    What is actually happening with yourscanf/fgets combo is that the scanf is getting the integer, but not reading the newline that you enter at the end of that number, so the first fgets picks that up.

    The ISO standard states it thus (in the specified sequence):

    Input white-space characters (as specified by the isspace function) are skipped, unless the specification includes a [, c, or n specifier.

    An input item is read from the stream, unless the specification includes an n specifier. An input item is defined as the longest sequence of input characters which does not exceed any specified field width and which is, or is a prefix of, a matching input sequence.

    The first character, if any, after the input item remains unread.

    That first and last paragraph is important. The first means the leading whitespace is skipped, the last means that trailing whitespace is left in the input stream to be read later.


    However, I’ll tell you what may mess it up (depending on the lengths of your input lines), it’s the fact that you allocate 50 characters space for each a[i] then read up to 100 characters into it. Thats undefined behaviour right there.


    If you want a robust input solution that handles buffer sizes correctly, detects lines that are too long, throws away the remainders of those lines and so on, see this answer.

    Alternatively, if you want to emulate line-based behaviour with the non-line-based scanf (and you’re sure that your lines are always less than 100 bytes), you can change:

    scanf("%d", &num);
    

    into:

    char junk[100];
    scanf ("%d", &num);
    fgets (junk, sizeof (junk), stdin);
    

    so that it grabs and throws away the rest of the line.

    And you should ensure that the maximum-size argument to fgets matches the amount of memory available (such as by using sizeof in the above code).

    One other thing, please don’t complicate code by multiplying things by sizeof(char) – that value is always 1 as per the standard.

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

Sidebar

Related Questions

Can't figure out how to do this in a pretty way : I have
Can't seem to figure out what's wrong with the simple getJSON call below. It's
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Can someone guide me on a possible solution? I don't want to use /bin/cp
Can any one help me in sorting this out in sed/awk/perl Input file Start
Can anyone help me trying to find out why this doesn't work. The brushes
Can anyone let me know how can we change the value of kendo combobox
Can I order my users in the database, so I don't have to say
Can I change the field public virtual ClassOne ClassOne { get; set; } to
Can I authenticate with just Google account username and password instead of using OAuth?

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.