473,326 Members | 2,147 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,326 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 #1
69 3269
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.
Do you think this question aim's at analising a person's C skills?
(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.
--
Just another C hacker.
Nov 13 '05 #2
On Mon, 22 Sep 2003 21:48:21 +0530, Ravi <me@privacy.net> wrote:
#define A B
#define B A

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


Nick.

Nov 13 '05 #3
Nick Austin <ni**********@nildram.co.uk> writes:
On Mon, 22 Sep 2003 21:48:21 +0530, Ravi <me@privacy.net> wrote:
#define A B
#define B A

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


Undefined behavior.


In that line? Where?
--
"I should killfile you where you stand, worthless human." --Kaz
Nov 13 '05 #4
On 22 Sep 2003 09:44:56 -0700, Ben Pfaff <bl*@cs.stanford.edu> wrote:
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.


What's worse it had void main() originally and also a clrscr()
somewhere in between.

And the funniest part:
"What is the output on compiling the program:"
When they actually meant running the program.

LOL
And they think they are testing someones technical skill.

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.

(Don't blame me as THEY did not feel like including any header file
and felt like using void main()
)
Nov 13 '05 #5
Ben Pfaff wrote:
Nick Austin <ni**********@nildram.co.uk> writes:

On Mon, 22 Sep 2003 21:48:21 +0530, Ravi <me@privacy.net> wrote:

#define A B
#define B A

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


Undefined behavior.

In that line? Where?


I'd bet on "using a variadic function without a prototype in scope"...

--
Bertrand Mollinier Toublet
"No sea vivo, Buendia" -- El presidente del tribunal,
in Cien anos de soledad, de Gabriel Garcia Marquez

Nov 13 '05 #6
Ben Pfaff <bl*@cs.stanford.edu> wrote:
Nick Austin <ni**********@nildram.co.uk> writes:
On Mon, 22 Sep 2003 21:48:21 +0530, Ravi <me@privacy.net> wrote:
>#define A B
>#define B A
>
>main()
>{
> int A=5;
> float B=6.0;
> printf("\n %d %f",A,B);


Undefined behavior.


In that line? Where?


No prototype for printf() in scope, hence UB.

Regards

Irrwahn
--
My other computer is a abacus.
Nov 13 '05 #7
On Mon, 22 Sep 2003 18:16:45 +0100, Nick Austin
<ni**********@nildram.co.uk> wrote:
On Mon, 22 Sep 2003 21:48:21 +0530, Ravi <me@privacy.net> wrote:
#define A B
#define B A

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


Undefined behavior.
printf(" %d %f",B,A);


Undefined behavior thrice.
}


Ok, I get the point. But if you were asked this question in a
technical test with 4 options

a) error during compilation
b) goes into an infinite loop
c) 5 6.000000 6 5.000000
d) 5 6.000000 0 0.000000

What would you choose?

Nov 13 '05 #8
Ravi <me@privacy.net> wrote:
On Mon, 22 Sep 2003 18:16:45 +0100, Nick Austin
<ni**********@nildram.co.uk> wrote:
On Mon, 22 Sep 2003 21:48:21 +0530, Ravi <me@privacy.net> wrote:
#define A B
#define B A

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


Undefined behavior.
printf(" %d %f",B,A);


Undefined behavior thrice.
}


Ok, I get the point. But if you were asked this question in a
technical test with 4 options

a) error during compilation
b) goes into an infinite loop
c) 5 6.000000 6 5.000000
d) 5 6.000000 0 0.000000

What would you choose?


e) I do not want to work for this company, bye.

;-)

Irrwahn
--
My other computer is a abacus.
Nov 13 '05 #9
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?
The same as if the #defines were not there.
Is it implementation dependent?
The possibility that it might successfully compile and run is
implementation dependent.
Do you think this question aim's at analising a person's C skills?
Only if the correct answer included noticing
1) failure to #include <stdio.h>, to provide the needed prototype for the
variadic function printf.
2) failure to specify the corrent return type of main(), required by C99.
3) failure to return a value from a function which (in C89/C90) promised
implicitly to return an int.
4) mismatched printf specifiers and arguments, attempting stupidly to
print a float with the int specifier %d and to print an int with the
double specifier %f.
5) failure to terminate the last output line with a line-termination
character ('\n'), resulting in implementation-defined behavior.
6) stupid (and pointless) preprocessor tricks.
(It was asked in a Technical skill paper)


And did the answer include the above points?

--
Martin Ambuhl

