473,425 Members | 1,856 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,425 software developers and data experts.

Your opinion

#define A B
#define B A

main()
{
int A=5;
float B=6.0;
printf("\n %d %f",A,B);
printf(" %d %f",B,A);
}

Take a look at this program. What is the output?
Is it implementation dependent?

Do you think this question aim's at analising a person's C skills?
(It was asked in a Technical skill paper)

--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Nov 13 '05
69 3281
Ravi <me@privacy.net> writes:
$ gcc -E temp.c
# 4 "temp.c"
main()
{
printf("\n %d %f",5,5);
printf(" %d %f",5,5);
}

If someone could help me understand this...


Please ask questions about your compiler in a newsgroup about
your compiler.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Nov 13 '05 #51
Ravi <me@privacy.net> writes:
On 23 Sep 2003 13:57:17 GMT, Da*****@cern.ch (Dan Pop) wrote:
In <m3************@localhost.localdomain> Micah Cowan <mi***@cowan.name> writes:
Ravi <me@privacy.net> writes:

One more redicilous question:
#define scanf "%s is a string"
void main()
{
printf(scanf,scanf);
}

What's the output?
I think the output of this is not certain as well.

Actually, the output is very certain, provided that the prototype for
main() is fixed, and appropriate headers #included.


Is it?
%s is a string is a string

Is what I would expect.


C99 7.1.3 Reserved identifiers:

- Each identifier with file scope listed in any of the following
subclauses (including the future library directions) is
reserved for use as a macro name and as an identifier with
^^^^^^^^^^^^^^^
file scope in the same name space if any of its associated
headers is included.

OTOH, even a C89 implementation can define the macro scanf if
<stdio.h> is included, so, the code, with your suggested fixes, may not
work there, either.

Then, we have the issue of the last line of output not being newline
terminated...

The proper way of fixing the code is:

#define scanf "%s is a string"
int printf(const char *format, ...);
int putchar(int);

int main()
{
printf(scanf, scanf);
putchar('\n');
return 0;
}

The true pedant would also attempt to fix the "restrict" issue in the
printf prototype ;-)


But if we replace scanf with MACDEF will the code be acceptable?


Yes (again, assuming <stdlib.h> was #included or appropriate
prototypes are in place). But in C99, the "restrict" issue Dan Pop
refers to would still be an issue, resulting in potential undefined
behavior.

(To readers who may not have caught the "restrict" issue): The problem
is that identical string literals are not required to be unique: the
string literal by which MACDEF would be replaced might be stored in
the very same memory location for both. In C99, printf()'s format
string argument is declared with the "restrict" specifier, which means
essentially that you promise not to pass it another pointer into the
same object. That promise would be violated by the above code (not to
mention that the printf() protype Dan stuck in there wouldn't be valid
anymore, either, as it lacks the "restrict" specifier).

-Micah
Nov 13 '05 #52
Ravi <me@privacy.net> wrote:
On Wed, 24 Sep 2003 00:50:43 +0530, Ravi <me@privacy.net> wrote:
On Tue, 23 Sep 2003 18:12:51 +0000 (UTC), Andreas Kahari
<ak*******@freeshell.org> wrote:
Or you can also tell me how it can be done on a Linux machine with all
standard development tools installed.

On all systems with the GNU Compiler Collection (GCC) installed
(almost any popular OS I can think of can run GCC):

gcc -E file.c

You've got TFM, now R it.


Thanks for the kind reply.

$ cat temp.c
#define A B
All instances of 'A' are now 'B'
#define B A
All instances of 'B' are now 'A'
#define A 5
All instances of 'A' are now '5'
main()
{
printf("\n %d %f",A,B);
printf(" %d %f",B,A);
} $ gcc -E temp.c
# 4 "temp.c"
main()
{
printf("\n %d %f",5,5);
printf(" %d %f",5,5);
} If someone could help me understand this...


What is confusing you?

Alex

