#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;
}
}
}
Its 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
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.
Related
I am trying to understand the inner workings of errno, and tried the following example file from cppreference, but got errors during compiling:
$ gcc errno_ex.c
errno_ex.c: In function 'main':
errno_ex.c:33:10: error: 'math_errhandling' undeclared (first use in this function)
puts(math_errhandling & MATH_ERRNO ? "set" : "not set");
^~~~~~~~~~~~~~~~
errno_ex.c:33:10: note: each undeclared identifier is reported only once for each function it appears in
errno_ex.c:33:29: error: 'MATH_ERRNO' undeclared (first use in this function)
puts(math_errhandling & MATH_ERRNO ? "set" : "not set");
^~~~~~~~~~
The (untouched) example code is:
#include <stdio.h>
#include <math.h>
#include <errno.h>
void show_errno(void)
{
const char *err_info = "unknown error";
switch (errno) {
case EDOM:
err_info = "domain error";
break;
case EILSEQ:
err_info = "illegal sequence";
break;
case ERANGE:
err_info = "pole or range error";
break;
case 0:
err_info = "no error";
}
fputs(err_info, stdout);
puts(" occurred");
}
int main(void)
{
fputs("MATH_ERRNO is ", stdout);
puts(math_errhandling & MATH_ERRNO ? "set" : "not set");
errno = 0;
1.0/0.0;
show_errno();
errno = 0;
acos(+1.1);
show_errno();
errno = 0;
log(0.0);
show_errno();
errno = 0;
sin(0.0);
show_errno();
}
I am not sure what is happening. These errors happen when I compile in VS code in Windows ( C complier set to C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.29.30037/bin/Hostx86/x86/cl.exe by default, and using C17 standard).
When I try to duplicate this on my Macbook VS code, it compiled without error. So I am not sure where is the problem: is my compiler too old to recognize these macros?
Edit:
I think the macro definition of math_errhandling is platform-specific, thus not quite portable across different OS platforms. errno macro still works in my Windows-based VS Code IDE, which I am happy with. It is just that the math_errhandling thing is a bit non-portable and mysterious.
so I was trying to run my first Code in CLion:
#include <stdio.h>
int main () {
int x;
x = 5/2;
printf("Result: %.2f", x);
return 0;
}
and received following error message:
makefile(136) : fatal error U1033: syntax error: "$(MAKE)" unexpected stop.
I'm looking forward for a reply.
I am working with Lexical Analysis. For this I am using Flex and I fetch following Problems.
work.l
int cnt = 0,num_lines=0,num_chars=0; // Problem here.
%%
[" "]+[a-zA-Z0-9]+ {++cnt;}
\n {++num_lines; ++num_chars;}
. {++num_chars;}
%%
int yywrap()
{
return 1;
}
int main()
{ yyin = freopen("in.txt", "r", stdin);
yylex();
printf("%d %d %d\n", cnt, num_lines,num_chars);
return 0;
}
then, I use following command and it work properly and create lex.yy.c.
Rezwans-iMac:laqb-2 rezwan$ flex work.l
then, I use following command.
Rezwans-iMac:laqb-2 rezwan$ gcc lex.yy.c -o b
and getting following error:
work.l: In function ‘yylex’:
work.l:3:4: error: ‘cnt’ undeclared (first use in this function); did you mean int’?
[" "]+[a-zA-Z0-9]+ {++cnt;}
^~~
int
work.l:3:4: note: each undeclared identifier is reported only once for each function it appears in
work.l:4:4: error: ‘num_lines’ undeclared (first use in this function)
\n {++num_lines; ++num_chars;}
^~~~~~~~~
work.l:4:17: error: ‘num_chars’ undeclared (first use in this function); did you mean ‘num_lines’?
\n {++num_lines; ++num_chars;}
^~~~~~~~~
num_lines
work.l: In function ‘main’:
work.l:15:23: error: ‘cnt’ undeclared (first use in this function); did you mean ‘int’?
return 0;
^
int
work.l:15:28: error: ‘num_lines’ undeclared (first use in this function)
return 0;
^
work.l:15:38: error: ‘num_chars’ undeclared (first use in this function); did you mean ‘num_lines’?
return 0;
^
num_lines
I am not getting above error, if I modify work.l file like this .
int cnt = 0,num_lines=0,num_chars=0; // then work properly above command.
%%
[" "]+[a-zA-Z0-9]+ {++cnt;}
\n {++num_lines; ++num_chars;}
. {++num_chars;}
%%
int yywrap()
{
return 1;
}
int main()
{ yyin = freopen("in.txt", "r", stdin);
yylex();
printf("%d %d %d\n", cnt, num_lines,num_chars);
return 0;
}
That is to say, If I use 1 tab before this line int cnt = 0,num_lines=0,num_chars=0;, it works properly.
Now I have two question:
Is necessary use 1 tab before this line int cnt = 0,num_lines=0,num_chars=0;? why? explain logically.
Is another solution to solve this error ?
I'm not very sure about the tab problem but one explanation is that if you don't place tab and write in the first section something like:
int cnt = 0;
Then note that in the first section you may also write "shortcuts" like:
Digit [0-9]
which is a regular expression that defines what digit is instead of writing all the time [0-9] to denote digit.
So when writing int cnt = 0; in the first column without using tab is like defining the keyword int (that's probably why the error tells you did you mean int’?). So tab is a way to distinguish the two above cases.
According to flex in order to write c/c++ code it needs to be inside: %{ ... c/c++ code... %} so for your example:
%{
int cnt = 0,num_lines=0,num_chars=0;
%}
So I thing the best way is to write your c-code inside %{ %}.
I was trying some awkward preprocessing and came up with something like this:
#include <stdio.h>
#define SIX =6
int main(void)
{
int x=6;
int y=2;
if(x=SIX)
printf("X == 6\n");
if(y=SIX)
printf("Y==6\n");
return 0;
}
gcc gives me the errors:
test.c: In function ‘main’:
test.c:10:8: error: expected expression before ‘=’ token
test.c:12:8: error: expected expression before ‘=’ token
Why is that?
The == is a single token, it cannot be split in half. You should run gcc -E on your code
From GCC manual pages:
-E Stop after the preprocessing stage; do not run the compiler proper. The output is in
the form of preprocessed source code, which is sent to the standard output.
Input files that don't require preprocessing are ignored.
For your code gcc -E gives the following output
if(x= =6)
printf("X == 6\n");
if(y= =6)
printf("Y==6\n");
The second = is what causes the error message expected expression before ‘=’ token
The preprocessor doesn't work at the character level, it operates at the token level. So when it performs the substitution, you get something equivalent to:
if (x = = 6)
rather than your desired:
if (x==6)
There are some specific exceptions to this, like the # stringification operator.
if(x=SIX)
is parsed as
if (x= =6).
So you get the error.
What toolchain are you using? If you are using GCC, you can add the -save-temps option and check the test.i intermediate result to troubleshoot your problem.
I suspect you have a space between the x= and the =6.
Hey I am trying to connect to a database using postgres
#include <stdio.h>
#include <stdlib.h>
#include <libpq-fe.h>
int main(int argc, char* argv[])
{
//Start connection
PGconn* connection = PQconnectdb("host=webcourse.cs.nuim.ie dbname=cs621 sslmode=require user=ggales password=1234");
if (PQstatus(connection) ==CONNECTION_BAD)
{
printf("Connection error\n");
PQfinish(connection);
return -1; //Execution of the program will stop here
}
printf("Connection ok\n");
//End connection
PQfinish(connection);
printf("Disconnected\n");
return 0;
}
And I keep getting this compile error:
main.c: In function ‘main’:
main.c:9:35: warning: missing terminating " character [enabled by default]
main.c:9:2: error: missing terminating " character
main.c:10:2: error: ‘dbname’ undeclared (first use in this function)
main.c:10:2: note: each undeclared identifier is reported only once for each function it appears in
main.c:10:9: error: ‘cs621’ undeclared (first use in this function)
main.c:10:15: error: expected ‘)’ before ‘sslmode’
main.c:10:56: warning: missing terminating " character [enabled by default]
main.c:10:15: error: missing terminating " character
main.c:16:1: error: expected ‘,’ or ‘;’ before ‘}’ token
main.c:16:1: error: expected declaration or statement at end of input
Can anyone see why this is happening?
Thanks.
Your code compiles just fine. If I paste it into x.c I can compile it with no problems:
gcc -I /usr/pgsql-9.2/include -L /usr/pgsql-9.2/lib x.c -lpq
(paths may differ on your system).
you may use the 64-bit libpq.lib in a 32-bit program.
you can use a 32-bit libpq.lib or change you platform to x64.
a 32-bit client + 64-bit server can not work well.