cppreference.com -> Standard C String & Character -> Details

Standard C String & Character


atof

Syntax:

  #include <stdlib.h>
  double atof( const char *str );

The function atof() converts str into a double, then returns that value. str must start with a valid number, but can be terminated with any non-numerical character, other than "E" or "e". For example,

    x = atof( "42.0is_the_answer" );

results in x being set to 42.0.

Related topics:
atoi() and atol().

atoi

Syntax:

  #include <stdlib.h>
  int atoi( const char *str );

The atoi() function converts str into an integer, and returns that integer. str should start with some sort of number, and atoi() will stop reading from str as soon as a non-numerical character has been read. For example,

    i = atoi( "512.035" );

would result in i being set to 512.

Related topics:
atof() and atol().

atol

Syntax:

  #include <stdlib.h>
  long atol( const char *str );

The function atol() converts str into a long, then returns that value. atol() will read from str until it finds any character that should not be in a long. The resulting truncated value is then converted and returned. For example,

    x = atol( "1024.0001" );

results in x being set to 1024L.

Related topics:
atof() and atoi().

isalnum

Syntax:

  #include <ctype.h>
  int isalnum( int ch );

The function isalnum() returns non-zero if its argument is a numeric digit or a letter of the alphabet. Otherwise, zero is returned.

    char c;
    scanf( "%c", &c );
    if( isalnum(c) )
      printf( "You entered the alphanumeric character %c\n", c );
Related topics:
isalpha(), iscntrl(), isdigit(), isgraph(), isprint(), ispunct(), and isspace().

isalpha

Syntax:

  #include <ctype.h>
  int isalpha( int ch );

The function isalpha() returns non-zero if its argument is a letter of the alphabet. Otherwise, zero is returned.

    char c;
    scanf( "%c", &c );
    if( isalpha(c) )
      printf( "You entered a letter of the alphabet\n" );
Related topics:
isalnum(), iscntrl(), isdigit(), isgraph(), isprint(), ispunct(), and isspace().

iscntrl

Syntax:

  #include <ctype.h>
  int iscntrl( int ch );

The iscntrl() function returns non-zero if its argument is a control character (between 0 and 0x1F or equal to 0x7F). Otherwise, zero is returned.

Related topics:
isalnum(), isalpha(), isdigit(), isgraph(), isprint(), ispunct(), and isspace().

isdigit

Syntax:

  #include <ctype.h>
  int isdigit( int ch );

The function isdigit() returns non-zero if its argument is a digit between 0 and 9. Otherwise, zero is returned.

    char c;
    scanf( "%c", &c );
    if( isdigit(c) )
      printf( "You entered the digit %c\n", c );
Related topics:
isalnum(), isalpha(), iscntrl(), isgraph(), isprint(), ispunct(), and isspace().

isgraph

Syntax:

  #include <ctype.h>
  int isgraph( int ch );

The function isgraph() returns non-zero if its argument is any printable character other than a space (if you can see the character, then isgraph() will return a non-zero value). Otherwise, zero is returned.

Related topics:
isalnum(), isalpha(), iscntrl(), isdigit(), isprint(), ispunct(), and isspace().

islower

Syntax:

  #include <ctype.h>
  int islower( int ch );

The islower() function returns non-zero if its argument is a lowercase letter. Otherwise, zero is returned.

Related topics:
isupper()

isprint

Syntax:

  #include <ctype.h>
  int isprint( int ch );

The function isprint() returns non-zero if its argument is a printable character (including a space). Otherwise, zero is returned.

Related topics:
isalnum(), isalpha(), iscntrl(), isdigit(), isgraph(), ispunct(), and isspace().

ispunct

Syntax:

  #include <ctype.h>
  int ispunct( int ch );

The ispunct() function returns non-zero if its argument is a printing character but neither alphanumeric nor a space. Otherwise, zero is returned.

Related topics:
isalnum(), isalpha(), iscntrl(), isdigit(), isgraph(), isprint(), and isspace().

isspace

Syntax:

  #include <ctype.h>
  int isspace( int ch );

The isspace() function returns non-zero if its argument is some sort of space (i.e. single space, tab, vertical tab, form feed, carriage return, or newline). Otherwise, zero is returned.

Related topics:
isalnum(), isalpha(), iscntrl(), isdigit(), isgraph(), and ispunct().

isupper

Syntax:

  #include <ctype.h>
  int isupper( int ch );

The isupper() function returns non-zero if its argument is an uppercase letter. Otherwise, zero is returned.

Related topics:
tolower()

isxdigit

