o/p problem

F

fidlee

what should be the ouptut of
printf("%d",'++');

the ascii value of + is 43... the output comes as 11051 in turboC... (i
was surprised it doesnt throw an error on compiling... ) someone
explain me as to how this output comes...
 
P

Pieter Droogendijk

what should be the ouptut of
printf("%d",'++');

the ascii value of + is 43... the output comes as 11051 in turboC...
(i was surprised it doesnt throw an error on compiling... ) someone
explain me as to how this output comes...

6.4.4.4 "Character constants" #10:
The value of an integer character constant containing more than one
character (e.g., 'ab'), or containing a character or escape sequence that
does not map to a single-byte execution character, is
implementation-defined.

In your case, 43 * 256 + 43.
 
R

Robert Gamble

fidlee said:
what should be the ouptut of
printf("%d",'++');

the ascii value of + is 43... the output comes as 11051 in turboC... (i
was surprised it doesnt throw an error on compiling... ) someone
explain me as to how this output comes...

Character constants can contain multiple characters, the integer value
of which is implementation-defined. You will need to consult the
documentation that came with your compiler for the details but you
might observe the fact that 43*256+43=11051.

Robert Gamble
 
F

fidlee

@ Robert

so u mean to say that this statement will behave differently on the
different compilers??
i also tried out
printf("%d",'+a'); and it gives 11105 on g++ (linux) and turboC
(windows) i dont think they are compiler dependednt...

Can someone please tell me as to why the compiler behaves in the
following way?? (43*256+43=11051)
 
S

Stefan Krah

* fidlee said:
what should be the ouptut of
printf("%d",'++');

the ascii value of + is 43... the output comes as 11051 in turboC... (i
was surprised it doesnt throw an error on compiling... ) someone
explain me as to how this output comes...

gcc gives this warning:

multichar.c:5:15: warning: multi-character character constant

Now you should be equipped for a Google search. ;)


Stefan Krah
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
@ Robert

so u mean to say that this statement will behave differently on the
different compilers??
i also tried out
printf("%d",'+a'); and it gives 11105 on g++ (linux) and turboC
(windows) i dont think they are compiler dependednt...

Certainly, the results will be compiler dependant. It's just that both
of your compilers give the same results :)

Consider that the result depends on
- - the width (sizeof()) of a character constant (hint: it isn't
necessarily == 1)
- - the 'endianness' of a character constant (hint: it isn't necessarily
little-endian)
- - the characterset used to express characters in (hint: it isn't always
ASCII)

Think of what the differences might be between your results and the
results of the same program when compiled and run on a system with
- - 32bit integer values
- - big-endian integers
- - EBCDIC-US characterset
Can someone please tell me as to why the compiler behaves in the
following way?? (43*256+43=11051)

Because, on /your/ system, character constants are stored as 16bit
integer values, in the ASCII characterset. Since the ASCII for '+' is
binary 43, then the first '+' (being shifted left into the high-order 8
bits of your 16bit integer) becomes 43*256 (or 43 << 8). The second '+'
(not moving from the low-order position) becomes 43. And the combination
of the two (a 16bit integer, remember?) becomes (45*256)+43



- --

Lew Pitcher, IT Specialist, Enterprise Data Systems
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFDq/8+agVFX4UWr64RAnAEAJ95BsHcGCYBqkvNRyHHzggeNFTPZwCeJvP6
ymeBG+DMNGdtX6rSOY+HKVI=
=Ra/t
-----END PGP SIGNATURE-----
 
F

fidlee

true that it does give the multi character constant warning... but in
ends up giving the same o/p right ?? :)
 
P

Pieter Droogendijk

so u mean to say that this statement will behave differently on the
different compilers??

Not quite. Different compilers, architectures, operating systems,
characters sets, etcetera.
i also tried out
printf("%d",'+a'); and it gives 11105 on g++ (linux)

g++ implies a C++ compiler, mind. You probably mean gcc.
and turboC (windows) i dont think they are compiler dependednt...

