This question already has answers here:
Formatted and unformatted input and output and streams
(3 answers)
Closed 9 years ago.
I want to know what is meant by formatted in the printf and scanf functions of the C language. I am new to C programming and did not understand what is meant by formatted. Can anyone please give me the appropriate explanation?
printf("%s %d", "abc", 123);
%s %d is the format string and contains instructions for what to do with the arguments. In this case, it says print string, print a space, then print an integer.
As in formatted output
So you can 'print' say a double called myMoney of 11003.145 as $11,003.15.
Related
This question already has answers here:
snprintf not working as expected with avr-gcc
(2 answers)
Problems with printf() on AVR in C with floating-point
(3 answers)
Closed 10 months ago.
I need to convert a float value to a string in my avr project. Therefore I tried it with the following code:
char buf[10];
float emg1 = 33.42;
sprintf(buf, "%f", emg1);
uart_puts(buf);
But the output is only a "?". I tried to change the char format from %f to %g but I only got "?".
Is there another way to simply convert float to string, or can somebody tell me where the mistake is?
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 4 years ago.
I am reading text file contains values like below
125602365 119653955 126374444 124463807 127312438 128395899
and the below code to read it.
if(!(pvtcheck(0)))
{
fscanf(fp,"%f",&deltime);
printf("\ndeltime0=%f\n",deltime); ///Actual value is 125602365, but i am getting 125602368.
}
if(!(pvtcheck(1)))
{
fscanf(fp,"\t%f",&deltime);
printf("\ndeltime1=%f\n",deltime);///Actual value is 119653955, but i am getting 119653952.
}
/// same for pvtcheck(2),pvtcheck(3),pvtcheck(4)
if(!(pvtcheck(5)))
{
fscanf(fp,"\t%f",&deltime);
printf("\ndeltime5=%f\n",deltime); ///Actual value is 128395899, but i am getting 128395896.
}
In the Comment i have written output , any solution to not change the values
You need to use the double type instead of float, and the %lf format specifier for scanf instead of %f.
double deltime;
fscanf(fp, "%lf", &deltime);
printf("\ndeltime0=%lf\n", deltime);
fscanf(fp, "\t%lf", &deltime);
printf("\ndeltime1=%f\n", deltime);
...
BTW: for printf you can use either %f or %lf they both have the same meaning.
Also read this SO article
This question already has answers here:
function parameter evaluation order
(5 answers)
Closed 5 years ago.
If you have a string as below
str="insert 111,name,123456789"
when you pass it to strtok and try to print the values, they are output in reverse.
For example:
char* token=strtok(str," ");
printf("%s %s %s %s\n",token,strtok(NULL,","),strtok(NULL,","),strtok(NULL,","));
output: insert 1234567 name 1111
instead of:insert 111 name 123456789
Why is this happening? How can this be fixed?
parameters are pushed to the stack in the Calling Convention order, which in your case, reverse... therefore parameter 5 is first to evaluate and pushed in the stack, then 4,3,2 and the format string.
as many comments before suggested, your call style is very discouraged,
and should be avoided.
This question already has answers here:
Turbo C++: Why does printf print expected values, when no variables are passed to it?
(5 answers)
Closed 9 years ago.
I have just tried to know the output without supplying the variable instead just %d and there is no error in compiling the program but i wonder how the output displayed like below.
#include <stdio.h>
int main()
{
printf("%d");
return 0;
}
The output became 7288368
"why formatting input output requires variable to be supplied?"
Because the implementation of printf demands so. From the manual page of printf:
"Each conversion specification is introduced by the character %, and ends with a conversion specifier... The arguments must correspond properly (after type promotion) with the conversion specifier."
You have used "%d" format string, which expects a integral argument appropriate for decimal conversion, yet you haven't provided any arguments, which resulted in an undefined behavior
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
How to escape the % (percent) sign in C's printf
How can I print '%' in C?
E.g.:
printf("A: %.2f%", pc);
...fails, the compiler complains there is an invalid conversion. Of course an easy way is;
printf("A: %.2f%c", pc, '%');
But it's rather inelegant...
I looked on the web, but I didn't find any escape sequence for %. I thought % would work, but it doesn't.
printf("A: %.2f%%", pc);
Just double the '%' in the format string and it will print '%'.
For future printf reference, type:
man 3 printf
on any Linux command prompt. It can do a lot of crazy stuff that most people just aren't aware of.
%% will output % using printf.
Check the example at the end of page here