What is %u and how and where can we use it?

Recommended Answers

All 9 Replies

%u is a format specifier for an unsigned integer used in printf and scanf.

can you please give an example?

Can you please RTFM?

commented: Harsh but fair :) +7

Can you please RTFM?

a code in 'Let Us C' by Y.Kanetkar confused me since i am only a beginner(there seem to be a lot of errors in the book)

#include<stdio.h>
main()
{
	int a=1, *x;
	float b=2.00, *y;
	char c='a', *z;
	printf("Value of x = %d\n", a);
	printf("Value of y = %f\n", b);
	printf("Value of z = %c\n", c);

	x = &a;
	y = &b;
	z = &c;

	printf("Original address in x = %u\n", x);
	printf("Original address in y = %u\n", y);
	printf("Original address in z = %u\n", z);

	x++;
	y++;
	z++;

	printf("New address in x = %u\n", x);
	printf("New address in y = %u\n", y);
	printf("New address in z = %u\n", z);
	return 0;
}

Thanks for the help Moschops :)

a code in 'Let Us C' by Y.Kanetkar confused me since i am only a beginner

A lot of people mistakenly use %u to print the value of a pointer. The correct method is using %p and casting non-void pointers to void:

printf("Original address in x = %p\n", (void*)x);
printf("Original address in y = %p\n", (void*)y);
printf("Original address in z = %p\n", (void*)z);

there seem to be a lot of errors in the book

From a cursory reading of that book, it's total crap. You'll probably learn more bad habits than good ones.

is this any better?

No.

or should i just follow the 'Starting C' section?

I'd suggest a good book.

  • Programming in C by Stephen G. Kochan is decent enough and accessible in my opinion.
  • C Programming: A Modern Approach by K. N. King is probably the most recommended beginner's book, but it's kind of heady.
  • Pointers on C by Kenneth A. Reek is my personal favorite, but it is out of date. Ideally you would get a book that covers C99.

Browsing Amazon I notice that there's a Head First C coming out early next year. That seems promising, but it won't help you right now. ;)

thanks a lot for your help and patience :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.