Strings and structuresin C
Strings and structuresin C
A string is a one dimensional array of characters that ends with the null character (‘\0’).
This array or string is used to control text be it words or sentences. Every character of
the array holds one byte of memory, and the last character must terminate with 0.
It is crucial to put a termination character (‘\0’) as it is the sole way to identify the end of
the string. When a string is set out such as char s[20], it initializes the s[20] character in
the memory with a null character (‘\0’).
1. By char array
2. By string literal
As we know, array index starts from 0, so it will be represented as in the figure given
below.
While declaring string, size is not mandatory. So we can write the above code as given
below:
char ch[]={'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't', '\0'};
For example:
char ch[]="javatpoint";
Comp211 1
In such case, '\0' will be appended at the end of the string by the compiler.
● We need to add the null character '\0' at the end of the array by ourself whereas, it is
appended internally by the compiler in the case of the character array.
● The string literal cannot be reassigned to another set of characters whereas, we can
reassign the characters of the array.
String Example in C
Let's see a simple example where a string is declared and being printed. The '%s' is used as a
format specifier for the string in c language.
1. #include<stdio.h>
2. #include <string.h>
3. int main(){
4. char ch[11]={'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't', '\0'};
5. char ch2[11]="javatpoint";
6.
7. printf("Char Array Value is: %s\n", ch);
8. printf("String Literal Value is: %s\n", ch2);
9. return 0;
10.}
Output
Char Array Value is: javatpoint
String Literal Value is: javatpoint
String Functions
Strings in c provide an extensive range of functions for manipulating null terminated
strings.
Comp211 2
Despite its nonvariable type categorization, the C programming library does not scrimp
on string manipulation methods. Using some of the numerous string functions, you can
do almost whatever you want with a string. And if those functions are insufficient, you
may create your own. Here are some functions in c that are used to manipulate strings:
1
strcpy(s1, s2);
Copies string s2 into string s1.
2
strcat(s1, s2);
Concatenates string s2 onto the end of string s1.
3
strlen(s1);
Returns the length of string s1.
4
strcmp(s1, s2);
Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if
s1>s2.
5
strchr(s1, ch);
Returns a pointer to the first occurrence of character ch in string s1.
6
strstr(s1, s2);
Returns a pointer to the first occurrence of string s2 in string s1.
Comp211 3
int main () {
return 0;
}
When the above code is compiled and executed, it produces the following result −
strcpy( str3, str1) : Hello
strcat( str1, str2): HelloWorld
strlen(str1) : 10
Comp211 4
Structures in C
A structure can be defined as a single entity holding variables of different data types that are
logically related to each other. All the data members inside a structure are accessible to the
functions defined outside the structure. To access the data members in the main function, you
need to create a structure variable.
In C, there are cases where we need to store multiple attributes of an entity. It is not necessary
that an entity has all the information of one type only. It can have different attributes of
different data types. For example, an entity Student may have its name (string), roll number
(int), marks (float). To store such type of information regarding an entity student, we have the
following approaches:
● Construct individual arrays for storing names, roll numbers, and marks.
● Use a special data structure to store the collection of different data types.
Comp211 5
Syntax to Define a Structure in C
struct structName
// structure definition
Data_type1 member_name1;
Data_type2 member_name2;
Data_type2 member_name2;
};
● Keyword struct: The keyword struct is used at the beginning while defining a structure
in C. Similar to a union, a structure also starts with a keyword.
● structName: This is the name of the structure which is specified after the keyword
struct.
● data_Type: The data type indicates the type of the data members of the structure. A
structure can have data members of different data types.
● member_name: This is the name of the data member of the structure. Any number of
data members can be defined inside a structure. Each data member is allocated a
separate space in the memory.
Variables of the structure type can be created in C. These variables are allocated a separate
copy of data members of the structure. There are two ways to create a structure variable in C.
Comp211 6
This way of declaring a structure variable is suitable when there are few variables to be
declared.
Syntax
struct structName
// structure definition
Data_type1 member_name1;
Data_type2 member_name2;
Data_type2 member_name2;
} struct_var1, struct_var2;
Example
struct bookStore
// structure definition
char storeName
int totalBooks;
char storeLicense[20];
The structure variables are declared at the end of the structure definition, right before
terminating the structure. In the above example, storeA and storeB are the variables of the
Comp211 7
structure bookStore. These variables will be allocated separate copies of the structure’s data
members that are- storeName, totalBooks, and storeLicense.
https://www.tutorialspoint.com/cprogramming/c_structures.htm
Comp211 8