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? 36 2292
"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).
--
Ben Pfaff
email: bl*@cs.stanford .edu
web: http://benpfaff.org
"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?
"JS" <dsa.@asdf.co m> wrote in message news:d1******** **@news.net.uni-c.dk... 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?
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);
return 0;
}
Alex
JS <dsa.@asdf.co m> scribbled the following: "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?
They don't. sizeof(char) is 1, but sizeof('g') is sizeof(int), which is
4 on your platform. 'g' is not a char. It's an int. Like Ben Pfaff
said, character constants are ints, not chars.
--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"The day Microsoft makes something that doesn't suck is probably the day they
start making vacuum cleaners."
- Ernst Jan Plugge
JS wrote: 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?
You have stumbled upon a difference between C and C++.
In C, literal 'g' is an int, while in C++ literal 'g' is a char.
Here are the C and C++ forms of the code, with the output of each.
#include <stdio.h>
#define COMPILER "<your compiler here> (C)"
int main(void)
{
printf(COMPILER " %d\n", (int) sizeof 'g');
return 0;
}
#include <cstdio>
using namespace std;
#define COMPILER "<your compiler here> (C++)"
int main(void)
{
printf(COMPILER " %d\n", (int) sizeof 'g');
return 0;
}
[outputs]
gcc (C) 4
bcc (C) 4
gcc (C++) 1
bcc (C++) 1
"JS" <dsa.@asdf.co m> writes: "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?
When do K&R do that?
--
Ben Pfaff
email: bl*@cs.stanford .edu
web: http://benpfaff.org
"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!
int main(void){ printf("%d\n",s izeof('g'));
}
Was it not supposed to return 1?
Contrary to what you might expect, a character constant such as 'g'
is an int value. So sizeof('g') == sizeof(int).
If you do sizeof( (char)'g' ) you should get 1.
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);
return 0; }
Alex
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?
--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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);
|
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
|
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 =...
|
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
| |
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;
|
by: Richard |
last post by:
Could someone point me to why "sizeof x" is/isnt preferable to "sizeof(x)",
|
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?
|
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;
}
|
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
|
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...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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...
|
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...
|
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,...
|
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...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |