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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:11:51+00:00 2026-05-14T03:11:51+00:00

Im trying to create a C based string menu where a user inputs a

  • 0

Im trying to create a C based string menu where a user inputs a command and then a block of code runs.

Whatever i do the conditional is never true:

char *input= "";
fgets(input, 50, stdin);
printf("%s",input);
printf("%d",strcmp( input,"arrive\0"));
if(strcmp( input,"arrive\0")==0){....

Im fairly new to c and am finding strings really annoying.

What am i doing wrong?

Note: current code crashes my program 🙁

  • 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-14T03:11:51+00:00Added an answer on May 14, 2026 at 3:11 am

    Why strcmp always return non 0:

    strcmp will return 0 only when the strings are identical. As for why it’s evaluating to different always. It is because fgets puts a newline character at the end of your input buffer before the null termination.

    /*Will print 0 if you type in arrive<enter>*/
    printf("%d",strcmp( input,"arrive\n"));
    

    Why your program crashes:

    Another problem is that input should be a char buffer. Like so: char input[1024];
    Currently you have input as a pointer to a null terminated string (which is read only memory)


    Friendly suggestion:

    Also don’t put the null terminated \0 inside the string literals. It is implied automatically when you use a string literal. It doesn’t matter to double null terminate as far as strcmp is concerned, but it may cause problems elsewhere in your future programs. And people will wonder why you’re doing double null termination.

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

Sidebar

Related Questions

I am trying to create a hierarchical xml based menu in MVC.NET <?xml version=1.0
So I'm trying to create a function that generates a SQL query string based
I’m trying to build a command string based to pass in a -e flag
I'm trying to create a Linq query based on a user's input. Writing sql
I'm trying to create a number of strings based on one long string that
I'm trying to create a string containing (unicode) 'stars' based on an integer rating.
I'm trying to create a simple spring based webservice that supports a post with
I am trying to create a conversations based messaging system. I want to group
Iam trying to create an iterative server based on datagram sockets (UDP). It calls
I am trying to create an XML file based on data fields from a

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.