Searching \ for ' String constants in ANSI C' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/language/index.htm?key=c
Search entire site for: 'String constants in ANSI C'.

No exact or substring matches. trying for part
PICList Thread
'[PICLIST] String constants in ANSI C'
2000\10\24@095916 by mike

flavicon
face
       
A couple of quick C questions - I'm actually using an ST7 compiler but
I would expect this stuff to be 'ansi standard' - I can't find the
answer in the couple of rather poor C books I have.
How do you concatenate parts of a string constant into a single string
at compile time?

For example, what I want to do is something like : #define clschar 0x01
#define tabchar 0x02

disp(clschar+tabchar);

Obviously the compiler complains about this!
I can of course do disp("\x01\x02");  but that defeats the point of
trying to use meaningful names for control characters.
How do I tell the compiler that I want a 2-character string "\x01\x02"
and not a single character 03 ?

Another problem I've found is when mixing ascii and escape characters,
e.g. disp("\x01Code");
the 'C' appears to be interpreted as part of the escape sequence, not
a new character. other than turning ascii 0-9,A-F into hex is there a
way round this ?  I'm not sure if this one is a compiler problem or a problem with the
Ansi spec - there's no reason it would need to look at more than 2
characters in the escape sequence.

--
http://www.piclist.com hint: PICList Posts must start with ONE topic:
"[PIC]:","[SX]:","[AVR]:"

'[OT]: String constants in ANSI C (Was : String c'
2000\10\24@102619 by David Kott

flavicon
face
A couple of quick C questions - I'm actually using an ST7 compiler but
I would expect this stuff to be 'ansi standard' - I can't find the
answer in the couple of rather poor C books I have.

How do you concatenate parts of a string constant into a single string
at compile time?

For example, what I want to do is something like :
#define clschar 0x01
#define tabchar 0x02

disp(clschar+tabchar);

Obviously the compiler complains about this!
I can of course do disp("\x01\x02");  but that defeats the point of
trying to use meaningful names for control characters.


Use the preprocessor concatenation operator, ##.

For ex:

#define past(front, back)   front ## back

From the venerable K&R:

"The preprocessor operator ## provides a way to concatenate actual arguments
during macro expansion.  If a parameter in the replacement text is adjacent
to a ##, the parameter is replaced by the actual argument, the ## and
surrounding white space are removed, and the result is re-scanned."(c)

Second Edition, pg. 90.

-d

(c) Prentice-Hall, Inc.

--
http://www.piclist.com hint: PICList Posts must start with ONE topic:
"[PIC]:","[SX]:","[AVR]:" =uP ONLY! "[EE]:","[OT]:" =Other "[BUY]:","[AD]:" =Ads




2000\10\24@131459 by rich+piclist

flavicon
face
C concats adjacent strings:

disp("\x01" "Code");

== Rich

On Tue, 24 Oct 2000, Mike Harrison wrote:

{Quote hidden}

--
http://www.piclist.com hint: PICList Posts must start with ONE topic:
"[PIC]:","[SX]:","[AVR]:"

2000\10\24@163012 by Bill Westfield

face picon face
   > How do you concatenate parts of a string constant into a single string
   > at compile time?
   >
   >  For example, what I want to do is something like :
   > #define clschar 0x01
   > #define tabchar 0x02
   >
   > disp(clschar+tabchar);
   >
   > Obviously the compiler complains about this!
   > I can of course do disp("\x01\x02");  but that defeats the point of
   > trying to use meaningful names for control characters.

static const char mystring[] = {clschar, tabchr, 't', 'e', 's', 't', 0};
disp(mystring);

BillW

--
http://www.piclist.com hint: PICList Posts must start with ONE topic:
"[PIC]:","[SX]:","[AVR]:" =uP ONLY! "[EE]:","[OT]:" =Other "[BUY]:","[AD]:" =Ads




2000\10\24@164429 by Pfaff, John
flavicon
face
Or maybe this

#define clschar "\x01"
#define tabchar "\x02"

disp(clschar tabchar);

If I remember correctly, the preprocessor will boil it down to this:

disp( "\x01" "\x02" );

which should give you what you want.

{Original Message removed}

2000\10\25@171032 by Peter L. Peres

picon face
Concatenating strings and concatenating characters are two different
things. You have two characters #defined. To concatenate them you must
first make them strings. Of course there are shortcuts. Since strings are
arrays of char in C, you can so this:

char buffer[8];

buffer[0]=CHAR1;
buffer[1]=CHAR2;
buffer[2]='\0'; /* this assignment is part of that proverbial rope */

disp(buffer);

Or:

sprintf(buffer,"%c%c",CHAR1,CHAR2);
disp(buffer);

