Difference Between Character array and string in c

22 Oct 2023 Balmiki Mandal 0 C Programming

Understanding the Contrast Between Character Arrays and Strings in C

The main difference between a character array and a string in C programming is that a string is a character array that is terminated by a null character (\0). The null character is a special character with the value 0. It is used to mark the end of the string.

A character array is simply an array of characters. It can be declared using the char keyword. For example, the following code declares a character array called name:

C program
char name[10];

This character array can store up to 10 characters.

A string is a character array that is terminated by a null character. It can be declared using the char keyword and the string literal. A string literal is a sequence of characters enclosed in double quotation marks (").

Example, the following code declares a string variable called name:

C program 
char name[] = "John Doe";

This string variable can store up to 11 characters, including the null character.

 Write A c Program to difference between string and Character array will se program 01 or program 02

Program 01: 

#include
void main()
{
    char a[]={'a','b','c','d'}; // cheracter array
    char s[]="ABCD";            //string
    printf("%d %d\n",sizeof(a),sizeof(s));
}

Output:- 4 5

Program 02:

#include
void main()
{
    char a[5]={'a','b','c','d'};  //character array
    char s[5]="ABCD"; //string
    printf("%d %d\n",sizeof(a),sizeof(s));
}
 output:- 5 5

Key differences between character arrays and strings in C programming

Feature Character array String
Definition An array of characters A character array that is terminated by a null character
Declaration char array_name[size]; char string_name[] = "string literal";
Null character Not required Required
Accessing characters array_name[index] string_name[index]
Getting length strlen(array_name) strlen(string_name)
Comparing strings strcmp(array_name1, array_name2) strcmp(string_name1, string_name2)

 

Short Note

Character Array: Character array is nothing but is collection of characters.

String: String is nothing but it is a collection of characters ending with '\0'.

Note:

  1. Here character array is taking 4 bytes but the string is taking 5 bytes because of string taking one extra escape sequence '\0' after the string element to prevent execution
  2. 1000 and 200 is starting  address of the  character and the address of the string 
  3. 97 and 65 is the Starting ASCII value of the small (a) and capital (A)  alphabet.

'\0' 

  • '\0' is one of the escape sequences.
  • '\0' indicates the end of valid data
  • '\0' acts as a separator between valid and invalid data.
  • To store '\0' we need 1 byte of memory.
  • '\0' in that 1 byte all bits are zeros

conclusion:

program 01 and program 02 both are similar program but  in program 01 we are not mentioning the size of the Character array or string so that's why the compiler considers the size of the character array or string  based upon  the initialized element  ( means counting the {  } element )

Top Resources

String in c Programming

 

Further Reading:

 For further information and examples, Please visit[ C-Programming From Scratch to Advanced 2023-2024]

 

Note: If you encounter any issues or specific errors when running this program, please let me know and I'll be happy to help debug them!

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.