-4

enter image description hereWord size in a 32-bit system is 4 byte. so char a='bcd'; is valid. 4 ASCII characters can fit in a 32-bit integer.

char a='bcd';
//output is d for this line
printf("%c\n",a); 

//output is 'z' when i use *(&a-1) and '�'(is -67 when manipulated)
printf("%c\n",*(&a-1)); 

Now here I just wants to know what &a-1 (this is not 'b') will point? is it Last memory address? and one more thing what is the order in which the characters are packed into an int.

------------------------------------------------------------------------------
| bits  : _ _ _ _ _ _ _ 8 _ _ _ _ _ _ _ 16 _ _ _ _ _ _ _ 24 _ _ _ _ _ _ _ 32 |
| order : ? ? ? ? ? ? ? 8 ? ? ? ? ? ? ? 16 ? ? ? ? ? ? ? 24 ? ? ? ? ? ? ? 32 |
------------------------------------------------------------------------------
Sandeep Rana
  • 3,251
  • 2
  • 24
  • 38
  • `'bcd'` isn't a valid single `char` literal. May be you're confusing with something like a character string literal: `"bcd"`. – πάντα ῥεῖ Jul 11 '15 at 15:09
  • 1
    you cannot do `char a = 'bcd'` – user007 Jul 11 '15 at 15:09
  • What makes you think a `char` has the size of an `32bit integer`? – deviantfan Jul 11 '15 at 15:10
  • No no ,char hasn'tt size of 32 bit but i think compiler adds some padding to make it easy to manipulate other things Thats why i am thinking that we can store things into that space – Sandeep Rana Jul 11 '15 at 15:17
  • There is padding, but that doesn't mean we can use all the space including the padding. – user007 Jul 11 '15 at 15:20
  • Printing `*(&a-1)` invokes undefined behaviour. – Mohit Jain Jul 11 '15 at 15:25
  • @SandeepSinghRana That´s great nonsense – deviantfan Jul 11 '15 at 15:32
  • @MohitJain I thought `char a = 'abc'` was undefined behaviour, because its implementation based acc to the links provided. Why is `*(&a - 1)` undefined?? – user007 Jul 11 '15 at 15:34
  • 1
    @user007 Implementation defined != undefined. And about `*(&a - 1)`, what do you think it does? `a` is a single char, doesn´t matter if it has some impl.defined value or not, it´s just one `char`. The previous memory address doesn´t belong to this `char`. – deviantfan Jul 11 '15 at 15:37
  • yupp @deviantfan and Thank you all for your precious time. and also for giving me -4 repo :P – Sandeep Rana Jul 11 '15 at 15:37
  • @MohitJain yes now it is clear to me last memory hasn't any relation to this instance of char – Sandeep Rana Jul 11 '15 at 15:39
  • @SandeepSinghRana Instead of making nonsensical accusations (it´s impossible for a single user to give you -4, and you don´t even know if any of the downvotes is from me), just learn the language better. (And if you´re using TurboC++, stop, because it´s impossible to learn it properly while using that.) – deviantfan Jul 11 '15 at 15:39
  • 2
    @deviantfan dont misunderstand dear that was not for you that was for all others that downvoted me and taught me an another lesson :P don't mind it yaar....I am new bee to the world of C lang. and i am using gcc . – Sandeep Rana Jul 11 '15 at 15:44

1 Answers1

5

"so char a='bcd'; is valid." No it is not, at least not in the sense you would expect (yes, 'bcd' is a multibyte character constant, but that does not store multiple characters as you seem to expect, but is some implementation defined number).

A char holds a single character, it is always one byte long. Writing char a gives you one byte to play with. You cannot access the bytes before and after that via a. The compiler also does not need to add any padding around your char if it does not want to. Since the literal 'bcd' has (at least on my system) a value that does not fit into a char (6447972 on my system), you end up with a single one byte char that has an implementation defined value. Even if there were padding bytes around a, you cannot assume they get written to.

printf("%c\n",*(&a-1));

invokes undefined behavior since you do not own that memory. This has nothing to do with any kind of "word size".

If you want to store a sequence of characters use

const char *str = "Some text";

(or

char str[] = "Some text";

if you want to modify the sequence) in C or

std::string str = "Some text";

in C++.


Ok, your edit made it more clear what you are talking about. Quoting from what you posted:

4 ASCII characters can fit in a 32 bit integer

But a char is not a 32 bit integer. It usually is an 8 bit integer. The text you posted is speaking about something like

int32_t a = 'bcd';

(or possibly uint32_t a). Now a is actually 32 bits long and can hold 32 bits of information. But as the text you quoted already says, this is rarely useful since the outcome of such an assignment is completely implementation dependent.

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
  • 4
    It's valid multibyte character constant in C. The actual behavior is _implementation defined_. The following [answer](http://stackoverflow.com/a/7459943/586873) explains that is also the case in C++. – Grzegorz Szpetkowski Jul 11 '15 at 15:10
  • yes @GrzegorzSzpetkowski thats what i am trying to say [link](http://stackoverflow.com/questions/7459939/what-do-single-quotes-do-in-c-when-used-on-multiple-characters/7459943#7459943) – Sandeep Rana Jul 11 '15 at 15:20
  • @SandeepSinghRana But you know that your linked post doesn´t use a `char` (but a real 4byte `int`)? (And it´s still implementation defined) – deviantfan Jul 11 '15 at 15:33