FalloutRanger FalloutRanger - 3 months ago 40
C Question

Input format using scanf

I can't solve this problem found in a C textbook:

Input data using the following scanf function, so that a=10, b=20, c1='A', c2='a', x=1.5, y=-3.75, z= 67.8. What do you type on the keyboard?


Basically it's a question about the input format in C. I tried typing the following, but it didn't work:


where 凵 represent a space. When I use printf to output the variables, it gives the following error message:

Unhandled exception at 0x000007FEEC8243CE (msvcr120d.dll) in Project1.exe: 0xC0000005: Access violation writing location 0x0000000ACCCCCCCC.

Can anyone help me, please?

Ben Ben

Your input doesn't need spaces, and needs a little adjustment:

Lets go through it:

%5d 00010  
%5d 00020
%c  A
%c  a
%f  1.5
%f  -3.75 // you got all of these
%*f -1.5   // Here *f means that the input will be ignored, 
          // but you still need a way to separate the number 
          // before and this one. A + or - will suffice.
,%f ,67.8 // Easy.

Then put it all together, no space:


Here is a live example.