strtoul - C stdlib.h

C examples for stdlib.h:strtoul

Type

function

From


<cstdlib>
<stdlib.h>

Description

Convert string to unsigned long integer return as an value of type unsigned long int.

Prototype

unsigned long int strtoul (const char* str, char** endptr, int base);

Parameters

Parameter Description
str C-string representation of an integral number.
endptr type char* set by the function to the next character in str after the numerical value.
base Numerical base (radix)

Return Value

On success, the function returns the converted integral number as an unsigned long int value.

For non valid conversion, a zero value is returned.

For out of the range values, the function returns ULONG_MAX (defined in <climits>), and errno is set to ERANGE.

Demo Code


#include <stdio.h>
#include <stdlib.h>

int main ()/*from  w w  w  .  j a  v a  2s  .com*/
{
  char buffer [256];
  
  unsigned long ul;
 
  printf ("Enter an unsigned number: ");
 
  fgets (buffer, 256, stdin);
 
  ul = strtoul (buffer, NULL, 0);
 
  printf ("Value entered: %lu. Its double: %lu\n",ul,ul*2);
 
  return 0;
}

Related Tutorials