473,396 Members | 1,864 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,396 software developers and data experts.

Printf %d and %ld

Hello everyone,
I think printf %d works fine with signed, unsigned and negative integer
values, and the same as %ld for long. Is that correct? If not, do we need to
special conversion?
thanks in advance,
George
Oct 24 '07 #1
4 3989
George wrote:
Hello everyone,
I think printf %d works fine with signed, unsigned and negative integer
values, and the same as %ld for long. Is that correct? If not, do we need to
special conversion?
George:

%d will only work for unsigned int if the value is not bigger than the
largest allowed value of int. Otherwise, a negative value will be displayed.

--
David Wilkinson
Visual C++ MVP
Oct 24 '07 #2
Hi David,
Only works for unsigned int? I have tried that -1 works. :-)

int i = -1;
printf ("i is: %d", -1);

I have tried it on Visual Studio 2005.
regards,
George

"David Wilkinson" wrote:
George wrote:
Hello everyone,
I think printf %d works fine with signed, unsigned and negative integer
values, and the same as %ld for long. Is that correct? If not, do we need to
special conversion?

George:

%d will only work for unsigned int if the value is not bigger than the
largest allowed value of int. Otherwise, a negative value will be displayed.

--
David Wilkinson
Visual C++ MVP
Oct 24 '07 #3
George wrote:
Hi David,
Only works for unsigned int? I have tried that -1 works. :-)

int i = -1;
printf ("i is: %d", -1);

I have tried it on Visual Studio 2005.
regards,
George

"David Wilkinson" wrote:
>%d will only work for unsigned int if the value is not bigger than the
largest allowed value of int. Otherwise, a negative value will be displayed.

--
David Wilkinson
Visual C++ MVP
George:

I meant:

For unsigned int, %d will only work if the value is not bigger than the
largest allowed value of int. Otherwise, a negative value will be
displayed. %u should be used with unsigned int.

%d is intended for use with int, and either positive or negative values
may be displayed.

Although not so well suited to localization, C++ streams handle this
issue much better, because they are overloaded for different types.

--
David Wilkinson
Visual C++ MVP
Oct 24 '07 #4
Thanks David,
I agree.
regards,
George

"David Wilkinson" wrote:
George wrote:
Hi David,
Only works for unsigned int? I have tried that -1 works. :-)

int i = -1;
printf ("i is: %d", -1);

I have tried it on Visual Studio 2005.
regards,
George

"David Wilkinson" wrote:
%d will only work for unsigned int if the value is not bigger than the
largest allowed value of int. Otherwise, a negative value will be displayed.

--
David Wilkinson
Visual C++ MVP

George:

I meant:

For unsigned int, %d will only work if the value is not bigger than the
largest allowed value of int. Otherwise, a negative value will be
displayed. %u should be used with unsigned int.

%d is intended for use with int, and either positive or negative values
may be displayed.

Although not so well suited to localization, C++ streams handle this
issue much better, because they are overloaded for different types.

--
David Wilkinson
Visual C++ MVP
Oct 25 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Grumble | last post by:
Hello, I have the following structure: struct foo { char *format; /* format string to be used with printf() */ int nparm; /* number of %d specifiers in the format string */ /* 0 <= nparm <=...
8
by: aditya | last post by:
hi, Can anybody please tell me that how the following printf(...) statement works- main(){ int d=9; printf("%d",printf("%d")); return 0;
7
by: teachtiro | last post by:
Hi, 'C' says \ is the escape character to be used when characters are to be interpreted in an uncommon sense, e.g. \t usage in printf(), but for printing % through printf(), i have read that %%...
188
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
29
by: whatluo | last post by:
Hi, c.l.cs I noticed that someone like add (void) before the printf call, like: (void) printf("Timeout\n"); while the others does't. So can someone tell me whether there any gains in adding...
4
by: pai | last post by:
Hi , Can any one tell me how this statement of printf is behaving . how the last digit is printed int a=2,b=4,c=7; printf("%d",printf("%d %d:",a,b)); //answer to this was 2 4:3
11
by: timmu | last post by:
Someone asked me a question about integer division and printf yesterday, I tell him he should do a casting to float/double before you do any interger division. But he doesn't think so, so I try...
19
by: RedDevilDan | last post by:
I am working on a Memory Footprint Reduction project. I came across an idea to disable all printf statements so that less memory is required. In addition, when there is no single printf statement,...
34
by: Old Wolf | last post by:
Is there any possible situation for printf where %hd causes a different result to %d, and the corresponding argument was of type 'short int' ?
1
by: linq936 | last post by:
Hi, I read in many places that the string to be outputted by printf() must be ending with newline, for example, it should be printf("Hello World.\n"); instead of printf("Hello World.");
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.