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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T11:45:14+00:00 2026-06-08T11:45:14+00:00

What is the use of width in the printf() statement? Why is the output

  • 0

What is the use of width in the printf() statement? Why is the output 7 in the example below?

Code

int add(int x, int y)
{
    return printf("%*c%*c",  x, '\r',  y, '\r');
}

int main()
{
    printf("Sum = %d", add(3, 4));
    return 0;
}

Output

Sum = 7

  • 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-08T11:45:16+00:00Added an answer on June 8, 2026 at 11:45 am

    printf returns the number of characters output. %*c means print a character and pad to the width specified by an argument. So printf("%*c", 3, '\r') means to print a carriage return with 2 spaces before it. printf("%*c%*c", n, '\r', m, '\r') therefore prints n + m characters – that’s n – 1 spaces to pad the first \r, m – 1 spaces to pad the second \r, and the two carriage returns. That’s why it returns 7.

    A carriage return takes you back to the start of the line, so after printing those n + m characters the next thing printed will appear at the start of the line. That means "Sum = " overwrites the output of the first printf.

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

Sidebar

Related Questions

is there a way to get with printf colored output? #!/usr/bin/perl use warnings; use
When I use printf/printf_s with two strings, I get the same output for both
I have a weird issue with printing data out. I use printf to print
I'd like to use width: calc(100% -100px); which does the job perfectly for what
I wanted to use full width stripe on my footer, but aparently it doesen't
I noticed today that I can't use * to pass width or precision arguments
Can i resize images in python to given height and width,i use python 2.5,
I'm having a document.body is null error in my javascript because I use: $(window).width()
Im trying to create a gallery for my website, so i use max-height/width to
I'm creating an output in Java using printf() to create table headers. One of

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.