Register | Sing In

New posts

Stacks in C programming Linked Lists in C programming File Input/Output in C programming Check prime numbers in C Programming C Programming Enumeration C Programming Union

Archives

2013-10 2013-09 2013-08

strtoul - convert string to unsigned long in C

September 8, 2013 Posted by: GPA Views: 802 0 comments

Problem

Write a program that parses the string in C programming using strtoul function.

Solution

In this section, you will learn to convert string with unsigned long and some letters to unsigned long value. You should include header file <stdlib.h> and use function strtoul().


Declaration:

double strtoul(const char *inputString, char **endinputString,int base); 

where inputString is the string to be converted;

endinputString - pointer to a string, it is the end of inputString after a long value.

base- indicates that the value to be converted can be in octal (base 8), decimal (base 10) or hexadecimal (base 16) format. The base can be 0 or any value between 2 and 36.

Example:

base=0;

inputString = "1234567dollars" then this function will return 1234567 and endinputString will be "dollars".

 

Example:

   


 
#include <stdio.h>
#include <stdlib.h>//use this header for function strtoul
//main function
int main(int argc, char *argv[])
{  
   char* inputString;//input string
   char* outputString;//output string
   int base=0;//base
   printf("globalproganswer.com\n");
   printf("Enter string (example \"1000000dollars\"): ");//promt the user to input string
   scanf("%s",&inputString);//read string from keyboard
   unsigned long inputValue=strtoul(&inputString,&outputString,base);//parce inputString to unsigned long and string using function strtoul
   printf("The input unsigned long value is %ld.\n\n",inputValue);//show result
   system("PAUSE");//delay
   return 0;//retun 0 exit program
}

Output

strtoul - parce string to unsigned long

Download this example

0 Comments...

Leave a Reply

Please, Sing In to leave a Reply for this material.



Online Chat

LiveZilla Live Help

Popular posts

Function strtok in C programming Check prime numbers in C Programming C Programming Union Linked Lists in C programming File Input/Output in C programming C Programming Enumeration

RSS-subscribe

New materials

Subscribe