473,795 Members | 2,922 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 2285
Mark Odell <od*******@hotm ail.com> writes:
Alex Fraser wrote:
printf("%d\n", (int)sizeof c);

Don't you mean:
printf("%u\n", sizeof c);


No, the result of sizeof has type size_t, which may not be type
unsigned.
--
int main(void){char p[]="ABCDEFGHIJKLM NOPQRSTUVWXYZab cdefghijklmnopq rstuvwxyz.\
\n",*q="kl BIcNBFr.NKEzjwC IxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+= strchr(p,*q++)-p;if(i>=(int)si zeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Nov 14 '05 #11
In article <d1**********@c hessie.cirr.com >, Christopher Benson-Manica wrote:
Ben Pfaff <bl*@cs.stanfor d.edu> spoke thus:
In C, character constants have type `int', so sizeof 'g' is equal
to sizeof(int).


(semi OT) Why did the authors of the standard make this arguably
non-intuitive stipulation?


Because you may be able to put more than one character into character
constants (but the actual number that fits into it is implementation-
defined any may be 1.)

putchar('foo');

This is only in ANSI/ISO C for compatibility with traditional C, and I
don't think anybody uses this feature anymore. The value of a multi-char
character constant is implementation-defined, so it is not portable at
all (gcc chooses to ignore all but the last character in such a
constant, so the outcome of 'foo' would be 'o'. In addition, gcc always
issues a diagnostic for multi-char character constants because it is so
unlikely that you really meant to use one.)

--
My real email address is ``nils<at>gnuli nux<dot>nl''
Nov 14 '05 #12
Mark Odell wrote:
Alex Fraser wrote:
No, a character in single quotes, as in 'g', is a constant of type signed
int in C (I think this is different in C++).

Try:

int main(void) {
char c;
printf("%d\n", (int)sizeof c);

Don't you mean:
printf("%u\n", sizeof c);


No, since there is no reason to think that a size_t is an unsigned int.
The "correct" specifier for an uncast size_t might be "%lu" or "%llu".
Nov 14 '05 #13
Nils Weller <me@privacy.net > writes:
In article <d1**********@c hessie.cirr.com >, Christopher Benson-Manica wrote:
Ben Pfaff <bl*@cs.stanfor d.edu> spoke thus:
In C, character constants have type `int', so sizeof 'g' is equal
to sizeof(int).


(semi OT) Why did the authors of the standard make this arguably
non-intuitive stipulation?


Because you may be able to put more than one character into character
constants (but the actual number that fits into it is implementation-
defined any may be 1.)


That is not a cause, that is an effect. If a character constant
had type `char', then by definition you could only fit one `char'
into it.
--
int main(void){char p[]="ABCDEFGHIJKLM NOPQRSTUVWXYZab cdefghijklmnopq rstuvwxyz.\
\n",*q="kl BIcNBFr.NKEzjwC IxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+= strchr(p,*q++)-p;if(i>=(int)si zeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Nov 14 '05 #14
Hey, Ben, long time no see.

Your sig block program -- I can't compile it and I can't figure out
why.

I get:

D:\Steve\mcc>mi ngw32-gcc -c sigc0.c -o sigc0.o
sigc0.c: In function `main':
sigc0.c:1: stray '\255' in program

I broke it out to multiline format:

int main(void){
char p[]="ABCDEFGHIJKLM NOPQRSTUVWXY*Za bcdefghijklmnop qrstuvwxyz.
\n",*q="kl BIcNBFr.NKEzjwC IxNJC";

int i=sizeof p/2;

char *strchr();

int putchar();
while(*q) {
i+=strchr(p,*q+ +)-*p;
if(i>=(int)size of p)i-=sizeof p-1;
putchar(p[i]);
}
return 0;
}

And it now reports the problem on line 10:

D:\Steve\mcc>mi ngw32-gcc -c sigc.c -o sigc.o
sigc.c: In function `main':
sigc.c:10: stray '\255' in program

Commenting out line 10 does, in fact permit it to compile.

I've dumped out the source file in hex, ascii and decimal, no 255
character in the source code.

What gives?

Nov 14 '05 #15
In article <87************ @benpfaff.org>, Ben Pfaff wrote:
Nils Weller <me@privacy.net > writes:
In article <d1**********@c hessie.cirr.com >, Christopher Benson-Manica wrote:
Ben Pfaff <bl*@cs.stanfor d.edu> spoke thus:

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

(semi OT) Why did the authors of the standard make this arguably
non-intuitive stipulation?


Because you may be able to put more than one character into character
constants (but the actual number that fits into it is implementation-
defined any may be 1.)


That is not a cause, that is an effect. If a character constant
had type `char', then by definition you could only fit one `char'
into it.


I thought this is a newsgroup about the C programming language, and not
about linguistics. The intent of what I said is obvious to anyone who
does not intentionally misinterpret it. But okay, I'll rephrase:

Because character constants were designed to be capable of carrying more
more than one character (and it follows, obviously, that a ``char'' is
not enough to carry more than one character - hence the need for a
larger data type.)

--
My real email address is ``nils<at>gnuli nux<dot>nl''
Nov 14 '05 #16
"Steven K. Mariner" <ma*******@eart hlink.net> writes:
Your sig block program -- I can't compile it and I can't figure out
why.

D:\Steve\mcc>mi ngw32-gcc -c sigc0.c -o sigc0.o
sigc0.c: In function `main':
sigc0.c:1: stray '\255' in program


I think your compiler must be buggy.
--
"I ran it on my DeathStation 9000 and demons flew out of my nose." --Kaz
Nov 14 '05 #17
Nils Weller <me@privacy.net > writes:
Because character constants were designed to be capable of carrying more
more than one character (and it follows, obviously, that a ``char'' is
not enough to carry more than one character - hence the need for a
larger data type.)


Okay--so, then, *why* were character constants designed to be
capable of carrying more than one character?
--
"...deficie nt support can be a virtue.
It keeps the amateurs off."
--Bjarne Stroustrup
Nov 14 '05 #18
Steven K. Mariner <ma*******@eart hlink.net> wrote:
Hey, Ben, long time no see. Your sig block program -- I can't compile it and I can't figure out
why. I get: D:\Steve\mcc>mi ngw32-gcc -c sigc0.c -o sigc0.o
sigc0.c: In function `main':
sigc0.c:1: stray '\255' in program I broke it out to multiline format: int main(void){
char p[]="ABCDEFGHIJKLM NOPQRSTUVWXY*Za bcdefghijklmnop qrstuvwxyz.
\n",*q="kl BIcNBFr.NKEzjwC IxNJC"; int i=sizeof p/2; char *strchr(); int putchar();
while(*q) {
i+=strchr(p,*q+ +)-*p;
if(i>=(int)size of p)i-=sizeof p-1;
putchar(p[i]);
}
return 0;
}
You have managed to get some strange character into it. On the line

i+=strchr(p,*q+ +)-*p;

the character directly in front of the 'p' at the end seems to be the
culprit - it wasn't in Bens signature and is some non-ASCII character
(it arrives here as 0xAD, looking like a '-' but that might be an arte-
fact of the newsreader). But also the line
char p[]="ABCDEFGHIJKLM NOPQRSTUVWXY*Za bcdefghijklmnop qrstuvwxyz.
\n",*q="kl BIcNBFr.NKEzjwC IxNJC";


is dangerous (the space before the '\n' might get lost and and you
would need a '\' at the end of the first line), better make that

char p[]="ABCDEFGHIJKLM NOPQRSTUVWXY*Za bcdefghijklmnop qrstuvwxyz. \n",
*q="kl BIcNBFr.NKEzjwC IxNJC";

Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@p hysik.fu-berlin.de
\______________ ____________ http://www.toerring.de
Nov 14 '05 #19
In article <87************ @benpfaff.org>, Ben Pfaff wrote:
Nils Weller <me@privacy.net > writes:
Because character constants were designed to be capable of carrying more
more than one character (and it follows, obviously, that a ``char'' is
not enough to carry more than one character - hence the need for a
larger data type.)


Okay--so, then, *why* were character constants designed to be
capable of carrying more than one character?


That I don't know. I'd guess that it was intended to automize bit
shifting for you. The value of a multi-char character constant is now
implementation-defined, but historically you really got all chars you
asked for:

'foo'

would turn into

'f' << (CHAR_BIT * 2) | 'o' << CHAR_BIT | 'o'

This may seem more plausible for values written in hexadecimal notation:

'\xff\x12'

would turn into

0xff << CHAR_BIT | 0x12

--
My real email address is ``nils<at>gnuli nux<dot>nl''
Nov 14 '05 #20

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
9672
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10439
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
10215
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
10165
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
9043
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...
0
6783
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();...
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3727
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
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.