Studying C at university. This is not homework, but I tried to do something (some “creative” part of the assignment) and got stuck.
I understand that this is possible
printf("%d\n", printf("23.4"));
but how can I use sprintf()as the first argument printf()?
sort of:
char * getFormatString(int n) {
char * buffer;
sprintf(buffer, "Value with %%d decimals is %%.%df", n);
return buffer;
}
void foo() {
int decimals = 2;
float pi = 3.141592;
printf(getFormatString(decimals), decimals, pi);
}
Is it possible? So far, I am getting a seg error while executing it.
source
share