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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:43:14+00:00 2026-05-25T18:43:14+00:00

I have the following program #include <stdio.h> main() { char ch[10]; gets(ch); printf(\nTyped: %s\n\n,

  • 0

I have the following program

#include <stdio.h>

main()
{

    char ch[10];

    gets(ch);

    printf("\nTyped: %s\n\n", ch);

    int i = 0;
    while ( ch[i] != '\0' )
    {
        printf("Letter: %c\n", ch[i]);
        i++;
    }

    printf("\nTyped: %s\n\n", ch);


}

and here is the output when i typed “Hello world is good”

hello world is good

Typed: hello world is good

Letter: h
Letter: e
Letter: l
Letter: l
Letter: o
Letter:
Letter: w
Letter: o
Letter: r
Letter: l
Letter:


Typed: hello worl♂

Why i am getting two different output for same command after while loop? does while loop has anything to do with it.. please help..

  • 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-25T18:43:14+00:00Added an answer on May 25, 2026 at 6:43 pm

    Your character array ch only has space for 10 chars. You typed something longer than 10 chars and tried to store it in that array, effectively writing beyond the end of the array into space that’s not reserved for anything (or at least isn’t reserved for your characters). You got lucky in this case and didn’t write over anything important (your program didn’t crash), but subsequent code comes along (the printf(), your int i, etc.) and changes that memory (it’s the stack, after all, so it gets used in FIFO order).

    Change your char ch[10] to char ch[2048] to give yourself a larger buffer to type into. You might also use fgets() instead of gets() so that you can limit the size of the input to your buffer size. If you use gets(), heed the warning on the man page:

    It is the caller’s responsibility to ensure that the input line, if
    any, is sufficiently short to fit in the string.

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

Sidebar

Related Questions

I have the following C program: #include <fcntl.h> #include <termios.h> #include <stdio.h> int main()
I have a question regarding the execution of the following program. #include<stdio.h> int main(void)
I have the following piece of code: #include <stdio.h> #include <stdlib.h> int main(int argc,
Let's consider this simple test program: #include <stdio.h> #include <string.h> int main(int argc, char
So I have the following test code: #include <string.h> #include <stdio.h> int main(int argc,
#include<stdio.h> #include<stdlib.h> int main() { int count=0; char c=0; printf(Reading this Source file\nFile name
I have this program, which I want to understand the following output: #include <stdio.h>
I have the following piece of code: #include <stdio.h> int main ( int argc,
I have the following program: ~/test> cat test.cc int main() { int i =
I have a very simple C++ application. #include <stdio.h> #include <iostream> int main(int argc,

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.