I declared one variable in one type (say char x ) and allocated. But i'm assigning integer or some other type value to it. Below is the code snippet which i'm facing problem,

char *tempstr = (char*)calloc(1,4); // declaring with char variable
tempstr[0] = '2'; // trying to assign 5 as integer value
//tempstr[1] = '(' // trying to assign '(' character
tempstr[2] = '50' //trying to assign '1' boolean value
tempstr[4] = 47 //trying to assign 47 integer value

but when i debugged and found in memory address that values stored for '2', '50', '47' are 30, 32 , 2f respectively. When i map these value to respective ASCII code only '2' get correct ascii code value.

Please suggest me a way to assign values in proper way.

Your compiler should have produced errors on line 4 because '50' is an invalid character. Line 5 is ok becuse it does not contain single quotes like like 4.

Ancient Dragon Thanks for quick reply!!

I'm using VS2010 compiler. when i use as above statement it didnt give any build error. it built fine and execute fine

I'm using VS2010 compiler. when i use as above statement it didnt give any build error. it built fine and execute fine

Cool story bro.

According to section 6.4.4.4 of C99, the behavior of a multi-character single-quoted constant like '50' is implementation-defined. A diagnostic is not required in this case, but you're still expected to know that you're relying on implementation-defined behavior (and if you want to know what the actual behavior is, it should be found in the compiler's documentation). I'm not sure what you're going for in that case anyway -- what would you expect to happen?

As for line 5, you allocated only 4 chars, but you assign to the 5th one, tempstr[4]. I suppose this is a typo, but you nevertheless invoke undefined behavior by writing off the end of an array.

look at Ascii table

all i whant to say that char is not int, it is one byte - 8 bites combination

#include <stdio.h>

int main()
{
   /*unsigned*/ char ucChar = 171;
   int iBit = 7;
   while(iBit >= 0)
   {
      printf("%d", (ucChar &(1 << iBit)) ? 1 : 0 );
      if(iBit % 4 == 0)
      {
         putchar(' ');
      }
      iBit--;
   }
   puts("b");

   return 0;
}

The number of bits in a byte is also implementation-defined. C99 section 3.6p3.

Even so, your code shows nothing, since ucChar gets promoted to int before any math actually happens (unless char is unsigned and UCHAR_MAX > INT_MAX, in which case it's promoted to unsigned instead). I'm not even sure what it was supposed to be showing in the first place.

I repeat my question: What did you expect to happen when you did tempstr[2] = '50'? The comment on that line ("trying to assign '1' boolean value") doesn't explain anything and in fact just makes it more confusing.

all i whant to say that char is not int,

Actually, char is just a small int whose range of values is +/- 127, see the header file limits.h Look at any standard ascii chart and you will see that every printable character has an int value.

I'm using VS2010 compiler. when i use as above statement it didnt give any build error. it built fine and execute fine

I got these warnings with your program, after adding semicolons at the end of the lines

c:\dvlp\test1\test1\test1.cpp(12): warning C4305: '=' : truncation from 'int' to 'char'
:\dvlp\test1\test1\test1.cpp(12): warning C4309: '=' : truncation of constant value

@Trentacle
the value stays the same 1010 1011 if i use char or unsigned char. even if i use 171 or -85 does not matter, how is it ? the only value that is displayed is changed if i cast to char or unsigned char.

Yes this is about assigning values. You can look at them as byte that represent char or unsigned char no ?

There is bit that represent sign so char is not 8bit it is 9 ??

#include <stdio.h>

int main()
{
   char ucChar = -85;
   int iBit = 7;
   printf("[%d]",ucChar);
   printf("sign %c", (ucChar &(1 << 8)) ? '+' : '-' );
   while(iBit >= 0)
   {
      printf("%d", (ucChar &(1 << iBit)) ? 1 : 0 );
      if(iBit % 4 == 0)
      {
         putchar(' ');
      }
      iBit--;
   }
   puts("b");

   return 0;
}

the value stays the same 1010 1011 if i use char or unsigned char. even if i use 171 or -85 does not matter, how is it ?

The 8 bits used to represent -85 and 171 in binary are identical. For a signed value, the first '1' is the sign bit.

the only value that is displayed is changed if i cast to char or unsigned char.

No idea what you mean.

Yes this is about assigning values. You can look at them as byte that represent char or unsigned char no ?

Yes.

There is bit that represent sign so char is not 8bit it is 9 ??

No. A char is always 8 bits*. An unsigned char uses all 8 bits. A signed char uses 7 bits + 1 sign bit.

----------

(* please don't be pedantic. do not confuse the matter by pointing out different systems/environments vs char sizes. the environment here is obvious.)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.