Nov 13 '05 #53
Ravi <me@privacy.net> writes:
$ cat temp.c
#define A B
#define B A
#define A 5
At this point, any behavior we discuss is off-topic, as you now have
undefined behavior by #defining the same symbol twice with different
replacement lists. However...
main()
{
printf("\n %d %f",A,B);
printf(" %d %f",B,A);
}

$ gcc -E temp.c
# 4 "temp.c"
main()
{
printf("\n %d %f",5,5);
printf(" %d %f",5,5);
}

If someone could help me understand this...


What's to understand? A and B have been replaced by their replacement
lists (which happens to be 5 for both of them). As to why it's 5, no
one can answer that, since it was undefined behavior to begin with. If
you're wondering about the first line, that's not C, it's special
symbols the preprocessor sends to hte compiler to tell it where the
rest of the code came from (since it snipped some lines which were
preprocessor directives).

-Micah
Nov 13 '05 #54

On Tue, 23 Sep 2003, Micah Cowan wrote:

Ravi <me@privacy.net> writes:
On 23 Sep 2003 13:57:17 GMT, Da*****@cern.ch (Dan Pop) wrote:

The true pedant would also attempt to fix the "restrict" issue
in the printf prototype ;-)


But if we replace scanf with MACDEF will the code be acceptable?


Yes (again, assuming <stdlib.h> was #included or appropriate
prototypes are in place). But in C99, the "restrict" issue Dan Pop
refers to would still be an issue, resulting in potential undefined
behavior.

(To readers who may not have caught the "restrict" issue): The problem
is that identical string literals are not required to be unique: the
string literal by which MACDEF would be replaced might be stored in
the very same memory location for both. In C99, printf()'s format
string argument is declared with the "restrict" specifier, which means
essentially that you promise not to pass it another pointer into the
same object. That promise would be violated by the above code (not to
mention that the printf() protype Dan stuck in there wouldn't be valid
anymore, either, as it lacks the "restrict" specifier).

Wait... so does standard C99 really make it unspecified whether

char *mystring = "%s";
printf("%s", mystring);

invokes undefined behavior? Pardon me, but that's just crazy.
I don't immediately see how to fix it, other than mandating
that each compiler make sure for themselves that string literals
don't interfere with any 'restrict' issues.

-Arthur,
learning something horribly new every day

Nov 13 '05 #55
[...]
#define A B
#define B A

main()
{
int A=5;
float B=6.0;
printf("\n %d %f",A,B);
printf(" %d %f",B,A);
}

Q: What does this program do?

A: It gets re-written.

--
Keith Thompson (The_Other_Keith) ks*@cts.com <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 13 '05 #56
"Arthur J. O'Dwyer" <aj*@nospam.andrew.cmu.edu> writes:
Wait... so does standard C99 really make it unspecified whether

char *mystring = "%s";
printf("%s", mystring);

invokes undefined behavior? Pardon me, but that's just crazy.
I don't immediately see how to fix it, other than mandating
that each compiler make sure for themselves that string literals
don't interfere with any 'restrict' issues.


I don't think that this code is undefined in C99. Check out
example 3 in C99 6.7.3.1:

10 EXAMPLE 3 The function parameter declarations
void h(int n, int * restrict p, int * restrict q, int * restrict r)
{
int i;
for (i = 0; i < n; i++)
p[i] = q[i] + r[i];
}
illustrate how an unmodified object can be aliased through two restricted pointers. In particular, if a and b
are disjoint arrays, a call of the form h(100, a, b, b) has defined behavior, because array b is not
modified within function h.

printf() doesn't modify its arguments[1] so I don't think that
printf("%s", "%s"); is undefined.

Or am I missing something?

[1] Well, certainly not in this case, unless you want to argue
that one should not pass string literals to printf().
--
A competent C programmer knows how to write C programs correctly,
a C expert knows enough to argue with Dan Pop, and a C expert
expert knows not to bother.
Nov 13 '05 #57
>Subject: Re: Your opinion
From: Ben Pfaff bl*@cs.stanford.edu
Date: 9/22/03 7:01 PM Hawaiian Standard Time
Message-id: <87************@pfaff.stanford.edu>

