User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 455,989 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,765 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 2872 | Replies: 4
Reply
Join Date: Jul 2004
Posts: 31
Reputation: Sukhbir is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
Sukhbir Sukhbir is offline Offline
Light Poster

string size problem

  #1  
Aug 29th, 2004
Consider the following programme
void main()
{
char str[]="\12345s\n";
printf("%d",sizeof(str));
}

Output is :6
can someone explain me why.And is sizeof contain null value while
gigving the size of any string.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2004
Posts: 3,755
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 17
Solved Threads: 147
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: string size problem

  #2  
Aug 29th, 2004
char str[] = "\12345s\n";
This is equivalent to the following.
char str[] = {0123,'4','5','s','\n','\0'};
The array has six elements, each of one byte in size -- so sizeof reports the array size as 6.
Reply With Quote  
Join Date: Jun 2004
Location: Marin, CA, USA
Posts: 434
Reputation: Chainsaw is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 10
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

Re: string size problem

  #3  
Aug 30th, 2004
And if you said:
void main()
{
char *str ="\12345s\n";
printf("%d",sizeof(str));
}

The output will be 4, the size of a pointer (not the size of what it points to)
Reply With Quote  
Join Date: Jul 2004
Posts: 31
Reputation: Sukhbir is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
Sukhbir Sukhbir is offline Offline
Light Poster

Re: string size problem

  #4  
Sep 2nd, 2004
char str[] = {0123,'4','5','s','\n','\0'};
how it is treated like this first element like 0123.it is very much confusing pls clear my doubt.
Reply With Quote  
Join Date: Apr 2004
Posts: 3,755
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 17
Solved Threads: 147
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: string size problem

  #5  
Sep 2nd, 2004
                    char str[]="\12345s\n";
Originally Posted by Sukhbir
how it is treated like this first element like 0123.it is very much confusing pls clear my doubt.
You used an escape sequence.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 9:33 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC