[Courses] C Programming For Absolute Beginners, Lesson 2: Fun With Printf, Scanf, Puts, and Variables

Carla Schroder carla at bratgrrl.com
Tue Feb 21 04:26:40 UTC 2012


=========
C Programming For Absolute Beginners, Lesson 2: Fun With Printf, Scanf, Puts, 
and Variables
=========

Allrighty then, in Lesson One we learned some basics of using gcc, the 
simplest structure of a C program, and some Linux tips for coders. Today we're 
going to continue with our simple example program and build on it, and learn 
how to make an interactive program that asks for user input and then does 
something with it.

Please remember there are no stupid questions-- the whole point of this is to 
learn, so ask!

## Fun With Printf ##

Printf is a fundamental C function, and it does all kinds of cool and useful 
things, so let's make it do stuff. We'll take our Lesson 1 example program and 
play some printf tricks with it. This example does multiple-line output:

/* 
   printfex (printf example); a simple example
   of how printf works
*/

#include <stdio.h>
void main()
{
  printf( "Hello, and welcome to the Beginning C Course!\n" );
  printf( "Look, we can do a second line.\n" );
  printf( "And a third line.\n");
  printf( "And a fourth.\n");
}

Remember to not use reserved words or existing command names for your source 
files and programs, or *fun confusion will result. (*May not be actually fun.)

****
Side note: A syntax-highlighting text editor will save you a lot of typos. For 
example, I never remember where to put the line break, \n, and my editor helps 
me with this by telling me when it's in the wrong place by changing the color.
****

Then compile and run it:

$ gcc -o printfex printfex.c

I copy my executables into a separate directory that I have put in my PATH; 
please review part 1 to learn how to do this.

$ printfex
Hello, and welcome to the Beginning C Course!
Look, we can do a second line.
And a third line.
And a fourth.

You can also do it this way:

#include <stdio.h>
int main()
{
  printf( "Hello, and welcome to the Beginning C Course!\n" "Look, we can do a 
second line.\n" "And a third line.\n" "And a fourth.\n");

  return 0;
}

Or like this:

#include <stdio.h>
int main()
{
  printf( "Hello, and welcome to the Beginning C Course!\n" 
  "Look, we can do a second line.\n" 
  "And a third line.\n" 
  "And a fourth.\n");

  return 0;
}

We don't even have to use printf, but can use the puts function instead, which 
automatically inserts a line break:

#include <stdio.h>
int main()
{
  puts( "Hello, and welcome to the Beginning C Course!" );
  puts( "Look, we can do a second line." );
  puts( "And a third line." );
  puts( "And a fourth.");

  return 0;
}

puts does one thing: print output followed by a line break. Let's make an 
interactive program to demonstrate puts:

/* 
   age; an example program showing 
   the difference between puts and printf
*/

#include <stdio.h>

int main()
{
    int age;

    puts( "Please enter your age: " );
    scanf( "%d", &age );
    printf( "Wow, you are %d years old.\n", age);

    return 0;
}

The output of this is nicely-formatted:

$ age
Please enter your age: 
80
Wow, you are 80 years old. 

There are a number of cool things happening in this tiny program: it asks for 
input, we enter something, and then it uses it correctly in a sentence. The 
scanf function scans and formats input, and printf formats output.  %d is a 
placeholder, and it means scan an integer as a signed decimal number. A signed 
integer can be either a negative or positive number; an unsigned integer is 
only positive. 

scanf( "%d", &age ); means "read the user input and place that value into the 
variable age." You must use the & symbol in front of your variable or it won't 
work. If you want to jump ahead this is your introduction to using pointers, 
which is pointing to a specific location on memory. Pointers is a weird and 
complicated subject to explain, so for now I'm going to leave it at "don't 
forget the & symbol when you're playing with scanf." (Anyone who wants to 
chime in with their own explanation of pointers is welcome!)

scanf has already assigned the value of the user input to our age variable, so 
all printf has to do is display it. 

Some other commonly-used scanf and printf placeholders are:

    %f -- float
    %c -- single character
    %s -- character strings
    %i -- signed integer, or hexadecimal when preceded by 0x and octal when 
preceded by 0

scanf is pretty simple and doesn't handle input errors very well. You can see 
for yourself by typing wrong input on purpose, such as letters and punctuation 
marks. In real life you'll use other functions, for example gets and fgets, 
which we will cover later in this course.

This example shows how to handle multiple variables with printf:

/* addition; an example of adding and displaying user input */

#include <stdio.h>

int main()
{
    int a, b, c;
     
    puts( "Please enter any number up to three digits: " );
    scanf( "%d", &a );
    puts( "Please enter another number up to three digits: " );
    scanf( "%d", &b );
    c = a + b;
    printf("%d + %d = %d\n", a, b, c);

    return 0;
}

When you run it it looks like this:

$ addition
Please enter any number up to three digits: 
34
Please enter another number up to three digits: 
567
34 + 567 = 601

Note how the order of each variable is managed-- if you change the order of a, 
b, c on the last printf line then you change the results.

I'm afraid I must stop here; we'll be back on track next Sunday with a nice 
long lesson. 

Homework: Modify the addition program to echo the user input as it is entered, 
so it looks something like this:

"Please enter any number up to three digits: 
34
You entered the number 34. Now please enter another number:"

You can also play around with the inputs and do different math operations.

Discussion: Perhaps someone could offer some thoughts on puts vs. printf, and 
what are some real-world functions to use in place of scanf?

-- 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Carla Schroder
ace Linux nerd
author of Linux Cookbook,
Linux Networking Cookbook,
Book of Audacity
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



More information about the Courses mailing list