bi*******@aol.comGetaGrip (Bigdakine) writes:
>From: Ravi me@privacy.net >#define A B
>#define B A
>

But the macros simply mean that when the code is compiled A and B are
switched..


No, it doesn't. Try a C preprocessor. The macros are no-ops.

You're absolutely right. My mistake.

Stuart
Nov 13 '05 #58
>Subject: Re: Your opinion
From: Ravi me@privacy.net
Date: 9/23/03 7:04 AM Hawaiian Standard Time
Message-id: <a8********************************@4ax.com>

On 23 Sep 2003 04:06:22 -0700, ne*****@tokyo.com (Mantorok Redgormor)
wrote:
Ben Pfaff <bl*@cs.stanford.edu> wrote in message

news:<87************@pfaff.stanford.edu>...
bi*******@aol.comGetaGrip (Bigdakine) writes:

> >From: Ravi me@privacy.net

> >#define A B
> >#define B A
> >

> But the macros simply mean that when the code is compiled A and B are
> switched..

No, it doesn't. Try a C preprocessor. The macros are no-ops.


Why does this perform no operation? What am I missing?


#define A B
#define B A
int main(void)
{
B;
return 1;
}

on compiling:
Error 7: Undefined symbol 'B'
_____________

//#define A B
#define B A
int main(void)
{
B;
return 1;
}

On compiling:
Error 7: Undefined symbol 'A'

This might make thing clear :)

Actually no. THis has nothing to do with the macros. In your code " B ;"
doesn't make sense. B has no type and is hence undefined. So when it is
replaced by A, it is still undefined.

My mistake was in thinking that the preprocessor didn't make recursive
substitutions.

Course, I wouldn't try that example in real life.

Stuart
Dr. Stuart A. Weinstein
Ewa Beach Institute of Tectonics
"To err is human, but to really foul things up
requires a creationist"
Nov 13 '05 #59
On Tue, 23 Sep 2003 23:16:10 +0530, in comp.lang.c , Ravi
<me@privacy.net> wrote:
On 23 Sep 2003 17:34:22 GMT, Da*****@cern.ch (Dan Pop) wrote:
Do yourself a favour and learn how to obtain the preprocessor's output:
it's much easier to solve preprocessing issues this way than by
examining compiler error messages.


How would I do that on Turbo C++?


either by reading the manual, or by asking in a TC group, or by typing
in TC /?

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
Nov 13 '05 #60
On Tue, 23 Sep 2003 13:16:18 +0200, in comp.lang.c , Irrwahn
Grausewitz <ir*****************@freenet.de> wrote:
Martin Ambuhl <ma*****@earthlink.net> wrote:
<SNIP>
Ask Don Pop about details on thinking before you post.

^
Is this a typo or a joke?

Don is Dan's twin brother - Don got the people skills, Dan the C
skills... :-)
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
Nov 13 '05 #61
bi*******@aol.comGetaGrip (Bigdakine) writes:
[...]
#define A B
#define B A
int main(void)
{
B;
return 1;
}

on compiling:
Error 7: Undefined symbol 'B'
_____________

//#define A B
#define B A
int main(void)
{
B;
return 1;
}

On compiling:
Error 7: Undefined symbol 'A'

This might make thing clear :)


Actually no. THis has nothing to do with the macros. In your code " B ;"
doesn't make sense. B has no type and is hence undefined. So when it is
replaced by A, it is still undefined.


The point, I think is that the compiler's error message, "Undefined
symbol 'B'" or "Undefined symbol 'A'", tells you what the symbol B was
ultimately replaced with by the preprocessor. Presumably the part of
the compiler that complains about undefined symbols runs after the
preprocessor (and doesn't have any visibility to the un-preprocessed
source).

It's an odd way of figuring out what the preprocessor is doing, but it
works in this case.

--
Keith Thompson (The_Other_Keith) ks*@cts.com <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 13 '05 #62
Ben Pfaff <bl*@cs.stanford.edu> writes:
"Arthur J. O'Dwyer" <aj*@nospam.andrew.cmu.edu> writes:
Wait... so does standard C99 really make it unspecified whether

char *mystring = "%s";
printf("%s", mystring);

invokes undefined behavior? Pardon me, but that's just crazy.
I don't immediately see how to fix it, other than mandating
that each compiler make sure for themselves that string literals
don't interfere with any 'restrict' issues.
I don't think that this code is undefined in C99. Check out
example 3 in C99 6.7.3.1:


<snipped example>
printf() doesn't modify its arguments[1] so I don't think that
printf("%s", "%s"); is undefined.

Or am I missing something?

[1] Well, certainly not in this case, unless you want to argue
that one should not pass string literals to printf().


Yeah, okay, that sounds right. In that case, Dan, what *was* the
issue? Or were you thinking the same way? Or was the "restrict issue"
the misdeclaration of printf()?

-Micah

Nov 13 '05 #63
In <m3************@localhost.localdomain> Micah Cowan <mi***@cowan.name> writes:
Yeah, okay, that sounds right. In that case, Dan, what *was* the
issue? Or were you thinking the same way? Or was the "restrict issue"
the misdeclaration of printf()?


The latter. It's trivial to fix by defining restrict as an empty macro
if the compiler doesn't claim C99 conformance and using the proper C99
prototype for printf.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #64
In <iu********************************@4ax.com> Ravi <me@privacy.net> writes:
On Tue, 23 Sep 2003 23:16:10 +0530, Ravi <me@privacy.net> wrote:
On 23 Sep 2003 17:34:22 GMT, Da*****@cern.ch (Dan Pop) wrote:
Do yourself a favour and learn how to obtain the preprocessor's output:
it's much easier to solve preprocessing issues this way than by
examining compiler error messages.
How would I do that on Turbo C++?


Back when I used it, some 13 years ago, it came with a separate utility
that was performing only the preprocessing. It was meant to help
in such cases.
Or you can also tell me how it can be done on a Linux machine with all
standard development tools installed.


Ever considered reading the gcc man page?

-E Stop after the preprocessing stage; do not run the
compiler proper. The output is preprocessed source
code, which is sent to the standard output.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #65
Groovy hepcat Steve Zimmerman was jivin' on Mon, 22 Sep 2003 19:57:54
GMT in comp.lang.c.
Re: Your opinion's a cool scene! Dig it!
Ravi wrote:
> #define A B
> #define B A
>
> main()
> {
> int A=5;
> float B=6.0;
> printf("\n %d %f",A,B);
> printf(" %d %f",B,A);
> }
>
> Take a look at this program. What is the output?
> Is it implementation dependent?
>
> Do you think this question aim's at analising a person's C skills?
> (It was asked in a Technical skill paper)
Thank you for this post, Ravi.

Output: 5 6.000000 0 0.000000
(There is one space before the 5.)

I don't know if it's implementation dependent.


If you don't know, why say anithing?
The code that you cite from the technical skill paper is referred to
as "spaghetti code" by _The New Hacker's Dictionary_. Spaghetti code
is code with an unnecessarily complex and tangled control structure.


Nonsense! This program has a very short and absolutely linear flow,
one statement after another - literally.

Nov 13 '05 #66
"Ben Pfaff" <bl*@cs.stanford.edu> wrote in message
news:87************@pfaff.stanford.edu...
Ravi <me@privacy.net> writes:
#define A B
#define B A


Pointless and stupid and effectively a no-op besides.
main()


You should declare main() as explicitly returning `int'. This is
required in C99. You should also write `void' within the
parentheses to give it a prototype, though it is not required.
{
int A=5;
float B=6.0;
printf("\n %d %f",A,B);
printf(" %d %f",B,A);


You should return a value from main().
}

Take a look at this program. What is the output?
Is it implementation dependent?


