Initializing String / Charater Array in C Programming

January 14, 2010 No Comments » Hits : 213





Ways of Initializing String


Way 1 :Unsized Array and Character

  • Unsized Array : Array Length is not specified
  • Array length is Automatically calculated by Compiler
  • Individual Characters are written inside pair of braces with comma as seperator
  • Note : NULL Character should be written
char name [] = {'P','R','I','T','E','S','H','\0'};

Way 2 : Directly initialize String Variable

  • Assign String to variable of type Character Array
  • No need to write NULL(‘\0′) character at end
  • NULL Character is appended Automatically
char name [ ] = "PRITESH";

Way 3 : Character Pointer Variable

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

Related Topic : 

Incoming search terms: