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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:21:17+00:00 2026-05-26T12:21:17+00:00

i have a piece of code in C .But i am not able to

  • 0

i have a piece of code in C .But i am not able to understand its output

#include<stdio.h>
main()
{

char a1[20];
char a2[30];
char a3[40];
scanf("%s",&a1);
gets(a2);
fgets(a3,sizeof(a3),stdin);
printf("%d,%d,%d\n",strlen(a1),strlen(a2),strlen(a3));
 }   

When i enter my input like

 amit
 singh

output comes out to be 4,0,6 and fgets doest not allow me to enter any string ,i am able to enter only 2 inputs?

  • 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-26T12:21:18+00:00Added an answer on May 26, 2026 at 12:21 pm
    1. input is "amit\nsingh\n"
    2. the scanf consumes “amit” (and writes that into a1)
    3. the gets consumes “\n” (and writes empty string to a2)
    4. the fgets consumes “singh\n” (which it writes to a3)

    The output is correct.

    Do not EVER use gets!

    1. http://pubs.opengroup.org/onlinepubs/9699919799/functions/fscanf.html
    2. http://pubs.opengroup.org/onlinepubs/9699919799/functions/gets.html
    3. http://pubs.opengroup.org/onlinepubs/9699919799/functions/fgets.html
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following piece of code. I do not understand why its not
you must have seen this piece of code somewhere else too,but obviously there's no
I have this piece of code which gives to SAXParseError i.e., it is not
I have Learnt about PRAGMA INLINE 11g feature , but I am not able
I have a piece of code that is working fine in IE, but it
Here I have piece of code, showing example of how I want to update
I have a piece of code like this: <div id=container runat=server> <div id=parent runat=server>
I have this piece of code in a function. I want to print the
I have a piece of code that was written by someone else before the
I have this piece of code: public static DateDialogFragment newInstance(Context context, DateDialogFragmentListener listener) {

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.