Thread: Assign this to a variable

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    3

    Assign this to a variable

    Hi I'm new in C, from this url, how do I assign the value to a variable from this printf result.

    Code:
    printf("The substring is: %.*s\n", j - i, &ch[i]);
    Thanks!

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Maybe like this:
    Code:
    int j;
    j=printf("The substring is: %.*s\n", j - i, &ch[i]);
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    printf() returns the number of bytes it prints, unless there is an error. I doubt that's what you want to assign.

    Looks like ch[i] is the base address of the string you want. Why not strcpy() it into some char array that you want it to go into? Lots of ways to do this.

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Use sprintf() instead:
    Code:
    int i, j;
    char str[100];
    sprintf( str, "The substring is: %.*s\n", j - i, &ch[i] );
    Although, you're missing a "%d" for the j-i part.
    Last edited by cpjust; 11-25-2009 at 08:11 AM.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  5. #5
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by cpjust View Post
    Use sprintf() instead:
    Code:
    int i, j;
    char str[100];
    sprintf( str, "The substring is: %.*s\n", j - i, &ch[i] );
    Although, you're missing a "%d" for the j-i part.
    The '*' will be replaced by the 'j-i' expression. For eg. j=5 and i=2, then the printf will be treated as %.3s.
    So, there's no need of the %d.
    Another eg.
    Code:
    float i=9.76,j=5,k=2;
    printf("%*.*f",j,k,i); // %5.2f
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by BEN10 View Post
    The '*' will be replaced by the 'j-i' expression. For eg. j=5 and i=2, then the printf will be treated as %.3s.
    So, there's no need of the %d.
    Another eg.
    Code:
    float i=9.76,j=5,k=2;
    printf("%*.*f",j,k,i); // %5.2f
    Ah OK, good to know. I never use those fancy format specifiers...
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Static Local Variable vs. Global Variable
    By arpsmack in forum C Programming
    Replies: 7
    Last Post: 08-21-2008, 03:35 AM
  2. variable being reset
    By FoodDude in forum C++ Programming
    Replies: 1
    Last Post: 09-15-2005, 12:30 PM
  3. Replies: 12
    Last Post: 10-14-2003, 10:17 AM
  4. float/double variable storage and precision
    By cjschw in forum C++ Programming
    Replies: 4
    Last Post: 07-28-2003, 06:23 PM
  5. The syntax to declaire a variable in a function.
    By loopy in forum C Programming
    Replies: 9
    Last Post: 05-25-2002, 06:17 AM