The main difference between Character and String is that Character refers to a single letter, number, space, punctuation mark or a symbol that can be represented using a computer while String refers to a set of characters.
In C programming, we can use char data type to store both character and string values. Moreover, the ASCII and Extended ASCII standards define the characters we can represent in a computer.
Key Areas Covered
1. What is Character
– Definition, Functionality
2. What is String
– Definition, Functionality
3. Difference Between Character and String
– Comparison of Key Differences
Key Terms
ASCII, Character, String
What is Character
Character refers to a single character value. In C programming, the programmer can use the char data type to store a single character. Furthermore, most compilers need a single byte of memory to store a character. An example program is as follows.
Figure 1: C program with characters
The grade is a variable of type char. Therefore, it can store a single character value. The printf statement indicates the user to enter the grade. Then, the entered grade is saved into the grade variable. The switch checks the grade with the corresponding cases and displays the output. However, if any of the cases do not match, it executes the statement in the default section. The user enters B and the corresponding statement (“Good”) is executed. In this program, the grade variable stores a single character.
What is String
String is a set of characters that ends with a null character (‘\0’). In C programming, we can use char data type to store a string. That is because a string is also a set of characters. As there should be a null character at the end of the string, the string size should be one more than the number of characters in the string.
Following is a declaration and initialization of a string.
char message[6] = {‘A’, ‘p’, ‘p’, ‘l’, ‘e’, ‘\0’};
We can also write it as follows.
char message[] = “Apple”;
But, here, it is not necessary for the programmer to place a null character at the end of the string. The compiler places a null character automatically at the end of the string at the time of initializing the array.
Figure 2: C program with a String
In the above program, fruit variable stores a set of characters which is a string. The printf statement displays that character on the console.
The “string.h” header file consists of predefined functions to perform operations on the strings. Some of them are as follows.
strcpy(s1, s2); – This function allows copying the string s2 into string s1.
strcat(s1,s2); – This function helps to combine string s2 at the end of string s1.
strlen(s1) – This function allows finding the length of the string s1.
Difference Between Character and String
Definition
A character is a single letter, number, punctuation mark or symbol that can be represented using a computer while a string is a one-dimensional array of characters terminated by a null character. Thus, this is the main difference between Character and String
Basis
Also, another difference between Character and String is that Character is an element while string is a set of characters.
Representation
Moreover, single quotes are used to represent a character whereas double quotes are used to represent a string.
Conclusion
The main difference between Character and String is that Character refers to a single letter, number, space, punctuation mark or a symbol that can be represented using a computer while String refers to a set of characters. In brief, String is a collection of characters.
References:
1.“Character.” Character Definition, Available here.
2.“C Strings – Javatpoint.” Www.javatpoint.com, Available here.
ncG1vNJzZmiolZm2oq2NnKamZ6edrrV5yKxkraCVYrGqssWeqZ6mk5p6o7HTsJyepl2YtaK%2BwJyrnqpdlruledKtqaKml2Q%3D