C Programming Under Linux: P2T Course, Martinmas 2003-4 C Lecture 3
C Programming Under Linux: P2T Course, Martinmas 2003-4 C Lecture 3
C Programming Under Linux: P2T Course, Martinmas 2003-4 C Lecture 3
Arrays
Strings
Reading in from the Keyboard
http://www.physics.gla.ac.uk/~kaiser/
int main()
{
data[0] = 34.0;
data[1] = 27.0;
data[2] = 45.0;
data[3] = 82.0;
data[4] = 22.0;
Output:
Total 210.000000 Average 42.000000
P2T: C Lecture 3 – p.4/21
Multidimensional Arrays
Arrays can have more than one dimension. The declaration for
a two-dimensional array is
type variable[size1][size2]; /* comment */
Example:
int matrix[2][4]; /* a typical matrix */
C does not follow the notation used in other languages, e.g.
matrix[10,12].
to access an element of the two-dimensional array matrix we
use the notation
matrix[1][2] = 10;
C allows to use as many dimensions as needed, limited only
by the amount of memory available. A four-dimensional array
four dimensions[10][12][9][5] is no problem.
(p array.c)
#include <stdio.h> printf("array[%d] ", 1);
printf("%d ", array[1,0]);
/* Array of numbers */ printf("%d ", array[1,1]);
int array[3][2]; printf("\n");
int product codes[3] = 10, 972, 45 ;
If no dimension is given, C will determine the dimension from
the number of elements in the initialisation list:
int product codes[] = 10, 972, 45 ;
The same kind of initialisation at declaration can also be used
for multidimensional arrays:
int matrix[2][4] =
{
{1, 2, 3 ,4},
{10, 20, 30, 40}
};
int main()
{
strcpy(first, "John"); /* Initialize first name */
strcpy(last, "Lennon"); /* Initialize last name */
Output:
The full name is John Lennon P2T: C Lecture 3 – p.12/21
Reading in Strings with fgets
The standard function fgets can be used to read a string from the
keyboard. The general form of an fgets statement is:
fgets(name, size, stdin);
name
is the name of a character array, aka a string variable. The line,
including the end-of-line character, is read into this array.
size
fgets reads until it gets a line complete with ending n or it
reads size - 1 characters. It is convenient to use thesizeof
function: fgets(name, sizeof(name), stdin);
because it provides a way of limiting the number of characters
read to the maximum number that the variable can hold.
stdin
is the file to read. In this case the ‘file’ is the standard input or
keyboard. Other files will be discussed later under file
input/output.
P2T: C Lecture 3 – p.13/21
fgets - Example 1
int main()
{
printf("Enter a line: ");
fgets(line, sizeof(line), stdin);
Output:
Enter a line: test
The length of the line is: 5
test has only 4 characters - but fgets also gets the n character.
int main() {
printf("Enter first name: ");
fgets(first, sizeof(first), stdin);
strcpy(full, first);
strcat(full, " ");
strcat(full, last);
int main()
{
printf("Enter a value: ");
Output:
kaiser@npl03:˜/oreilly/pracc/double> ./double
Enter a value: 21
Twice 21 is 42
int main()
{
printf("Enter width height? ");
The source code of tri.c contains two of the most common errors:
One missing */ at the end of a comment:
int height; /* the height of the triangle */
and one missing semi-colon at the end of the second printf
statement:
printf("The area is %d n", area);