what is the difference between %d and %*d in c languaga?

Answer Posted / shivam chaurasia

The %*d in a printf allows you to use a variable to control the field width, along the lines of:
int wid = 4;
printf ("%*d
", wid, 42);

output,...
..42


if the form is like this...
printf ("%*d %*d
", a, b);
is undefined behaviour as per the standard, since you should be providing four arguments after the format string, not two (and good compilers like gcc will tell you about this if you bump up the warning level). From C11 7.20.6 Formatted input/output functions:

If there are insufficient arguments for the format, the behavior is undefined.

It should be something like:

printf ("%*d %*d
", 4, a, 4, b);


check this link for extra detail....
http://www.cplusplus.com/reference/cstdio/printf/

Is This Answer Correct ?    1 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What do you mean by keywords in c?

621


What are inbuilt functions in c?

556


Can you assign a different address to an array tag?

695


write a program to print the consecutive repeated character from the given string... input string is : hhhhjkutskkkkkggggj output should be like this: hhhhkkkkkgggg anyone help me...

1481


How do you generate random numbers in C?

651






What is c basic?

591


find out largest elemant of diagonalmatrix

1644


What are multidimensional arrays?

656


How many types of operators are there in c?

611


How can I swap two values without using a temporary?

610


List the difference between a While & Do While loops?

629


What are the benefits of organizational structure?

566


Why do we use int main?

602


What is the basic structure of c?

551


What is the purpose of the following code? Is there any problem with the code? void send(int count, short *to, short *from) { /* count > 0 assumed */ register n = (count + 7) / 8; switch (count % 8) { case 0: do { *to = *from++; case 7: *to = *from++; case 6: *to = *from++; case 5: *to = *from++; case 4: *to = *from++; case 3: *to = *from++; case 2: *to = *from++; case 1: *to = *from++; } while (--n > 0); } }

1953