469,366 Members | 2,254 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,366 developers. It's quick & easy.

sizeof 'A'

Hi All,
When I run the below program in MSVC, I get the output as
1
4
Could you tell me why sizeof 'A' is taken as 4? Is it standard defined
or compiler specific?
Thanks,
Nishu
/**************************************/
#include<stdio.h>

int main(void)
{

const char ch = 'A';

printf("%d\n", sizeof ch);
printf("%d\n", sizeof 'A');

return 0;
}

/*******************/

Jul 10 '07
58 6675
['A' has type int]
Incredible! Splint 3.1.1:
test.c:77:27: Operands of != have incompatible types (int, char):
getchar() != '\n'

But wait a moment...
A character constant is used as an int. Use +charintliteral to allow
character constants to be used as ints. (This is safe since the actual type
of a char constant is int.)

(Right above that I read:
test.c:77:12: Operand of ! is non-boolean (int): !feof(stdin)
The operand of a boolean operator is not a boolean. Use +ptrnegate to allow !
to be used on pointers. (Use -boolops to inhibit warning)
Should I throw this program in the trash?)
--
Army1987 (Replace "NOSPAM" with "email")
"Never attribute to malice that which can be adequately explained
by stupidity." -- R. J. Hanlon (?)

Jul 12 '07 #51
Army1987 <ar******@NOSPAM.itwrote:
['A' has type int]
Incredible! Splint 3.1.1:
test.c:77:27: Operands of != have incompatible types (int, char):
getchar() != '\n'
Then Splint is, quite simply, wrong.
But wait a moment...
A character constant is used as an int. Use +charintliteral to allow
character constants to be used as ints. (This is safe since the actual type
of a char constant is int.)

(Right above that I read:
test.c:77:12: Operand of ! is non-boolean (int): !feof(stdin)
The operand of a boolean operator is not a boolean. Use +ptrnegate to allow !
to be used on pointers. (Use -boolops to inhibit warning)
Should I throw this program in the trash?)
Yes. Both those warnings are entirely spurious.

Or perhaps - I don't know Splint - you should turn off C++ mode. If
you're in C++ mode, that would explain at least the first warning, and
possibly - I don't know C++ this precisely, either, but it _is_ stricter
than C where type conversions are involved - the second. Never use any
program's C++ mode when you're handling C code.

Richard
Jul 13 '07 #52
On Fri, 13 Jul 2007 06:23:18 +0000, Richard Bos wrote:
Army1987 <ar******@NOSPAM.itwrote:
>['A' has type int]
Incredible! Splint 3.1.1:
test.c:77:27: Operands of != have incompatible types (int, char):
getchar() != '\n'

Then Splint is, quite simply, wrong.
>But wait a moment...
A character constant is used as an int. Use +charintliteral to allow
character constants to be used as ints. (This is safe since the actual type
of a char constant is int.)
It does know that '\n' has type int, but it chooses to ignore
that!
>(Right above that I read:
test.c:77:12: Operand of ! is non-boolean (int): !feof(stdin)
The operand of a boolean operator is not a boolean. Use +ptrnegate to allow !
to be used on pointers. (Use -boolops to inhibit warning)
Should I throw this program in the trash?)

Yes. Both those warnings are entirely spurious.

Or perhaps - I don't know Splint - you should turn off C++ mode. If
you're in C++ mode, that would explain at least the first warning, and
possibly - I don't know C++ this precisely, either, but it _is_ stricter
than C where type conversions are involved - the second. Never use any
program's C++ mode when you're handling C code.
I don't think so, its manpage doesn't even mention C++, and the
file extension was .c. And I read:
-standard
The default mode. All checking done by weak, plus modifies check-
ing, global alias checking, use all parameters, using released
storage, ignored return values or any type, macro checking,
unreachable code, infinite loops, and fall-through cases. The
types bool, int and char are distinct. Old style declarations
are reported.

-checks
Moderately strict checking. All checking done by standard, plus
must modification checking, rep exposure, return alias, memory
management and complete interfaces.

