3
#include <stdio.h>

int main()
{
    int answer;
    printf("Please insert your desired budget :"); //normal printf functions.
    printf(" $_____\b\b\b\b"); //This should move the curser back 4 spaces.
    //The program outputs the line followed with 4 inverted question marks.
    scanf("%d", &answer);
    printf("So your budget is %d", answer);
    return 0;
}

How come the output is 4 inverted question marks? I am using xcode on mac, could that the problem?

  • A few things: 1) I would imagine using a floating type for money. 2) you have 5 underscores in the second `printf`. 3) you should do some error checking on the input. 3) You probably want a `\n` on that last print. Otherwise, this looks like it should work. – chrsblck Mar 28 '13 at 17:16
  • 1
    Which version of gcc you are using on Mac? It is running fine on Ubuntu. `printf(" $____\b\b\b\b");` Mistakenly, you have inserted 5 underscores instead of four, anyhow it is working fine on mine. – Arslan Ali Mar 28 '13 at 17:17

2 Answers2

4

You need to run it in a terminal environment that supports \b escape sequences. The console in Xcode must not understand them.

If you run it in the Terminal app, it should be fine.

teppic
  • 8,039
  • 2
  • 24
  • 37
1

There is an example in the book [C Primer Plus] 6th Page.91

list3.10 escape.c

/* escape.c -- uses escape characters */
#include <stdio.h>


 int main(void)
{
     float salary;
     printf("\aEnter your desired monthly salary:");/* 1 */ 
     printf(" $_______\b\b\b\b\b\b\b"); /* 2 */ 
     scanf("%f", &salary);
     printf("\n\t$%.2f a month is $%.2f a year.", salary,salary * 12.0); /* 3 */        
     return 0; 
}

The author says :

The actual behavior could be different. For instance, XCode 4.6 displays the \a, \b, and \r characters as upside down question marks!

They shows Xcode didn't support the \b and the Apple Apple Support Communitiesalso shows the situation.

In my Xcode 5.0,it has the same situation. enter image description here