pls answer

R

rammy

In one of interviews i was asked what will return following funcion can
any body answer to this

sizeof("");
pls note that no space is there
 
P

pemo

rammy said:
In one of interviews i was asked what will return following funcion
can any body answer to this

sizeof("");
pls note that no space is there

The sizeof a const char *
 
?

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

pemo said:
The sizeof a const char *
Not at all, sizeof a string literal is the length
of that array.


sizeof("") is 1.
sizeof(" ") is 2.
 
V

Vladimir Oka

Nils said:
Not at all, sizeof a string literal is the length
of that array.


sizeof("") is 1.
sizeof(" ") is 2.

No. Not the "length", rather the size in bytes.

int a[10];

sizeof a == 10 * sizeof(int)

It's coincidental that the size of `char` is guaranteed to be 1. So:

sizeof "" == 1 * sizeof(char)
sizeof "a" == 2 * sizeof(char)
 
F

Flash Gordon

pemo said:
The sizeof a const char *

Wrong on two counts:

The type of string literals is not const although modifying them is
undefined behaviour.

sizeof is one of the context in which string literals do not
degenerate to a pointer to the first character.

Did you actually try this? Both you and the OP should try printing out
the size of various string literals, you might notice a pattern if you
do a few of different sizes. Also, reading what your text book and.or
the C standard says about sizeof could be illuminating.
 
M

Martin Ambuhl

pemo said:
The sizeof a const char *

Wanna bet? Try the following:

#include <stdio.h>
int main(void)
{
printf("sizeof(\"\") = %u\n", (unsigned) sizeof(""));
printf("sizeof(\" \") = %u\n", (unsigned) sizeof(" "));
printf("sizeof(\" \") = %u\n", (unsigned) sizeof(" "));
printf("sizeof(\" \") = %u\n", (unsigned) sizeof(" "));
return 0;
}
 
J

Jordan Abel

Wrong on two counts:

The type of string literals is not const although modifying them is
undefined behaviour.

except the size of a char * is the same as the size of a const char *
 
K

Kenneth Brody

Jordan Abel wrote:
[...]
except the size of a char * is the same as the size of a const char *

Must it be? While I can't imagine a situation where they wouldn't, I
do recall that people here have posted that sizeof(foo *) does not have
to be the same as sizeof(bar *). (Or am I misremembering that?)

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:[email protected]>
 
R

Roberto Waltman

Kenneth Brody said:
Jordan Abel wrote:
[...]
except the size of a char * is the same as the size of a const char *

Must it be? While I can't imagine a situation where they wouldn't, I
do recall that people here have posted that sizeof(foo *) does not have
to be the same as sizeof(bar *). (Or am I misremembering that?)

Embedded systems with different addressing schemes for ROM/FLASH and
RAM memories come to mind.
"Old" 80x86 code with small/large pointer qualifiers.
 
P

pete

Kenneth said:
Jordan Abel wrote:
[...]
except the size of a char * is the same as the size of a const char *

Must it be?

Yes.

N869
6.2.5 Types
[#27]
Similarly, pointers to qualified or unqualified versions of
compatible types shall have the same representation and
alignment requirements.

While I can't imagine a situation where they wouldn't,
I do recall that people here have posted that sizeof(foo *)
does not have to be the same as sizeof(bar *).

It doesn't.
 
F

Flash Gordon

C

CBFalconer

rammy said:
In one of interviews i was asked what will return following funcion
can any body answer to this

sizeof("");
pls note that no space is there

Don't use silly abbreviations (plz) and capitalize I. This helps
to make your post readable.

sizeof is NOT A FUNCTION. It is an operator. As you have it
above, it will evaluate to 1, because the empty string requires a
terminating nul byte. Since it is an operator the parentheses are
not needed, EXCEPT when applying it to a type rather than an
object.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
I

Ian Collins

Flash said:
Wrong on two counts:

The type of string literals is not const although modifying them is
undefined behaviour.
Surely something else C99 should have fixed....
 
K

Keith Thompson

Ian Collins said:
Surely something else C99 should have fixed....

"Fixing" this would have broken existing code.

For example:

#include <stdio.h>

void func(char *s)
{
printf("func(\"%s\")\n", s);
}

int main(void)
{
func("Hello, world");
return 0;
}

If string literals were const, the call to func() would be a
constraint violation. (Logically it should be, since func() might
attempt to modify the string.) This would almost certainly have been
done differently if the language were being designed from scratch
today.
 
I

Ian Collins

Keith said:
"Fixing" this would have broken existing code.
I remember the same debate when C++ made the change, the result was the
benefit was worth the cost, like many things, compatibility options to
the compiler saved the day.
For example:

#include <stdio.h>

void func(char *s)
{
printf("func(\"%s\")\n", s);
}

int main(void)
{
func("Hello, world");
return 0;
}

If string literals were const, the call to func() would be a
constraint violation. (Logically it should be, since func() might
attempt to modify the string.) This would almost certainly have been
done differently if the language were being designed from scratch
today.
There's still time to change while retaining compatibility.
 
J

Jordan Abel

Don't use silly abbreviations (plz) and capitalize I. This helps
to make your post readable.

sizeof is NOT A FUNCTION. It is an operator. As you have it
above, it will evaluate to 1, because the empty string requires a
terminating nul byte. Since it is an operator the parentheses are
not needed, EXCEPT when applying it to a type rather than an
object.

Or when applying it to a cast expression (though why you would want
to...)
 
R

Roberto Waltman

Or when applying it to a cast expression (though why you would want
to...)

While being aware of that, for a more consistent "look and feel", I
use the parenthesis with sizeof all the time, required or not. I see
no room for mistakes here, we all know that if (...), while (...) and
sizeof (...) are not function calls.

(I just checked the source files in one project I'm working on. sizeof
is used 40 times, 18 applied to typedef structs, which would require
the parenthesis. The rest to arrays or struct variables. Not once to
the basic data types.)

Ignoring personal taste, habits, and aesthetics considerations, is
there any practical, objective reason not to use parenthesis unless
applying sizeof to a type?
 
R

Richard Heathfield

Roberto Waltman said:
(I just checked the source files in one project I'm working on. sizeof
is used 40 times, 18 applied to typedef structs, which would require
the parenthesis.

They would also require a diagnostic.

And in any case, the number of times you need the size of a /type/ is
extraordinarily rare. I'm very surprised at your 45% figure.
The rest to arrays or struct variables. Not once to
the basic data types.)

Ignoring personal taste, habits, and aesthetics considerations, is
there any practical, objective reason not to use parenthesis unless
applying sizeof to a type?

My practical objective reason for not using them is that they do nothing
good and don't stop anything bad happening. In other words, they're a waste
of time.
 

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,183
Messages
2,570,968
Members
47,517
Latest member
TashaLzw39

Latest Threads

Top