Introduction to 1D Array - Integer & Character Array in C Programming & Edit set conversion, gets()

preview_player
Показать описание
In C language, arrays are referred to as structured data types. An array is defined as finite ordered collection of homogenous data, stored in contiguous memory locations.

Arrays are used to store list of Employee or Student names, or to store marks of students, or or to store list of numbers or characters etc.
Since arrays provide an easy way to represent data, it is classified amongst the data structures in C. Other data structures in c are structure, lists, queues, trees etc. Array can be used to represent not only simple list of data but also table of data in two or three dimensions.

Strings are defined as an array of characters. The difference between a character array and a string is the string is terminated with a special character ‘\0’.

Declaration of strings: Declaring a string is as simple as declaring a one-dimensional array. Below is the basic syntax for declaring a string.

char str_name[size];
In the above syntax str_name is any name given to the string variable and size is used to define the length of the string, i.e the number of characters strings will store. Please keep in mind that there is an extra terminating character which is the Null character (‘\0’) used to indicate the termination of string which differs strings from normal character arrays.

How to initialize strings?
You can initialize strings in a number of ways.

char c[] = "abcd";

char c[50] = "abcd";

char c[] = {'a', 'b', 'c', 'd', '\0'};

char c[5] = {'a', 'b', 'c', 'd', '\0'};

gets()
gets is a more convenient method of reading a string of text containing whitespaces.
Unlike scanf(), it does not skip whitespaces.
It is used to read the input until it encounters newline.

Edit Set Conversion in C: %[^\n]
The edit conversion code %[^\n] can be used as an alternative of gets.
C supports this format specification with scanf() function.
This edit conversion code can be used to read a line containing characters like variables and even whitespaces.
In general scanf() function with format specification like %s and specification with the field width in the form of %ws can read-only strings till non-whitespace part.
It means they cannot be used for reading a text containing more than one word, especially with Whitespaces.
Рекомендации по теме
Комментарии
Автор

If I would have known you earlier I would have been a good programmer by now, , , but I thank God knowing you it's not late.... I'm enjoying your classes

NabothBulaliInzai
welcome to shbcf.ru