C Tutorial

Back to Tutorials

Next Lesson

 

Lesson 3: Strings and Input / Output

Lesson Requirements
A C compiler.

Instructions on using the LCCWin32 Compiler.

Lesson Summary
Now it's time to really get into C, and in particular, a concept that many people find quite hard to understand -- Strings. Let's take things slowly, and don't worry if you can't understand all of it at first.

What is a String?
A string is a series of characters, such as "Hello, Quake World!" from our C Tutorial: Lesson 1. Now for something that upsets a lot of programmers moving to C -- C doesn't have a String type! So how do we deal with strings in C?

In short, pretty much the same way that other programming languages deal with strings, except in C we get to see the inside-workings. A string is merely a list (or array) of characters. Here is how we represent strings in C:

char mystring[20];

This creates an array of 20 characters called "mystring". Now that we have an idea of how to represent strings, let's move onto Input / Output and then some example programs.

Input / Output
In this Lesson we are going to look at the Console I/O -- eg, reading and writing text to the DOS prompt. First of all, consider the term "Input / Output". In other words, reading text from the keyboard, and putting text onto the screen.

Here are two typical I/O functions which are defined in C's standard I/O Library functions (remember the "stdio.h" file?):

printf()
scanf()

These two functions are defined in a standard C Library (ie, someone has already written them for us). When we link our program, we include the Library for printf() and scanf(). At this point you might wonder how the compiler knows what printf() and scanf() take as parameters, etc. All of the standard functions are prototyped in header files (.h files), and the file "stdio.h" contains the prototypes for the standard I/O functions. You may notice that "stdio" is an abbreviation for "standard input output".

printf()
printf() is defined as:

int printf( const char *format [, argument]... );

What does this mean? Well, printf() is a function which returns an integer (either the number of characters printed, or a negative value if it failed). Its first arguement is a string representing the formatting, followed by an optional number of variables to be substitued into the format string.

The formatting string can contain various formatting symbols. So far we have seen "%d" to print an integer, "%s" for a string, and "%c" for a character. There are also control codes such as "\n" for a newline. Perhaps you should consult a good C reference manual along with this course to learn about the other symbols.

scanf()
scanf() is defined as:

int scanf( const char *format [,argument]... );

This is the reverse of printf() -- instead of printing formatted text, it reads formatted text from the Console (keyboard). It returns the number of variables it successfully retrieved.

Example
Let's write a simple program to ask for the user's name and age.

#include <stdio.h>

int main(int argc, char *argv[])
{
  char *name[40];
  int age;

  printf("What is your name?\n");
  scanf("%40s", name);
  printf("How old are you %s?\n", name);
  scanf("%d", &age);
  printf("So %s, you are %d years old and you're writing C.\n",   name, age);
}

Some points to notice here:

scanf("%40s", name); -- This line means we want to read in a string of maximum 40 characters, as we have only allocated memory for that number in our string (char *name[40];).

scanf("%d", &age); -- Do you notice the "&" in front of the variable name? This allows us to change its value (much akin to a var parameter in pascal). The "&" character will be explained in more detail in a later tutorial.

That about wraps up this tutorial. In the next tutorial we'll bring together some of the things we've learned. In the meantime, why don't you try this exercise:

1. Read in a list of 10 integers from the console, and print out the largest.

Till next time!

 

Back to Tutorials

Next Lesson

 


Copyright 1997, John Crickett & Neil Henderson.
Legal Information