It is undefined for at least two reasons: first, trying to print
out an `int' using %f; second, for failing to write a final
new-line character to stdout.


The last omission merely invokes implementation defined behaviour.

[The standard means to say that in order to be a line, an implementation may
require the presense of a trailing \n, otherwise it will be ignored.
Unfortunately, C99 has a much worse situation of UB in the guise of IDB,
namely in the potential signal raised in conversions to signed integers.]
Do you think this question aim's at analising a person's C skills? ^^^^^^^^^
Quite probably!
(It was asked in a Technical skill paper)


I think it aims at testing something, but it has little to do
with C skills, especially considering the stupid macro
definitions. It tests knowledge of C trivia, not knowledge of C
usage.


I'd be asking if it was a typical example of the companies code... ;)

--
Peter
Nov 13 '05 #67
In <3f******@news.rivernet.com.au> "Peter Nilsson" <ai***@acay.com.au> writes:
Ravi <me@privacy.net> writes:
> Do you think this question aim's at analising a person's C skills?

^^^^^^^^^
Quite probably!


Few non-native English speakers are likely to get this one.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #68
Dan Pop <Da*****@cern.ch> scribbled the following:
In <3f******@news.rivernet.com.au> "Peter Nilsson" <ai***@acay.com.au> writes:
Ravi <me@privacy.net> writes:
> Do you think this question aim's at analising a person's C skills? ^^^^^^^^^
Quite probably!

Few non-native English speakers are likely to get this one.


I got it and I'm a non-native English speaker. What do I win?

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"A friend of mine is into Voodoo Acupuncture. You don't have to go into her
office. You'll just be walking down the street and... ohh, that's much better!"
- Stephen Wright
Nov 13 '05 #69
On 6 Oct 2003 17:02:25 GMT, Joona I Palaste <pa*****@cc.helsinki.fi>
wrote:
Dan Pop <Da*****@cern.ch> scribbled the following:
In <3f******@news.rivernet.com.au> "Peter Nilsson" <ai***@acay.com.au> writes:
Ravi <me@privacy.net> writes:
> Do you think this question aim's at analising a person's C skills?
^^^^^^^^^
Quite probably!

Few non-native English speakers are likely to get this one.


I got it and I'm a non-native English speaker. What do I win?


Obviously you get to be declared (or is it defined) one of the few.
And if you watch American TV commercials that makes you one of the
brave and proud.
<<Remove the del for email>>
Nov 13 '05 #70

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

Similar topics

4
by: Avery Warren | last post by:
I am investigating converting a wiki site to plone. I am having a lot of difficulty finding good documentation programmatically accessing the ZODB API. A lot of the user feedback is centered on...
11
by: Michael Kalina | last post by:
Hi everybody! I have three questions, maybe somebody could help me with it. 1. "If you have a minute or two..." I usually do not do a lot with CSS, but I decided to have my site run on pure...
11
by: csomberg | last post by:
SQL 2000 I thought I would throw this out there for some feedback from others. I'd like to know if you feel using MS auto-increment field is a good solution these days or should one grow their...
192
by: Vortex Soft | last post by:
http://www.junglecreatures.com/ Try it and tell me what's happenning in the Microsoft Corporation. Notes: VB, C# are CLS compliant
9
by: Tony Johansson | last post by:
Hello! Some information to be able to have a change to answer the question. Assume I have a large system developed in MFC. In this system we have only C++ code so no other language exist. This...
15
by: CR | last post by:
I've noticed that the trend these days is to declare variables in the middle of code instead of at the top. What is the advantage of this? It seems like it makes it hard to reuse variables. Here...
20
by: C# Beginner | last post by:
I'm currently creating a database class, which contains a member called Open(). With this method users can open different databases. When a user tries to open a database which happens to be secured...
11
by: www.douglassdavis.com | last post by:
I'm looking for advice here, and I would really appreciate it if you could help. Is there a VB 2005 book that you like and would recommend (and why)? Would you consider it good for...
15
by: =?Utf-8?B?TWljaGVsIFBvc3NldGggW01DUF0=?= | last post by:
In my opinion rethrowing exceptions without providing anny extra information is a totall waste Examples : in my opinion wrong : A: Public sub DoSomeStuff() Try do it
2
by: cephal0n | last post by:
Hi All! First of I apologize for my previews post needing help on union select and not providing so more explanation, but thank you very much for your opinion and sugestion. I have thought about my...
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,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.