Can i print int in binary format? [duplicate] - c

This question already has answers here:
Is there a printf converter to print in binary format?
(57 answers)
Closed 2 years ago.
Can I print an integer in binary without additional cycles, conditions, and so on? I know that you can print a number in HEX printf("%x", a) in decimal printf("%d", a) in octal printf("%o", a), but i can't find args for binary. Can you help me?
12 -> 00001100

First of all, no, printf doesn't provide a formater to print a integer in binary.
Second, do not get fooled. Using printf adds additional cycles. Converting an integer to a string (displaying it as decimal, hexadecimal, binary or other) takes some operations. In the case of binary, it's just that you have to do the conversion for printf and provide it the string containing the binary representation.

Related

How can i convert an int to a hexadecimal number and print it as a 3 digit number in c? [duplicate]

This question already has an answer here:
How to print in HEX format the MSBs bits when they're equal to 0
(1 answer)
Closed 2 years ago.
I need to code an int to a hexadecimal number, I know that the int wont take more than 3 hexadecimal digits. And i need to add zeroes to number if needed. Such that 7A ill be printed as 07A.
So i need to write a function that can convert an int to hexadecimal and add zeroes if the number has less than 3 hexadecimal digits.
I tried to make it work but i only found functions that convert an int to a string that represents the converted hexadecimal number.
Thank you.
Since the concept of base-conversion only makes sense in the context of textual representations of a number, I'm assuming you mean "I want to represent an int as a 3-digit hex number, with leading zeroes if it's not at least 3 digits wrong".
This is simple:
int myValue = /*whatever*/;
char strNum[10];
snprintf(strNum, 10, "%.3X", myValue);
strNum will now contain a hex representation of myValue. If it's less than three digits long, it will be padded with zeroes till it has length 3.

Stop printf from padding exponent [duplicate]

This question already has answers here:
How to control the number of exponent digits after 'e' in C printf %e?
(3 answers)
Closed 3 years ago.
When I use this:
printf("%e", 2000.0);
I get output 2.000000e+003 which is correct but very ugly. I then tried like this:
printf("%.1e", 2000.0);
Which outputs 2.0e+003 which is far better. The question is how to remove the 2 padding zeroes in the exponent so that the output is 2.0e+3. Is there a specific printf specifier for that or do I have to write a function manually that analyzes the floating point number and prints it the way I want (I'd hate to have to do that)?
The standard C features for printf do not provide any means of controlling the number of digits in the exponent. If you want a different format, you will have to write your own code or obtain code from somewhere else.
(The %a conversion does use only as many digits in the exponent as needed, but it prints the value with hexadecimal.)

C printf %d incorrect value with leading zeros? [duplicate]

This question already has an answer here:
Strange behavior in C when calculating sum of digits with leading zeroes
(1 answer)
Closed 5 years ago.
The C function printf seems to print different values depending on whether or not leading zeros are present.
I was trying to determine the numerical values for the mode argument in the Linux 'open' system call.
printf("mode:%d\n",S_IRWXU);
printf("mode:%d\n",00700);
both gave me 448, while
printf("mode:%d\n",700); gives me 700, as I would expect from both.
What is going on here?
I am using gcc (Ubuntu 5.4.0-6ubuntu1~16.04.5) 5.4.0 20160609
A numerical constant with one or more leading zeros is an octal constant, while one without a leading zero is a decimal constant and one with a leading 0x is a hexadecimal constant. This holds in any context, whether the value is passed to printf or any other function.
In the case of printf, you're using the %d format specifier which prints the value in decimal. If you pass in an octal constant, you'll see the decimal value of that octal number. In this example, 0700b8 == 7 * 8^2 + 0 * 8^1 + 0 * 8^0 == 7 * 64 == 448b10
If you're dealing with file permissions, those values are typically denoted as octal, so you should always use a leading 0 with those.

Convert dec to hex number in C [duplicate]

This question already has answers here:
How can I convert an integer to a hexadecimal string in C?
(7 answers)
Closed 8 years ago.
I tried to convert dec to hex. For example convert 255 to hex.
opa = 255.ToString("X");
gives me error:
error: invalid suffix "ToString" on floating constant
I spent lots of time to convert, but couldn't find right way.
You're asking about the wrong language. C does not support the dot operator on integers. To do this in C, you need to print it to a string like so.
char numstr[10];
sprintf(numstr, "%X", 255)

C language how to format a double to 2 digits after the decimal point? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Limit floating point precision?
In C language, I need to format a number to 2 digits after the decimal point from user input
For example:
float x ;
printf("Enter number");
Let's suppose the user enters 4.54234635
I need to print and process in the whole program:
4.54
Thanks advance
scanf("%.2f",&x);
And i think that will solve a problem
The complete list
Integer display
%d print as decimal integer
%6d print as decimal integer, at least 6 characters wide
%f print as floating point
%6f print as floating point, at least 6 characters wide
%.2f print as floating point, 2 characters after decimal point
With the required modification (scanf("%.2f",&x); in the last entry) will solve your problem
use
scanf("%.2f",&number);
or
printf("%.2f", number);

Resources