what is the difference between %d and %*d in c languaga?
Answers were Sorted based on User's Feedback
Answer / sivakumar
anand and manini your expectations are wrong. because %d
give the original value of the variable and %*d give the
address of the variable.
eg:-
int a=10,b=20;
printf("%d%d",a,b);
printf("%*d%*d",a,b);
result is 10 20 1775 1775
here 1775 is the starting address of the memory allocation
for the integer.a and b having same address because of
contagious memory allocation.
| Is This Answer Correct ? | 35 Yes | 19 No |
Answer / azad sable,chiplun.
In first case i.e. '%d' the '%' indicates that the
conversion specification follows. And 'd' known as data
type charactor indicates that the no. to be read is in
intiger mode.
* is an input field which specifie field width.
example
scanf("%d%*d%d",&a,&b);
will assign the data 123 456 789 as follows.123 to a 456
skipp because of * 789 to b.
| Is This Answer Correct ? | 9 Yes | 5 No |
Answer / vishal pandey
int v=23,d=89;
printf("%d %*d",v,d);
then o/p v=23 and d=address value and address value change with processor but original value does not change.
| Is This Answer Correct ? | 3 Yes | 2 No |
Answer / 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 |
In the following control structure which is faster? 1.Switch 2.If-else and which consumes more memory?
How arrays can be passed to a user defined function
main() { char *p1="Name"; char *p2; p2=(char *)malloc(20); while(*p2++=*p1++); printf("%s\n",p2); }
where are auto variables stored? What are the characteristics of an auto variable?
Find the second largest element in an array with minimum no of comparisons and give the minimum no of comparisons needed on an array of size N to do the same.
What is the purpose of sprintf?
what is the advantage of software development
Develop a program that computes the new price of an item. The program should receive a character variable colour and a double precision floating-point variable price from the user. Discount rate is determined based on the colour of the discount sticker, as shown in the following table. An error message should be printed if an invalid colour has been entered
What is getch() function?
how can i make a program with this kind of output.. Enter a number: 5 0 01 012 0123 01234 012345 01234 0123 012 01 0
Hello. How to write a C program to check and display president party like if i type in the console "biden" and hit enter the output shoud be : "biden is democrat" and if i type "trump" and hit enter the output shoud be: "trump is republican"
write a program to reverse the words in the sentence.NOTE:not reverse the entire string but just the occurance of each word