C uses the char type to store characters and letters. Char is considered an integer type. Each char integer value is mapped with a corresponding character using a numerical code. The most common numerical code is ASCII.
Declare a char
The char keyword is used to declare a character type variable followed by the variable name -
char ch;
Initialise a char
A char character variable can be initialised with a character literal or an integer type. A character literal contains one character surrounded by a single quotation (‘’).
The following example declares a char variable character and initialises it with a character literal 'a'
ch='a';
Because the type is the integer type, you can also initialise or assign a char variable using an integer. -
char ch=65;
Single quotes vs double quotes
In C, single quotes identify a single character; double quotes create a string array. 'a' represents a single character literal, while "a" is a string literal consisting of an 'a' and the null terminator '/0'.
Print characters in C
To print characters in C, use the printf() function. The syntax for printf() is
int printf( const char* str, ... );
The string pointed to by str contains the data types to be printed on screen with the data type format specifiers proceeded with %. These are replaced by the variable values passed to the printf() function as additional arguments.
The printf function is not part of the C language but is found in the <stdio.h> header file
The following code example demonstrates how to print characters in C using both integers and char literals
#include "stdio.h"; /* Declare and initialize two char variables */ char c1 = 'A'; char c2 = 90; int main( void ) { /* Print variable c1 as a character, then as a number */ printf("\nvariable c1 As a character, is %c", c1); printf("\nvariable c1 As a number, is %d", c1); /* Do the same for variable c2 */ printf("\nvariable c2 As a character, is %c", c2); printf("\nvariable c2 As a number, is %d\n", c2); return 0; }
Char Arrays
C-strings are arrays of type char terminated with null character or \0.
To declare and initialise a character string
Character arrays can be declared and initialised on a character-by-character basis using an array-style initialiser however it's much easier to initialise a character array with a string literal -
char greeting[6] = {'h','e','l','l','o','\0'} (array style initialiser with the NULL character being added manually) char greeting[] = "hello" ;(initialisation with a string literal with the null character being added automatically.
Initialising a char array with the value '\0' creates a NULL or empty string -
char greeting[0] = {'\0'};
Inserting '\0' anywhere in the middle of the array would not change the size of the array but it would mean that string processing would stop at that point. Sending the following to the char array to screen would only produce the characters 'hel'
char greeting[] = "hel\0lo"
Assign new values to a char array
To change the contents of the string after the initial assignment, it is necessary to change the array contents individually. Assigning the new value to the existing char array ie greetings[]="HELLO" won't work because the = operator isn't defined to copy the contents of a string literal to an array.
greetings[0] = 'H'; greetings[1] = 'E'; greetings[2] = 'L'; greetings[3] = 'L'; greetings[4] = 'O'; greetings[5] = '\0';
Since assigning new array string values individually is not practical, C uses the strcpy or the strncpy function to assign the contents of an array outside of a declaration. The strcpy is found in the string.h header, The syntax for strcpy is-
strcpy(greetings,"hello");
Pointers and Arrays
Arrays elements can also be accessed by the use of pointers. The pointer is declared and assigned to the first element of the array. After assigning the array pointer, the individual elements can be accessed by increasing or decreasing the pointer value.
The code sample below outputs the same letters of an array string by using pointers and array indexing
#include "stdio.h"; int main() { char str[31]="this is a string to array test"; //declares char array char *pChar='\0';; //declares char pointer and sets to null pChar=str; //Sets pointer to start of array int i; for(i=0; i<=31; i++) { printf("%c", *(pChar+i));//prints value of char array } return 0; }
Pointers and String Literals
A string literal is a constant null-terminated sequence of characters enclosed in double quotation marks. All C compilers create a read-only string table for storing these string literals. Programmers can allocate their own pointers to store and access characters held in string tables.
int main() { const char *ptrsl= "this is a string literal.\n"; //creates pointer ptrsl to to start of string array printf (ptrsl); return 0; }
Attempting to modify a string literal using a pointer can result in undefined behaviour so any pointer to a string literal should be declared as const.
Array of Pointers
An array of pointers to strings is an array of character pointers where each pointer points to the first string character or base address of the string. To declare and initialise an array of pointers to strings -
char *day[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday","Friday","Saturday"};
Since initialisation of the array is done at the time of declaration we can omit the size of an array.
Each element of the day[] array is a string literal and since a string literal points to the base address of the first character, the base type of each element of the sports array is a pointer to char or (char*).
To access the values pointed to by an array of pointers it is necessary to iterate and dereference each pointer individually.
#include "stdio.h" const int MAX = 7; int main () { char *day[] = {"Sunday","Monday","Tuesday", "Wednesday", "Thursday","Friday","Saturday"}; int i = 0;
for ( i = 0; i < MAX; i++)
{ printf("day[%d] = %s\n", i, day[i] ); } return 0;
}