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 8606621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:08:26+00:00 2026-06-12T03:08:26+00:00

This is my C code, compiled with gcc. #include<stdio.h> int main() { int a=1;

  • 0

This is my C code, compiled with gcc.

#include<stdio.h>

int main() 
{ 
    int a=1; 
    switch(a)
    {
       int x=10;
       case 1:
           printf("%d\n",printf("%d\b",x));
           break;
       default:
           printf("%d\n",printf("%d\b",x));
    }
    return 0;
}

printf() is supposed to return the number of elements it printed successfully.
printf("%d\b", x) should have printed 10 by itself(since the \b takes the printing pointer one step behind (to the digit 0 in 10) and there is nothing to print after that.
So it should have just printed 10. That is 2 characters. Now the outer printf would display 2.
The output should have been 102.
The output I actually see is 2.

And in case of nested printfs is the printing pointer position remembered? I mean, if there is a \b in the inside printf , it would take the printing pointer one step behind. And when the control now goes to the outer printf, is that changed position remembered? Will it overwrite over that last character?

  • 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-06-12T03:08:28+00:00Added an answer on June 12, 2026 at 3:08 am
    printf("%d\b",x)
    

    prints the characters '1', '0' (because x==10) and \b. The \b is a backspace character; if you print to a terminal, it will print 10 and then move the cursor back one column.

    A call to printf returns the number of characters it printed; in this case, the result is 3 (yes, '\b' counts as a character).

    printf("%d\n",printf("%d\b",x));
    

    The inner printf call works as I explained above, and returns 3. The outer printf call prints "3\n".

    So the entire statement will print:

    10\b3\n
    

    The '\b' causes the 3 to be replace the 0 on the screen, so the final displayed result (when I run the program on my system) is:

    13
    

    If I pipe the output through cat -v, I get:

    10^H3
    

    where ^H represents the backspace character.

    EDIT :

    The question was just edited, and the modified program’s behavior is quite different. The switch statement causes control to jump past the declaration int x = 10;, but into the scope in which x is declared. As a result, x is uninitialized when printf is called. This causes undefined behavior, and most likely garbage output (I just got -1217572876^H12). If x happens to be 0, I suppose you’d get 0^H2, which would look like 2.

    Whatever you’re trying to do, please find a better way to do it.

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

Sidebar

Related Questions

Given this C code compiled with gcc 4.3.3 #include <stdio.h> #include <stdlib.h> int main(int
I was trying to execute this code through gcc compiler: #include <stdio.h> int main()
Compiling this code using Emscripten: #include <stdio.h> int main() { unsigned long d1 =
I came across this code accidentally: #include<stdio.h> int main() { int i; int array[3];
This code compiles and produce output like I would expect. #include <stdio.h> int* ar[];
Here's a simple C file: #include <stdio.h> #include <stdlib.h> int main() { printf(hi there!\n);
I have created this code: #include <stdio.h> typedef unsigned int uint; uint in[2]={1,2},out[2]={3,4}; int
This code compiles fine with GCC and Clang but not with MSVC 2010: #include
This is the code #include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h>
#include <stdio.h> #include <unistd.h> #include <string.h> int good(int addr) { printf(Address of hmm: %p\n,

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.