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

explanation required???

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

Mar 18 '06 #1
5 1459
On 2006-03-18, aa*****@gmail.com <aa*****@gmail.com> wrote:
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.
why??

Mar 18 '06 #2
aa*****@gmail.com opined:
Hi all,
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).

--
BR, Vladimir

Those who in quarrels interpose, must often wipe a bloody nose.

Mar 18 '06 #3
Vladimir S. Oka opined:
aa*****@gmail.com opined:
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...


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

Mar 18 '06 #4
aa*****@gmail.com wrote:
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?
Mar 19 '06 #5
aa*****@gmail.com writes:
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.

--
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 19 '06 #6

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

Similar topics

1
by: jimfortune | last post by:
From: http://groups-beta.google.com/group/comp.databases.ms-access/msg/769e67e3d0f97a90?hl=en& Errata: 19 solar years = 2939.6018 days should be 19 solar years = 6939.6018 days Easter...
6
by: Buck Rogers | last post by:
Hi guys! Love your work! The below program is from K&R2, p22. ================================= #include <stdio.h> /* count digits, white space, others */ main() {
70
by: rahul8143 | last post by:
hello, 1) First how following program get executed i mean how output is printed and also why following program gives different output in Turbo C++ compiler and Visual c++ 6 compiler? void main()...
14
by: Akhil | last post by:
plz c d following code #include<stdio.h> void main() { int x=4,y=1; y=x++++; //gives error message lvalue required y=x++ + ++y;//no errors
4
by: aarklon | last post by:
Hi all, In the article http://en.wikipedia.org/wiki/C_language it is written as follows C has the following important features: 1) A simple core language, with important functionality...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
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,...

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.