Nov 13 '05 #10
Nick Austin wrote:
On Mon, 22 Sep 2003 21:48:21 +0530, Ravi <me@privacy.net> wrote:

#define A B
#define B A

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

Undefined behavior.


Balony. Printing an int with %d and a float with %f is just fine.


--
Martin Ambuhl

Nov 13 '05 #11
On Tue, 23 Sep 2003 00:12:57 +0530
Ravi <me@privacy.net> wrote:
On Mon, 22 Sep 2003 18:16:45 +0100, Nick Austin
<ni**********@nildram.co.uk> wrote:
On Mon, 22 Sep 2003 21:48:21 +0530, Ravi <me@privacy.net> wrote:
#define A B
#define B A

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


Undefined behavior.
printf(" %d %f",B,A);


Undefined behavior thrice.
}


Ok, I get the point. But if you were asked this question in a
technical test with 4 options

a) error during compilation
b) goes into an infinite loop
c) 5 6.000000 6 5.000000
d) 5 6.000000 0 0.000000

What would you choose?


Depends on whether I wanted the job. They probably expected d, but I
would definitely be inclined to point out that the question was absurd
and that the code invoked UB. However I don't think I would want a job
where they asked such questions.
--
Mark Gordon
Paid to be a Geek & a Senior Software Developer
Although my email address says spamtrap, it is real and I read it.
Nov 13 '05 #12
On Tue, 23 Sep 2003 00:12:57 +0530, Ravi <me@privacy.net> wrote:
On Mon, 22 Sep 2003 21:48:21 +0530, Ravi <me@privacy.net> 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);
}


Ok, I get the point. But if you were asked this question in a
technical test with 4 options

a) error during compilation
b) goes into an infinite loop
c) 5 6.000000 6 5.000000
d) 5 6.000000 0 0.000000

What would you choose?


a) is a serious contender, but the examiner probably expects d).

The question is obviously flawed for not including:

e) outputs only a single newline.

Nick.

Nov 13 '05 #13
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.

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.

http://www.jargon.8hz.com/jargon_34.html#SEC41

_The New Hacker's Dictionary_ is by Eric S. Raymond.
--Steve

Nov 13 '05 #14
On Mon, 22 Sep 2003 21:29:32 +0200, Irrwahn Grausewitz
<ir*****************@freenet.de> wrote:
Ravi <me@privacy.net> wrote:
On Mon, 22 Sep 2003 18:16:45 +0100, Nick Austin
<ni**********@nildram.co.uk> wrote:
On Mon, 22 Sep 2003 21:48:21 +0530, Ravi <me@privacy.net> wrote:

#define A B
#define B A

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

Undefined behavior.

printf(" %d %f",B,A);

Undefined behavior thrice.

}


Ok, I get the point. But if you were asked this question in a
technical test with 4 options

a) error during compilation
b) goes into an infinite loop
c) 5 6.000000 6 5.000000
d) 5 6.000000 0 0.000000

What would you choose?


e) I do not want to work for this company, bye.


But I want to.
Great pay for a fresher like me!

Nov 13 '05 #15

On Mon, 22 Sep 2003, Steve Zimmerman wrote:

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.

STOP STOP STOP STOP STOP STOP STOP STOP STOP!

Thank you.

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.


Two variables and linear control flow is *not* spaghetti code,
no matter how little you understand of it. "Spaghetti code"
refers to code with complex and/or hard-to-follow control
flow, such as would be difficult to trace on paper.

-Arthur

Nov 13 '05 #16
Steve Zimmerman <st******@sonic.net> wrote:
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.)


And some small town in southern North Dakota being blown up. =%O

I don't know if it's implementation dependent.
It is. Every single bit of it. Seriously.

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.


Code like this is referred to as being downright idiotic. Period.

Regards

Irrwahn
--
Close your eyes and press escape three times.
Nov 13 '05 #17
Nick Austin <ni**********@nildram.co.uk> wrote:
On Tue, 23 Sep 2003 00:12:57 +0530, Ravi <me@privacy.net> wrote:
On Mon, 22 Sep 2003 21:48:21 +0530, Ravi <me@privacy.net> 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);
}


Ok, I get the point. But if you were asked this question in a
technical test with 4 options

a) error during compilation
b) goes into an infinite loop
c) 5 6.000000 6 5.000000
d) 5 6.000000 0 0.000000

What would you choose?


a) is a serious contender, but the examiner probably expects d).

The question is obviously flawed for not including:

e) outputs only a single newline.

As well as:

f) most certainly invokes nasal demons when ran on a DeathStation 9000
g) surely you're joking, Mr. employer
h) /What/ the /heck/ is /that/?!?
i) May I have another cup of tea? It's gonna be a loooong answer...
j) Uck!
h) Well, ...

Alright, I stop it now; this list could be continued ad infinitum.

Irrwahn
--
My other computer is a abacus.
Nov 13 '05 #18
On Mon, 22 Sep 2003 19:57:30 GMT, Martin Ambuhl
<ma*****@earthlink.net> wrote:
Nick Austin wrote:
On Mon, 22 Sep 2003 21:48:21 +0530, Ravi <me@privacy.net> wrote:

#define A B
#define B A

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

Undefined behavior.


Balony. Printing an int with %d and a float with %f is just fine.


So an extern int printf(const char *, ...); is not required?

Nick

Nov 13 '05 #19
Nick Austin wrote:
On Tue, 23 Sep 2003 00:12:57 +0530, Ravi <me@privacy.net> wrote:

On Mon, 22 Sep 2003 21:48:21 +0530, Ravi <me@privacy.net> 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);
}

Ok, I get the point. But if you were asked this question in a
technical test with 4 options

a) error during compilation
b) goes into an infinite loop
c) 5 6.000000 6 5.000000
d) 5 6.000000 0 0.000000

What would you choose?

a) is a serious contender, but the examiner probably expects d).

I don't get it. Why would d) be expected ?

--
Bertrand Mollinier Toublet
Currently looking for employment in the San Francisco Bay Area
http://www.bmt.dnsalias.org/employment

Nov 13 '05 #20
Nick Austin wrote:
On Mon, 22 Sep 2003 19:57:30 GMT, Martin Ambuhl
<ma*****@earthlink.net> wrote:
Nick Austin wrote:
On Mon, 22 Sep 2003 21:48:21 +0530, Ravi <me@privacy.net> wrote:
#define A B
#define B A

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


Balony. Printing an int with %d and a float with %f is just fine.


So an extern int printf(const char *, ...); is not required?


The prototype is indeed required. Perhaps Martin didn't notice that
<stdio.h> had not been included.

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #21
Richard Heathfield wrote:
Nick Austin wrote: The prototype is indeed required. Perhaps Martin didn't notice that
<stdio.h> had not been included.


Maybe Martin had posted even earlier a list of 6 errors, fully explaned,
including this one. What Martin didn't know was that Nick Austin just
threw in 1-line sentence fragments that tell nothing to the poster unless
he already knew what was wrong
Undefined behavior.

while expecting the posted code to not be fragmentary (a true sign of an
egocentric), and that Nick Austin used a news server that didn't observe
cancel requests (the cancel was sent within 5 seconds of my posting).

Perhaps Nick Austin should take a remedial writing course and change to a
non-broken news server.
--
Martin Ambuhl

Nov 13 '05 #22
>Subject: Your opinion
From: Ravi me@privacy.net
Date: 9/22/03 6:18 AM Hawaiian Standard Time
Message-id: <op**************@news.individual.net>

#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?
Never the lack of #include<stdio.h>

or int main() or having a return statement at the end of main..

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

So that as far the compiler is concerned the code is :
main()
{
int B=5;
float A=6.0;
printf("\n %d %f",B,A);
printf(" %d %f",A,B);
}


Since B is an integer and A is a float the first printf should return:

5 6.00000

In the second printf you run into trouble, because A is a float, not an int.
Hence the second printf won't cause the code to bomb, but it will not behave in
a defined manner.

Compiling it as is should generate a few warnings which may be implementation
specific, but the results of the last printf should be gibberish no matter what
you run it on..

As to what it was desinged to test, I have no idea.

At least thats my 2cents.

Hope I past whatever it was..

Stuart
Nov 13 '05 #23
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.
Nov 13 '05 #24
On Mon, 22 Sep 2003 23:04:46 +0530, Ravi wrote:
One more redicilous question:
#define scanf "%s is a string"
void main()
{
printf(scanf,scanf);
}
What's the output?


Assuming that printf has been declared as usual and the compiler is borken
and accepts void main() I'd expect

%s is a string is a string

--
NPV
"Linux is to Lego as Windows is to Fisher Price." - Doctor J Frink