which does exactly the same thing but is more flexible. If you already use
a printf statement in your code then you can use the second form. If you
have no other printf then the first form will (may) save program space.

hope this helps,

Peter

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics




2000\10\25@172002 by briang

flavicon
face
In-Reply-To: <00ed01c03dc6$34630f20$810000c0@dkott>

David Kott <dkottspamKILLspamtestprod.com> wrote:
> Use the preprocessor concatenation operator, ##.

You are answering the wrong question.

Brian Gregory.
.....briangKILLspamspam.....cix.co.uk

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics




2000\10\25@204042 by Bob Ammerman

picon face
----- Original Message -----
From: Peter L. Peres <EraseMEplpspam_OUTspamTakeThisOuTACTCOM.CO.IL>
To: <PICLISTspamspam_OUTMITVMA.MIT.EDU>
Sent: Thursday, October 26, 2000 2:33 PM
Subject: Re: [OT]: String constants in ANSI C (Was : String constants in
ANSI C)


{Quote hidden}

Unfortunately, sprintf uses most of the same code as printf, so it won't
reduce program size to use sprintf.

Bob Ammerman
RAm Systems
(contract development of high performance, high function, low-level
software)

{Quote hidden}

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics




2000\10\25@211440 by Bill Westfield

face picon face
>    #define clschar "\x01"
>    #define tabchar "\x02"
>    disp(clschar tabchar);
>
>    If I remember correctly, the preprocessor will boil it down to this:
>    disp( "\x01" "\x02" );
>    which should give you what you want.

Ahh.  Forgot about that one.  I don't thing compiler string concatenation
("ab" "cd" --> "abcd") is standardized, but some compilers do this, and
I like this much better than my initialized string example!  Watch out
for accidentally adding the comma, cause that'll be quite different!

BillW

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics




2000\10\26@113212 by Douglas Wood

flavicon
face
Actually, all of the xprintf functions use the core routines of sprintf to
create the formatted output  string before sedning that data to the target
output (console for printf, files for fprintf, etc.)

Douglas Wood
Software Engineer

{Original Message removed}

2000\10\26@134513 by Peter L. Peres

picon face
>Unfortunately, sprintf uses most of the same code as printf, so it won't
>reduce program size to use sprintf.

What I was trying to say was, that if you already use printf or sprintf in
your program, then go ahead and use it here too, else avoid it. I am sorry
if I did not make myself clear.

Peter

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.




2000\10\26@141421 by Bob Ammerman

picon face
----- Original Message -----
From: Douglas Wood <@spam@douglas_woodKILLspamspamARIUSA.COM>
To: <KILLspamPICLISTKILLspamspamMITVMA.MIT.EDU>
Sent: Thursday, October 26, 2000 11:29 AM
Subject: Re: [OT]: String constants in ANSI C (Was : String constants in
ANSI C)


> Actually, all of the xprintf functions use the core routines of sprintf to
> create the formatted output  string before sedning that data to the target
> output (console for printf, files for fprintf, etc.)


Actually, it depends on the C library, but most of them have a core function
whose prototype might look something like this:

void    __anyprintf( void f(char), void *ctx, char const *, va_args *ap);

ie: this is a function that takes an arbitrary function 'f' and context
'ctx' and feeds it the characters resulting from formatting the data.

now fprintf, printf, sprintf just call __anyprintf with an appropriate
function to dispose of the characters properly.

Bob Ammerman
RAm Systems
(contract development of high performance, high function, low-level
software)

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.




2000\10\26@144747 by Peter L. Peres

picon face
I forgot to mention that it is VERY wasteful to define short STRINGS. This
is because of the terminating '\0' which consumes at least one extra
location per string. For example, five one-character constant strings
consume 10 program locations for definition, and at least one more for
each invocation in the code (to load the pointer, for a total of at least
15 !, whereas five #defined one character constants consume one location
each in program space, for every time they are invoked, and no constant
storage at all. In other words, the code consumption for #defined one
character constants is at least 3 times lower than for #defined or
constant strings. Similar logic applies for two character strings. The
break even happens at three character strings with 12 and 14 bit PICs I
think. These ideas are valid for any architecture (but with different
break even sizes).

Peter

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.




2000\10\26@150114 by rich+piclist

flavicon
face
> Actually, it depends on the C library, but most of them have a core function
> whose prototype might look something like this:
>
> void    __anyprintf( void f(char), void *ctx, char const *, va_args *ap);
And man, many's the time I wished it was standard. Great for streaming
formatted text to somewhere w/o need of intermediate buffer, but
unfortunately a dangerous hack.

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.




More... (looser matching)
- Last day of these posts
- In 2000 , 2001 only
- Today
- New search...