-1

Consider:

#include <avr/io.h>

int main(void)
{
    DDRB = 0b11111111; // PORTB as output port connected to motors;
    DDRC = 0b0000000; // PORTC Input port connected to sensors;
    int left_sensor = 0;
    int right_sensor = 0;
    while(1)
    {
        left_sensor = PINC&0b00000001;
        right_sensor = PINC&0b00001000;
        if((left_sensor==0b0000000) && (right_sensor==0b0000000))
        {
            PORTB = 0b00000000;
        }
        else if((left_sensor==0b00000001) && (right_sensor==0b00001000))
        {
            PORTB = 0b00010010;
        }
        else if((left_sensor==0b0000000) && (right_sensor==0b0001000))
        {
            PORTB = 0b00000010;
        }
        else if((left_sensor==0b00000001) && (right_sensor==0b0000000))
        {
            PORTB = 0b00010000;
        }
    }
}

It's showing "expected expression before while". I have tried everything, but I am not getting any solution to it.

Error:

Expected Expression before 'while'

Compiler: Atmel Studio 7

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • I think you're getting this error because `DDRB` and/or `DDRC` are undefined. Backtrack through "" and ensure the correct device files are being included. – Ed King Mar 11 '17 at 10:42
  • Include the compiler output verbatim (copy & paste) in your question so we can see the exact error, and the compiler invocation. The compiler used and its version would also help anyone who might answer – Clifford Mar 11 '17 at 13:36
  • Atmel Studio 7 is an IDE not a compiler. It can be used with a variety of compilers. And that is not the complete build log - compiler diagnostics are useful for diagnosing problems and contain information you have omitted. – Clifford Mar 11 '17 at 14:03
  • 1
    Please use textual constants and not "magic numbers". Now I can see why standard C doesn't allow binary literals... – Lundin Mar 13 '17 at 10:35
  • That is caused by [GREEK QUESTION MARK](https://www.utf8-chartable.de/unicode-utf8-table.pl?start=890) (looks exactly like a semicolon). How did that enter the code???? It can be searched for (and replaced) using the regular expression `\x{037E}` in any modern text editor or IDE (note: The notation is different in Visual Studio Code (and probably others): `\u037E` (instead of `\x{037E}`)). – Peter Mortensen May 07 '23 at 02:14
  • OK, the OP has left the building: *"Last seen more than 1 year ago"* – Peter Mortensen May 07 '23 at 02:20
  • Even if it wasn't caused by copying code from a web page or PDF document, the analysis of the stray errors (that were present and ***not reported in this question*** (see the answer)) and the resolution would be the same (non-ASCII Unicode characters used in code, outside comments). The canonical question is *[Compilation error: stray ‘\302’ in program, etc.](https://stackoverflow.com/questions/19198332)*. – Peter Mortensen May 07 '23 at 02:25

1 Answers1

1

If I copy & pasted the code from your question remove the AVR specific lines and attempt to compile it I get the following:

source_file.c: In function ‘main’:
source_file.c:4:10: error: stray ‘\315’ in program
          int right_sensor=0;
          ^
source_file.c:4:10: error: stray ‘\276’ in program
source_file.c:5:10: error: expected ‘,’ or ‘;’ before ‘while’
          while(1)
          ^

Deleting the line with the declaration of right_sensor and rewriting it solves the problem - you appear to have some strange non-printing characters in the line.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Clifford
  • 88,407
  • 13
  • 85
  • 165
  • That is [U+037E](https://codepoints.net/U+037E) (GREEK QUESTION MARK) (UTF-8 sequence 315 276 (octal) → 0xCD 0xBE). It looks ***exactly*** like a semicolon, which probably why it was entered. It can be searched for the regular expression `\x{037E}` in any modern text editor or IDE (note: The notation is different in Visual Studio Code (and probably others): `\u037E` (instead of `\x{037E}`)). In the code, there are 7 ordinary semicolons and 5 U+037E's (3 in code lines and 2 at the end of comments)—thus likely entered manually and not by some automatic means (e.g., copying from a web page). – Peter Mortensen May 07 '23 at 02:04
  • 1
    On that page: *"The glyph is a Canonical composition of the glyph Semicolon."* (whatever that means) – Peter Mortensen May 07 '23 at 02:12