Compilation Fails Due to Strange Characters Appearing in Source Code - c

I'm trying to write a simply Hello World program but when attempting to compile with GCC, I receive the following error:
helloworld.c:5:18: error: expected ‘)’ before ‘World’
printf(“Hello World”);
^
helloworld.c:5:18: error: stray ‘\342’ in program
helloworld.c:5:18: error: stray ‘\200’ in program
helloworld.c:5:18: error: stray ‘\235’ in program
Does anyone know why this is?

printf(“Hello World”);
This should be written as -
printf("Hello World");
Straight quotes should be used. Try changing style .

The octal sequence 342 200 234 is the UTF-8 byte sequence for the typographic double quote. See this link.
To fix it, replace it with the regular double quote, i.e. " instead of “.

Related

Preprocessor error when defining =

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.

Error when connecting to Postgres database in C - using libpq-fe.h

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.

strange errors compiling c code [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I have the code below I am trying to build
#include stdio.h
#include stdlib.h
#include signal.h
#include wiringPi.h
#include softPwm.h
void control_event(int sig);
int HARD_PWM_PIN=1; Hardware PWM Pin(GPIO18-12)
int SOFT_PWM_PIN=0; Software PWM Pin(GPIO0-11)
int DELAY_MS=10;
int main(void)
{
(void)signal(SIGINT,control_event);
(void)signal (SIGQUIT,control_event);
printf(Hardware and software based PWM test on LEDn);
if(getuid()!=0) wiringPi requires root privileges
{
printf(ErrorwiringPi must be run as root.n);
return 1;
}
if(wiringPiSetup()==-1)
{
printf(ErrorwiringPi setup failed.n);
return 1;
}
pinMode(HARD_PWM_PIN,PWM_OUTPUT); setup hardware pwm
softPwmCreate(SOFT_PWM_PIN,0,100); setup software pwm pin
int up;
int down;
while(1)
{
for(up=1;up=5;down--)
{
pwmWrite(HARD_PWM_PIN,down);
softPwmWrite(SOFT_PWM_PIN,down);
delay(DELAY_MS2);
}
delay(DELAY_MS5);
}
}
void control_event(int sig)
{
printf(bbExiting...n);
pwmWrite(HARD_PWM_PIN,0);
softPwmWrite(SOFT_PWM_PIN,0);
delay(100); wait a little for the pwm to finish write
exit(0);
}
But I keep getting the following errors this is only a portion of them but they are pretty much the same throughout with the odd symbols and numbers.
test1.c:20:1: error: stray â\302â in program
test1.c:20:1: error: stray â\240â in program
test1.c:21:1: error: stray â\302â in program
test1.c:21:1: error: stray â\240â in program
test1.c:22:1: error: stray â\302â in program
test1.c:22:1: error: stray â\240â in program
test1.c:23:1: error: stray â\302â in program
test1.c:23:1: error: stray â\240â in program
test1.c:23:1: error: stray â\302â in program
test1.c:23:1: error: stray â\240â in program
test1.c:24:1: error: stray â\302â in program
test1.c:24:1: error: stray â\240â in program
test1.c:24:1: error: stray â\302â in program
test1.c:24:1: error: stray â\240â in program
test1.c:25:1: error: stray â\302â in program
test1.c:25:1: error: stray â\240â in program
test1.c:26:1: error: stray â\302â in program
test1.c:26:1: error: stray â\240â in program
test1.c:26:38: error: unknown type name âsetupâ
test1.c:26:53: error: expected â=â, â,â, â;â, âasmâ or â__attribute__â before âpwmâ
What could be wrong? The place I am getting this code from is here
You have some syntax errors -
aside from that your editor included unicode characters instead of the ASCII expected by gcc. - a possible example for " as a backward double quote or forward double quote instead of ASCII 34.
You have to be careful about your editor settings when you are using a non-western character set or extended unicode as your default encoding.
This is also why " characters do not show up in your post.
The odd symbols and numbers are because some of the spaces in the file aren't actually spaces. Looking at some of the lines in the file in your link, after copying and pasting:
'void control_event(int sig);\n'
'int HARD_PWM_PIN=1; //Hardware PWM Pin(GPIO18-12)\n'
'int SOFT_PWM_PIN=0; //Software PWM Pin(GPIO0-11)\n'
'int DELAY_MS=10;\n'
'int main(void)\n'
'{\n'
'\xc2\xa0 (void)signal(SIGINT,control_event);\n'
'\xc2\xa0 (void)signal (SIGQUIT,control_event);\n'
'\xc2\xa0 printf("Hardware and software based PWM test on LED\\n");\n'
'\xc2\xa0 if(getuid()!=0) //wiringPi requires root privileges\n'
Those \xc2\xa0s are the non-breaking space characters ( ), or 302/240 in octal.
Also note that you seem to have lost several comment markers (//) in transit, which is causing different problems of its own as the compiler is trying to interpret comments as code.

Why my .c coding can't be compile in GCC?

I have a simple code in c :
#include <stdio.h>
main()
{
printf(“Hello, world! /n”);
}
but I can't compile it in GCC. The warning when i try to compile it :
1. gcc hello.c -o hello
2. hello.c: In function 'main:
3. hello.c:4:1: error: stray '\342' in program
4. hello.c:4:1: error: stray '\200' in program
5. hello.c:4:1: error: stray '\234' in program
6. hello.c:4:11: error: 'Hello' undeclared (first use in this function)
7. hello.c:4:11: note: each undeclared identifier is reported only once for each function
it appears in
8. hello.c:4:18: error: 'world' undeclared (first use in this function)
9. hello.c:4:23: error: expected ')' before '!' token
10. hello.c:4:23: error: stray '\342' in program
11. hello.c:4:23: error: stray '\200' in program
12. hello.c:4:23: error: stray '\235' in program
anyone can help me please?
You get those errors because you presumably copy and pasted that code from a formatted source. “ and ” aren't the same as ". Change them to that and the code will compile.
You should probably also follow the convention of having main defined as:
int main(void)
and therefore returning an int.
You probably also want to change /n to \n

Eclipse goto label not working in C

I am using the Eclipse CDT and I have a goto label and a FILE definition after it and when I compile the project it gives me the error: Expression expected before FILE.
Thanks in advance,
Mr. Man
EDIT:
Ok, so this is what I get from the command line:
iOS.c: In function ‘main’:
iOS.c:45: error: expected expression before ‘FILE’
iOS.c:49: error: ‘preFile’ undeclared (first use in this function)
iOS.c:49: error: (Each undeclared identifier is reported only once
iOS.c:49: error: for each function it appears in.)`
And this is what code throws the error:
fileExists:
FILE *preFile = fopen("prefix.txt","r");
As you're coding in C, you need to declare the variable at the beginning of the function:
void foo()
{
FILE* preFile;
// some code
fileExists:
preFile = fopen("prefix.txt","r");
}

Resources