Module 4
Module 4
Ch 13: STRINGS
Introduction
• A string is a null-terminated character array.
• Declaration Syntax: char str[size];
• Example: char str[] = “HELLO”; H E L L O \0
• Char str[]=“H”; H \0
• Char ch=‘H’; H
• Char str[]=“ ”; \0
• The name of the character array (or the string) is a pointer to
the beginning of the string.
• If we declare str as, char str[5] = “HELLO”;
• The size of the string should be equal to maximum number
of characters in the string plus one.
Introduction (Contd..)
• Similar to arrays, subscripts are used to access the elements
of the character array. The subscript starts with a zero.
• The other way to initialize a string :
char str[ ]={‘H’,’E’,’L’,’L’,’O’,’\0’};
• We can also declare a string with size much larger than the
number of elements that are initialized.
• char str[10]=”HELLO”;
H E L L O \0 \0 \0 \0 \0 \0
2) struct employee
{
int id;
char name[10];
float salary;
}emp1,emp2;