Difference between Character and String in C

Question: What is Difference Between a Character and a String in C?

Answer: A character is just a single character enclosed in single quotes. For example:

    char initial = 'A';    /* initial declared to be a character */

And a character string is a sequence of 0 or more characters enclosed in double quotes. Each string is terminated by a NULL byte. Therefore, string containing 0 characters is called an empty string. For example,

    char str[] = "hello, where are you these days?";		
    char empty_str[] = "";	/* empty string */ 
    char *str_lit = "this is string literal";

Further, size of a character can be evaluated using sizeof operator while of strings, strlen function does so. strlen function returns actual no. of characters in the string excluding NULL byte. Therefore, while declaring a character array to be a string, keep one byte extra for NULL terminator plus size, in bytes, for maximum no. of characters in the string. For example, for a string containing maximum 10 characters, we need to declare character array of size 11 bytes, as:

advertisement
advertisement
    char msg[11] = "hello dear";    /* 'msg' is 10 char string */

Notice that ‘msg’ is a 10 characters string while it’s declared and initialized to contain 11 characters. Last character is NULL byte to terminate the string ‘msg’. Let’s now take a simple program and observe its output.

/* diff_char_str.c -- program differentiates character and char string */
#include <stdio.h>
#include <string.h>
 
int main(void)
{
    char initial = 'A';     /* initial initialized with character 'A' */
    char str[] = "hello, where are you these days?";
    char empty_str[] = "";  /* empty string */
    char *str_lit = "this is a string literal";
    char msg[11] = "hello dear"; /* 'msg' is 10 char string */
 
    puts("\n*****Program differentiates character and string*****\n");
    printf("Size of character \'%c\' is %d byte\n",
            initial, sizeof(initial));
    printf("Size of string \"%s\" is %d bytes\n", str, strlen(str));
    printf("Size of empty string \"%s\" is %d bytes\n",
            empty_str, strlen(empty_str));
    printf("Size of string literal \"%s\" is %d bytes\n", 
            str_lit, strlen(str_lit));
    printf("Size of msg \"%s\" is %d bytes\n", msg, strlen(msg));
    puts("");
    return 0;
}

Output of the above program as:

*****Program differentiates character and string*****
 
Size of character 'A' is 1 byte
Size of string "hello, where are you these days?" is 32 bytes
Size of empty string "" is 0 bytes
Size of string literal "this is string literal" is 22 bytes
Size of msg "hello dear" is 10 bytes

Notice that size of empty string is 0 bytes while of ‘msg’, which was declared to contain 11 characters, gives size as 10 bytes for string of 10 characters.

advertisement

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

If you wish to look at all C Tutorials, go to C Tutorials.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.