strtol c code example

Example 1: strtol

// definition: long int strtol (const char* str, char** endptr, int base);

/* strtol example */
#include <stdio.h>      /* printf */
#include <stdlib.h>     /* strtol */

int main ()
{
  char szNumbers[] = "2001 60c0c0 -1101110100110100100000 0x6fffff";
  char * pEnd;
  long int li1, li2, li3, li4;
  li1 = strtol (szNumbers,&pEnd,10);
  li2 = strtol (pEnd,&pEnd,16);
  li3 = strtol (pEnd,&pEnd,2);
  li4 = strtol (pEnd,NULL,0);
  printf ("The decimal equivalents are: %ld, %ld, %ld and %ld.\n", li1, li2, li3, li4);
  return 0;
}

Example 2: c strlen

int my_strlen(char *str) {
  int i = -1;
  
  while (str && str[++i]);
  return (i);
}

Example 3: strncpy c

char *strncpy(char *dest, const char *src, size_t n)

Example 4: strtol in c

long int strtol(const char *str, char **endptr, int base)

Example 5: strtol c

long strtol( const char * theString, char ** end, int base );