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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:09:01+00:00 2026-05-28T03:09:01+00:00

This may sound a bit stupid but this is how I am getting it

  • 0

This may sound a bit stupid but this is how I am getting it and I have googled it but no relevant solution was found.

I always thought that in C, strings was terminated by '\0' 

is a single character. (hence we used single quotes)

Also, we are able to do something like,

while ( a[i]!='\0' )
do blah blah


which suggests that '\0'

is a single character and ideally should be stored in a single position.

But when I declare an array like this:

char a[3];

and when I try to put some value say “hi” in it.

Then

printing a[0] gives "h"
printing a[1] gives empty space  
printing a[2] gives 0

which is suggestive that \ was stored at position 1 and ‘0’ at position 2. and the whole thing ‘\0’ was not stored together despite us using it as a single character.

Why is it so? Can anyone shed some clarity on the same?

Thanks!

EDIT:

#include <stdio.h>
void main()
{
int i=0;
char a[2];
fgets(a,sizeof(a),stdin); // Here I input "Hi".



while(a[i]!='\0')
    {
    printf("%c",a[i]);
    fflush(stdin);
    i=i+1;
    }
}
  • 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-28T03:09:01+00:00Added an answer on May 28, 2026 at 3:09 am

    fgets takes the size of the buffer as the second argument and reads that minus one characters, then makes the last character a \0. Because your array is sized 2 (which is too small for the string hi, by the way), fgets reads one character and makes the last character a \0, so you have an array that holds h\0.

    To get it working, make your array sized 3; one for the h, one for the i, and one for the \0:

    #include <stdio.h>
    
    void main() {
        int i = 0;
        char a[3];
        fgets(a, sizeof(a), stdin); // Here I input "Hi".
    
        while(a[i] != '\0') {
            printf("%c",a[i]);
            fflush(stdin);
            i=i+1;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So this may sound a bit awkward, but I would like to have the
This may sound like a bit of a rhetorical question, but I ask it
This may sound like a stupid question, perhaps it is. But I'm only trying
This may sound stupid but... When I create big SQL commands I want to
This may sound like a stupid question but I'm a beginner not just to
Ok this question may sound a bit convoluted, or at least esoteric, but I'll
This might sound a bit dumb. I always had this impression that web.config should
I know this may sound silly little bit, but there is no other way
This may sound a bit provocative but it actually is a real question. Feel
This may sound like a bit of a dumb question but how do I

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.