Confusion on printing strings as integers in C - c

I would like to know how the string is represented in integer, so I wrote the following program.
#include <stdio.h>
int main(int argc, char *argv[]){
char name[4] = {"#"};
printf("integer name %d\n", *(int*)name);
return 0;
}
The output is:
integer name 64
This is understandable because # is 64 in integer, i.e., 0x40 in hex.
Now I change the program into:
#include <stdio.h>
int main(int argc, char *argv[]){
char name[4] = {"##"};
printf("integer name %d\n", *(int*)name);
return 0;
}
The output is:
integer name 16448
I dont understand this. Since ## is 0x4040 in hex. So it should be 2^12+2^6 = 4160
If I count the '\0' at the end of the string, then it should be 2^16+2^10 = 66560
Could someone explain where 16448 comes from?

Your math is wrong: 0x4040 == 16448. The two fours are the 6th and 14th bits respectively.

Your code actually invokes undefined behavior because you must not alias a char * with an int *. This is known as the strict aliasing rule. To see just one reason why this should be disallowed, consider what would otherwise have to happen if the code is run on a little and a big endian machine.
If you want to see the hex pattern of the string, you should simply loop over its bytes and print out each byte.
void
print_string(const char * strp)
{
printf("0x");
do
printf("%02X", (unsigned char) *strp);
while (*strp++);
printf("\n");
}
Of course, instead of printing the bytes, you can shift them into an integer (that will very soon overflow) and only finally output that integer. While doing this, you'll be forced to take a stand on “your” endianness.
/* Interpreting as big endian. */
unsigned long
int_string(const char * strp)
{
unsigned long value = 0UL;
do
value = (value << 8) | (unsigned char) *strp;
while (*strp++);
return value;
}

This is how 16448 comes :
0x4040 can be written like this in binary :
4 0 4 0 -> Hex
0100 0000 0100 0000 -> Binary
2^14 2^6 = 16448
Because here 6th and 14th bit are set.
Hope you got it :)

Related

why itoa fuction returns 32 bits if the size of variable in 16 bit

size of short int is 2 bytes(16 bits) on my 64 bit processor and mingw compiler but when I convert short int variable to a binary string using itoa function
it returns string of 32 bits
#include<stdio.h>
int main(){
char buffer [50];
short int a=-2;
itoa(a,buffer,2); //converting a to binnary
printf("%s %d",buffer,sizeof(a));
}
Output
11111111111111111111111111111110 2
The answer is in understanding C's promotion of short datatypes (and char's, too!) to int's when those values are used as parameters passed to a function and understanding the consequences of sign extension.
This may be more understandable with a very simple example:
#include <stdio.h>
int main() {
printf( "%08X %08X\n", (unsigned)(-2), (unsigned short)(-2));
// Both are cast to 'unsigned' to avoid UB
return 0;
}
/* Prints:
FFFFFFFE 0000FFFE
*/
Both parameters to printf() were, as usual, promoted to 32 bit int's. The left hand value is -2 (decimal) in 32bit notation. By using the cast to specify the other parameter should not be subjected to sign extension, the printed value shows that it was treated as a 32 bit representation of the original 16 bit short.
itoa() is not available in my compiler for testing, but this should give the expected results
itoa( (unsigned short)a, buffer, 2 );
your problem is so simple , refer to itoa() manual , you will notice its prototype which is
char * itoa(int n, char * buffer, int radix);
so it takes an int that to be converted and you are passing a short int so it's converted from 2 byte width to 4 byte width , that's why it's printing a 32 bits.
to solve this problem :
you can simply shift left the array by 16 position by the following simple for loop :
for (int i = 0; i < 17; ++i) {
buffer[i] = buffer[i+16];
}
and it shall give the same result , here is edited version of your code:
#include<stdio.h>
#include <stdlib.h>
int main(){
char buffer [50];
short int a= -2;
itoa(a,buffer,2);
for (int i = 0; i < 17; ++i) {
buffer[i] = buffer[i+16];
}
printf("%s %d",buffer,sizeof(a));
}
and this is the output:
1111111111111110 2

C - Missing 0 in hexadecimal output

I had to test if my machine is using the little or the big endian. For this reason i wrote this code. I can see that my machine is using the little endian. But i dont know why the output from my first byte is only D. Should it not be 0D?
union int2byte
{
unsigned char bytes[4];
unsigned int hex;
};
int main(int argc, const char* argv[])
{
union int2byte hexby;
hexby.hex = 0xBAADF00D;
printf("%u\n",hexby.hex);
int counter;
for(counter = 0; counter < 4; counter = counter + 1)
{
printf("Hex for Byte %u is %X.\n",counter+1, hexby.bytes[counter]);
}
}
Output:
3131961357
Hex for Byte 1 is D.
Hex for Byte 2 is F0.
Hex for Byte 3 is AD.
Hex for Byte 4 is BA.
%X does not output leading zeros. Use %02X instead. The 2 tells it to output at least 2 digits, and the 0 tells it to pad the output on the left side with a '0' character if the output is less than 2 digits.

Storing int value into char p[] index

