[Courses] [C] Beginner's Lesson 4A: Arrays, Qualifiers, and R eading Numbers

Anand R anand.r at cybertech.co.in
Fri Oct 11 16:01:10 EST 2002

[root at tux linuxchix]# gcc a.c -o a -Wall
a.c: In function `main':
a.c:13: warning: passing arg 2 of `strcat' makes pointer from integer
without a cast
[root at tux linuxchix]# 

>>>>  Debugging exercise.  What is wrong with the following program?
		#include <string.h>
		#include <stdio.h>
		int main(void)
			char first[100];
			char last[100];
			char full[200];

			strcpy(first, "John");
			strcpy(last, "Doe");

			strcpy(full, first);
			strcat(full, ' ');
			strcat(full, last);
			printf("The name is %s\n", full);
			return 0;
	Okay, now it looks like things are going to get interesting!
	So far, we've been supplying all the data to our programs.
	Now it looks like we're going to learn a way to enter some
	data from the keyboard! !@#$%&*8^)

	The standard library function fgets() can be used to read a
	string from the keyboard. The general form of fgets() is:

		fgets( array_name, sizeof(array_name), stdin);

	where array_name is the name of a character array;
	is used to indicate the maximum characters to read, minus one for
	and stdin is the file to read.  In this case stdin is the standard
	input, or keyboard.  This program reads a line entered by the user
	and reports the length of the line. Since fgets() includes the
	end-of-line character, your string length will include a newline 

		#include <string.h>
		#include <stdio.h>
		int main(void)
			char line[80];

			printf("Enter a line: ");
			fgets(line, sizeof(line), stdin);
			printf("The length of the line is: %d\n",
			return 0;

	Why don't we change the name program listed above to ask for the
	user's first and last name?  Here is one way to do it:

		#include <string.h>
		#include <stdio.h>
		int main(void)
			char first[20];
			char last[20];
			char full_name[40];
			printf("Enter first name: ");
			fgets(first, sizeof(first), stdin);
			first[strlen(first) -1] = '\0'; /* trim off last
character */
			printf("Enter last name: ");
			fgets(last, sizeof(last), stdin);
			last[strlen(last) -1] = '\0'; /* trim off last
character */
			strcpy(full, first);
			strcat(full, " "); 
			strcat(full, last); 
			printf("The name is %s.\n", full);
			return 0;

	Running the program gives the following output:

			Enter first name: Jane
			Enter last name: Doe
			The name is Jane Doe.
	So what's the deal with having to trim off the last character?
	Since fgets() includes the newline and since there is a NUL
	at the end of the string, if you don't trim off the last
	character, the output would look like this:

			The name is Jane
	Here is what the array last[] looks like after "Doe" is entered:

		last[0] = 'D'
		last[1] = 'o'
		last[2] = 'e'
		last[3] = '\n'
		last[4] = '\0'  /* end of string */

	The line  last[strlen(last) - 1] = '\0';  overwrites the newline
	with a NUL so the name will all come out on the same line. Cool.

Multiple dimensional arrays have more than one dimension. We can
declare a two dimensional array like this:

	/* comment stating what the array is for */
	type array_name[size1][size2];

Note that each dimension is in square brackets.  matrix[5][5] is
a two dimensional array that is 5 elements by 5 elements. We might
access an element like this:  matrix[2][4] = 10;
We can add as many dimensions to an array as we have memory for it:


Initialize a multi-dimensional array by enclosing each element in
curly braces {}.  char tictac[3][3]; declares a tic-tac-toe board 
and initializes its contents to blanks:

	char tictac[3][3] =
			{' ', ' ', ' ',},
			{' ', ' ', ' ',},
			{' ', ' ', ' ',},
To put an 'X' in the middle of the board: tictac[1][1] = 'X';

Look at it like this:   
       0   1   2
   0     |   |
   1     | X |
   2     |   |

We're ready to start reading numbers now.  Just about every other
textbook out there uses the scanf() function to do this, but Steve
Oualline has a better idea.  He says that scanf() is notorious for
its poor end-of-line handling.  But he knows how to get around that:
we won't use scanf() at all!  Hooray!  Instead, we'll use fgets()
to read a line of input, and use sscanf() to convert the text into
numbers.  This sounds like fun.

We'll use the character variable `line' to read from the keyboard:

	char line[100];  /* Line of keyboard input */

when we want to process input, we use:

	fgets(line, sizeof(line), stdin);
	sscanf(line, _format_, &variable1, &variable2, ...);

Note the ampersand in front of variable1. Don't forget it!
The _format_ is a string similar to the printf() format string.

In this program we use sscanf() to get a number and double it.

	#include <stdio.h>
	int main(void)
		char line[100];
		int value;

		printf("Enter a number: ");
		fgets(line, sizeof(line), stdin);
		sscanf(line, "%d", &value);
		printf("Twice %d is %d\n", value, value * 2);
		return 0;

I'm going to stop here. I'll finish this later.

Happy Programming!

More information about the Courses mailing list