I new to Arduino and I'm attempting to writte a simple string compare code like this:

void loop() 

void distance(char[3] unit) 
  if (unit[] == "cm") 

Could somebody please advise me how to writte it correctly? Thanks in advance!

  • 1
    Use strcmp(). Jul 8 '15 at 11:57
  • Thank you for your answer. I've written this statement: if (strcmp(unit, "cm") = 0) ,but I think I have a problem with void distance(char[3] unit) and "reading" the string. IDE output: sketch_jul08a:15: error: 'unit' was not declared in this scope expected ',' or '...' before 'unit' Jul 8 '15 at 12:26
  • It's char unit[3], or, better char * unit. And you call like distance("cm"), with the quotes. Jul 8 '15 at 12:33
  • 1
    I've written this statement: if (strcmp(unit, "cm") = 0) -- that should be == 0 to do a comparison.
    – Nick Gammon
    Jul 9 '15 at 0:08
if (strcmp (unit,"cm") == 0) {

    // matches cm


For syntax like this the intellisense in products such as Atmel Studio or Visual Studio can really help. Whilst you still need to understand what you might be looking for the intellisense makes all the available Arduino functions visible from the code. If you install the Arduino plugin then there will be an Arduino reference explorer in both ide's which explains everything.

  • Shameless plug!
    – PhillyNJ
    Jul 9 '15 at 2:07
  • @PhillyNJ Hi, not true. It is entirely correct, what users often doesn't realize will help most. + I didn't post any links! Jul 9 '15 at 15:07
  • Shameless or not, Atmel Studio 7 Beta runs all the Arduino libraries with no plugin.
    – PhillyNJ
    Jul 9 '15 at 15:10
  • Sure you can use the import tool to convert some Arduino projects to cpp projects. they are then no longer Arduino compatible, no ability to easily take core upgrades from Arduino, no usb upload, only a debugger if you hack your board, only debug with optimization disabled, difficult to add new libraries and the list goes on. Great for some pro users but not for others. Have you tried it? maybe not? Jul 9 '15 at 15:15
  • Yes - Both Visual Macro and Beta 7.
    – PhillyNJ
    Jul 9 '15 at 15:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.