0% found this document useful (0 votes)
9 views

Strings and structuresin C

The document explains strings in C, describing them as one-dimensional arrays of characters ending with a null character ('\0'). It details two methods for declaring strings: using a char array and a string literal, highlighting the differences between them. Additionally, it introduces string manipulation functions and structures in C, providing examples and syntax for defining and using structures.

Uploaded by

ba23018846
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Strings and structuresin C

The document explains strings in C, describing them as one-dimensional arrays of characters ending with a null character ('\0'). It details two methods for declaring strings: using a char array and a string literal, highlighting the differences between them. Additionally, it introduces string manipulation functions and structures in C, providing examples and syntax for defining and using structures.

Uploaded by

ba23018846
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Strings in 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’).

There are two ways to declare a string in c language.

1. By char array
2. By string literal

Declaring string by char array in C language.


char ch[10]={'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't', '\0'};

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'};

Declaring the string by the string literal in C language.

For example:

char ch[]="javatpoint";

Comp211 1
In such case, '\0' will be appended at the end of the string by the compiler.

Difference between char array and string literal


There are two main differences between char array and literal.

● 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:

Sr.No Function & Purpose


.

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.

The following example uses some of the above-mentioned functions −


Live Demo
#include <stdio.h>
#include <string.h>

Comp211 3
int main () {

char str1[12] = "Hello";


char str2[12] = "World";
char str3[12];
int len ;

/* copy str1 into str3 */


strcpy(str3, str1);
printf("strcpy( str3, str1) : %s\n", str3 );

/* concatenates str1 and str2 */


strcat( str1, str2);
printf("strcat( str1, str2): %s\n", str1 );

/* total lenghth of str1 after concatenation */


len = strlen(str1);
printf("strlen(str1) : %d\n", len );

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.

Why use structure?

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;

};

Below is the description of Structure in C programming

Description of the Syntax

● 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.

Next, let us see how to declare variable of structure in C programming

How to Declare Structure Variables?

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.

● Declaration of Structure Variables with Structure Definition

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];

} storeA, storeB; // structure variables

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.

Structures in C Programming Example

https://www.tutorialspoint.com/cprogramming/c_structures.htm

Comp211 8

You might also like