Nov 13 '05 #25
On Tue, 23 Sep 2003 06:01:50 GMT, "Nils Petter Vaskinn"
<no@spam.for.me.invalid> wrote:
On Mon, 22 Sep 2003 23:04:46 +0530, Ravi wrote:
One more redicilous question:
#define scanf "%s is a string"
void main()
{
printf(scanf,scanf);
}
What's the output?


Assuming that printf has been declared as usual and the compiler is borken
and accepts void main() I'd expect

%s is a string is a string


And can you explain how you come to this conclusion?
Do you think it is a good question? (Of course assume it was asked
with the header included and was int main and returned etc.)

Nov 13 '05 #26
Bigdakine wrote:
Subject: Your opinion
From: Ravi me@privacy.net
Date: 9/22/03 6:18 AM Hawaiian Standard Time
Message-id: <op**************@news.individual.net>

#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);
}
[...] But the macros simply mean that when the code is compiled A and B are
switched..
This is wrong, wrong, wrongity wrong.

So that as far the compiler is concerned the code is :

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


No, 'tis'nt. 'Tis:

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

Ask Don Pop about details on thinking before you post. The #defines make
no effective change in the code.

--
Martin Ambuhl

Nov 13 '05 #27
Nils Petter Vaskinn wrote:
On Mon, 22 Sep 2003 23:04:46 +0530, Ravi wrote:
One more redicilous question:
#define scanf "%s is a string"
void main()
{
printf(scanf,scanf);
}
What's the output?
Assuming that printf has been declared as usual and the compiler is borken
and accepts void main()


Compilers are free to accept, reject, or twist void main programs at their
discretion (or indiscretion). A compiler is not broken just because it
accepts a broken program.
I'd expect

%s is a string is a string


Would you? The behaviour is undefined, not only because of void main() but
also because the program calls a variadic function - printf - without a
valid function prototype in scope.

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #28

On Tue, 23 Sep 2003, Ravi wrote:

On Tue, 23 Sep 2003 06:01:50 GMT, "Nils Petter Vaskinn" wrote:
On Mon, 22 Sep 2003 23:04:46 +0530, Ravi wrote:

#include <stdio.h>
#define scanf "%s is a string"
int main(void) {
printf(scanf,scanf); return 0; }
%s is a string is a string
And can you explain how you come to this conclusion?


Well, the preprocessor might expand the two 'scanf' parts to

printf("%s is a string", "%s is a string");

(although AFAIK it's not required to, since #defining scanf
and then #including <stdio.h> I think causes undefined behavior).

And it's obvious what *that* line prints, if it compiles. The
"%s" is the printf format specifier for "character string".
Do you think it is a good question? (Of course assume it was asked
with the header included and was int main and returned etc.)


Of course not!

-Arthur

Nov 13 '05 #29
Martin Ambuhl wrote:
Richard Heathfield wrote:
Nick Austin wrote:

The prototype is indeed required. Perhaps Martin didn't notice that
<stdio.h> had not been included.


Maybe Martin had posted even earlier a list of 6 errors, fully explaned,
including this one.


Yes, you did. I had actually read it, too - albeit some while earlier - so I
have no excuse whatsoever for calling your attention to detail into
question. My apologies.

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #30
Bertrand Mollinier Toublet <be*****************************@enst-bretagne.fr> writes:
Nick Austin wrote:
On Tue, 23 Sep 2003 00:12:57 +0530, Ravi <me@privacy.net> wrote:
On Mon, 22 Sep 2003 21:48:21 +0530, Ravi <me@privacy.net> 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);
>}

Ok, I get the point. But if you were asked this question in a
technical test with 4 options

a) error during compilation
b) goes into an infinite loop
c) 5 6.000000 6 5.000000
d) 5 6.000000 0 0.000000

What would you choose?

a) is a serious contender, but the examiner probably expects d).

I don't get it. Why would d) be expected ?


None of the answers make any real sense, but d) seems the easiest
conclusion to arrive at if you have a *very* broken conception of how
C works. I would not expect (in the real world) any of the above, but
instead garble for some of the values, with a possible runtime
crash. Any of a through d, obviously, is possible (theoretically), and
none are more correct than the others...

-Micah
Nov 13 '05 #31
Ravi <me@privacy.net> writes:
On 22 Sep 2003 09:44:56 -0700, Ben Pfaff <bl*@cs.stanford.edu> wrote:
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.


What's worse it had void main() originally and also a clrscr()
somewhere in between.

And the funniest part:
"What is the output on compiling the program:"
When they actually meant running the program.

LOL
And they think they are testing someones technical skill.

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.

%s is a string is a string

