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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:13:44+00:00 2026-05-27T21:13:44+00:00

int main( ) { char str[200]; int n,tc; scanf(%d,&tc); while(tc–) { scanf(%d,&n); gets(str); puts(str);

  • 0
   int main( )
    {
        char str[200];
        int n,tc;
        scanf("%d",&tc);
        while(tc--)
         {
           scanf("%d",&n);
           gets(str);
           puts(str);
        }
    return 34;
    }

Why this code is not scanning no. of strings(str) equals to tc?
Platform UBUNTU 10.04

  • 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-27T21:13:45+00:00Added an answer on May 27, 2026 at 9:13 pm

    Lets break down the code one line at a time (more or less):

    scanf("%d",&tc);
    

    reads an integer into tc.

    while(tc--)
    

    loops tc times

        scanf("%d",&n);
    

    reads an integer into n

        gets(str);
    

    reads the rest of the line into str, removing the trailing newline

        puts(str);
    

    prints out the string, followed by a newline

    So you’re reading an integer, and then reading that many integer+rest of line pairs and printing out the lines (but not the integers — they get thrown away). Reading an integer involves skipping any blank space (spaces, tabs, and entire blank lines) before the integer but not any after it, so this can be a little confusing, depending on what kind of input you’re feeding to the program.

    In your example, you say you’re feeding it the input:

    3
    4
    anil kuma
    2
    abc
    4
    ams
    

    So lets go through the program and see what it does:

    • read the integer 3 into tc
    • loop 3 times
    • (first loop)
      • read 4 into n
      • read the rest of the line (blank) into str,
      • print the blank line
    • (second loop)
      • try to read an integer into n, but the input (anil kuma) isn’t an integer, so sets the error flag on stdin
      • read the rest of the line (anil kuma) into str
      • prints anil kuma
    • (third loop)
      • read 2 into n
      • read the rest of the line (blank) into str
      • print the blank line
    • loop is done, return exit code 34.

    You never check the return value of functions or the error code on stdin, so the fact that you got an error trying to read an integer on the second iteration is completely invisible.

    You might get an output more like what you are expecting by changing your scanf calls to:

    scanf("%d ", &n);
    

    Note the extra space after the %d conversion, which causes scanf to read and throw away whiltespace until it finds a non-whitespace character. This will cause it to throw away the newlines after the integers, as well as any following blank lines and any spaces/tabs at the beginning of the next non-blank line

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

Sidebar

Related Questions

Why this code does not work? int main(){ char *str =abcde; scanf(%s,str); printf(%s,str); }
#include<stdio.h> int main() { char a[5]=hello; puts(a); //prints hello } Why does the code
This part of my code works fine: #include <stdio.h> int main(){ //char somestring[3] =
Here is my code: void reverseStr(char *str) { if (str == NULL) return; int
#include<stdio.h> #include<string.h> #include<stdlib.h> int main() { // int char str[40],ch; FILE*fp,*fp1,*fp2; fp=fopen(ide_input,w); fp1=fopen(error_log,w); fp2=fopen(lex_output,w);
Why does the following code seg fault before returning: int main() { char iD[20];
#include <stdio.h> int main() { char read = ' '; while ((read = getchar())
hi can anybody tell me the error in this? #include<stdio.h> int main() { char
#include<cstdio> #include<stdlib.h> int main() { char* ptr=NULL; printf(%s,ptr); return 0; } It prints (null)
Let's say I have: sample.c int main (...) { char str*; get s through

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.