It might do the same on a whole lot of compilers for Windows and Linux.
Try it on a few more systems, especially if they don't use ASCII.

Or better yet, try it on a mainframe.
Can someone please tell me as to why the compiler behaves in the
following way?? (43*256+43=11051)

Not I. Perhaps your compiler documentation or operating system manual can
help you there.
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Lew said:
Certainly, the results will be compiler dependant. It's just that both
of your compilers give the same results :)

Consider that the result depends on
- the width (sizeof()) of a character constant (hint: it isn't
necessarily == 1)
- the 'endianness' of a character constant (hint: it isn't necessarily
little-endian)
- the characterset used to express characters in (hint: it isn't always
ASCII)

Think of what the differences might be between your results and the
results of the same program when compiled and run on a system with
- 32bit integer values
- big-endian integers
- EBCDIC-US characterset

For example, here's a test on /my/ system

source code:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
printf("The character constant '++' == %d\n",'++');
return EXIT_SUCCESS;
}


execution results:

The character constant '++' == 20046


Of course, this was compiled and executed on an IBM OS/390 operating
system using the OS/390 C V2 R10 compiler (5647A01). OS/390 uses EBCDIC
variant charactersets, and the source code was written using the
EBCDIC-US variant.

[snip]

- --

Lew Pitcher, IT Specialist, Enterprise Data Systems
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFDrAJ2agVFX4UWr64RAiFlAJ9Lqz5umtvPU9xWbKkFRyX85PXWZwCgiCYF
GAh416UYHPyruVU5PoEusow=
=RCUr
-----END PGP SIGNATURE-----
 
F

fidlee

@Pieter
thanx for the help offered...
but i still do think that there must be some logic as to y the c
compiler behaves in this fashion...

if someone knows d logic, just let me know ...
 
R

Robert Gamble

[Original context restored]

Please read the document located at http://cfaj.freeshell.org/google/
before posting again to this group.
@ Robert

so u mean to say that this statement will behave differently on the
different compilers??

I mean what I said which was that the value of such character constants
is implementation-defined, it may very well behave differently on
different compilers and you can be sure that not all compilers will
treat them the same way.
i also tried out
printf("%d",'+a'); and it gives 11105 on g++ (linux) and turboC
(windows) i dont think they are compiler dependednt...

First off, g++ is a c++ compiler, C and C++ are different languages,
the latter of which is off-topic here. Secondly, just because two or
more compilers behave in the same manner does not mean that the results
are portable or even Standard conforming, thinking otherwise would be a
serious error.
Can someone please tell me as to why the compiler behaves in the
following way?? (43*256+43=11051)

As I and others have already told you, the Standard declares this to be
implementation-defined, therefore any reasons for why this behavior is
defined in a specific way on an a certain implementation would not be
found in the Standard but in the documentation that came with your
compiler. If your documentation does not provide an explanation to
your liking, ask the implementors for a rationale, not us.

Robert Gamble
 
S

Stefan Krah

* fidlee said:
true that it does give the multi character constant warning... but in
ends up giving the same o/p right ?? :)

My point (which you didn't quote) was that your question could possibly
be answered by googling for "multi-character character constant".


Stefan Krah
 
F

fidlee

@Lew Pitcher
thanx for your answer... but the reason y i am getting confused is
this:
char a='i';
printf("%d",a);

the following code gives the output as 1

whereas
printf("%d",'i')

gives the output as 2

arent both character constants here??

why does the compiler react differently??

this may be a simple question for you guys but i have been tryin hard
to understand...
so please dont lose your temper :)
 
C

Chuck F.

fidlee said:
@Pieter
thanx for the help offered...
but i still do think that there must be some logic as to y the c
compiler behaves in this fashion...

if someone knows d logic, just let me know ...

I is spelled capitalized. I know of no c compiler called y, thus
cannot explain its aberrant and unspecified behaviour. I also
never heard of d logic. Is this something like so-called female logic?

Believe it or not, there is a reason that Usenet protocols have
evolved over the past thirty plus years, and that these include
adequate context and spelling.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
 
F

fidlee

@Chuck F

I am really sorry for all the spelling mistakes. I will make sure that
there are no more of them from now on.
By the way it seems that you are the guardian of the 'Usenet messaging
standards'
Because I have seen similar comments from you on many other posts.

Have you ever posted a message that has proved helpful to someone ? :))
 
Z

Zara

what should be the ouptut of
printf("%d",'++');

the ascii value of + is 43... the output comes as 11051 in turboC... (i
was surprised it doesnt throw an error on compiling... ) someone
explain me as to how this output comes...


It is implementation dependent, although it is legal.

In your case, b ut not guaranteed to be portable, you have got an int
composed of two bytes with value '+' (43), thta is: 257*43=11051

Don't count on it for other compilers/platforms

Regards,

Zara
 
F

Flash Gordon

fidlee said:
@Chuck F

I am really sorry for all the spelling mistakes. I will make sure that
there are no more of them from now on.

In general we don't mind spelling mistakes or poor grammar since we
recognise that posting in a foreign language can be difficult. However,
since most languages have similar capitalisation rules that in general
is not a good reason for not using any capitalisation. Also,
abbreviating words to single characters makes it hard to read posts, and
why should many people have to work to read a post when the author for
negligible extra effort can write it out in full?
By the way it seems that you are the guardian of the 'Usenet messaging
standards'

He is not, but *many* of us agree with Chuck. Since you are using Google
Groups read http://cfaj.freeshell.org/google/
Because I have seen similar comments from you on many other posts.

That is because you have been making it hard for people to understand
your posts and the instructions are posted here regularly, so you should
have seen them before you first posted.
Have you ever posted a message that has proved helpful to someone ? :))

Chuck has posted many helpful posts, and that includes the posts telling
people how to post properly. :)
 
T

Thad Smith

fidlee said:
@Lew Pitcher
thanx for your answer... but the reason y i am getting confused is
this:
char a='i';
printf("%d",a);

the following code gives the output as 1

whereas
printf("%d",'i')

gives the output as 2

arent both character constants here??

Yes, 'i' is a character constant in both cases, but it has type int. In
the first case you are assigning it to a type char, possibly signed
(implementation-defined), then converting the type char to int. In the
second case, you are not converting to type char. If your char type is
signed and (char)'i' is negative (depends on the character set and
number of bits in a byte), you can get different values printed.

In your situation, I suspect that there are other reasons that are not
visible in the snippets you posted.
 
K

Keith Thompson

fidlee said:
@Lew Pitcher
thanx for your answer... but the reason y i am getting confused is
this:
char a='i';
printf("%d",a);

the following code gives the output as 1

What do you mean by "following"?
whereas
printf("%d",'i')

gives the output as 2

arent both character constants here??

why does the compiler react differently??

Are you sure about that? Please show us a small, complete, compilable
program that demonstrates this. On my system, the following program:

#include <stdio.h>
int main(void)
{
char a='i';
printf("%d\n", a);
printf("%d\n", 'i');
return 0;
}

produces the following output:

105
105

If your system uses ASCII or an ASCII-like character set (as it almost
certainly does), the output of both printf statements should be 105.
Regardless of the character set, the output should the the same for
both statements.

And please read <http://cfaj.freeshell.org/google/> and follow its
advice.
 
J

Joe Wright

fidlee said:
@ Robert

so u mean to say that this statement will behave differently on the
different compilers??
i also tried out
printf("%d",'+a'); and it gives 11105 on g++ (linux) and turboC
(windows) i dont think they are compiler dependednt...

Can someone please tell me as to why the compiler behaves in the
following way?? (43*256+43=11051)
Naturally..
'+'
00000000 00000000 00000000 00101011
43
'++'
00000000 00000000 00101011 00101011
11051
'+++'
00000000 00101011 00101011 00101011
2829099
'++++'
00101011 00101011 00101011 00101011
724249387

...don't you think?
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top