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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:06:16+00:00 2026-05-29T08:06:16+00:00

I have been looking all over for how to compare char elements in a

  • 0

I have been looking all over for how to compare char elements in a char array correctly, but it is not working out for me. I am trying to compare the first char in a char array to an operator (checking to see if a + or -) exists.

EDIT: Saw my mistake. Logic error in the if statement. Thank you all for such quick responses.

At first I tried this:

    main(int argc, char *argv[]){

    int i;
    int len;
    char input[10];

    for(i = 1; i < 8; i++){

        len = strlen(argv[i]);
        strcpy(input,argv[i]);

        //debug purposes
        printf("%c\n",input[0]);

        if(input[0] != '+' || input[0] != '-')  {
            printf("incorrect number format. %s has no sign.\nnow terminating.\n", input);
        }// end if
    }//end for
}//end main

Then I read some more posts and saw that I should be comparing the chars using strcompare, so then I tried this: if( !strcmp(input[0],'+') || !strcmp(input[0],'-') )

However, they still don’t seem to be comparing properly. I get a segmentation fault with the above code and with my previous code, it would print the operator, but still go into the if statement saying that the format was incorrect. I am still fairly new to C, so any advice or tips on how I can get these chars to compare correctly will be much appreciated. Thanks.

  • 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-29T08:06:17+00:00Added an answer on May 29, 2026 at 8:06 am

    The logic in the original if condition is incorrect:

    if(input[0] != '+' || input[0] != '-')
    

    That will always be true: if '+'== input[0] the condition equates to false || true, and vice versa for '-' == input[0].

    It should be:

    if(input[0] != '+' && input[0] != '-')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been looking over the web but have not come across an answer
I have been looking all over for an answer to this question, but can't
I've been looking all over for a simple example of how to have an
I've been looking all over the place, but I haven't landed an answer yet.
I have been looking for a while now but I can not find an
OK I have been looking all over the place for ways to add URL
I have been looking all over google to find some answers to my questions
I have been looking all over google to find what the hex output of
I have been looking all over the Internet for an answer to this question
So I have been looking all over and I haven't quite found what 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.