473,320 Members | 1,857 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

printf question

21
Hi,

Check this program.

#include <stdio.h>

int main()
{
int x = 0xFFFFFFF0;
printf("%X\n",x);
char y = x;
printf("%X\n", y);
getchar();
}


I have FFFFFFF0 in int variable x and i am assigning it to char. Since a char can hold only 8 bits, it should have only F0. But when i try to print it, it prints the entire content as we had in int.

If i change the signed char to unsigned char it is working as per expectation. Can any one help me in understanding the underlying conversions.

If we have a signed char, will the value be stored in 2's complement ?
Feb 22 '08 #1
1 929
weaknessforcats
9,208 Expert Mod 8TB
The int is 32 bits. The largest vaue you can out in there is 0x7FFFFFFF.

The left bit is reserved for the sign bit and is not part of the data value.

When the left bit is set (negative) the number is stored in 2's complement.

Assigning an int to a char (assuming the char is signed) will produce a maximum value of 127 since the left bit is the sign bit again.

When you use unsigned int and unsigned char, you lose the sign bit which is now part of the value but you also lose the ability to store negative numbers.
Feb 22 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Wenjie | last post by:
Hello, Is printf("%d", var); OK for the unsigned long var = SOME_VAR? Or should some other print format be specified? Thanks and best regards, Wenjie
14
by: Mantorok Redgormor | last post by:
Would this by any chance invoke undefined behavior? extern int printf(const char *, ...); int main(void) { printf("Hello\n"); return 0; } That is, providing my own printf prototype would...
188
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
31
by: DeltaOne | last post by:
#include<stdio.h> typedef struct test{ int i; int j; }test; main(){ test var; var.i=10; var.j=20;
12
by: John Devereux | last post by:
Hi, I would like to know if their is a conversion specifier, or other method, that will allow me to not use particular arguments to printf. Failing that, is it permissable to simply "not use"...
11
by: rahul8143 | last post by:
Does printf output depends on compilers or operating systems? I have one program int main() { int i=5; printf("%d %d %d",i++,i++,i++); return 0; } Output of this program on DOS (TC) 7 6 5...
27
by: jacob navia | last post by:
Has anyone here any information about how arrays can be formatted with printf? I mean something besides the usual formatting of each element in a loop. I remember that Trio printf had some...
7
by: Martin Jørgensen | last post by:
Hi, Perhaps a stupid question but I wonder if this isn't possible using C: printf("\nOr push <enter> for default (which is theta=%ld)%s", theta, ": "); It would be nicer to have it on one...
5
by: Why Tea | last post by:
int printf(const char *fmt,...) The fmt is const, indicating that it can't be changed during runtime. If I want to print a series of integers that I only know the largest value beforehand,...
7
by: Rajesh S R | last post by:
printf("%hhd",89);/*Assume char has 8 bits and is signed*/ Is it valid? I know that it has been discussed in comp.std.c. ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.