C - IntroductionC - Hello World ProgramC - Exercise 1C - Basic structure of a C programC - TokensC - Data TypesC - Type ConversionC - Exercise 2C - Character Input Output OperationsC - Input Output operation using scanf and printf functions


C - Arithmetic OperatorsC - Relational OperatorsC - Logical OperatorsC - Assignment OperatorsC - Increment Decrement OperatorsC - Bitwise Operators

Precedence and Associativity

C - Precedence and AssociativityC - Exercise 3


C - If Else decision making statementsC - Switch Case decision making statements


C - While LoopC - Do While LoopC - For LoopC - Exercise 4


C - ArraysC - Two Dimensional ArraysC - Multi Dimensional ArraysC - Exercise 5


C - StringC - Exercise 6C - String Manipulation


C - FunctionsC - Functions CategoryC - Function Call - Flow of ControlC - RecursionC - Functions and ArraysC - Functions and Strings


C - StructuresC - Structures and ArraysC - Passing structure to functionC - Function returning structureC - Structure in Structure


C - PointersC - Pointers and VariablesC - Pointers and Variables Memory RepresentationC - Pointers Chaining

Pointers and Arrays

C - Pointers and One Dimensional ArrayC - Pointers and Two Dimensional ArrayC - Array of Pointers

Pointers and Strings

C - Pointers and Strings

Pointers and Functions

C - Pointers and Functions - Call by Value and Call by ReferenceC - Function returning pointer

Pointers and Structures

C - Pointers and StructuresC - Pointers and Array of StructuresC - Passing structure pointer to function

Handling Files

C - File Handling - Getting StartedC - File Handling - Read and Write CharactersC - File Handling - Read and Write IntegersC - File Handling - Read and Write multiple dataC - File Handling - Randomly Access Files

Command Line Arguments

C - Command Line Arguments

Dynamic Memory Allocation

C - Dynamic Memory Allocation - Getting StartedC - Dynamic Memory Allocation - malloc functionC - Dynamic Memory Allocation - calloc functionC - Dynamic Memory Allocation - realloc function

C - String

C Programming

In this tutorial we will learn about strings in C programming language.

What is a string?

A string is a sequence of characters enclosed in double quotes.

There is no string data type in C.

To store string values in C we create an array of type char.

Syntax of a character array to store string value is given below.

char strName[size];

Where, strName is the name of the variable and size tells us about the number of characters the array will hold.

Strings end with a NULL \0 character.

If we want to store a string like "Hello" having 5 characters then we will need a character array of size (5 + 1) i.e., 6 as the last character will be the NULL character.

Creating and initialising string variables

In the following example we are creating a string variable which is a character array of size 6 and assigning "Hello" to it.

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

We can skip the size and C will determine the size based on the number of elements in the array.

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

We can also assign the value in the following way.

char str[6] = "Hello";

All the methods shown above will create an array str of size 6 and can be represented as follows.

If the size is greater than the number of characters then in that case rest of the spaces in the array is filled with NULL \0 character.

char str[10] = "Hello";

And it can be represented as follows.

String input using scanf function

We can use the scanf() function to take string input from the user.

In the following example we are taking a string input from the user and saving it in str variable.

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

Points to note when using scanf function to take string input.

  • We don't have to use the & character before variable name.
    scanf("%s", varName);    //this is correct
    scanf("%s", &varName);   //this is wrong, & is not required
  • The scanf function will automatically add the NULL \0 character at the end of the string. So, we don't have to worry about it. But make sure that the character array size is big enough to hold the input string and the NULL character.
  • The scanf function will terminate user input as soon as it encounters a white space. So, if the user input is "Hello World" then only "Hello" will be saved.

Specifying field width for scanf

We specify the field width when we want to read specific number of characters.

In the following example we have a character array of size 10 and we are going to read only 4 characters.

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

In the above code %4s specify that we are restricting input to 4 characters.

Two things can happen in the above scenario.

  • If the field width is greater than the input string then rest of the space will be filled with NULL character.
  • If the field width is smaller than the input string then extra characters will be truncated.

String input using gets function

We use the gets method to take string input from the user without worrying about white spaces.

The gets() function is available in the stdio.h header file.

In the following code we are taking user input using the gets function.

char str[100];

String output using printf function

We can use the printf() function to output string.

In the following code we are printing out "Hello World" string.

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

String output using puts function

We can also use the puts() function to print out the string.

The puts() function is available in the stdio.h header file.

In the following code we are printing out "We are learning C programming language." string.

char str[100] = "We are learning C programming language.";