473,387 Members | 1,486 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 1637
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mohammed Mazid | last post by:
Hi folks! Can anyone please help me with this? I am developing a Quiz program but I am stuck with "multiple answers". Basically I need some sort of code that would select multiple answers...
4
by: Mohammed Mazid | last post by:
Hi folks! Can anyone please help me with this? I am developing a Quiz program but I am stuck with "multiple answers". Basically I need some sort of code that would select multiple answers...
9
by: Dave H | last post by:
Hello, I have a query regarding definition lists. Is it good practice semantically to use the dt and dd elements to mark up questions and answers in a frequently asked questions list, or FAQ? ...
33
by: Nigel Molesworth | last post by:
I've Googled, but can't find what I need, perhaps I asking the wrong question! I want a "FAQ" page on a web site, I hate those pages that scroll you to the answer so and I figured that a good...
2
by: amelia170 | last post by:
I am creating a database with a yes or no answer for question 1. Based on that answer, (if they answer yes) they will answer question 2, then three....However, if they answer no to question one, I...
14
by: Peter Mount | last post by:
Hello I'm having trouble with " scanf("%c", &answer);" on line 20 below. When I run the program in cygwin on Windows 98SE it skips that line completely and ends the program. Does scanf have...
12
by: Bernie Yaeger | last post by:
I asked two fairly simple questions, or so I thought, but have received no ideas at all. 1. What is the control that MS uses in Outlook Express that contains attachments? It appears to be a...
2
by: windsorben | last post by:
I'd like to have an image appear after the student answers each short answer question correctly. I can't seem to get it to work properly. See code below. Thanks! <html> <head>...
2
by: Ken Fine | last post by:
I want to add the security question and answer security feature to the ChangePassword control. I am aware that this functionality is built into the PasswordRecovery tool. I have implemented the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
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,...
0
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...
0
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,...
0
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...

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.