How to format a float number to the right in C? - 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);

Related

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

#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.

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')

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 increase the potential value of an variable in a 16 bit machine using C

I have created a binary to decimal converter but even small numbers written in binary have many digits and thus are too large to be held by an integer variable on a 16 bit machine. Is there any way around this. The program is in C. Here is the code, thanks:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
clrscr();
int a,b,d=0,x=1;
int check(int y);
printf("Enter your number in Binary:");
scanf("%d",&a);
if(check(a)==0)
{
printf("Number tolerable. Conversion Process Initiated.");
}
else
{
printf("Number not binary. Try again.");
exit(1);
}
while(a!=0)
{
if(a%10==1)
{
d=d+x;
}
a=a/10;
x=x*2;
}
printf("\nDecimal:%d",d);
getch();
}
int check(int y)
{
while(y!=0)
{
if(y%10!=0&&y%10!=1)
{
return 1;
}
else
{
y=y/10;
}
}
return 0;
}
A simple way would be to store binary number into a character array.And to convert it into number see following steps-
1.Loop starting from i=n-1 to i>=0 ( array of size n).
2.Check if character at index i is 0 or 1.
Recognize 0 and 1 as follows-
3.If 0 then digit is 0.
4.If 1 then digit will be equal to 2^i (i being the index).
5.Last step would be add them.
Else use an integer array.
The %d format specifier to scanf expects a number to be entered in decimal format. There is no binary format specifier, so you have to read in the number as a string and parse through it.
For example, if the user enters "101011", you need to recognize this as 2^5 + 2^3 + 2^1 + 2^0 and add those value to the resulting number (to be clear, I'm using ^ to denote exponentiation instead of bitwise xor).
there's no real need to store the binary digits.
#include <string.h>
int n=0,c;
printf("Enter your number in Binary:");
while(strchr("01",c=getch()))
n+=n+c-'0';

why my program isn't showing correct result?

I wrote the following code for
this
Given an integer number, Write a C program that displays the number as follows:
First line : all digits
Second line : all digits except first digit
Third line: all except first two digits
last line : the last digit
For eg.,
the number 5678 will be displayed as:
5 6 7 8
6 7 8
7 8
8
=>
#include<stdio.h>
#include<math.h>
main()
{
long int x,y,n,z,i=1;
printf("enter no. of digits=");
scanf("%d",&n);
printf("x=");
scanf("%d",&x);
while(i<=n)
{
y=x/pow(10,i);
z=y*pow(10,i);
printf("%d\n",(x-z));
i++;
}
}
The code works(if we ignore the formatting) but does some rounding of and stuff fr some output values ...don't know why??
There are solutions using array and all...but whts wrong with this one??
I'm assuming by "some values" with problems, you're referring to values that have zeroes in them, such as input like 50345, which will print:
50345
345
345
45
5
rather than:
50345
0345
345
45
5
The problem is that the integer representation of numerical values does not acknowledge leading zeroes as being a unique integer value.
If you must print the values, including leading zeroes, you're going to have to treat your number like a token or string, meaning that a value that has leading zeroes is a unique string value from the version without leading zeroes. This is why the array-versions, which treat the numeral value like a string, work, and your current version does not when presented with this type of input case.
The rounding off you are seeing could be because of the use of int as data type for all the variables. So something like (5/10) would be rounded to 0 and not 0.5.
#include<stdio.h>
#include<math.h>
main()
{
int num, count=0, x,no;
printf("Enter a number\n");
scanf("%d",&num);
no=num;
printf("The number you entered is:\n %d\n",num);
while(num){
num=num/10;
count++;
}
for(;count>1;count--){
x=pow(10,count-1);
printf("%d\n",no%x);
}
}
#include <stdio.h>
#include<conio.h>
void main()
{
int number,i=0;
int digits[8];
scanf("%d"&number);
while(number!=0)
{
digits[i]=numder%10;
number=number/10;
i++
}
for(i=i-1;i>=0;i--){
for(j=i;j>=0;j--){
printf("%d ",digits[j]);
}
printf("\n"):
}
}

Resources