Declaring String : [Character Array] in C Programming
Declaring String Or Character Array in C
String data type is not supported in C Programming. 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 |
Precautions to be taken while declaring Character Variable :
- String / Character Array Variable name should be legal C Identifier.
- String Variable must have Size specified.
- Do not use String as data type because String data type is included in later languages such as C++ / Java. C does not support String data type
- When you are using string for other purpose than accepting and printing data then you must include following header file in your code -
char city[];
Above Statement will cause compile time error.
String city;
#include<string.h>