Thread: printing last 2 characters of a long/string

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    2

    printing last 2 characters of a long/string

    Hi

    Hope you can assist with a very basic C question. Im trying to print the last two characters of a long obtained from a table. lets say the value is 130. How do i print just the 30.

    This is the piece of code:

    sprintf(unidades_standar,"%ld:%02ld",standar/60,standar%60)

    So in the section after the ':' where the %02ld is, i need this to print the last two decimal values. Hope it makes sense

    Cheers

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    If you want to print out the last two char's in a string of numbers:

    Code:
    char n1, n2;
    char str[] = {"130"];
    n1 = str[strlen(str)-1];  //if you have removed the newline char from the string 
    
    n1 = str[strlen(str)-2];  //if you have not removed the newline char from the string str.
    
    //same thing for n2, just subtract one more than you did for n1 to be correct
    
    // if it's in a number, then: 
    
    int n1, n2, number = 130;
    
    n1 = number % 10;  //gets the right most digit from "number"  (the 0)
    number /= 10;  //divide number by 10
    n2 = number % 10;  //gets the new right most digit from "number" (the 3)

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Or you could simply display modulo 100 of the integer...

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    2
    Thanks

    That helps a lot...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-16-2009, 06:00 AM
  2. Replies: 8
    Last Post: 12-06-2008, 02:43 PM
  3. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  4. printing non-ASCII characters (in unicode)
    By dbaryl in forum C Programming
    Replies: 1
    Last Post: 10-25-2002, 01:00 PM
  5. Printing extended ASCII characters
    By Jonny M in forum C Programming
    Replies: 2
    Last Post: 03-01-2002, 10:12 AM