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

  • Home
  • SEARCH
  • 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 6704359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:16:46+00:00 2026-05-26T07:16:46+00:00

I just stumbled upon the idea that since printf returns the no.of characters it

  • 0

I just stumbled upon the idea that since printf returns the no.of characters it has printed to the output stream why not use it to find length of any variable in c?

The code goes something like this,

#include<stdio.h>
int main()
{
 char *a="Length";
 int i=1000;
 printf("Size: %d\n",printf("%d\n",i)-1);
 printf("String Size: %d",printf("%s\n",a)-1);
 return 1;
}

Am I right? I am not concerned about where it is being used. just wanted to know if my understanding is right.

  • 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-26T07:16:47+00:00Added an answer on May 26, 2026 at 7:16 am

    What do you mean by “length of any variable”? You mean the number of bytes used to store the variable (which better achieved with the sizeof and strlen functions) or the byte length of the string representation of the variables?

    For the latter one, you should be careful, since you can use format options to actually modify the results. Consider the following example:

    float num = 10.0f;
    printf("%.10f", num); // return value should be 13
    printf("%f", num);    // return value depends on the implementation of printf and the float value supplied
    

    Also you have to consider that there are other representations than the decimal one.

    As others have already stated, printf has the side effect that it actually writes to stdout. If you do not want this, you can use snprintf to write to a buffer and not to stdout:

    char buffer[256];
    int x = 10;
    snprintf(buffer, sizeof(buffer), "%d", x); // return value is '2'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just stumbled upon propertyGrid and its awesome! However, i have one task that i
Lately I've stumbled upon an error in a lib that used to work just
I just stumbled upon the following article: http://www.josscrowcroft.com/2011/code/utf-8-multibyte-characters-in-url-parameters-%E2%9C%93/ The article talks about using UTF-8
I stumbled upon some code the other day that was making use of query
I've just began to use the GAE and I have stumbled upon an issue
At work, we stumbled upon Bugzilla creating HTML output that led to lines much
I just stumbled upon something i don't quite understand. I know that variables in
I just stumbled upon something in ORACLE SQL (not sure if it's in others),
I just stumbled upon a change that seems to have counterintuitive performance ramifications. Can
I just started to use SVN with Xcode and stumbled upon several problems. I

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.