Syntax error on #define

A

Anshul

Hi,

I tried compiling this code -

int main()
{
char abc[10] = "%s";
printf("\033[1m" abc "\033[m" , "doing");
}

with gcc version 2.96. I get this error message -

parse error before `abc'

Please explain, whats wrong here. The intention is to print a string
in bold characters on the screen.

Thanks,
Anshul
 
S

santosh

Anshul said:
Hi,

I tried compiling this code -

int main()
{
char abc[10] = "%s";
printf("\033[1m" abc "\033[m" , "doing");

Only adjacent string literals are concactenated. The expression abc is
not a string literal, but a pointer constant value.

Try:

printf("\033[1m", " %s ", "\033[m\n", "doing");
 
R

Richard Heathfield

santosh said:
Anshul said:
Hi,

I tried compiling this code -

int main()
{
char abc[10] = "%s";
printf("\033[1m" abc "\033[m" , "doing");

Only adjacent string literals are concactenated. The expression abc is
not a string literal, but a pointer constant value.

Try:

printf("\033[1m", " %s ", "\033[m\n", "doing");

Er, no. :)

printf("\033[1m%s\033[m\n", "doing");

or even just:

printf("\033[1mdoing\033[m\n");

(although the former version is perhaps slightly more maintainable, in
that it's easier to see what needs changing when the need arises).
 
J

Joachim Schmitz

Anshul said:
Hi,

I tried compiling this code -
#include said:
int main()
{
char abc[10] = "%s";
printf("\033[1m" abc "\033[m" , "doing");
printf("\033[1m%s\033[m" , "doing");
fflush(stdout);
return 0;
}

with gcc version 2.96. I get this error message -

parse error before `abc'

Please explain, whats wrong here. The intention is to print a string
in bold characters on the screen.

Thanks,
Anshul
Bye, Jojo
 
J

Joachim Schmitz

santosh said:
Anshul said:
Hi,

I tried compiling this code -

int main()
{
char abc[10] = "%s";
printf("\033[1m" abc "\033[m" , "doing");

Only adjacent string literals are concactenated. The expression abc is
not a string literal, but a pointer constant value.

Try:

printf("\033[1m", " %s ", "\033[m\n", "doing");
Still doesn't work, got to be:
printf("\033[1m%s\033[m\n", "doing");

Bye, Jojo
 
R

Richard Tobin

santosh said:
Only adjacent string literals are concactenated. The expression abc is
not a string literal, but a pointer constant value.

Try:

printf("\033[1m", " %s ", "\033[m\n", "doing");
^ ^

Take out those commas!

-- Richard
 
S

santosh

santosh said:
Anshul said:
Hi,

I tried compiling this code -

int main()
{
char abc[10] = "%s";
printf("\033[1m" abc "\033[m" , "doing");

Only adjacent string literals are concactenated. The expression abc is
not a string literal, but a pointer constant value.

Try:

printf("\033[1m", " %s ", "\033[m\n", "doing");

Sorry to all about this. It should be:

printf("\033[1m" "%s" "\033[m\n", "doing");

or better yet, versions that Richard, Richard and Joachim posted.
 
D

Dik T. Winter

> "santosh said:
> >> Hi,
> >>
> >> I tried compiling this code -
> >>
> >> int main()
> >> {
> >> char abc[10] = "%s";
> >> printf("\033[1m" abc "\033[m" , "doing");
> >
> > Only adjacent string literals are concactenated. The expression abc is
> > not a string literal, but a pointer constant value.
> >
> > Try:
> >
> > printf("\033[1m", " %s ", "\033[m\n", "doing");

Eh, no. But the following *will* work:
#define abc "%s"
printf("\033[1m" abc "\033[m", "doing");
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top