Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7005597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:22:16+00:00 2026-05-27T21:22:16+00:00

I expect this simple line of code printf(foo\b\tbar\n); to replace o with \t and

  • 0

I expect this simple line of code

printf("foo\b\tbar\n");

to replace “o” with “\t” and to produce the following output

fo     bar

(assuming that tab stop occurs every 8 characters).
On the contrary I get

foo    bar

It seems that my shell interprets \b as “move the cursors one position back” and \t as “move cursor to the next tab stop”. Is this behaviour specific to the shell in which I’m running the code? Should I expect different behaviour on different systems?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T21:22:17+00:00Added an answer on May 27, 2026 at 9:22 pm

    Backspace and tab both move the cursor position. Neither is truly a ‘printable’ character.

    Your code says:

    1. print “foo”
    2. move the cursor back one space
    3. move the cursor forward to the next tabstop
    4. output “bar”.

    To get the output you expect, you need printf("foo\b \tbar"). Note the extra ‘space’. That says:

    1. output “foo”
    2. move the cursor back one space
    3. output a ‘ ‘ (this replaces the second ‘o’).
    4. move the cursor forward to the next tabstop
    5. output “bar”.

    Most of the time it is inappropriate to use tabs and backspace for formatting your program output. Learn to use printf() formatting specifiers. Rendering of tabs can vary drastically depending on how the output is viewed.

    This little script shows one way to alter your terminal’s tab rendering. Tested on Ubuntu + gnome-terminal:

    #!/bin/bash
    tabs -8 
    echo -e "\tnormal tabstop"
    for x in `seq 2 10`; do
      tabs $x
      echo -e "\ttabstop=$x"
     done
    
    tabs -8
    echo -e "\tnormal tabstop"
    

    Also see man setterm and regtabs.

    And if you redirect your output or just write to a file, tabs will quite commonly be displayed as fewer than the standard 8 chars, especially in “programming” editors and IDEs.

    So in otherwords:

    printf("%-8s%s", "foo", "bar"); /* this will ALWAYS output "foo     bar" */
    printf("foo\tbar"); /* who knows how this will be rendered */
    

    IMHO, tabs in general are rarely appropriate for anything. An exception might be generating output for a program that requires tab-separated-value input files (similar to comma separated value).

    Backspace '\b' is a different story… it should never be used to create a text file since it will just make a text editor spit out garbage. But it does have many applications in writing interactive command line programs that cannot be accomplished with format strings alone. If you find yourself needing it a lot, check out “ncurses”, which gives you much better control over where your output goes on the terminal screen. And typically, since it’s 2011 and not 1995, a GUI is usually easier to deal with for highly interactive programs. But again, there are exceptions. Like writing a telnet server or console for a new scripting language.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Boy, this one is really weird. I expect the following code to print 1990,
For a simple command-line todo manager (that is to be implemented in C), this
I'd expect this to be fairly routine, but cannot find a simple approach for
My expect code does this: It does a ssh connect to another machine, sends
what behaviour can I expect when I run this code: do while(testA) { //
This code gives me an empty result. I expect it to print out the
I would expect this code to display a border around a table. It does
I was just writing some simple code and I noticed that using document.writeln doesn't
I have some simple function that takes in a value This value is the
I am hoping this is a simple stupid noob mistake that can be fixed

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.