Strings In C

Strings, Array of characters!

You know what is ‘char’ datatype?

char refers to Character datatype. It stores one character at a time. For example:

char choice;

printf(“Do you want coffee? y or n : “);

scanf(“%c”,&choice);

The problem with char datatype is that you can only store one byte(i.e. one character at a time). So simply, here you need string.

String is nothing but array of character variables.

This is how you can declare a string.

Syntax:

char <stringname>[size];

Example:

char name[20];

I used ‘name’ character variable of size 20. Means it can take 20 characters at a time now.

You will get to know how to use string in taking input and showing output in next post.

Even string have inbuilt function that we will see later.

In string, a value is stored in a sequence in memory location. For example, “Tree” will store in variable a[10] like this:

ValueTree\0
Variable a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]
Memory Location 101478 101479 101480 101481 101482 101483 101484 101485 101486 101487

Published by Yash Kukreja

I am more than a proGrammer();

Leave a comment

Design a site like this with WordPress.com
Get started