Sei sulla pagina 1di 5

Introduction to Character Array

In C Programming we don’t have Sting data type hence use array of character to
creating string. Suppose we have to accept the name of the person then n
characters are collected together and string or character array is created.

Some Points Regarding Character Array | String in C


Programming :
1. String in C is also called as Character Array.
'C','4','L','E','A','R','N'
2. String is enclosed within Double quotes.
"Pritesh" is a example of String
3. Each Character Occupy 1 byte of Memory.
4. Each Character is stored in consecutive memory location.
Address of 'A' = 2000
Address of 'H' = 2001
Address of 'M' = 2002
Address of 'E' = 2003
Address of 'D' = 2004
5. String always Terminated with NULL Character (‘\0′).
char name[6] = {'A','H','M','E','D','\0'}
6. NULL Character is having ASCII value 0.
ASCII Value of '\0' = 0
7. A String is nothing but an array, so it is Possible to Access Individual
Character like:
name[0] = 'A';
name[1] = 'h';
name[2] = 'm';
name[3] = 'e';
name[4] = 'd';
name[5] = '\0';
Declaring String Or Character Array in C
String means Collection of Characters to form particular word. String is useful
whenever we accept name of the person, address of the person, some descriptive
information. We cannot declare string using String Data Type, instead of we use
array of type character to create String.

 Character Array is Called as ‘String’


 Character Array is Declared Before Using it in Program

Syntax :
char String_Variable_name [ SIZE ] ;

Examples :
char city[30];
char name[20];
char message[50];

These are some sample declarations of the String.In the first example we have
defined string to store name of city.Maximum Size to store City is 30 which must
be specified inside the Square brackets.

Explanation of the Example


Consider -

char city[30];

above example ,
Point Explanation

Significance We have declared array of Character [i.e String]

Size of String 30 Bytes

Bound Checking C Does not Support Bound Checking i.e if we store City with size
greater than 30 then C will not give you any error

Data Type Char

Maximum Size 30
Some Precautions to be taken while declaring Character
Variable :
1. String / Character Array Variable name should be legal C Identifier.
2. String Variable must have Size specified.
char city[];
Above Statement will cause compile time error.

3. When you are using string for other purpose than accepting and
printing data then you must include following header file in your code -
#include<string.h>

Initializing String [Character Array] :


Whenever we declare a String then it will contain garbage values inside it. We
have to initialize String or Character array before using it. Process of Assigning
some legal default data to String is Called Initialization of String. There are
different ways of initializing String:

1. Initializing Unsized Array of Character


2. Initializing String Directly
3. Initializing String Using Character Pointer

METHOD 1 : Unsized Array and Character


1. Unsized Array : Array Length is not specified while initializing character
array using this approach
2. Array length is Automatically calculated by Compiler
3. Individual Characters are written inside Single Quotes , Separated by
comma to form a list of characters. Complete list is wrapped inside Pair of
Curly braces
4. Please Note : NULL Character should be written in the list because it is
ending or terminating character in the String/Character Array
char name [] = {'A','H','M','E','D','\0'};
METHOD 2 : Directly initialize String Variable
1. In this method we are directly assigning String to variable by writing text
in double quotes.
2. In this type of initialization, we don’t need to put NULL or Terminating
character at the end of string. It is appended automatically by the compiler.
char name [ ] = "AHMED";

METHOD 3 : Character Pointer Variable


1. Declare Character variable of pointer type so that it can hold the base
address of “String”
2. Base address means address of first array element i.e (address of
name[0] )
3. NULL Character is appended Automatically
char *name = "AHMED";

NULL Character / Terminating Character in String

Why do we need Terminating Character ?


1. String is not data type in C.
2. String is necessary Data Structure in C Programming.
3. String is nothing but Variable length Structure stored inFixed
Length Structure.
4. Array Size is not the Actual Length of the String , So to recognize the End
of the String we use NULL character
Explanation :
1. Before Initializing String it Contain Garbage Characters .
2. Length of String is 7 so it Contain 7 Garbage Characters .
3. When we attempt to store “CAT” in the string of Character length 7 then
String Would be “CAT@’af“
4. So to have only “CAT” in the Variable of String we use NULL Character

In Short :
To Store "CAT" instead of "CAT@'af" we use NULL Character

ASCII Value of NULL Character


1. NULL Character is Denoted by ‘\0′
2. ASCII Value = 0 ;

Potrebbero piacerti anche