why?

X

xcm

#include<stdio.h>
main()
{ printf("%d %d\n",sizeof('a'),sizeof('A'));}

why does it printf 2 2 in tc.
 
P

pete

pete said:
My guess is that sizeof(int) equals 2 in tc.

A more better way of writing it would be

#include <stdio.h>

int main(void)
{
printf("%u %u\n", (unsigned)sizeof('a'), (unsigned)sizeof('A'));
return 0;
}
 
J

john_bode

xcm said:
#include<stdio.h>
main()
{ printf("%d %d\n",sizeof('a'),sizeof('A'));}

why does it printf 2 2 in tc.

Character constants are typed int, not char (confusing, I know). So
sizeof('a') is equivalent to sizeof(int).
 
J

James McIninch

<posted & mailed>

sizeof() returns the size of the type of the data specified as the argument.
Here, you've specified an integer ('a' and 'A'), so, the size is whatever
the size of integer is on your system (apparently 2).
 
X

xcm

is 'a' an integer when use the sizeof?
why?
the result is 1 in vc but 2 in tc.
why dose the compiler assume that 'a' is not a char but an integer
thanks for the reply;
 
P

Paul Mesken

A more better way of writing it would be

#include <stdio.h>

int main(void)
{
printf("%u %u\n", (unsigned)sizeof('a'), (unsigned)sizeof('A'));
return 0;
}

Why the cast? size_t is unsigned (see 7.1.6 of the Standard).
 
P

Paul Mesken

is 'a' an integer when use the sizeof?
why?
the result is 1 in vc but 2 in tc.
why dose the compiler assume that 'a' is not a char but an integer

Ah, this is a difference between using C++ and C. If sizeof('a')
equals 1 then you're using a C++ compiler, if it equals something
greater than 1 (when it returns the same value for sizeof(int)) then
you're using a C compiler.

You see :

In C character literals (like 'a') have sizeof(int).
In C++ character literals have sizeof(char).
 
E

Emmanuel Delahaye

xcm wrote on 29/05/05 :
#include<stdio.h>
main()
{ printf("%d %d\n",sizeof('a'),sizeof('A'));}
Your code is incorrect because the type returned byte sizeof is size_t.
Nota that the parens after sizeof are only mantadory with a type.

C90 (paranoid portability):

printf ("%lu %lu\n", (unsigned long) sizeof 'a', (unsigned long)
sizeof 'A');

C90 (sensible mode):

printf ("%u %u\n", (unsigned) sizeof 'a', (unsigned) sizeof 'A');

C99:

printf ("%zu %zu\n", sizeof 'a', sizeof 'A');
why does it printf 2 2 in tc.

Because
- in C, a character literal has the type int
- Turbo C's CHAR_BIT is 8 and its INT_MAX is 32767 (see <limits.h>),
hence an int uses 16 bits that needs 2 char.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

I once asked an expert COBOL programmer, how to
declare local variables in COBOL, the reply was:
"what is a local variable?"
 
E

Emmanuel Delahaye

Paul Mesken wrote on 29/05/05 :
Why the cast? size_t is unsigned (see 7.1.6 of the Standard).

Nope. size_t is defined as an "unsigned integer" (according to the
target, could be an unsigned {char|short|int|long}).

unsigned integer != unsigned int.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

I once asked an expert COBOL programmer, how to
declare local variables in COBOL, the reply was:
"what is a local variable?"
 
E

Emmanuel Delahaye

xcm wrote on 29/05/05 :
is 'a' an integer when use the sizeof?
why?

'a' is always an integer with type int in C. It's written on the
standard. Don't ask why.
the result is 1 in vc but 2 in tc.

If the result is 1, you are probably using a C++ compiler. Check your
extension. Should be .c and neither .cpp nor .C.

In C++ a character literal has the type char. Once again, don't ask
why.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

..sig under repair
 
E

Emmanuel Delahaye

Paul Mesken wrote on 29/05/05 :
Ah, this is a difference between using C++ and C. If sizeof('a')
equals 1 then you're using a C++ compiler, if it equals something
greater than 1 (when it returns the same value for sizeof(int)) then

Bzzt. "greater or equal".

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Clearly your code does not meet the original spec."
"You are sentenced to 30 lashes with a wet noodle."
-- Jerry Coffin in a.l.c.c++
 
C

CBFalconer

xcm said:
is 'a' an integer when use the sizeof?
why?
the result is 1 in vc but 2 in tc.
why dose the compiler assume that 'a' is not a char but an integer

VC is wrong, or is not being used as a C compiler. 'a' is not a
char, but an integer constant. It's so because that is what the
standard specifies.

--
Some informative links:
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html
 
P

Paul Mesken

Paul Mesken wrote on 29/05/05 :

Bzzt. "greater or equal".

You're very right.

Int might be the same size as char on the system (which is _extremely_
rare). The Standard doesn't require int to be greater than char, only
that int can at least represent the values between -32767 and 32767
and char is at least 8 bits.

I'm just so locked into thinking of char as an 8 bit byte :)
 
P

Paul Mesken

Paul Mesken wrote on 29/05/05 :

Nope. size_t is defined as an "unsigned integer" (according to the
target, could be an unsigned {char|short|int|long}).

unsigned integer != unsigned int.

Yep, 7.1.6 : "... size_t which is the unsigned integral type of the
result of the sizeof operator...". Int is an integral type but so is
char, short and long.

I'm learning something new everyday :)
 
R

roberth+news

| xcm wrote on 29/05/05 :
| > is 'a' an integer when use the sizeof?
| > why?
|
| 'a' is always an integer with type int in C. It's written on the
| standard. Don't ask why.

The standard allows for character literals with more than one character
also (like 'ab' or 'aaaa'). Don't ask why.

| > the result is 1 in vc but 2 in tc.
|
| If the result is 1, you are probably using a C++ compiler. Check your
| extension. Should be .c and neither .cpp nor .C.
|
| In C++ a character literal has the type char. Once again, don't ask
| why.

This is not entirely correct. The C++ standard also allows for
character literals with more than one character, and for these the type
is int. As in C such literals have an implementation defined value.
Once again, don't ask why.
 
R

Robert Gamble

| xcm wrote on 29/05/05 :
| > is 'a' an integer when use the sizeof?
| > why?
|
| 'a' is always an integer with type int in C. It's written on the
| standard. Don't ask why.

The standard allows for character literals with more than one character
also (like 'ab' or 'aaaa'). Don't ask why.

You mean character constants, there is no such thing as character
literals in C.

Rob Gamble
 

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

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,439
Latest member
shasuze

Latest Threads

Top