f = va_arg(argp, double) returns 0

Sorry guys, probably it's not directly related to Arduino, but to avr-gcc.

I'm trying to write custom printf function.

f = va_arg(argp, double)

returns 0.

Tried both on device(ATMega328P) and Atmel Studio 6 simulator, same result.

Sounds like something to do with linker options?

I'm trying to write custom printf function...returns 0.

Nothing in that snippet proves that there is any kind of problem.

I've tried to isolate the problem into separate program, but now Atmel Studio won't let me put breakpoint to see the value of f.
Here is the isolated code:

#include <avr/io.h>
#include <stdlib.h>
#include <stdarg.h>

void debug_print(const char *fmt, ...)
{
	const char *p;
	va_list argp;
	double f;
	char *s;
	char fmtbuf[80];
		
	va_start(argp, fmt);
	for(p = fmt; *p != '\0'; p++) 
	{
		if(*p != '%')
			continue;
		switch(*++p)
		{
			case 'f':
				f = va_arg(argp, double);
				s = dtostrf(f, 10, 6, fmtbuf);
				break;
		}			
	}		
	va_end(argp);
}	

int main(void)
{
	debug_print("%f", 123.456);
    while(1)
	{
    }
}

Do you know why Atmel Studio declines breakpoint? Device is set correct as in real project file, where breakpoints works just fine.

In that code, you may be assigning the correct value to f (it looks like you are), but I don't see you actually using that value.

Turns out to be optimization issue.

When I set compiler optimization to O1, then I got 0. When I set it to None, then I got correct result. Now I will update the code so you can see the usage and optimization will let simulator work.

With optimization -O1 you will see the problem, debugger said f optimized out and without optimization it works as expected.

So what exactly I have to do in order to avoid f optimizing out?

So what exactly I have to do in order to avoid f optimizing out?

Use the value. Presumably, you want to print it. So, print it.

Solved. Debugger is showing cached value (0) but actually it contains correct data.