C Strings Declarations

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 ,

PointExplanation
SignificanceWe have declared array of Character [i.e String]
Size of String30 Bytes
Bound CheckingC 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 Typechar
Maximum Size30

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.
  3. char city[];

    Above Statement will cause compile time error.

  4. 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
  5. String city;
    
  6. When you are using string for other purpose than accepting and printing data then you must include following header file in your code -
  7. #include<string.h>
    

C Programming String - Summary

This tutorial is overall summary of C Programming String concept. In this tutorial we are listing out some of the key concepts related to C Programming Strings.

Introduction to C Programming String :

Like Java, we don’t have C Programming String data type. In C we have to use different approach to store the string. We are creating 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.

C Programming String : Summary

A. Character array

C programming string is also called as Character Array. String is nothing but the collection of the individual array elements or characters stored at contiguous memory locations

'C','4','L','E','A','R','N'

B. Double quotes

String is enclosed within Double quotes

"Pritesh" is a example of String

If string contains the double quote as part of string then we can use escape character to keep double quote as a part of string.

"Prit\"esh" is a example of String

Re-commanded Example : Multi-line Printf statement

C. Memory

Each Character Occupy 1 byte of Memory

Size of "Pritesh" = Size of 'P' +
                  = Size of 'R' +
                  = Size of 'I' +
                  = Size of 'T' +
                  = Size of 'E' +
                  = Size of 'S' +
                  = Size of 'H';
Size of "Pritesh" = 7 BYTES

Each Character is stored in consecutive memory location

Address of 'P' = 2000
Address of 'R' = 2001
Address of 'I' = 2002
Address of 'T' = 2003
Address of 'E' = 2004
Address of 'S' = 2005
Address of 'H' = 2006

D. NULL Character

String always Terminated with NULL Character (‘/0′)

char name[10] = {'P','R','I','T','E','S','H','\0'}

NULL Character is having ASCII value 0

ASCII Value of '\0' = 0

As String is nothing but an array , so it is Possible to Access Individual Character

name[10] = "Pritesh";

It is possible to access individual character

name[0] = 'P';
name[1] = 'r';
name[2] = 'i';
name[3] = 't';
name[4] = 'e';
name[5] = 's';
name[6] = 'h';
name[7] = '\0';