Beginners Guide to Strings in C Programming with Codes and Examples

Article by: Manish Methani

Last Updated: November 2, 2021 at 2:04pm IST
3 min 36 sec read

Introduction:

Strings are a fundamental data type in C programming used to store and manipulate text. In this tutorial, you will learn about the basics of strings in C programming with codes and examples.

Declaring Strings:

To declare a string in C programming, you need to define a char array with a size that is large enough to store the string plus one for the null terminator character. For example:

char str[10];

In this example, the size of the char array is 10, which means it can store a string of up to 9 characters plus one for the null terminator.

Initializing Strings:

There are two ways to initialize a string in C programming. You can either initialize it at the time of declaration or assign a value to it later. For example:

char str1[] = "Hello";
char str2[6];
str2[0] = 'W';
str2[1] = 'o';
str2[2] = 'r';
str2[3] = 'l';
str2[4] = 'd';
str2[5] = '';

In the first example, the string "Hello" is assigned to the char array str1 at the time of declaration. In the second example, the string "World" is assigned to the char array str2 by assigning each character individually and adding the null terminator character at the end.

String Input/Output:

To input a string in C programming, you can use the scanf() function with the %s format specifier. For example:

char str[20];
scanf("%s", str);

In this example, the user can input a string of up to 19 characters, which will be stored in the char array str.

To output a string in C programming, you can use the printf() function with the %s format specifier. For example:

char str[] = "Hello, World!";
printf("%s", str);

In this example, the string "Hello, World!" will be outputted to the console.

String Functions: C programming provides various string functions to manipulate strings. Here are some examples:

  • strlen() function: This function returns the length of the string.
char str[] = "Hello";
int len = strlen(str);

In this example, the variable len will contain the value 5.

  • strcpy() function: This function copies the string from the source to the destination.
char src[] = "Hello";
char dest[10];
strcpy(dest, src);

In this example, the string "Hello" is copied from the char array src to the char array dest.

  • strcat() function: This function concatenates two strings.
char str1[] = "Hello, ";
char str2[] = "World!";
strcat(str1, str2);

In this example, the string "World!" is concatenated to the string "Hello, " resulting in the string "Hello, World!".

Conclusion: In this tutorial, you have learned about the basics of strings in C programming with codes and examples. You now know how to declare, initialize, and manipulate strings in C, as well as use string input/output and string functions. By mastering strings, you can unlock the full potential of C programming and create powerful applications.

 

Discover My FlutterFlow Courses and Template Apps

FlutterFlow Course: Basic to Advanced (2025) – Dating App
Learn to Create the Dating App with Admin Dashboard using No-Code Tool FlutterFlow in this comprehensive course in 2025.
FlutterFlow Course: Learn to Build the Grocery Delivery App
Learn to Create Grocery-Delivery App in FlutterFlow, ideal for anyone looking to implement a functional E-Commerce App.
FlutterFlow Course: Learn to Build the Online Courses App
Learn to Create Online Courses App in FlutterFlow, ideal for anyone looking to implement a functional Courses App.
Codzify Logo

Terms and Conditions    Cookie Policy   Refund Policy   Adsense Disclaimer

Contact: teamcodzify@gmail.com