-strict
Absurdly strict checking. All checking done by checks, plus modi-
fications and global variables used in unspecified functions,
strict standard library, and strict typing of C operators. A spe-
cial reward will be presented to the first person to produce a
real program that produces no errors with strict checking.
Considering it gave 22 warnings in a 121-line source file, even
after I fixed all the useful and relevant ones, (some of these are
useful in some situations but not in the one I had, and I didn't
feel like polluting my code with /*@iknowwhatiamdoing@*/ comments
and (void)fprintf(stderr, "What would I be supposed to do if this"
" failed?\n" casts), and I was using the default mode, I will not
dare to imagine what the -strict mode does...

--
Army1987 (Replace "NOSPAM" with "email")
"Never attribute to malice that which can be adequately explained
by stupidity." -- R. J. Hanlon (?)

Jul 13 '07 #53
In article <46**********************@news.orange.frjacob navia <ja***@jacob.remcomp.frwrites:
Eric Sosman wrote:
jacob navia wrote:
>
As Pietro said, 'A' is considered an int in C, not
a character. This is one of the many inconsistencies of the
language that you must learn by heart.
Just curious: With what is the int-ness of 'A' "inconsistent?"

With it being a character of course.
How about 'ab'?
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Jul 17 '07 #54
Keith Thompson wrote:
All C expressions of integer type are of type int, unsigned int, or
something larger. Even the name of an object of type char is promoted
to int (or, very rarely, to unsigned int) unless it's the operand of
"&" or "sizeof". ...
No... promotion occurs only where stated by the standard. Examples of
statements with expressions where no promotion occurs:

char c = 'A', f(char c);

c; /* c not promoted */
f(c); /* neither argument nor return type promoted */
c = c; /* no promotion */
The only case where it makes any difference is when a character
constant is the operand of sizeof.
That it makes no difference does not say anything about how it is.
--
DPS
Jul 17 '07 #55
In article <46*****************@news.xs4all.nlrl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
Nishu <na**********@gmail.comwrote:
On Jul 10, 1:07 am, Pietro Cerutti wrote:
Nishu wrote:
Could you tell me why sizeof 'A' is taken as 4? Is it standard defined
or compiler specific?
>
Because 'A' alone is an int.
>
OK. What is the reason for considering it as int? I think we use
single quotes to say that it is a char.

For hysterical raisins, the type of a character constant is int.
Indeed. C originates from a typeless language.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Jul 17 '07 #56
Dietmar Schindler <DS***@Arcor.Dewrites:
Keith Thompson wrote:
>All C expressions of integer type are of type int, unsigned int, or
something larger. Even the name of an object of type char is promoted
to int (or, very rarely, to unsigned int) unless it's the operand of
"&" or "sizeof". ...

No... promotion occurs only where stated by the standard.
[...]

Yes, my error was pointed out (and I acknowledged it) some time ago.

--
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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 17 '07 #57
Keith Thompson schrieb:
>
Dietmar Schindler <DS***@Arcor.Dewrites:
Keith Thompson wrote:
All C expressions of integer type are of type int, unsigned int, or
something larger. Even the name of an object of type char is promoted
to int (or, very rarely, to unsigned int) unless it's the operand of
"&" or "sizeof". ...
No... promotion occurs only where stated by the standard.
[...]

Yes, my error was pointed out (and I acknowledged it) some time ago.
Thanks, now I've seen the posting by pete that I had missed. (Strangely,
it shows up in Google Groups, but not on the company's news server.)

--
DPS
Jul 19 '07 #58
On Wed, 11 Jul 2007 05:04:12 GMT, pete <pf*****@mindspring.comwrote:
Keith Thompson wrote:
All C expressions of integer type are of type int, unsigned int, or
something larger. Even the name of an object of type char is promoted
to int (or, very rarely, to unsigned int) unless it's the operand of
"&" or "sizeof".

There's no promotion when the left and right operands
of the assignment operator are of type char.
or when calling a prototyped function. Or when returning from a
function, but that's not an operator (which was the statement
upthread). (Formally there's no _promotion_ if the LHS/target is
non-char or more generally non-narrow; instead there's a _conversion_
which may happen to be the same as a promotion.)
There's no promotion when an expression of type char
is added to a pointer.
(including the pointer addition implied by subscripting)
The logical operators don't cause the promotion
of their operands,
Arguable. They say 'compare [un]equal to zero' but don't actually say
it's as-if == 0; if it is, that nominally promotes, but the promotion
has no visible effect on the results. The same applies to control
statements if, while, do-while, for.
and neither does the comma operator.
Right.

And as already noted, casts and expression-statements.

- formerly david.thompson1 || achar(64) || worldnet.att.net
Jul 22 '07 #59

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Sunil Menon | last post: by
19 posts views Thread by Martin Pohlack | last post: by
9 posts views Thread by M Welinder | last post: by
7 posts views Thread by dam_fool_2003 | last post: by
42 posts views Thread by Christopher C. Stacy | last post: by
8 posts views Thread by junky_fellow | last post: by
90 posts views Thread by pnreddy1976 | last post: by
32 posts views Thread by Abhishek Srivastava | last post: by
5 posts views Thread by Francois Grieu | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.