Lesson 9

ArtyArty
4 min read

Strings and characters

  • A program may contain character constants.

  • The value of a character constant is the integer value.

  • There is a table that represents the ASCII character set.

  • A string is a series of characters treated as a single unit

  • A string in C is an array of characters ending in the NULL character ('\0’).

    • char colour[] = "Blue";

    • const char *colour = "Blue";

    • const char *colour = {'B', 'l', 'u', 'e', '\0'};

  • Make sure that char arrays are big enough when reading data (eg. scanf("%s", word);

Character Handling Library <ctype.h>

Character Classification Functions

These functions return a nonzero (true) value if the given character satisfies the condition; otherwise, they return 0 (false).

FunctionDescription
isalnum(int c)Checks if c is an alphanumeric character (letter or digit).
isalpha(int c)Checks if c is a letter (uppercase or lowercase).
isascii(int c)Checks if c is a valid ASCII character (0-127).
isblank(int c)Checks if c is a blank character (space or tab).
iscntrl(int c)Checks if c is a control character.
isdigit(int c)Checks if c is a digit (0-9).
isgraph(int c)Checks if c is a printable character, excluding space.
islower(int c)Checks if c is a lowercase letter (a-z).
isprint(int c)Checks if c is a printable character, including space.
ispunct(int c)Checks if c is a punctuation character.
isspace(int c)Checks if c is a whitespace character (space, tab, newline, etc.).
isupper(int c)Checks if c is an uppercase letter (A-Z).
isxdigit(int c)Checks if c is a hexadecimal digit (0-9, A-F, a-f).

Character Conversion Functions

These functions return the converted character if applicable; otherwise, they return the input character unchanged.

FunctionDescription
tolower(int c)Converts c to lowercase if it is an uppercase letter.
toupper(int c)Converts c to uppercase if it is a lowercase letter.

String Conversion functions in <stdlib.h>

Integer Conversion Functions

FunctionDescription
int atoi(const char *str)Converts str to an int.
long atol(const char *str)Converts str to a long integer.
long long atoll(const char *str)Converts str to a long long integer.
int strtol(const char *str, char **endptr, int base)Converts str to a long integer with a specified base (e.g., binary, decimal, hexadecimal).
long strtoll(const char *str, char **endptr, int base)Converts str to a long long integer with a specified base.
unsigned long strtoul(const char *str, char **endptr, int base)Converts str to an unsigned long integer with a specified base.
unsigned long long strtoull(const char *str, char **endptr, int base)Converts str to an unsigned long long integer with a specified base.

Floating-Point Conversion Functions

FunctionDescription
double atof(const char *str)Converts str to a double.
double strtod(const char *str, char **endptr)Converts str to a double and sets endptr to point to any remaining characters.
float strtof(const char *str, char **endptr)Converts str to a float.
long double strtold(const char *str, char **endptr)Converts str to a long double.

Standard Input and Output Functions in <stdio.h>

FunctionDescription
int printf(const char *format, ...)Prints formatted output to stdout.
int scanf(const char *format, ...)Reads formatted input from stdin.
int putchar(int c)Writes a single character to stdout.
int getchar(void)Reads a single character from stdin.
int puts(const char *str)Writes a string followed by a newline to stdout.
char *gets(char *str)Reads a string from stdin (unsafe, use fgets() instead).
char *fgets(char *str, int n, FILE *stream)Reads a string from stream with buffer overflow protection.
int fputs(const char *str, FILE *stream)Writes a string to stream.
0
Subscribe to my newsletter

Read articles from Arty directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Arty
Arty