Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

How "back slash(\)" behaves in strings?

How "back slash(\)" behaves in strings?

How "back slash(\)" behaves in strings?

void main()
char s[]="\12345s\n";
Output:6 How?

void main()
char *s="\12345s\n";
Output:2 How?
What is the difference?

RE: How "back slash(\)" behaves in strings?

The only reason I could think that there would be a difference is if the memory model you're using makes s[] a far pointer, and *s a near pointer.  You're asking for the size of the pointer, not the size of the string anyway, so that must be the difference.  I'm not willing to bet but it's what I would guess.

I don't think that the \ has anything to do with it.  Shouldn't you be using malloc anyway?  When you declare *s, you only allocate a pointer, you don't actually allocate memory, same with s[], right?  Otherwise, where are you putting the data?

As an answer to your original question, the difference is in pointer size, which I believe is a near/far pointer difference.


As always, I hope that helped!

Beware: Studies have shown that research causes cancer in lab rats.

RE: How "back slash(\)" behaves in strings?


Example 1:

char s[]="\12345s\n";

The backslash results in 123 being treated as an octal value. The bytes in s[] can be broken down as:

0123 (ascii 'S')
\0   (terminating null character added by compiler)

Total=6 bytes

Note that s[] is an array and /not/ a pointer.

Example 2:

char *s="\12345s\n";

Same sequence of characters, except for s is now a pointer. sizeof calculates the size of the pointer itself, which on your implementation is 2 (on mine it's 4). You would get the same result with the expression:

printf("%d\n",sizeof(char *));

It is perfectly ok to initialize a pointer like this BTW. The compiler creates storage for the string literal and sets s to point to the first element.

Pedantic Note 1:

main should return an int and be of the form:

int main() or int main(int,char **)

The results of returning void from main are undefined and therefore may result in your program crashing (or little green men invading your home and abducting your family).

Pedantic Note 2:

In your sizeof expressions the parentheses aren't necessary as sizeof is an operator, not a function, so:

printf("%d",sizeof s);

The parentheses /are/ necessary for type names:


MWB, if you were betting, I'd be willing to take you up on it ;-)



RE: How "back slash(\)" behaves in strings?

Thank you for all
You made me happy now!!!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close