Syntax:

  #include <ctype.h>
  int isxdigit( int ch );

The function isxdigit() returns non-zero if its argument is a hexidecimal digit (i.e. A-F, a-f, or 0-9). Otherwise, zero is returned.

Related topics:
isalnum(), isalpha(), iscntrl(), isdigit(), isgraph(), ispunct(), and isspace().

memchr

Syntax:

  #include <string.h>
  void *memchr( const void *buffer, int ch, size_t count );

The memchr() function looks for the first occurrence of ch within count characters in the array pointed to by buffer. The return value points to the location of the first occurrence of ch, or NULL if ch isn't found. For example:

    char names[] = "Alan Bob Chris X Dave";
    if( memchr(names,'X',strlen(names)) == NULL )
      printf( "Didn't find an X\n" );
    else
      printf( "Found an X\n" );
Related topics:
memcpy() and strstr().

memcmp

Syntax:

  #include <string.h>
  int memcmp( const void *buffer1, const void *buffer2, size_t count );

The function memcmp() compares the first count characters of buffer1 and buffer2. The return values are as follows:

ValueExplanation
less than 0buffer1 is less than buffer2
equal to 0buffer1 is equal to buffer2
greater than 0buffer1 is greater than buffer2

Related topics:
memchr(), memcpy(), and strcmp().


memcpy

Syntax:

  #include <string.h>
  void *memcpy( void *to, const void *from, size_t count );

The function memcpy() copies count characters from the array from to the array to. memcpy() returns to. The behavior of memcpy() is undefined if to and from overlap.

Related topics:
memmove().

memmove

Syntax:

  #include <string.h>
  void *memmove( void *to, const void *from, size_t count );

The memmove() function is identical to memcpy(), except that it works even if to and from overlap.

Related topics:
memcpy().

memset

Syntax:

  #include <string.h>
  void *memset( void *buffer, int ch, size_t count );

The function memset() copies ch into the first count characters of buffer, and returns buffer. memset() is useful for intializing a section of memory to some value. For example, this command:

    memset( the_array, '\0', sizeof(the_array) );

is a very efficient way to set all values of the_array to zero.

Related topics:
memcmp(), memcpy(), and memmove().

strcat

Syntax:

  #include <string.h>
  char *strcat( char *str1, const char *str2 );

The strcat() function concatenates str2 onto the end of str1, and returns str1. For example:

    printf( "Enter your name: " );
    scanf( "%s", name );
    title = strcat( name, " the Great" );
    printf( "Hello, %s\n", title );
Related topics:
strchr(), strcmp(), and strcpy().

strchr

Syntax:

  #include <string.h>
  char *strchr( const char *str, int ch );

The function strchr() returns a pointer to the first occurence of ch in str, or NULL if ch is not found.

Related topics:
strpbrk(), strspn(), strstr(), and strtok().

strcmp

Syntax:

  #include <string.h>
  int strcmp( const char *str1, const char *str2 );

The function strcmp() compares str1 and str2, then returns:

Return valueExplanation
less than 0str1 is less than str2
equal to 0str1 is equal to str2
greater than 0str1 is greater than str2

For example:

    printf( "Enter your name: " );
    scanf( "%s", name );
    if( strcmp( name, "Mary" ) == 0 )
      printf( "Hello, Dr. Mary!\n" );
Related topics:
memcmp(), strchr(), strcpy(), and strncmp().

strcoll

Syntax:

  #include <string.h>
  int strcoll( const char *str1, const char *str2 );

The strcoll() function compares str1 and str2, much like strcmp. However, strcoll() performs the comparison using the locale specified by the setlocale() function.


strcpy

Syntax:

  #include <string.h>
  char *strcpy( char *to, const char *from );

The strcpy() function copies characters in the string from to the string to, including the null termination. The return value is to.

Related topics:
memcpy(), strchr(), strcmp(), strncmp(), and strncpy().

strcspn

Syntax:

  #include <string.h>
  size_t strcspn( const char *str1, const char *str2 );

The function strcspn() returns the index of the first character in str1 that matches any of the characters in str2.

Related topics:
strrchr(), strpbrk(), strstr(), and strtok().

strerror

Syntax:

  #include <string.h>
  char *strerror( int num );

The function strerror() returns an "implementation defined string corresponding to num."


strlen

Syntax:

  #include <string.h>
  size_t strlen( char *str );

The strlen() function returns the length of str (determined by the number of characters before null termination).

Related topics:
memcpy(), strchr(), strcmp(), and strncmp().

strncat

Syntax:

  #include <string.h>
  char *strncat( char *str1, const char *str2, size_t count );