Is what I would expect.

-Micah
Nov 13 '05 #32
In article <m3************@localhost.localdomain>,
Micah Cowan <mi***@cowan.name> wrote:
Bertrand Mollinier Toublet <be*****************************@enst-bretagne.fr>
writes:
Nick Austin wrote:
On Tue, 23 Sep 2003 00:12:57 +0530, Ravi <me@privacy.net> wrote:

>>On Mon, 22 Sep 2003 21:48:21 +0530, Ravi <me@privacy.net> 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);
>>>}
>>
>Ok, I get the point. But if you were asked this question in a
>technical test with 4 options
>
>a) error during compilation
>b) goes into an infinite loop
>c) 5 6.000000 6 5.000000
>d) 5 6.000000 0 0.000000
>
>What would you choose?
a) is a serious contender, but the examiner probably expects d).

I don't get it. Why would d) be expected ?


None of the answers make any real sense, but d) seems the easiest
conclusion to arrive at if you have a *very* broken conception of how
C works. I would not expect (in the real world) any of the above, but
instead garble for some of the values, with a possible runtime
crash. Any of a through d, obviously, is possible (theoretically), and
none are more correct than the others...


The macro usage is complicated enough that I don't care what the macros
do; code doing this kind of thing has to be changed. If I had to write a
C preprocessor or a C compiler then I would study what the C Standard
says and do The Right Thing whatever that is, but since I don't write C
preprocessors it is completely unnecessary to know what this does. Run
it through a compiler if you want to know.

Assuming that the macros A and B produce different replacement text, the
second printf would have undefined behaviour even _if_ the right header
file had been included. Since it hasn't been included, both printf calls
have undefined behavior and compilers that I use will _not_ produce any
meaningful results.
Nov 13 '05 #33
In article <m3************@localhost.localdomain>,
Micah Cowan <mi***@cowan.name> wrote:
Ravi <me@privacy.net> writes:
On 22 Sep 2003 09:44:56 -0700, Ben Pfaff <bl*@cs.stanford.edu> wrote:
> 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.


What's worse it had void main() originally and also a clrscr()
somewhere in between.

And the funniest part:
"What is the output on compiling the program:"
When they actually meant running the program.

LOL
And they think they are testing someones technical skill.

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.

%s is a string is a string

Is what I would expect.


scanf is quite possibly a macro defined in stdlib.h, and in that case
the #define will give an error because you attempt to redefine the
macro.
Nov 13 '05 #34
On Tue, 23 Sep 2003 12:03:13 +0530, Ravi wrote:
%s is a string is a string
And can you explain how you come to this conclusion?


I said assuming printf has been declared as usual. I should have added
"without using #include <stdio.h> to do it, so that the macro doesn't
crash with scanf"

I also said assuming void main() is accepted. I should have added "and the
compiler defines the behaviour as the same as for int main() without a
return."

And assuming that the platform flushes stdout for you, or fflush(stdout)
is added to main (after fflushaving been declared).

Basically if all the problems are fixed I'd expect

%s is a string is a string
Do you think it is a good question? (Of course assume it was asked with
the header included and was int main and returned etc.)


No!

Actually the unchanged original is a better question, as a trick question
mind, because of the "spot the error" factor.

--
NPV
"Linux is to Lego as Windows is to Fisher Price." - Doctor J Frink

Nov 13 '05 #35
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?

I thought the following:

#define A B
#define B A

worked by first replacing all As' with Bs' then all Bs' with As' but
that is not how this works. Why is this?
Nov 13 '05 #36
Martin Ambuhl <ma*****@earthlink.net> wrote:
<SNIP>
Ask Don Pop about details on thinking before you post.

^
Is this a typo or a joke?

Irrwahn
--
My other computer is an abacus.
Nov 13 '05 #37
Ben Pfaff <bl*@cs.stanford.edu> wrote:
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.


Never having thought seriously about such pathological code, this
surprised me, but let me try to explain this to myself: even though
6.10.3.4 starts with "After all parameters in the replacement list have
been substituted", that whole section, including #2, actually applies to
object-like as well as to function-like macros. Right?
And therefore, wherever an A is encountered in the source code, this is
replaced by a B, which is replaced by an A, which, having already been
processed, is not replaced again; and when a B is encountered, the same
procedure is followed, thus B is replaced by A, which is replaced by B,
which is not re-replaced.

Richard
Nov 13 '05 #38
In <bk************@ID-168218.news.uni-berlin.de> Bertrand Mollinier Toublet <be*****************************@enst-bretagne.fr> writes:
Nick Austin wrote:
On Tue, 23 Sep 2003 00:12:57 +0530, Ravi <me@privacy.net> wrote:

On Mon, 22 Sep 2003 21:48:21 +0530, Ravi <me@privacy.net> 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);
>}

Ok, I get the point. But if you were asked this question in a
technical test with 4 options

a) error during compilation
b) goes into an infinite loop
c) 5 6.000000 6 5.000000
d) 5 6.000000 0 0.000000

What would you choose?

a) is a serious contender, but the examiner probably expects d).

I don't get it. Why would d) be expected ?


Because that's what you get on a typical x86 implementation ;-)

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #39
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 ;-)

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #40
In <gg********************************@4ax.com> Ravi <me@privacy.net> writes:
On Mon, 22 Sep 2003 18:16:45 +0100, Nick Austin
<ni**********@nildram.co.uk> wrote:
On Mon, 22 Sep 2003 21:48:21 +0530, Ravi <me@privacy.net> wrote:
#define A B
#define B A

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


Undefined behavior.
printf(" %d %f",B,A);


Undefined behavior thrice.
}


Ok, I get the point. But if you were asked this question in a
technical test with 4 options

a) error during compilation
b) goes into an infinite loop
c) 5 6.000000 6 5.000000
d) 5 6.000000 0 0.000000

What would you choose?


It depends on the date the question was asked ;-)

Before the release of C89, the answer would be a) or b): the macro
replacement causes infinite recursion, which could result in either an
error message or the preprocessor going into an infinite loop:

mentor:~/tmp 12> gcc -traditional -E test.c
test.c:6: macro or `#include' recursion too deep
test.c:7: macro or `#include' recursion too deep
test.c:8: macro or `#include' recursion too deep
test.c:8: macro or `#include' recursion too deep
test.c:9: macro or `#include' recursion too deep
test.c:9: macro or `#include' recursion too deep
# 1 "test.c"

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

In standard C, the macro definitions are harmless, the nested macro
replacement being stopped when the original macro name comes up again,
so by the time A becomes A again, the replacement is stopped. But,
in standard C, calling printf without a prototype in scope and passing
an argument of the wrong type to printf results in undefined behaviour,
which includes *all* the above listed options, as well as an infinity
of others.

And a C99 compiler would also object to the definition of the main
function, which requires an explicit return type.

However, the program has the look and feel of a pre-ANSI C program...

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #41
ne*****@tokyo.com (Mantorok Redgormor) writes:
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?

I thought the following:

#define A B
#define B A

worked by first replacing all As' with Bs' then all Bs' with As' but
that is not how this works. Why is this?


`A' gets expanded to `B', which gets expanded to `A'. At that
point, the preprocessor stops expanding because it has already
expanded `A'; it will not expand a macro from within its own
expansion. The explanation for `B' is similar.
--
"In My Egotistical Opinion, most people's C programs should be indented six
feet downward and covered with dirt." -- Blair P. Houghton
Nov 13 '05 #42
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 :)

Nov 13 '05 #43
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?

--
//Hit reply or have some fun ;)
main(){char str[18]="sbwjAeftqbnnfe/dpn";
int i;for(i=0;i<18;putc(str[i++]-1));}
Nov 13 '05 #44
In <a8********************************@4ax.com> Ravi <me@privacy.net> 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 :)


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.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #45
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++?

Thank a lot for helping :)
--
//Hit reply or have some fun ;)
main(){char str[19]="sbwjAeftqbnnfe/dpn";
int i;for(i=0;i<18;putc(str[i++]-1));}
Nov 13 '05 #46
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++?

Thank a lot for helping :)


Or you can also tell me how it can be done on a Linux machine with all
standard development tools installed.

--
//Hit reply or have some fun ;)
//I thought of being kind to the spammers putCHAR ;))
main(){char str[19]="sbwjAeftqbnnfe/dpn";
int i;for(i=0;i<18;putchar(str[i++]-1));}
Nov 13 '05 #47
In article <iu********************************@4ax.com>, Ravi wrote:
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.
[cut] 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.

--
Andreas Kähäri
Nov 13 '05 #48
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.

--
main(){char s[19]="sbwjAeftqbnnfe/dpn";
int i;for(i=0;i<18;putchar(s[i++]-1));}
Nov 13 '05 #49
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
#define B A
#define A 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...

TIA.
--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Nov 13 '05 #50

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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.