Thread: string conversion strtol function

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    82

    string conversion strtol function

    Code:
    int main() {
    	const char *str="-1234567abc";
    	char *PTRremainder;
    	long x;
    	x = strtol(str, &PTRremainder, 0);
    	printf("The string \"%s\" to long is: long %ld and string is %s, x+3 is %ld",str,x,PTRremainder,x+3);
    	_getch(); 
    	return 0; 
    }
    it says that if i write NULL instead of &PTRremainder, the remainder string will be ignored. what does ignored actaully mean? and to "prove" that it's been ignored? like doesn't print anything or something. and what does the '0' argument mean? well it says it's the base in which the long number is printed, so what does 0 mean? i tried to write 2 instead of 0 (to print the number in 0s and 1s) but it printed "-1" as number and "234567abc" as the remainder string. how do i understand this?

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    it says that if i write NULL instead of &PTRremainder, the remainder string will be ignored. what does ignored actaully mean? and to "prove" that it's been ignored?
    Another way to put it is that the argument is not used. By passing in NULL you are saying that you don't care where the conversion stopped, you just want a number.

    Normally, when you pass in a working pointer, you are saying that you want to check it later to see where the conversion stopped. For example, puts(PTRremainder); is abc.

    well it says it's the base in which the long number is printed, so what does 0 mean?
    The 3rd argument is the base number. Basically, you are telling strtol what base the string is written in. For example there are base 10 numbers like -1234567 and hex numbers like 0x12D687. There are many useful base number systems in the world. When you say 0, you are asking strtol to figure out what base the string is written in for you. Most of the time, it will interpret the number in base 10, unless the string is prefixed with "0x", indicating base 16, or a "0", indicating base 8.
    i tried to write 2 instead of 0 (to print the number in 0s and 1s) but it printed "-1" as number and "234567abc" as the remainder string. how do i understand this?
    Since base 2 is binary, the only string that works would be made up of 1s and 0s.

    I recommend reading this and this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 12-16-2012, 06:48 PM
  2. Does function strtol work??
    By Programmer_P in forum C++ Programming
    Replies: 16
    Last Post: 07-17-2010, 07:41 PM
  3. explanation of STRTOL function
    By adash in forum C Programming
    Replies: 2
    Last Post: 05-25-2009, 02:54 PM
  4. String to int using strtok and strtol
    By jtullo in forum C Programming
    Replies: 2
    Last Post: 05-06-2007, 04:37 PM
  5. splitting a string using strtol
    By kopite in forum C Programming
    Replies: 6
    Last Post: 12-03-2003, 09:22 AM