The function strncat() concatenates at most count characters of str2 onto str1, adding a null termination. The resulting string is returned.

Related topics:
strcat(), strnchr(), strncmp(), and strncpy().

strncmp

Syntax:

  #include <string.h>
  int strncmp( const char *str1, const char *str2, size_t count );

The strncmp() function compares at most count characters of str1 and str2. The return value is as follows:

Return valueExplanation
less than 0str1 is less than str2
equal to 0str1 is equal to str2
greater than 0str1 is greater than str2

If there are less than count characters in either string, then the comparison will stop after the first null termination is encountered.

Related topics:
strcmp(), strnchr(), and strncpy().

strncpy

Syntax:

  #include <string.h>
  char *strncpy( char *to, const char *from, size_t count );

The strncpy() function copies at most count characters of from to the string to. If from has less than count characters, the remainder is padded with '\0' characters. The return value is the resulting string.

Related topics:
memcpy(), strchr(), strncat(), and strncmp().

strpbrk

Syntax:

  #include <string.h>
  char *strpbrk( const char *str1, const char *str2 );

The function strpbrk() returns a pointer to the first ocurrence in str1 of any character in str2, or NULL if none are present.

Related topics:
strspn(), strrchr(), strstr(), and strtok().

strrchr

Syntax:

  #include <string.h>
  char *strrchr( const char *str, int ch );

The function strrchr() returns a pointer to the last occurrence of ch in str, or NULL if no match is found.

Related topics:
strpbrk(), strspn(), strstr(), strtok(),

strspn

Syntax:

  #include <string.h>
  size_t strspn( const char *str1, const char *str2 );

The strspn() function returns the index of the first character in str1 that doesn't match any character in str2.

Related topics:
strpbrk(), strrchr(), strstr(), strtok(),

strstr

Syntax:

  #include <string.h>
  char *strstr( const char *str1, const char *str2 );

The function strstr() returns a pointer to the first occurrence of str2 in str1, or NULL if no match is found.

Related topics:
strchr(), strcspn(), strpbrk(), strspn(), strtok(), strrchr(),

strtod

Syntax:

  #include <stdlib.h>
  double strtod( const char *start, char **end );

The function strtod() returns whatever it encounters first in start as a double. end is set to point at whatever is left in start after that double. If overflow occurs, strtod() returns either HUGE_VAL or -HUGE_VAL.

Related topics:
atof()

strtok

Syntax:

  #include <string.h>
  char *strtok( char *str1, const char *str2 );

The strtok() function returns a pointer to the next "token" in str1, where str2 contains the delimiters that determine the token. strtok() returns NULL if no token is found. In order to convert a string to tokens, the first call to strtok() should have str1 point to the string to be tokenized. All calls after this should have str1 be NULL.

For example:

    char str[] = "now # is the time for all # good men to come to the # aid of their country";
    char delims[] = "#";
    char *result = NULL;

    result = strtok( str, delims );

    while( result != NULL ) {
        printf( "result is \"%s\"\n", result );
	result = strtok( NULL, delims );
    }

The above code will display the following output:

    result is "now "
    result is " is the time for all "
    result is " good men to come to the "
    result is " aid of their country"
Related topics:
strchr(), strcspn(), strpbrk(), strrchr(), and strspn().

strtol

Syntax:

  #include <stdlib.h>
  long strtol( const char *start, char **end, int base );

The strtol() function returns whatever it encounters first in start as a long, doing the conversion to base if necessary. end is set to point to whatever is left in start after the long. If the result can not be represented by a long, then strtol() returns either LONG_MAX or LONG_MIN. Zero is returned upon error.

Related topics:
atol().

strtoul

Syntax:

  #include <stdlib.h>
  unsigned long strtoul( const char *start, char **end, int base );

The function strtoul() behaves exactly like strtol(), except that it returns an unsigned long rather than a mere long.

Related topics:
strtol()

strxfrm

Syntax:

  #include <string.h>
  size_t strxfrm( char *str1, const char *str2, size_t num );

The strxfrm() function manipulates the first num characters of str2 and stores them in str1. The result is such that if a strcoll() is performed on str1 and the old str2, you will get the same result as with a strcmp().

Related topics:
strcmp(), strcoll(),

tolower

Syntax:

  #include <ctype.h>
  int tolower( int ch );

The function tolower() returns the lowercase version of the character ch.

Related topics:
toupper(),

toupper

Syntax:

  #include <ctype.h>
  int toupper( int ch );

The toupper() function returns the uppercase version of the character ch.

Related topics:
tolower(),