What is the difference between conversion specifiers %i and %d in formatted IO functions (*printf / *scanf)
They are the same when used for output, e.g. with printf
.
However, these are different when used as input specifier e.g. with scanf
, where %d
scans an integer as a signed decimal number, but %i
defaults to decimal but also allows hexadecimal (if preceded by 0x
) and octal (if preceded by 0
).
So 033
would be 27 with %i
but 33 with %d
.
What's the difference between printf(%i) and printf(%d)
There is no difference.
From the C99 standard document, section 7.19.6.1:
d, i
The int argument is converted to signed decimal in the style [−]dddd.
The precision specifies the minimum number of digits to appear; if the
value being converted can be represented in fewer digits, it is
expanded with leading zeros. The default precision is 1. The result of
converting a zero value with a precision of zero is no characters
problems with scanf and conversion specifiers
These are the very basics of C Programming, and I strongly advise you to get a decent book - The C Programming Language by Dennis Ritchie would be a good start.
There are numerous errors in your code.
A
char
can contain only one character, like 'A', or 'a' or something like that. When you're scanning a name, it is going to be a group of characters, like 'E', 'd', 'd', 'y'. To store multiple characters, you need to use a character array. Also, the format specifier used to scan/print characters is%c
,%s
is for when you need to scan a group of characters, also called a string into an array.When you use
printf
, you do not supply a pointer to the variable you are trying to print (&x
is a pointer to variablex
). The pointer is a 32/64-bit integer, which is likely why you see a random integer when trying to print.printf("%c\n", charVar)
is sufficient.scanf
does not need an&
while using%s
as the format specifier, assuming you have passed a character array as the argument. The reason is,scanf
needs to know where to store the data you are reading from the input - and that is given by a pointer to the memory location. When you need to scan an integer, you need to pass an&x
- which means, pointer to memory location of x. But when you pass a character array, it is already in the form of a memory address, and doesn't need to be preceded by an ampersand.
I once again recommend you look up some decent tutorials online, or get a book (the one I mentioned above is a classic). Type the examples as given in the material. Experiment. Have fun. :)
What is the difference between %i and %d in Python?
Python copied the C formatting instructions.
For output, %i
and %d
are the exact same thing, both in Python and in C.
The difference lies in what these do when you use them to parse input, in C by using the scanf()
function. See Difference between format specifiers %i and %d in printf.
Python doesn't have a scanf
equivalent, but the Python string formatting operations retained the two options to remain compatible with C.
The new str.format()
and format()
format specification mini-language dropped support for i
and stuck with d
only.
Which one is prefered %i or %d
They are the same. Use whichever one you like. One stands for "integer" and the other stands for "decimal".
At a guess, there are two from early, conflicting implementations. And I'm talking like half a century ago.
I use %d
and, to be honest, I haven't the faintest idea why.
They mean different things for scanf
, though!
How can I read the scanf without changing the printf positions?
I am not familiar with the "getNumber" function, but here is one possible method of using the "scanf" function to input the two integers and a one-character operator (e.g. "+", "-", "*", or "/") following is an updated version of your program code for your consideration and experimentation.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
Block zum Kopieren:
printf(
"\n|----------------------------------------------------------|"
"\n| Bitoperatoren-Rechner |"
"\n| |"
"\n| Eingabe Zahl 1: |"
"\n| Operator: |"
"\n| Eingabe Zahl 2: |"
"\n| |"
"\n|----------------------------------------------------------|"
"\n| |"
"\n| | dez. | okt. | hex. | Binaerdarstellung |"
"\n| Zahl 1 | | | | |"
"\n| Operator | | | | |"
"\n| Zahl 2 | | | | |"
"\n| -------------------------------------------------------- |"
"\n| Ergebnis | | | | |"
"\n| |"
"\n|----------------------------------------------------------|");
*/
void print1(short Zahl1, short Zahl2, char* Oper)
{
char Zahl_okt1[6], Zahl_okt2[6], Zahl_hex1[16], Zahl_hex2[16], Er_okt[6], Er_hex[16];
int Ergebnis = 0;
if ((strcmp(Oper, "+")) == 0)
Ergebnis = Zahl1 + Zahl2;
if ((strcmp(Oper, "-")) == 0)
Ergebnis = Zahl1 - Zahl2;
if ((strcmp(Oper, "*")) == 0)
Ergebnis = Zahl1 * Zahl2;
if ((strcmp(Oper, "/")) == 0)
Ergebnis = Zahl1 / Zahl2; /* This will be an integer result as is */
sprintf(Zahl_okt1, "%o", Zahl1); /* To get the octal equivalent */
sprintf(Zahl_okt2, "%o", Zahl2);
sprintf(Er_okt, "%o", Ergebnis);
sprintf(Zahl_hex1, "%02x", Zahl1); /* To get the hexadecimal equivalent */
sprintf(Zahl_hex2, "%02x", Zahl2);
sprintf(Er_hex, "%08x", Ergebnis);
/* You will probably need to search for a good decimal to binary conversion function. */
/* I did not spend any time looking for one.*/
printf(
"\n|----------------------------------------------------------|"
"\n| Bitoperatoren-Rechner |"
"\n| |"
"\n| Eingabe Zahl 1: %8d |"
"\n| Operator......: %s |"
"\n| Eingabe Zahl 2: %8d |"
"\n| |"
"\n|----------------------------------------------------------|"
"\n| |"
"\n| | dez. | okt. | hex. | Binaerdarstellung |"
"\n| Zahl 1 |%8d |%8s|%8s| |"
"\n| Operator | %s | | | |"
"\n| Zahl 2 |%8d |%8s|%8s| |"
"\n| ---------------------------------------------------------|"
"\n| Ergebnis |%8d |%8s|%8s| |"
"\n| |"
"\n|----------------------------------------------------------|\n\n",
Zahl1, Oper, Zahl2, Zahl1, Zahl_okt1, Zahl_hex1, Oper, Zahl2, Zahl_okt2, Zahl_hex2, Ergebnis, Er_okt, Er_hex);
}
int main()
{
short Zahl1;
short Zahl2;
char Operator[16];
printf("Geben Sie Variable 1 ein: "); /* Sorry if my German is rusty */
scanf("%hu", &Zahl1);
printf("\nGeben Sie Variable 2 ein: ");
scanf("%hu", &Zahl2);
printf("\nGeben Sie den Betreiber ein ( + - * /): ");
scanf("%s", Operator);
printf("\n\n"); /* Just to keep things looking clean */
print1(Zahl1, Zahl2, Operator);
return 0;
}
In testing this code out here is a sample of the input and output on my terminal (I requested 55 * 84).
Geben Sie Variable 1 ein: 55
Geben Sie Variable 2 ein: 84
Geben Sie den Betreiber ein ( + - * /): *
|----------------------------------------------------------|
| Bitoperatoren-Rechner |
| |
| Eingabe Zahl 1: 55 |
| Operator......: * |
| Eingabe Zahl 2: 84 |
| |
|----------------------------------------------------------|
| |
| | dez. | okt. | hex. | Binaerdarstellung |
| Zahl 1 | 55 | 67| 37| |
| Operator | * | | | |
| Zahl 2 | 84 | 124| 54| |
| ---------------------------------------------------------|
| Ergebnis | 4620 | 11014|0000120c| |
| |
|----------------------------------------------------------|
When using the "scanf" function, it is usually proceeded by a "printf" function that prompts the user as to what type of data should be entered. Also, I included the "<string.h>" file reference in order to utilize various string functionality such as the "sprintf" function.
I hope that provides you with some ideas and helps you progress.
Regards.
Are x and u printf/scanf conversion specifiers equally applicable to the same types?
Is it true than no matter if it ends in
x
or inu
with the same width specifier in front I can pass values of the same time and it'll always be legal?
Yes, if a width specifier is valid for use with one of those then it is valid for use with both, and also with o
and X
, and the resulting directives serve the same data type -- an unsigned integer of the specified width. The only difference is the way the output is formatted (or for scanf
, the format that is expected of the input).
There's a whole bunch of width specifiers (
h
,hh
,L
,I
, etc) and looks
like each of them can be used with both u and x conversion specifier
but each time I'm allowed to pass values of unsigned type of the right
width.
There indeed is a wide variety of width specifiers. Most, but not all, of them are applicable to and intended for integer types, both the unsigned ones corresponding to x
, X
, o
, and u
and the signed ones corresponding to d
and i
. The standard does not specify L
among those, however, and it does not specify I
at all. Some implementations might nevertheless accept those as integer width specifiers, with implementation-specific behavior.
Related Topics
Deprecated Header ≪Codecvt≫ Replacement
When Were the 'And' and 'Or' Alternative Tokens Introduced in C++
How to Open an Std::Fstream (Ofstream or Ifstream) With a Unicode Filename
Using Stdlib'S Rand() from Multiple Threads
Why Will Std::Sort Crash If the Comparison Function Is Not as Operator ≪
Smart Pointers (Boost) Explained
Std::Vector Versus Std::Array in C++
What Is the Purpose of Std::Launder
Why Are C++ Inline Functions in the Header
Difference Between Conversion Specifiers %I and %D in Formatted Io Functions (*Printf/*Scanf)
Why Is a Pure Virtual Function Initialized by 0
Will Using Goto Leak Variables
How Is Std::Function Implemented
C++ Function Template Partial Specialization
C++: Print Out Enum Value as Text
Order of Calling Constructors/Destructors in Inheritance