0

scanf can read optimally signed decimal,octal, or hexadecimal input but printf can't output said read input? Or is there some other specifier I'm not aware of and not using.

example code

scanf("%i", &x);     //enter 0150 (octal)                     
printf("%i ",x);     //outputs 104(decimal)
j.yang29
  • 65
  • 9

4 Answers4

2

Yes, there is another specifier. You should read as string and write what you read to write exactly what you read.

char x[100];
scanf("%99s", x);
printf("%s ", x);
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • Nice answer, but there might be some problem with the data type which not actually a numeric. Since it's in array of char, some might found it difficult to operate/manipulate the array of char with other numeric data. Still, it's nice answer. – ichan-akira Feb 24 '16 at 09:32
0

See here, printf doesn't identify "%i" and "%d" separately. But it is identified in scanf. Also please write the question properly next time :)

These links may help you-

  1. Difference between format specifiers %i and %d in printf

  2. Octal to Decimal in C

  3. http://www.tutorialspoint.com/cprogramming/c_type_casting.htm

Community
  • 1
  • 1
Tarang Gupta
  • 139
  • 1
  • 3
  • 13
0

Guess there is no specifier that does what %i can do in scanf, I wasn't sure if there was one or not but ichan confirmed that there isn't one

j.yang29
  • 65
  • 9
-1

You might found this reference is useful to you:

printf
http://www.cplusplus.com/reference/cstdio/printf/ printf

scanf
http://www.cplusplus.com/reference/cstdio/scanf/ scanf


EDIT:
There is no single specifier, that one size fit all. You should know what you will be printing, and use the specific specifier to get the job done.

ichan-akira
  • 443
  • 5
  • 15
  • The input is not known and is determined by the user. But I see, if there is no one size fits all then I am out of luck – j.yang29 Feb 24 '16 at 04:27
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/11383535) – Magisch Feb 24 '16 at 09:02
  • @Magisch I've been added the answer from the link. I'm using image, since Stack Overflow doesn't support html tag.
    – ichan-akira Feb 24 '16 at 09:31