Been stuck on a problem. figured out my issue. I have looked online quite a bit and have read many possible solutions, but nothing seems to work for me.
I have this program, lets call it test.c
#include <stdio.h>
int main() {
int x = 21345;
char p[10];
p[0] = x;
printf("%d\n", x);
printf("%d", p[0]);
return 0;
}
As you would guess, I want the two printf statements to have the same output. Instead, I get the following output...
21345
97
I need 21345 to be at p[0], and when I call printf("%d", p[0]);
It needs to print out 21345, not 97
Any help? :(
EDIT:
Looks like you guys answered, thanks! Changing to int p[10] would solve the problem. I have another complication though, I also need to be able to store strings inside the indexes of p, along with ints.. how should I tackle this?
A char does not have the same type as int and is not identical in number of bits. You would need int p[10]; for that to work.
You have an array of signed chars, they take 1 byte each, the max value would be 127 and minimum -128. Now the x's value takes more bytes to represent, there is an overflow, and it truncates the leftmost bits,
21345 has the binary representation: 0101 0011 0110 0001
now discard the leftmost byte and you get: 0110 0001 which is 97
char store only 1 byte value so that you are getting that output. If you change char p[10] to int p[10] you will get output.
char is simply not big enough to store 21345. Therefore compiler takes just low 8 bits and puts them into char, so it becomes 97. When passing to printf it will convert it back to int but the highest bits are already lost.
You need to use int array instead of char
It should be like
#include <stdio.h>
int main() {
int x = 21345;
int p[10];
p[0] = x;
printf("%d\n", x);
printf("%d", p[0]);
return 0;
}
Output
21345
21345
Well if you want to use same array for strings too, use 2 dimensions char array
because you can store a integer value in 4 characters array correctly.
so you need to use p[10][4]
Now store each byte of int in corresponding char array index
p[0][0] = (x >> (8*0)) & 0xff;
p[0][1] = (x >> (8*1)) & 0xff;
p[0][2] = (x >> (8*2)) & 0xff;
p[0][3] = (x >> (8*3)) & 0xff;

c and bit shifting in a char

I am new to C and having a hard time understanding why the code below prints out ffffffff when binary 1111111 should equal hex ff.
int i;
char num[8] = "11111111";
unsigned char result = 0;
for ( i = 0; i < 8; ++i )
result |= (num[i] == '1') << (7 - i);
}
printf("%X", bytedata);
You print bytedata which may be uninitialized.
Replace
printf("%X", bytedata);
with
printf("%X", result);
Your code then run's fine. code
Although it is legal in C, for good practice you should make
char num[8] = "11111111";
to
char num[9] = "11111111";
because in C the null character ('\0') always appended to the string literal. And also it would not compile as a C++ file with g++.
EDIT
To answer your question
If I use char the result is FFFFFFFF but if I use unsigned char the result is FF.
Answer:
Case 1:
In C size of char is 1byte(Most implementation). If it is unsigned we can
use 8bit and hold maximum 11111111 in binary and FF in hex(decimal 255). When you print it with printf("%X", result);, this value implicitly converted to unsigned int which becomes FF in hex.
Case 2: But when you use char(signed), then MSB bit use as sign bit, so you can use at most 7 bit for your number whose range -128 to 127 in decimal. When you assign it with FF(255 in decimal) then Integer Overflow occur which leads to Undefined behavior.

Why does C print my hex values incorrectly?

So I'm a bit of a newbie to C and I am curious to figure out why I am getting this unusual behavior.
I am reading a file 16 bits at a time and just printing them out as follows.
#include <stdio.h>
#define endian(hex) (((hex & 0x00ff) << 8) + ((hex & 0xff00) >> 8))
int main(int argc, char *argv[])
{
const int SIZE = 2;
const int NMEMB = 1;
FILE *ifp; //input file pointe
FILE *ofp; // output file pointer
int i;
short hex;
for (i = 2; i < argc; i++)
{
// Reads the header and stores the bits
ifp = fopen(argv[i], "r");
if (!ifp) return 1;
while (fread(&hex, SIZE, NMEMB, ifp))
{
printf("\n%x", hex);
printf("\n%x", endian(hex)); // this prints what I expect
printf("\n%x", hex);
hex = endian(hex);
printf("\n%x", hex);
}
}
}
The results look something like this:
ffffdeca
cade // expected
ffffdeca
ffffcade
0
0 // expected
0
0
600
6 // expected
600
6
Can anyone explain to me why the last line in each block doesn't print the same value as the second?
The placeholder %x in the format string interprets the corresponding parameter as unsigned int.
To print the parameter as short, add a length modifier h to the placeholder:
printf("%hx", hex);
http://en.wikipedia.org/wiki/Printf_format_string#Format_placeholders
This is due to integer type-promotion.
Your shorts are being implicitly promoted to int. (which is 32-bits here) So these are sign-extension promotions in this case.
Therefore, your printf() is printing out the hexadecimal digits of the full 32-bit int.
When your short value is negative, the sign-extension will fill the top 16 bits with ones, thus you get ffffcade rather than cade.
The reason why this line:
printf("\n%x", endian(hex));
seems to work is because your macro is implicitly getting rid of the upper 16-bits.
You have implicitly declared hex as a signed value (to make it unsigned write unsigned short hex) so that any value over 0x8FFF is considered to be negative. When printf displays it as a 32-bit int value it is sign-extended with ones, causing the leading Fs. When you print the return value of endian before truncating it by assigning it to hex the full 32 bits are available and printed correctly.

Resources