LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-03-2005, 08:24 AM   #1
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,203

Rep: Reputation: 45
print unsigned int


Hi how i can print an unsigned int in c? Is the %2d the correct operand or not for the printf function
 
Old 06-03-2005, 08:29 AM   #2
trevelluk
Member
 
Registered: Nov 2003
Location: Bristol, UK
Distribution: Debian Lenny, Gentoo (at work)
Posts: 388

Rep: Reputation: 32
I'd just use %d normally. %2d will force the width of the output to be at least two characters, for example if x = 1:

Code:
/* This will print
1
*/
printf ("%d\n", x);

/* This will print
 1
(note the space at the beginning)
*/
There's many more options than that: see man printf for more information.
 
Old 06-03-2005, 08:48 AM   #3
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,203

Original Poster
Rep: Reputation: 45
thx... i have just asked because i was curious to learn if the %d was enough for the double variable
 
Old 06-03-2005, 09:22 AM   #4
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
%u is the correct format specifier for an unsigned int. Otherwise printf() will end up displaying negative numbers, which we all know an unsigned int shouldn't be:
Code:
itsme@itsme:~$ cat toobig.c
#include <stdio.h>

int main(void)
{
  unsigned int num = 0x80000000;

  printf("%%d shows: %d, %%u shows: %u\n", num, num);

  return 0;
}
itsme@itsme:~$ ./toobig
%d shows: -2147483648, %u shows: 2147483648

Last edited by itsme86; 06-03-2005 at 09:25 AM.
 
1 members found this post helpful.
Old 06-03-2005, 09:34 AM   #5
trevelluk
Member
 
Registered: Nov 2003
Location: Bristol, UK
Distribution: Debian Lenny, Gentoo (at work)
Posts: 388

Rep: Reputation: 32
Gah, you're right! Sorry, I wasn't paying enough attention there. Need coffee, need coffee....
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
printf unsigned long long int? blackzone Programming 9 03-04-2008 12:41 PM
How to print unsigned long int alaios Programming 6 07-14-2005 10:27 AM
convert unsigned char * to unsigned long int linux_lover2005 Programming 3 04-26-2005 11:38 PM
64 bit CPU unsigned long int GodSendDeath Programming 8 03-29-2005 01:19 PM
int value larger then unsigned long jpc82 Programming 3 03-12-2004 03:54 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:09 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration