Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > o/p problem

Reply
Thread Tools

o/p problem

 
 
Mark McIntyre
Guest
Posts: n/a
 
      12-24-2005
On 23 Dec 2005 06:03:06 -0800, in comp.lang.c , "fidlee"
<> wrote:

>@Pieter
>thanx for the help offered...


firstly, can you please read this:
>

Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
<
>but i still do think that there must be some logic as to y the c
>compiler behaves in this fashion...


Secondly,. please do not use abbreviations in posts, such as y for
why, d for the. Your posts will be easier to read if you avoid
unnecessary abbreviations.

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


There *is* no single logic, it depends on your compiler, operating
system, character set (anyone tried it on a non-ISO charset?), etc
etc.

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
 
Reply With Quote
 
 
 
 
Mark McIntyre
Guest
Posts: n/a
 
      12-24-2005
On 23 Dec 2005 06:45:51 -0800, in comp.lang.c , "fidlee"
<> wrote:

>By the way it seems that you are the guardian of the 'Usenet messaging
>standards'


No, he's just a responsible citizen.

>Because I have seen similar comments from you on many other posts.


And yet you still don't follow his advice. Strange...

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


He's already posted advice that will be helpful to you. For instance
if you continue to ignore the comment about google posting, people
will start ignoring your posts entirely.

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
 
Reply With Quote
 
 
 
 
fidlee
Guest
Posts: n/a
 
      12-24-2005

Keith Thompson wrote:
> "fidlee" <> writes:
> > @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.
>

sorry. my mistake.
it was actually
1)
char a='i';
printf("%d",sizeof(a));

2)
char a='i';
printf("%d",sizeof('i'));

this problem comes up in turbo C

 
Reply With Quote
 
Lew Pitcher
Guest
Posts: n/a
 
      12-24-2005
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

fidlee wrote:
[snip]
> sorry. my mistake.
> it was actually
> 1)
> char a='i';


You defined [a] as a char
You initialized [a] from a character constant.
This is roughly the same as saying
char a = 43;
in that 43 is an integer, and 'i' is an integer.
But, you can still make the assignment, with a loss of precision.

> printf("%d",sizeof(a));


By definition sizeof(char) is 1
So, sizeof(a) is 1


> 2)
> char a='i';
> printf("%d",sizeof('i'));


Here, you take the sizeof of a character constant.
As I said before, character constants (the things in single quotes) are
/integers/ not characters.
Apparently, on your platform, integers are 2 characters wide, so
sizeof('i')
like
sizeof(292)
will return the size of an integer constant (2).

- --
Lew Pitcher

Master Codewright & JOAT-in-training | GPG public key available on request
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.7 (GNU/Linux)

iD8DBQFDrNRnagVFX4UWr64RArxwAKCUTHBJf9wM3c9Z2+S73L 7s5fyd0gCgmb92
89A/GHd3MfqY1OkCzxn4hmM=
=nRIN
-----END PGP SIGNATURE-----
 
Reply With Quote
 
Chuck F.
Guest
Posts: n/a
 
      12-24-2005
fidlee wrote:
> Keith Thompson wrote:
>

.... snip ...
>>
>> 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.
>>

> sorry. my mistake.
> it was actually
> 1)
> char a='i';
> printf("%d",sizeof(a));
>
> 2)
> char a='i';
> printf("%d",sizeof('i'));
>
> this problem comes up in turbo C


Congratulations on learning to quote, and to avoid silly
abbreviations. To avoid mistakes you should also always cut and
paste your code samples (from compilable source). This avoids
silly typos invalidating any conclusions.

In this newsgroup we deal only with ISO standard C. Thus the fact
that you are using Turbo C does not matter. What does matter is
that you are using only features of standard C, and no extensions
such as Turbos <conio.h> That way we are all talking about the
same language.

The differences you are seeing in your code are because 'i' is an
integer constant, and thus its size is that of an int. However it
can be stored in a char (the int is sufficiently small), and the
sizeof a char is 1 by definition.

These relative sizes, for your system, are spelled out in the
include file <limits.h> Thus your code can always adapt to the
actual situation.

--
"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/>
 
Reply With Quote
 
fidlee
Guest
Posts: n/a
 
      12-24-2005

> Congratulations on learning to quote, and to avoid silly
> abbreviations. To avoid mistakes you should also always cut and
> paste your code samples (from compilable source). This avoids
> silly typos invalidating any conclusions.


thanx a lot for the advice. I am really sorry i hadnt realised that
typing mistakes could be confusing. because I post messages from
Google Groups.

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      12-24-2005
"fidlee" <> writes:
[...]
> sorry. my mistake.
> it was actually
> 1)
> char a='i';
> printf("%d",sizeof(a));
>
> 2)
> char a='i';
> printf("%d",sizeof('i'));
>
> this problem comes up in turbo C


This demonstrates why it's important to post the actual code that you
compiled and ran. Don't try to summarize it; cut-and-paste it.
Otherwise, we can't tell which problems are in your actual code and
which were introduced when you posted it.

The "%d" format expects an int. The sizeof operator yields a value of
type size_t. It's likely to work if int and size_t happen to be the
same size, but you should make sure the types match:

printf("%d", (int)sizeof(a));
printf("%d", (int)sizeof('i'));

Finally, this isn't a problem; it's a feature. See question 8.9 in
the comp.lang.c FAQ.

--
Keith Thompson (The_Other_Keith) kst- <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.
 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      12-24-2005
fidlee a écrit :
> what should be the ouptut of
> printf("%d",'++');


Undefined behavour. The a character constant only have 1 character in
conforming mode.

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

Get a better compiler, or learn how to use it properly.

Compiling: main.c
main.c:9:17: warning: multi-character character constant

In addition, you should add a '\n' to the output string to be sure it
will be displayed in time.

--
A+

Emmanuel Delahaye
 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      12-24-2005
On Sat, 24 Dec 2005 16:28:54 +0100, in comp.lang.c , Emmanuel Delahaye
<> wrote:

>fidlee a écrit :
>> what should be the ouptut of
>> printf("%d",'++');

>
>Undefined behavour. The a character constant only have 1 character in
>conforming mode.


I think you've made a mistake there. A character constant is defined
thus (6.4.4.4):

"An integer character constant is a sequence of one or more multibyte
characters enclosed in single-quotes, as in 'x'."

and further

"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."


----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      12-24-2005
Lew Pitcher <> writes:
[...]
>> 2)
>> char a='i';
>> printf("%d",sizeof('i'));

>
> Here, you take the sizeof of a character constant.
> As I said before, character constants (the things in single quotes) are
> /integers/ not characters.
> Apparently, on your platform, integers are 2 characters wide, so
> sizeof('i')
> like
> sizeof(292)
> will return the size of an integer constant (2).


You missed an important distinction. A character constant isn't just
an integer; it's specifically of type int. Strictly speaking, there's
no such thing as "the size of an integer constant"; an integer
constant is a source code construct such as 42 or 0x137LL. The result
of sizeof('i') or sizeof 'i' is the same as sizeof(int).

--
Keith Thompson (The_Other_Keith) kst- <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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem problem problem :( Need Help Mike ASP General 2 05-11-2004 08:36 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57