0

In below program why data of character printed properly in normal int variable case and why it is not printing the data properly in int?

Case 1

#include <stdio.h>
int main()
{
   int *i = NULL;
   char s = 'A';
   i = (int *)&s; // storing
   printf("i - %d\n",*i);
   return 0;
}

Output :

i - 1837016897

Why 65 value not printed here?

Case 2

#include <stdio.h>
int main()
{
    int *i = NULL;
    char s = 'A';
    i = (int *)&s; // storing
    printf("i - %c\n",*i);   // if we display character stored here 
                             // then it is printed properly
    return 0;
}

Output:

i - A

Case 3

#include <stdio.h>
int main()
{
    int i = 0;
    char s = 'A';
    i = s;
    printf("i - %d\n",i); // in this case data is properly printing
    return 0;
}

Output:

i - 65
1
  • sizeof(int) and sizeof(char) give different values.......so...... Commented Jul 5, 2016 at 9:41

4 Answers 4

3

int is 4 bytes long, char is 1 byte. This means that you cannot convert like this. You are taking the pointer of a 1-byte variable and are telling the program to interpret it like a 4-byte variable, so whatever is behind it in the memory will also be used in the program.

1837016897 in hexadecimal value becomes 0x6D7EA741, where the last byte (0x41) is actually decimal 65, so the character does show up in your result (if you're wondering why it is the last byte and not the first, this is because of endianness - you can read up on that yourself if you like).

Sign up to request clarification or add additional context in comments.

Comments

3

Your programs 1 and 2 exhibit undefined behaviour because they refer to an object of type char via an lvalue of type int. This is not allowed according to section 6.5 paragraph 7 of the standard.

Your program 3 is OK because char is implicitly converted to int when passed to a function like printf, which is perfectly normal and well-defined.

2 Comments

I feel like it's worth explaining to him why 2 appears to work though. The first one fails because it is taking the 4 bytes in memory (3 of which are garbage) and prints the decimal value of that. Obviously this yields undefined results. The second however, is going to just grab the first byte which for his system conveniently is the byte the integer decided to store it in. This is undefined since the compiler could choose to store the byte wherever it wanted inside the integer but it worked specifically for him because the byte grabbed by printf was the byte that had 'A' stored in it.
Or to rephrase that, the compiler could chose to grab whatever byte from the integer it wanted for the print command. This system specifically just works as he needed it to.
1
i = (int *)&s;

this makes sure i points to the address of s, but since s is char and i is int* when dereferencing i, (*i) the compiler looks for int type unless using cast like so ( *(char*)i ), so it looks at the address of s, but looks at 4 bytes instead of 1 (assuming 32-bit int)

Comments

1

int requires 4 bytes to be stored in memory while char requires only 1 byte to be stored in memory. So char s = 'A' stores only one byte with value 65 at memory address &s.

In case 1, you try to print 4 bytes at the memory address pointed by &s as am integer. Now the memory adjacent to &s may have garbage values hence, you get 1837016897 instead of 65.

In case 2, you are printing using %c which recasts i into a char and hence prints only one byte.

In case 3, i=s stores the value of s i.e. 65 into i, hence you get 65 as the output.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.