1

I am new to C. This code runs on a Flyport module and compiles on their own IDE but I should think my error should be easy to spot for anyone familiar with C.

I can't get this code to compile despite changing almost everything, what am I doing wrong?

char string_serial[50]="starting Value";    

if (string_serial[0] = "*")
        {   

            UARTWrite(2,"First Char OK");
            UARTWrite(2,"\r\n");

            else 

            UARTWrite(2,"First Char NOT OK");
            UARTWrite(2,"\r\n");

        }

If it's not obvious I am trying to check if character 0 in the array is *.

Thanks

2
  • Line 2: "*" isn't a character, but a string (const char [2] to be exact). Use single quotes for characters => '*'. Line 3: you have a braces problem, the correct syntax is => if (/* condition /*) { /* code if true */ } else { /* code if false /* } Commented Dec 1, 2012 at 13:27
  • You have to use == and * should be in single quotes like if (string_serial[0] == '*') and also the braces are to be kept in proper positions. if{/*if block*/} else {/*else block*/} Commented Dec 3, 2012 at 17:00

4 Answers 4

6

The syntax for if/else conditions is wrong

The syntax should be

 if(condition)
  {
  }

  else
  {
  }

For your specific code it should be

if (string_serial[0] == '*')
        {   

            UARTWrite(2,"First Char OK");
            UARTWrite(2,"\r\n");
         }

else 
        {
            UARTWrite(2,"First Char NOT OK");
            UARTWrite(2,"\r\n");

        }

The = operator is assignment, it assigns the value (returning true when successful). What you need is == (for comparing).

Also use single quotes for characters as shown in the code above.

Sign up to request clarification or add additional context in comments.

2 Comments

I think that is all good but I still get an error "syntax error at end of input" but maybe this is specific to the Flyport. It is great here I get an answer in 2 minutes, on the Flyport forum I can wait a week! Many thanks
@Simon_A: syntax error at end of input is mostly because of unmatched flower brackets, check in your program whether each opened { has a matching }
2

You should replace double quotes with single quotes around "*", and use ==:

if (string_serial[0] == '*')

A single character enclosed in single quotes means "a character"; a single character enclosed in double quotes means "a one-character C string".

A single = means "assignment"; a double == means "compare".

1 Comment

Perhaps, changing = to == as well.
1

You if structure is not correct

try this:

char string_serial[50]="starting Value";    

if (string_serial[0] == '*')
{   

   UARTWrite(2,"First Char OK");
}

else {

  UARTWrite(2,"First Char NOT OK");
  UARTWrite(2,"\r\n");

}

You need to change = to == and also get else block out of the if block

Comments

1

'' is for a single char, but "" is for strings. The following line should work.

if (string_serial[0] == '*')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.