what is meaning of %5d%*.*f in c language - c

#include <stdio.h>
int main()
{
int val =10;
float val1 = 56.40;
printf("%d%*.*f", val,val,val-8,val1);
}
output is 10 56.40
what is actual menaing of %5d%*.*f
#include <stdio.h>
int main()
{
int val =10;
float val1 = 56.40;
printf("%d%f", val,val,val-8,val1);
}
output is 100.000000
why output is getting different by just outting %d%f ?
Code containing %d%f code, which giving output as 100.0000
Code containing %d%*.*f code, which giving output as 10 56.40

%5d means print an int with a minimum field width of 5. The the value isn't 5 characters wide it will be left padded with space. You either can't tell or you didn't paste in the spaces in your example output.
%*.*f means print a float with a minimum field width and precision (digits after the .) provided as arguments as signified by the *. In this case field width is val and precision val-8.
Your 2nd code sample is simply incorrect usage. You claim to pass a int and float but pass in 3 int and a float.

Related

getting different output from different compilers

The question was:
Define a function getint(), which would receive a numeric string from keyboard, convert it to an integer number and return the integer to the calling function.
My code is:
#include<stdio.h>
#include<math.h>
#include<string.h>
int getint();
int main()
{
int a;
a = getint();
printf("you entered %d",a);
return 0;
}
int getint()
{
char str[10];
printf("Enter number: ");
gets(str);
int d=0,len = strlen(str),r = len-1;
for(int i=0;str[i] != '\0';i++,r--)
d += (str[i]-48)*pow(10,r);
return d;
}
while I run this program from sublime text or code block the output was coming wrong
output(from sublime and codeblocks):
Enter number: 123
you entered 122
But when I used onlinegdb.com/online_c_compiler the output was coming correct
So how can output differ from compiler to compiler for the same program
The pow function works with floating point numbers. As such, the result may not be exactly equal to what you expect the numerical result to be. For example, pow(10,2) could output a number slightly larger or slightly smaller than 100. If the result is just below 100, the fractional part gets truncated and you're left with 99.
Rather than using pow for an integer power, just multiply the current value of d by 10 before adding the value for the next digit.
d = 10 * d + (str[i]-'0')

Expected Expression with square roots

#define PI = 3.141593
#define G = 6.67259E-11
#define g = 9.80665
#define M = 5.972E+24
#define r = 6378000
#define h = 220
#include <stdio.h>
#include <math.h>
int main(void)
{
int value;
value =sqrt((G/M)/(r+h))
printf("This is the tangential speed:") value;
return 0;
}
I am very new to coding, and my program is giving me several errors in code blocks, can anyone give me some guidance?
Remove the = from all the #define statements. They are preprocessor macro definitions, not assignment statements, and they do not use equal signs.
Change int value to double value, to use floating-point instead of integers.
Add a ; after value =sqrt((G/M)/(r+h)). Statements in C generally end with a semicolon.
Change printf("This is the tangential speed:") value; to printf("This is the tangential speed: %g.\n", value);. printf is a function call, not a statement, so you pass everything it needs inside a set of parentheses. The string is a format string; it contains both literal text you want printed and conversion specifications like %g that tell it to convert an argument to a string. %g tells it to convert a double argument to a general floating-point display form.
I see two problems:
Smallest problem, but might be significant, is that I assume you want value to be a float or double instead of an int, thus replace
int value;
by
float value;
The print statement is incorrect:
printf("This is the tangential speed:") value;
Assuming value is a float, change it to
printf("This is the tangential speed: %f\n", value);
\n makes a new line.
And of course don't forget the remark by chux.

print float up to four place on either side

I am newbie, I am trying to print an float value upto four place on either side. For example 11.3 will be 0011.3000. For this I am using following line:-
float t = 11.3;
printf("%4.4f", t);
But I am getting 11.3000. So, is it even possible what i am trying to do? If yes then how? Thanks
The type of f is incorrect in your code fragment, it should be float or double.
To produce leading zeroes, use the 0 printf modifier and specify the minimum width expected, 9 characters in your example (4 places before the . plus the . plus 4 more places after the .).
The format is therefore %09.4f.
Here is the code:
#include <stdio.h>
int main() {
double t = 11.3;
printf("%09.4f\n", t);
return 0;
}
Output:
0001.3000
Note however that if the number is negative, you will only get 3 places before the . and if the number is too large in absolute value (<= -999.99995 or >= 9999.99995), the output will have more than 9 characters. If you mean to have 4 places before the . for negative values too, you should use % 010.4f: the number will then be prefixed with a space if positive and a - if negative:
#include <stdio.h>
int main() {
double t = 11.3;
printf("%09.4f\n", t);
printf("% 010.4f\n", t);
printf("% 010.4f\n", -t);
return 0;
}
Output:
0011.3000
0011.3000
-0011.3000
float t = 11.3;
printf("%4.4f", t);
In your code above 4 before . means total number of characters to be printed.
and 4 after . mean number of characters after decimal.
Since you want xxxx.xxxx hence you should write it like:
printf("%9.4f", t);
The first part of the format spec is the width of the field you are printing in, not the number of digits before the decimal place. Your desired output has 9 characters in it, zero padded on the left, so do
float t = 11.3;
printf("%09.4f", t);
This might break down if for example the integer part of your number gets too big For a finer level of control, work with integers:
float t = 11.3;
int i, f;
i = (int)t;
f = (int)((t - i) * 10000 + 0.5);
printf("%04d.%04d", i, f);
All this assumes positive numbers. Neither example shown here will work properly with negatives.

How to format a float number to the right in C?

I'd like to print a float number with only 2 decimal places and align it to the right in a 6 characters space.
I tried to do this, but didn't work:
printf("%6.2f", value);
What you've posted will fit the whole float into a 6 char wide column with the .xx taking up the last 3. If you want the integer portion in a six char wide column with the '.' and the fractional portion after these 6 characters, its %9.2f. Quick example program to show the differences
#include <stdio.h>
int main(void) {
float x = 83.4;
printf("....|....|....|\n");
printf("%6.2f\n", x); // prints " 83.40"
printf("%9.2f\n", x); // prints " 83.40"
return 0;
}
And the output:
....|....|....|
83.40
83.40
You have to put - after %
printf("%-6.2f", value);

What is the use of * in printf?

I have a this code:
#include <stdio.h>
#include <conio.h>
void main()
{
int n = 5;
clrscr();
printf("n=%*d", n);
getch();
}
The output which I got is: n= 5. Why is there a space? How is it generated? What is the use of * in the code?
When in doubt, read the docs:
*:
The width is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.
However, you appear to be using it wrong. The proper way to use it would be like this:
printf("n=%*d", 2, n);
It is clearly mentioned in the C Manual.
The answer is already given by Richard J. Ross III. Just quoting again what is said from the manual.
The width is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.
Consider this code:
#include<stdio.h>
main()
{
int a,b;
float c,d;
a = 15;
b = a / 2;
printf("%d\n",b);
printf("%3d\n",b);
printf("%03d\n",b);
c = 15.3;
d = c / 3;
printf("%3.2f\n",d);
}
The output would be:
7
7
007
5.10
You can see here, how the printf function can be used for formatting output. Hope it helps. :)
With this *, you can set the width of your print with a variable.

Resources