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 19 1569
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
==============
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
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.
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)
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
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;
}
"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
==============
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 *
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>
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.
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
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
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.
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.
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.
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/>
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...)
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?
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) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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?
...
|
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...
|
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...
|
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...
|
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...
|
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>...
|
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...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
|
by: Matthew3360 |
last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function.
Here is my code.
header("Location:".$urlback);
Is this the right layout the...
|
by: AndyPSV |
last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
|
by: Arjunsri |
last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
|
by: WisdomUfot |
last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
|
by: Matthew3360 |
last post by:
Hi,
I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
|
by: Oralloy |
last post by:
Hello Folks,
I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA.
My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
|
by: BLUEPANDA |
last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
| |