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

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 7120
['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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Sunil Menon | last post by:
Dear All, A class having no member variables and only a method sizeof(object) will return 1byte in ANSI and two bytes in Unicode. I have the answer for this of how in works in ANSI. But I don't...
2
by: Xiangliang Meng | last post by:
Hi, all. What will we get from sizeof(a class without data members and virtual functions)? For example: class abnormity { public: string name() { return "abnormity"; }
19
by: Martin Pohlack | last post by:
Hi, I have a funtion which shall compute the amount for a later malloc. In this function I need the sizes of some struct members without having an instance or pointer of the struct. As...
9
by: M Welinder | last post by:
This doesn't work with any C compiler that I can find. They all report a syntax error: printf ("%d\n", (int)sizeof (char)(char)2); Now the question is "why?" "sizeof" and "(char)" have...
7
by: dam_fool_2003 | last post by:
#include<stdio.h> int main(void) { unsigned int a=20,b=50, c = sizeof b+a; printf("%d\n",c); return 0; } out put: 24
42
by: Christopher C. Stacy | last post by:
Some people say sizeof(type) and other say sizeof(variable). Why?
8
by: junky_fellow | last post by:
Consider the following piece of code: #include <stddef.h> int main (void) { int i, j=1; char c; printf("\nsize =%lu\n", sizeof(i+j));
90
by: pnreddy1976 | last post by:
Hi, How can we write a function, which functionality is similar to sizeof function any one send me source code Reddy
32
by: Abhishek Srivastava | last post by:
Hi, Somebody recently asked me to implement the sizeof operator, i.e. to write a function that accepts a parameter of any type, and without using the sizeof operator, should be able to return...
5
by: Francois Grieu | last post by:
Does this reliably cause a compile-time error when int is not 4 bytes ? enum { int_size_checked = 1/(sizeof(int)==4) }; Any better way to check the value of an expression involving sizeof...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.