Negative value returned on unsigned int type

This is a discussion on Negative value returned on unsigned int type within the C Programming forums, part of the General Programming Boards category; While experimenting with different data types I noticed that when using all unsigned data types and performing a calculation resulting ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    8

    Negative value returned on unsigned int type

    While experimenting with different data types I noticed that when using all unsigned data types and performing a calculation resulting is a negative value, the computer still outputs the negative value. Here's an example of the code I am using:

    Code:
    #include <stdio.h>
    
    int main()
    {
    	unsigned int a = 1;
    	unsigned int b = 0;
    	unsigned int c;
    	
    	c = b - a;
    	
    	printf( "%d", c );
    
    	return 0;
    }
    I am not receiving any errors in compilation or during execution. Can someone explain to me what's going on? Thanks!

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    %d (or %i) prints a signed int. Use %u for an unsigned int.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    8
    Ahh.. thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 04:42 PM
  2. Need Help Please
    By YouShallNotPass in forum C++ Programming
    Replies: 3
    Last Post: 08-22-2006, 12:22 PM
  3. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 12:58 PM
  5. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21