468,736 Members | 1,930 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,736 developers. It's quick & easy.

pls answer

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

May 10 '06 #1
19 1468
rammy wrote:
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 *
--
==============
Not a pedant
==============
May 10 '06 #2
pemo wrote:
rammy wrote:
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 *


And it's not a function.

--
imalone
May 10 '06 #3
pemo wrote:
rammy wrote:
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 *

Not at all, sizeof a string literal is the length
of that array.
sizeof("") is 1.
sizeof(" ") is 2.
May 10 '06 #4

Nils O. Selåsdal wrote:
pemo wrote:
rammy wrote:
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 *

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)

May 10 '06 #5
pemo wrote:
rammy wrote:
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 *


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.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
May 10 '06 #6
pemo wrote:
rammy wrote:
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 *


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;
}

May 10 '06 #7
"Nils O. Selåsdal" wrote:
pemo wrote:
rammy wrote:
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 *

Not at all, sizeof a string literal is the length
of that array.
sizeof("") is 1.
sizeof(" ") is 2.


Yes, my mistake.

--
==============
Not a pedant
==============
May 10 '06 #8
On 2006-05-09, Flash Gordon <sp**@flash-gordon.me.uk> wrote:
pemo wrote:
rammy wrote:
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 *


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 *

May 10 '06 #9
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:Th*************@gmail.com>
May 10 '06 #10
Kenneth Brody <ke******@spamcop.net> wrote:
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.
May 10 '06 #11
Kenneth Brody wrote:

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.

--
pete
May 10 '06 #12
Jordan Abel wrote:
On 2006-05-09, Flash Gordon <sp**@flash-gordon.me.uk> wrote:
pemo wrote:
rammy wrote:
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 *

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 *


Probably true, but if someone believes it is const in one context they
presumably believe it is always const.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc

Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php
May 10 '06 #13
Flash Gordon wrote:
pemo wrote:
rammy wrote:
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 *

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....

--
Ian Collins.
May 10 '06 #14
Ian Collins <ia******@hotmail.com> writes:
Flash Gordon wrote:
pemo wrote:
rammy wrote:

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 *

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....


"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.

--
Keith Thompson (The_Other_Keith) 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.
May 10 '06 #15
Keith Thompson wrote:
Ian Collins <ia******@hotmail.com> writes:
Flash Gordon wrote:
pemo wrote:
rammy wrote:
>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 *
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....

"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.

--
Ian Collins.
May 10 '06 #16
rammy wrote:

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/>
May 10 '06 #17
On 2006-05-09, CBFalconer <cb********@yahoo.com> wrote:
rammy wrote:

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.


Or when applying it to a cast expression (though why you would want
to...)
May 10 '06 #18
Jordan Abel <ra*******@gmail.com> wrote:
<cb********@yahoo.com> wrote:
sizeof is NOT A FUNCTION. It is an operator...
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...)


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?

May 10 '06 #19
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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
May 10 '06 #20

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Dave H | last post: by
33 posts views Thread by Nigel Molesworth | last post: by
14 posts views Thread by Peter Mount | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
xarzu
2 posts views Thread by xarzu | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.