explanation required???

A

aarklon

Hi all,


1) #define COMBINE(x,y) (x##y)
int main(void)
{

int i;
i=COMBINE(14+1 ,1+14);
printf("\n i = %d",i);/* produces 39*/

i=COMBINE(10+5 ,5+10);
printf("\n i = %d",i); /*produces 75*/

i=COMBINE(13+2 ,2+13);
printf("\n i = %d",i); /*produces 48*/

return(0);
}

why these different results are produced???

2) int i =- -2;
printf("i =%d ",i); yields 2,why maximal munch rule is not applied
here???

3) int i = *(int*)2;
int main(void) {return 0; }
why this is generating illegal initialization error message???

4) why ~= operator is not permitted in C???

5) char a = 'AB'
printf("a =%c ",a);produces A on some compilers,B on some compilers
why??

Thanks in advance for any replies
 
J

Jordan Abel

Hi all,


1) #define COMBINE(x,y) (x##y)
int main(void)
{

int i;
i=(14+11+14);
printf("\n i = %d",i);/* produces 39*/

i=(10+55+10);
printf("\n i = %d",i); /*produces 75*/

i=(13+22+13);
printf("\n i = %d",i); /*produces 48*/

return(0);
}

why these different results are produced???

I've taken the liberty of preprocessing your code. This should give you
the answer.
2) int i =- -2;
printf("i =%d ",i); yields 2,why maximal munch rule is not applied
here???

Because =- is not a token (anymore).
3) int i = *(int*)2;
int main(void) {return 0; }
why this is generating illegal initialization error message???

Because it is an illegal initialization.
4) why ~= operator is not permitted in C???

Because ~ is not a binary operator.
5) char a = 'AB'
printf("a =%c ",a);produces A on some compilers,B on some compilers

Because the value of multi-character constants is
implementation-defined.
 
V

Vladimir S. Oka

(e-mail address removed) opined:

This looks suspiciously like homework/test, so I'll give you
pointers, not necessarily the answers.
1) #define COMBINE(x,y) (x##y)

int main(void)
{

int i;
i=COMBINE(14+1 ,1+14);
printf("\n i = %d",i);/* produces 39*/

i=COMBINE(10+5 ,5+10);
printf("\n i = %d",i); /*produces 75*/

i=COMBINE(13+2 ,2+13);
printf("\n i = %d",i); /*produces 48*/

return(0);
}

why these different results are produced???

Macro operator `##` concatenates its operands.
2) int i =- -2;
printf("i =%d ",i); yields 2,why maximal munch rule is not
applied here???

Think about what characters are symbol delimiters...
3) int i = *(int*)2;
int main(void) {return 0; }
why this is generating illegal initialization error
message???

Cast and dereferencing makes for a non-constant expression. Your
diagnostic should have said as much (mine said: "initializer
element is not constant").
4) why ~= operator is not permitted in C???

Ask yourself this: is `~` unary or binary operator.
5) char a = 'AB'
printf("a =%c ",a);produces A on some compilers,B on some
compilers why??

What is the type of a character constant like `AB`? (H-int,
H-int: think of endianness and type sizes).
 
V

Vladimir S. Oka

Vladimir S. Oka opined:
(e-mail address removed) opined:


Think about what characters are symbol delimiters...

Caught me out on this one.

Operator `=-` is no longer allowed, hence the above parses as
`int i = (-(-2));`.


--
BR, Vladimir

Hiccuping & trembling into the WASTE DUMPS of New Jersey like
some drunken CABBAGE PATCH DOLL, coughing in line at
FIORUCCI'S!!
 
M

Martin Ambuhl

Hi all,


1) #define COMBINE(x,y) (x##y)
int main(void)
{

int i;
i=COMBINE(14+1 ,1+14);
printf("\n i = %d",i);/* produces 39*/

i=COMBINE(10+5 ,5+10);
printf("\n i = %d",i); /*produces 75*/

i=COMBINE(13+2 ,2+13);
printf("\n i = %d",i); /*produces 48*/

return(0);
}

why these different results are produced???

what did you think these should produce?

i = (14 + 11 + 14);
printf("\n i = %d", i);

i = (10 + 55 + 10);
printf("\n i = %d", i);

i = (13 + 22 + 13);
printf("\n i = %d", i);

you give no indication of what you think is strange or "different"
2) int i =- -2;
printf("i =%d ",i); yields 2,why maximal munch rule is not applied
here???

Since the very archaic '=-' version of the operator '-=' makes no sense
here, it is obvious that the initialization is
int i = --2;
What did you think it should be?
4) why ~= operator is not permitted in C???

'~' is not a binary operator. All
<var> <op>= <exp>
forms are essentially (safer) shorthand for
<var> = <var> <op> <exp>
What do you think
i ~= 1;
should mean?
 
K

Keith Thompson

1) #define COMBINE(x,y) (x##y)
int main(void)
{

int i;
i=COMBINE(14+1 ,1+14);
printf("\n i = %d",i);/* produces 39*/

i=COMBINE(10+5 ,5+10);
printf("\n i = %d",i); /*produces 75*/

i=COMBINE(13+2 ,2+13);
printf("\n i = %d",i); /*produces 48*/

return(0);
}

why these different results are produced???

2) int i =- -2;
printf("i =%d ",i); yields 2,why maximal munch rule is not applied
here???

3) int i = *(int*)2;
int main(void) {return 0; }
why this is generating illegal initialization error message???

4) why ~= operator is not permitted in C???

5) char a = 'AB'
printf("a =%c ",a);produces A on some compilers,B on some compilers
why??

Thanks in advance for any replies

In all cases, the answer is that the compiler is behaving exactly the
way it's supposed to.

Beyond that, do your own homework.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top