Strings in C Programming for Beginners with Examples

Strings in C Programming

A string in C programming is a collection of characters stored in an array. Strings are used to store text such as names, messages, and sentences.


What is a String?

In C, a string is an array of characters that ends with a special character called null character (\0).


Declaration of a String

Syntax:


char string_name[size];

Example:


char name[20];


Initialization of a String


char name[] = "Anand";

Here, C automatically adds the null character at the end.

Printing a String


#include <stdio.h>

int main() {

    char name[] = "Programming";

    printf("%s", name);

    return 0;

}

The %s format specifier is used to print strings.

Common String Functions

To use string functions, include:


#include <string.h>

Function Purpose
strlen() Find length of string
strcpy() Copy one string to another
strcat() Concatenate two strings
strcmp() Compare two strings

Why Strings Are Important

  • Used for storing text data
  • Essential in user input programs
  • Common in real-world applications

Conclusion

Strings allow programs to work with text data efficiently. After understanding strings, the next important topic is functions in C programming.

Comments

Popular posts from this blog

Structures in C Programming with Examples

What is C programming?

Arrays in C Programming for Beginners with Examples