Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > fix code

Reply
Thread Tools

fix code

 
 
aarklon@gmail.com
Guest
Posts: n/a
 
      03-08-2006
Hi all,

the following is a question which i found on a book,the reader is asked
to predict the output

#include<stdio.h>

#define SUM(F_NAME,DATA_TYPE,L)\
void F_NAME(DATA_TYPE x,DATA_TYPE y)\
{\
DATA_TYPE add;\
add = x + y;\
printf("The summation of "#DATA_TYPE""\
" values is %"#L"\n",add);\
}

void sum_int(int,int);
void sum_float(float,float);
int main(void)
{
sum_int(3,5);
sum_float(3.1,5.3);

return 0;
}

SUM(sum_int,int,d);
SUM(sum_float,float,f);

Output is given as

the summation of int values is 8
the summation of float values is 8.400000


But I am getting the error as # operator is not followed by a macro
argument name.
can anybody suggest ways to fix this code??

 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      03-08-2006


http://www.velocityreviews.com/forums/(E-Mail Removed) wrote On 03/08/06 13:59,:
> Hi all,
>
> the following is a question which i found on a book,the reader is asked
> to predict the output
>
> #include<stdio.h>
>
> #define SUM(F_NAME,DATA_TYPE,L)\
> void F_NAME(DATA_TYPE x,DATA_TYPE y)\
> {\
> DATA_TYPE add;\
> add = x + y;\
> printf("The summation of "#DATA_TYPE""\
> " values is %"#L"\n",add);\


Insert a space between the L and the following ".

The sequence L"\n" is a wide string literal that
will (eventually) produce a zero-terminated array of
wide characters. This sequence is recognized during
translation phase 3; macro processing doesn't happen
until phase 4. By that time, the L is long gone so
the macro processing encounters

string_literal # wide_string_literal

.... and the error results. Separating the L from what
follows means the combination is no longer recognized
as a wide string literal, so during macro expansion
you have

string_literal # L string_literal

.... as intended.


> }
>
> void sum_int(int,int);
> void sum_float(float,float);
> int main(void)
> {
> sum_int(3,5);
> sum_float(3.1,5.3);
>
> return 0;
> }
>
> SUM(sum_int,int,d);
> SUM(sum_float,float,f);


Another improvement would be to get rid of the
semicolons in these two lines. After macro expansion
you'll have

void sum_int(...) {
...
}
;
void sum_float(...) {
...
}
;

--
(E-Mail Removed)

 
Reply With Quote
 
 
 
 
aarklon@gmail.com
Guest
Posts: n/a
 
      03-08-2006
Can you explain what is meant by phase 4.
as far as i know the different phases of a compiler are

lexical analysis
syntax analysis
semantic analysis
intermediate code generation
code optimization
code generation

is it during intermediate code generation phase?

 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      03-08-2006
(E-Mail Removed) writes:

> Can you explain what is meant by phase 4.


The C standard divides translation into 8 phases. Phase 4 is
this:

4. Preprocessing directives are executed, macro invocations
are expanded, and _Pragma unary operator expressions are
executed. If a character sequence that matches the
syntax of a universal character name is produced by token
concatenation (6.10.3.3), the behavior is undefined. A
#include preprocessing directive causes the named header
or source file to be processed from phase 1 through phase
4, recursively. All preprocessing directives are then
deleted.

--
"I'm not here to convince idiots not to be stupid.
They won't listen anyway."
--Dann Corbit
 
Reply With Quote
 
Micah Cowan
Guest
Posts: n/a
 
      03-08-2006
(E-Mail Removed) writes:

> Hi all,
>
> the following is a question which i found on a book,the reader is asked
> to predict the output
>
> #include<stdio.h>
>
> #define SUM(F_NAME,DATA_TYPE,L)\
> void F_NAME(DATA_TYPE x,DATA_TYPE y)\
> {\
> DATA_TYPE add;\
> add = x + y;\
> printf("The summation of "#DATA_TYPE""\
> " values is %"#L"\n",add);\
> }
>
> void sum_int(int,int);
> void sum_float(float,float);
> int main(void)
> {
> sum_int(3,5);
> sum_float(3.1,5.3);
>
> return 0;
> }
>
> SUM(sum_int,int,d);
> SUM(sum_float,float,f);
>
> Output is given as
>
> the summation of int values is 8
> the summation of float values is 8.400000
>
>
> But I am getting the error as # operator is not followed by a macro
> argument name.
> can anybody suggest ways to fix this code??


Yes.

1. Remove the final semicolons from your invocations of SUM(). Does
the original have these?
2. Use a space between the L and the " following it, or use a
different letter.

Your problem is that the sequence {L"} is the start of a /wide string
literal/. The L will never be tokenized into an identifier, and
therefore isn't recognized as the macro parameter.

HTH,
-Micah
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      03-09-2006
Eric Sosman <(E-Mail Removed)> writes:
> (E-Mail Removed) wrote On 03/08/06 13:59,:
>> Hi all,
>>
>> the following is a question which i found on a book,the reader is asked
>> to predict the output
>>
>> #include<stdio.h>
>>
>> #define SUM(F_NAME,DATA_TYPE,L)\
>> void F_NAME(DATA_TYPE x,DATA_TYPE y)\
>> {\
>> DATA_TYPE add;\
>> add = x + y;\
>> printf("The summation of "#DATA_TYPE""\
>> " values is %"#L"\n",add);\

>
> Insert a space between the L and the following ".
>
> The sequence L"\n" is a wide string literal that
> will (eventually) produce a zero-terminated array of
> wide characters. This sequence is recognized during
> translation phase 3; macro processing doesn't happen
> until phase 4. By that time, the L is long gone so
> the macro processing encounters
>
> string_literal # wide_string_literal
>
> ... and the error results. Separating the L from what
> follows means the combination is no longer recognized
> as a wide string literal, so during macro expansion
> you have
>
> string_literal # L string_literal
>
> ... as intended.


Or use an identifier other than L.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[FIX] Another possible fix for the missing ASP .Net tab problem Patrick Philippot ASP .Net 0 04-14-2006 11:48 AM
Xah's Edu Corner: The Concepts and Confusions of Pre-fix, In-fix, Post-fix and Fully Functional Notations Xah Lee Perl Misc 21 03-21-2006 07:02 AM
Xah's Edu Corner: The Concepts and Confusions of Pre-fix, In-fix, Post-fix and Fully Functional Notations Xah Lee Python 23 03-21-2006 07:02 AM
Xah's Edu Corner: The Concepts and Confusions of Pre-fix, In-fix, Post-fix and Fully Functional Notations Xah Lee Java 22 03-21-2006 07:02 AM
Possible code fix for Forms Authentication vulnerability. Ken Dopierala Jr. ASP .Net 0 10-01-2004 06:34 PM



Advertisments