Home » Programming Tips & Tricks » C - Tips & Tricks

An amazing trick to print maximum value of an unsigned integer in C

By: IncludeHelp, on 29 JAN 2017

Today, I am here with a trick to print the maximum value of an unsigned integer in c programming language. This trick and example is based on 32 bits compiler where unsigned integer takes 4 bytes in the memory.

Consider the following steps:

Step 1: Declare an unsigned integer variable using unsigned int.

Step 2: Assign -1 to the variable.

unsigned int maxValue=-1;

Step 3: Print the value with %u format specifier.

Now, maximum value of an unsigned integer will print on the screen.


Here is the example:

#include <stdio.h>
int main()
{
	unsigned int maxValue=-1;
	
	printf("Maximum value of unsigned int is: %u\n",maxValue);
	
	return 0;
}

Output

    Maximum value of unsigned int is: 4294967295

Explanation

A signed type variable contains both kind of values –ve to +ve but when any unsigned type variable is assigned with -1, it represents the maximum value of that type and %u is a format specifier which prints the value in unsigned format (without sign).

See the below captured image of calculator with maximum value of 32 bits (4 bytes), here I set all 32 bits.

How to print maximum value of an unsigned int in c language




Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.