473,795 Members | 2,983 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sizeof operator

JS
I read in K&R page 204 that sizeof use on a char returns 1. But when I write
the following I get 4!

int main(void){
printf("%d\n",s izeof('g'));

}

Was it not supposed to return 1?
Nov 14 '05
36 2284


JS wrote:
"Ben Pfaff" <bl*@cs.stanfor d.edu> skrev i en meddelelse
news:87******** ****@benpfaff.o rg...
"JS" <dsa.@asdf.co m> writes:
I read in K&R page 204 that sizeof use on a char returns 1. But when I write the following I get 4!

printf("%d\n",s izeof('g'));


In C, character constants have type `int', so sizeof 'g' is equal
to sizeof(int).


Why do they write char if they mean int?


lookup Integer Promotion
Nov 14 '05 #31
Perhaps.

Manual examination noted that a '-' got translated to '--' somehow
during my transfering of it to a text file. When I switched from MinGW
to an old bash shell version of gcc, I got a more intelligent error
about trying to modify an lvalue. That plus a comment below by Jens
helped me find the problem, and when I fixed it so my text file more
closely matched your source, it compiled just fine.

I thought it would simply be a cute display message, but I was dying to
see the message without having to completely reverse-engineer it.

And I was right. It's a cute little display from a fairly basic cipher
Nice.

Nov 14 '05 #32
Strange character was inserted (as best I can deduce) by cut-n-paste
processes. In the final analysis, a single minus sign translated
somehow to a double (probably some kind of auto-adjust courtesy of the
Microsoft products I'm using in this environment [*gag*]).

Corrected it by hand and the program now compiles and functions
correctly.

Thanks for the note.

Nov 14 '05 #33
Neil Kurzman wrote:
JS wrote:
"Ben Pfaff" <bl*@cs.stanfor d.edu> skrev:
"JS" <dsa.@asdf.co m> writes:

I read in K&R page 204 that sizeof use on a char returns 1.
But when I write the following I get 4!

printf("%d\n",s izeof('g'));

In C, character constants have type `int', so sizeof 'g' is equal
to sizeof(int).


Why do they write char if they mean int?


lookup Integer Promotion


This has nothing to do with integer promotion. The argument
to 'sizeof' does not undergo any promotions or conversions.

Nov 14 '05 #34
Jack Klein wrote:

Let's just assume ASCII for a moment.

Consider that the only differences between 'A', 65, 0x41,
and 081 exist syntactically in the way they are parsed


081 ?

Nov 14 '05 #35
"Old Wolf" <ol*****@inspir e.net.nz> writes:
Neil Kurzman wrote:
JS wrote:
"Ben Pfaff" <bl*@cs.stanfor d.edu> skrev:
"JS" <dsa.@asdf.co m> writes:

> I read in K&R page 204 that sizeof use on a char returns 1.
> But when I write the following I get 4!
>
> printf("%d\n",s izeof('g'));

In C, character constants have type `int', so sizeof 'g' is equal
to sizeof(int).

Why do they write char if they mean int?


lookup Integer Promotion


This has nothing to do with integer promotion. The argument
to 'sizeof' does not undergo any promotions or conversions.


It's relevant, but only very indirectly. Since expressions of type
char are almost always promoted to int, there would be little point
(in C) in making character constants be of type char rather than of
type int.

This is part of the rationale for why the standard is the way it is;
it's not something from which you can logically infer that character
constants are of type int. The standard could just as easily have
said that character constants are of type char; they would then be
immediately promoted to int, so it would make a difference only when
the character constant is the operand of sizeof.

(Note that if plain char were unsigned, and sizeof(int)==1 (which
requires CHAR_BIT>=16), character constants of type char would promote
to unsigned int rather than int, but that doesn't apply to most
implementations .)

The real answer to "Why are character constants of type int rather
than char?" is "Because the standard says so". *Why* the standard
says so is another question.

--
Keith Thompson (The_Other_Keit h) 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.
Nov 14 '05 #36
Old Wolf <ol*****@inspir e.net.nz> scribbled the following:
Jack Klein wrote:
Let's just assume ASCII for a moment.

Consider that the only differences between 'A', 65, 0x41,
and 081 exist syntactically in the way they are parsed
081 ?


Shouldn't that be 0101?

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"Bad things only happen to scoundrels."
- Moominmamma
Nov 14 '05 #37

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

70
8916
by: Roy Yao | last post by:
Does it mean "(sizeof(int))* (p)" or "sizeof( (int)(*p) )" ? According to my analysis, operator sizeof, (type) and * have the same precedence, and they combine from right to left. Then this expression should equal to "sizeof( (int)(*p) )", but the compiler does NOT think so. Why? Can anyone help me? Thanks. Best regards. Roy
31
2390
by: Anjali M | last post by:
#include <stdio.h> int main() { int number = 10; printf("Number is: %d\n", number); printf("Sizeof of number is: %d\n", sizeof(number++)); printf("Number now is: %d\n", number);
7
1939
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
12
1889
by: ozbear | last post by:
If one were writing a C interpreter, is there anything in the standard standard that requires the sizeof operator to yield the same value for two different variables of the same type? Let's assume that the interpreter does conform to the range values for, say, type int, but allocates storage for the variables based on their value. So, for two variables foo and bar int foo = 0; /* interpreter allocates two bytes */ int bar =...
12
2422
by: sonu | last post by:
#include<stdio.h> main() { int x=10,y; y=sizeof(++x); printf("x=%d\ny=%d\n",x,y); } Oput Put
15
2248
by: Alex Vinokur | last post by:
Why does one need to use two kinds of sizeof operator: * sizeof unary-expression, * sizeof (type-name) ? Their behavior seem not to be different (see an example below). ------ C++ code ------ #include <iostream> using namespace std;
43
781
by: Richard | last post by:
Could someone point me to why "sizeof x" is/isnt preferable to "sizeof(x)",
28
6759
by: Howard Bryce | last post by:
I have come across code containing things like sizeof int How come that one can invoke sizeof without any parentheses surrounding its argument? Is this admissible within the standard? Can it in general be done with one argument functions? Why would one want to do it anyway, other than showing that one knows and to save two characters?
36
2816
by: Frederick Gotham | last post by:
sizeof has the same precedence as a cast, and they both bind from right to left. The following won't compile for me with gcc: int main(void) { sizeof(double)5; return 0; }
4
6062
by: Divick | last post by:
Hi, does sizeof operator evaluate the size at compile time or run time ? In other words is sizeof(SomeType) evaluated at compile time or at runtime? Please provide the reasoning involved as well. TIA, Divick
0
10438
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10214
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10164
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10001
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9042
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7540
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6780
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.