Click here to Skip to main content
15,891,653 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
The reference book shows the answer is
h(g) = 3.1415e-005
count = 18

but my answer is
h(g) = 3.1415e-005count = 0

#include <stdio.h>
int main()
{
	int count, a = 2046;
	short b = 234;
	long c = 5555;
	unsigned int d = 1234;
	char e = 'z';
	float f = 3.14159f;
	double g = 3.1415926535898;
	double h = 3.1415e-5;
	
	printf("a(d) = %d\n", a);
	printf("a(o) = %o\n", a);
	printf("a(x) = %x\n", a);
	printf("b(d) = %d c(d) = %d\n", b, c);
	printf("d(u) = %u\n", d);
	
	printf("e(c) = %c\n", e);
	
	printf("f(f) = %f\n", f);
	printf("g(f) = %f\n", g);
	printf("g(e) = %e\n", g);
	printf("g(g) = %g\n", g);
	printf("h(f) = %f\n", h);
	printf("h(e) = %e\n", h);
	printf("12345678901234567890\n");
	printf("h(g) = %g%n\n", h, &count);
	printf("count = %d\n", count);
	printf("b(d) = %d f(f) = %f\n", b, f);
	
	return 0;
}


What I have tried:

remove %n and & in
printf("h(g) = %g%n\n", h, &count);
Posted
Updated 22-Jun-17 21:24pm

Nowhere do you assign a value to count, so it remains at the fortuitously initialised vaue of zero.
 
Share this answer
 
Comments
barneyman 22-Jun-17 23:20pm    
yeah - i saw that too - but %n is doc'd as returning 'chars printf'd so far' - TIL !
Peter_in_2780 23-Jun-17 0:46am    
Hmmm. I missed that. Maybe OP has a non-conforming implementation, but I think that rather unlikely.
Mayvis 39 22-Jun-17 23:33pm    
First of all, thank you for your reply. Can I ask one more question about how can I show h(g) char in %n format by using &count?
Peter_in_2780 23-Jun-17 0:58am    
According to my reading of the standard, it should produce something like "count = 11" (11 characters in what it printed for h(g) ). Maybe it is a bug in your compiler/runtime library. What are you using?
Mayvis 39 23-Jun-17 4:32am    
I got the answer below. Btw, I will try your suggestion. Thank you.
The program
C
#include <stdio.h>
int main()
{
  double h = 3.1415e-5;
  int count;

  printf("h(g) = %g%n\n", h, &count);
  printf("count = %d\n", count);
  return 0;
}


Outputs
h(g) = 3.1415e-05
count = 17


On my Linux box (gcc 5.4.0).
 
Share this answer
 
Comments
Mayvis 39 23-Jun-17 4:29am    
Thank you.
Mayvis 39 23-Jun-17 4:46am    
Maybe IDE causes this problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900