473,402 Members | 2,072 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,402 software developers and data experts.

sizeof operator on string(charcter array)

I have seen the following program.

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. int main()
  3. {
  4.         char str[]="S\065AB";
  5.         printf("\n%d", sizeof(str));
  6.         return(0);
  7. }
It's output is 5.
Please, someone explain why it is giving o/p 5.

Thank You.
Jan 1 '09 #1
6 3806
Banfa
9,065 Expert Mod 8TB
Because you have supplied a string that is 5 characters long. Try printing out the string as well as its size.
Jan 1 '09 #2
Thanks for your reply.
I have tried to print its output. It is "S5AB". Why it is neglecting "\06"? I was thinking it should print only "S" considering '\0' as end of string. Please explain this.
Thank You.
Jan 1 '09 #3
donbock
2,426 Expert 2GB
Use of numeric escape sequences has such potential for confusion that it may be helpful take advantage of implicit concatenation to isolate the escape sequence:
Expand|Select|Wrap|Line Numbers
  1. char str[] = "S" "\065" "AB"; 
You should review the action of the escape character in string literals to see why I chose to break the string at these points.

Compare the following strings. What is the size (returned by sizeof), length (returned by strlen), and content of each?
Expand|Select|Wrap|Line Numbers
  1. char str1[] = "S\065AB";
  2. char str2[] = "S" "\065" "AB";
  3. char str3[] = "S" "\06" "5AB";
  4. char str4[] = "S" "\0" "65AB";
Jan 1 '09 #4
Banfa
9,065 Expert Mod 8TB
Has a hint you may also want to look up the ASCII code 065, remembering that a digit sequence starting with 0 is in octal (base 8).

Or try reading Escape Sequences (C)

or try both of course :-)
Jan 1 '09 #5
donbock
2,426 Expert 2GB
Octal numbers ... what a pain! Why is the C language defined such that it is so easy to bump into that archaic radix?

35-40 years ago octal was much more commonly used than hexadecimal. In that milieu nobody questioned the decision to make octal the default radix for character constants.

This is another example of how some programs outlast their implicit assumptions. Let those who have eyes, see.

-------

Many years ago, when I was a callow youth, I wrote a static initializer for a large integer array. I thought it would be nice if the initial values lined up nicely in rows in columns so I padded the initial values with leading zeroes so they were all the same width. The initializer was beautiful, but truly evil. That day I learned initial values should only be padded with leading whitespace.
Jan 1 '09 #6
Thank you both donbock and Banfa for your replies. I got the concept.
Thank You.
Jan 1 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Jeff Johnson | last post by:
Hi, I've begun converting an ASP site over to .NET and I'm a novice at both the new platform as well as C#. I have a COM+ object that returns a string array when it is called. The size of...
11
by: Zordiac | last post by:
How do I dynamically populate a string array? I hope there is something obvious that I'm missing here Option Strict On dim s() as string dim sTmp as string = "test" dim i as integer ...
5
by: Paulers | last post by:
Hello all, I have a string array with duplicate elements. I need to create a new string array containing only the unique elements. Is there an easy way to do this? I have tried looping through...
6
by: Niyazi | last post by:
Hi all, What is fastest way removing duplicated value from string array using vb.net? Here is what currently I am doing but the the array contains over 16000 items. And it just do it in 10 or...
2
by: anon.asdf | last post by:
Hi! Q. 1) How does one write: sizeof(array of 5 "pointers to double") ??? I know that sizeof(pointer to an array of 5 doubles) can be written as: sizeof(double (*));
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.