473,386 Members | 1,819 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

fix code

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??

Mar 8 '06 #1
5 2009


aa*****@gmail.com 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(...) {
...
}
;

--
Er*********@sun.com

Mar 8 '06 #2
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?

Mar 8 '06 #3
aa*****@gmail.com 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
Mar 8 '06 #4
aa*****@gmail.com 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
Mar 8 '06 #5
Eric Sosman <Er*********@sun.com> writes:
aa*****@gmail.com 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) ks***@mib.org <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.
Mar 9 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

51
by: Mudge | last post by:
Please, someone, tell me why OO in PHP is better than procedural.
9
by: bigoxygen | last post by:
Hi. I'm using a 3 tier FrontController Design for my web application right now. The problem is that I'm finding to have to duplicate a lot of code for similar functions; for example, listing...
4
by: jason | last post by:
Hello. Newbie on SQL and suffering through this. I have two tables created as such: drop table table1; go drop table table2; go
16
by: Dario de Judicibus | last post by:
I'm getting crazy. Look at this code: #include <string.h> #include <stdio.h> #include <iostream.h> using namespace std ; char ini_code = {0xFF, 0xFE} ; char line_sep = {0x20, 0x28} ;
109
by: Andrew Thompson | last post by:
It seems most people get there JS off web sites, which is entirely logical. But it is also a great pity since most of that code is of such poor quality. I was looking through the JS FAQ for any...
5
by: ED | last post by:
I currently have vba code that ranks employees based on their average job time ordered by their region, zone, and job code. I currently have vba code that will cycle through a query and ranks each...
0
by: Namratha Shah \(Nasha\) | last post by:
Hey Guys, Today we are going to look at Code Access Security. Code access security is a feature of .NET that manages code depending on its trust level. If the CLS trusts the code enough to...
18
by: Joe Fallon | last post by:
I have some complex logic which is fairly simply to build up into a string. I needed a way to Eval this string and return a Boolean result. This code works fine to achieve that goal. My...
37
by: Alan Silver | last post by:
Hello, Newbie here, so please forgive what is probably a basic question ... I see a lot of discussion about "code behind", which if I have understood correctly, means that the script code goes...
171
by: tshad | last post by:
I am just trying to decide whether to split my code and uses code behind. I did it with one of my pages and found it was quite a bit of trouble. I know that most people (and books and articles)...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.