String in C - Notes for GATE | Testbook.com

Last Updated on Feb 18, 2025
Download As PDF
IMPORTANT LINKS

The realm of programming languages is vast, and understanding each concept is crucial, especially when you're preparing for competitive exams like GATE. This article provides an in-depth look at an essential topic - strings in C. Our aim is to make the topic as clear as possible for you, covering each aspect that is relevant to the GATE exam. We hope this comprehensive guide will enhance your understanding of strings in C.

Understanding Strings in C

In the simplest terms, a string in C can be seen as an array of characters, terminated by a NULL character.

For instance, consider this example: char string[] = "C string"

Crack AE & JE Civil Exam with India's Super Teachers

Get SuperCoaching @ just

₹11399 ₹5769

Your Total Savings ₹5630
Purchase Now

How to Define Strings in C

The syntax to define a string in C is as follows:

char string_name[array_size];

For example: char string[10] = "Testbook"

Declaring Strings in C

There are two primary ways to declare a string in C:

  1. Using a character array
  2. Using a string literal

1. Declaring a String Using a Character Array

char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};

2. Declaring a String Using a String Literal

Following the rule of array initialization, the above statement can be written as follows:

char greeting[] = "Hello";

Here's how the above-defined string is represented in memory in C/C++:

Test Series
124.7k Students
GATE Mathematics & General Aptitude (Common for All) Test Series
92 TOTAL TESTS
  • 54 Chapter Test
  • 10 Subject Test
  • 28 Previous Year Paper

Get Started

String Examples in C Programming

Let's take a look at some examples to understand strings in C programming better.

Functions and Uses of Strings in C

Strings in C have several functions and uses, including:

  • strcpy(s1, s2) - Copies string s2 into string s1.
  • strcat(s1, s2) - Concatenates string s2 onto the end of string s1.
  • strlen(s1) - Returns the length of the string.
  • strcmp(s1, s2) - Compares strings s1 and s2. It returns 0 if s1 and s2 are the same; less than 0 if s1 is less than s2; greater than 0 if s1 is greater than s2.
  • strchr(s1, ch) - Returns a pointer to the first occurrence of the specified character in the string.
  • strstr(s1, s2) - Returns a pointer to the first occurrence of string s2 in string s1.

Practice Problems on Strings in C

Practice is the key to mastering any programming concept. Here are some practice problems on strings in C:

Q. Consider the following C program.

#include<stdio.h>

#include<string.h>

int main () {

char* c = "GATECSE2023";

char* p = c;

printf ("%d", (int)strlen (c+2[p]-6[p]-1));

return 0;

}

What is the output of the program? Try to solve it.

Additional Resources for GATE Aspirants

To keep up with the latest updates on the GATE exam, stay tuned. Also, explore the following resources:

More Articles for GATE

Frequently Asked Questions

Strings are actually a one-dimensional array of characters terminated by a null character.

The basic syntax for declaring a string in C is: char str_name[size];

Report An Error