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

Lecture6 Complex C Data Types

The document discusses complex data types in C programming, focusing on arrays and character strings. It explains how arrays are used to store ordered sequences of data elements and highlights the importance of the null character in defining strings. Additionally, it emphasizes that strings are essentially arrays of characters and outlines the differences between string constants and character constants.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lecture6 Complex C Data Types

The document discusses complex data types in C programming, focusing on arrays and character strings. It explains how arrays are used to store ordered sequences of data elements and highlights the importance of the null character in defining strings. Additionally, it emphasizes that strings are essentially arrays of characters and outlines the differences between string constants and character constants.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

C Programming:

complex Data Types

1
Arrays
 There are times when we need to store a complete list of numbers or other data
items. You could do this by creating as many individual variables as would be
needed for the job, but this is a hard and tedious process.
 For example, suppose you want to read in five numbers and print them out in
reverse order. You could do it the hard way as:
 int al,a2,a3,a4,a5;
 scanf("%d %d %d %d %d",&a1,&a2,&a3,&a4,&a5);
 printf("%d %d %d %d %d'',a5,a4,a3,a2,a1);
 What if the problem was read 100 or more values and print it in reverse order?
 What we would really like to do is to use a name like a[i] where i is a variable
which specifies which particular value we are working with.
 This is the basic idea of an array and nearly all programming languages provide
this sort of facility.

© Prof Suvendi Rimer, UJ


Array Types [1]
 An array is an ordered sequence of data elements of one type.
 Array types indicate data structures, which may contain one or more
elements of any type including other arrays.
 An array can contain only homogeneous elements.
 Array variables are defined by appending square brackets to the variable
name: [], enclosing a value which indicates the size of the array.
 In C you have to declare an array before you use it - in the same way you
have to declare any sort of variable.
 For example,int a[5];
 declares an array called a with five elements. The first element is a[0]
and the last a[4].
 C arrays always start counting at zero!
 type array[size]
 declares an array of the specified type and with size elements. The first
array element is array[0] and the last is array[size-1].

© Prof Suvendi Rimer, UJ


Array Types [2]
 The name of an array variable used on its own results in a pointer to the first
element of that array, and not in the value of that array.
 Arrays can thus not be assigned to one another.
 An element in an array may be selected by applying the array element
selection operator to it, which consists of square brackets enclosing an
integer expression, which is used as an index into the array.
 Arrays in the C language may have more than one dimension and are stored
in row-major order.
 Using arrays, the problem of reading in and printing out a set of values in
reverse order becomes simple.

© Prof Suvendi Rimer, UJ


Character Strings [1]
 C has no special variable type for strings.
 Instead, strings are stored in an array of char type.
 An array of character variables is in no way different from an array of
numeric variables
 Example, if you want to read in and print your name.
 The trouble with character arrays is that to use them as if they were text
strings you have to remember how many characters they hold.
 In other words, if you declare a character array 40 elements long and store
H E L L O in it you need to remember that after element 4 the array is empty.
 This is such a nuisance that C uses the simple convention that the end of a
string of characters is marked by a null character.
 A null character is the character with ASCII code 0.
 If you want to store the null character in a character variable you can use
the notation ‘\0’
 In C programming, a string is a sequence of characters terminated with a
null character \0. For example: char c[] = “C STRING";
 C automatically adds a null character and stores each character in a
separate element when you use a string constant.
 When the compiler encounters a sequence of characters enclosed in the
double quotation marks, it appends a null character \0 at the end by default.
C S T R I N G \0
© Prof Suvendi Rimer, UJ
Character strings [2]
 NOTE: only difference is the declared type of the array and the %c used to
specify that the data is to be interpreted as a character in scanf and printf.
 A string constant is indicated by double quotes as opposed to a character
constant which is indicated by a single quote. For example: "A“ is a string
constant, but 'A‘ is a character constant.
 Remember: "A" consists of two characters, the letter A followed by \0 whereas
'A' is just the single character A.
 How to declare a string? char str[5]; str[0] str[1] str[2] str[3] str[4]

 In C, the fundamental data type is the array, and strings are grafted on. For
example, if you try something like:
 char name[40]; name="Hello" it will not work.
 How to initialize strings? You can initialize strings in a number of ways.
char c[] = "abcd";
char c[50] = "abcd"; c[0] c[1] c[2] c[3] c[4]
char c[] = {'a', 'b', 'c', 'd', '\0'}; a b c d \0
char c[5] = {'a', 'b', 'c', 'd', '\0'};

© Prof Suvendi Rimer, UJ


Character strings [3]
 Example: char c[5] = "abcde";
 Here, we are trying to assign 6 characters (the last character is '\0') to a char
array having 5 characters. This is bad and you should never do this.

 Assigning Values to Strings


 Arrays and strings are second-class citizens in C; they do not support the
assignment operator once it is declared. For example: char c[100];
c = "C programming"; // Error! array type is not assignable.

 However, you can print strings using printf and read them into character
arrays using scanf.

© Prof Suvendi Rimer, UJ


Character strings [4]
 For example:
#include <stdio.h>

void main(void)
{
char name[40] ="hello";

printf("%s\n",name);
scanf("%s",name);
printf("%s",name);
}
 This program reads in the text that you type, terminating it with a null and
stores it in the character array name.
 It then prints the character array treating it as a string, i.e. stopping when it
hits the first null string.
 Note the use of the "%s" format descriptor in scanf and printf to specify that
what is being printed is a string.

© Prof Suvendi Rimer, UJ


Summary
 A character string is a series of one or more characters: “Hello World!”
 The double quotation marks are not part of the string.
 They are there to mark off the string, just as a single quotation mark is used
to mark of a character.
 The null character (\0) is used to mark the end of a string.
 The null character is not the digit zero. It is the non-printing character whose
ASCII code number is 0.
 Strings in C are always stored with this terminating null character.
 The presence of the null character means that the array must always have at
least one more cell than the characters to be stored.

 Strings versus characters


 The string “x” is not the same as the character ‘x’.
 One difference is that ‘x’ is a basic type (char) and “x” is a derived type,
an array of char.
 A second difference is that “x” consists of two characters: ‘x’ and the null
character.

© Prof Suvendi Rimer, UJ

You might also like