C Strings

Strings are used for storing text/characters. For example, "Hello, World" is a string of characters.


Unlike many other programming languages, C does not have a String type to easily create string variables.



  char greetings []='Hello World'

  #include 

  int main()
  {
  char greetings[] = "Hello, World!";
  printf("%s", greetings);
  return 0;
  }
        

Output

Hello, World!


Explanation

As C does not have string type, So you can use the Char type and create an array of characters to make a string in C.


To output the string, you can use the printf() function together with the format specifier %s to tell C that we are now working with strings.


• Since strings are actually arrays in C, you can access a string by referring to its index number inside square brackets [].



  char greetings [] = 'Hello, World!';
  printf('%c', greetings [0]);
        

Output:

H



Note: We must use the %c format specifier to print a single character.







© Copyright 2022 Advanced PLC. All rights reserved.