You are on page 1of 6

Now, we're going to

look at a program

that has both input and output.

So again, very critical.

We frequently as a
user, we're used to it.

Let's say in an app,

we're always entering


information.

Somehow, our program,

our app has to capture


that information,

and then it makes use of it in

delivering a response
or computation.

It's going to be
a very simple one

that we all know about,

which is how to compute


the area of a circle.

We're going to do that


in terms of meters.

So let's look at this code again.

We're just getting used to how to

write a simple C program,

some of the elements of it.

Later, we're going to go


into detail again about what

each thing is and how


it's constructed.

So as before,

there's our standard


program comment.

It's really for readability.


In this case, it's
telling us this program

is to be used for computing


area of a circle.

Standard I/O. The


pre-process command

brings in both printf


and now scanf.

Scanf is a basic routine


for doing input.

By default, most cases we


will come from our keyboard.

In the very old days,

this could have come from a


card deck or a paper tape.

So it depends what you default.

In our case, standard


input is defaulted

to the computer keyboard.

Here we see another directive.

It's called a defined directive.

The define directive does


an interesting thing.

We have, and this


is by convention,

a pre-processor token.

In this case, we'll call it Pi,

for the obvious reasons.

Again, we want this


to be documentation.

We wanted to make the


program readable.

We will define Pi to

six significant
figures because that's
what we get to store
in a normal double.

So it's 3.14159.

Again, we introduce a
program, which is main.

We begin with what's called

the open brace for a


compound statement.

The first statement is a


declaration statement,

in which we define
area and radius.

Area and radius are


two identifiers.

Identifiers can be
ordinary words,

unless they're
keywords like double.

So unless they are keyword,

you can make use of whatever


you want to think about.

We initialize area to
zero and radius to zero.

We use 0.0 because that's

a clear indicator that we're

re-doing constants
which are double.

We could use zero,

and that would be allowed.

We then ask the user,

"Please enter a radius."

Otherwise, if we just
didn't do anything,

and we had omitted this,

and try omitting it,


you'll see that
your screen stops,

and you won't know what to do.

So this is what we call a prompt.

So it prompts the
user to enter radius.

This says, "Entered the radius


as a double long float."

We need the special


operator, ampersand here.

We'll explain that later.

But for the moment, trust me.

Just use ampersand of


whatever that variable is.

We're going to explain that much

later in detail in
the course because

that's really a parameter.

That's the address of radius.

We'll have to understand


things like address,

memory, pointer to really


understand what's going on.

Here's our area calculation.

Everybody should remember that

from grade school or high school,

Pi times radius times radius.

We don't have a simple


way to say square.

So there isn't a notation


like star star two.

So we use repeated
multiplication,

and then we print our answer.


Return with zero.

So let's make sure we


can use that program.

I've already compiled it.

Now, I'm going to execute it.

So I compiled it into code.

So we call dot exe. It


says, "Enter a radius."

Let's try the radius 1.0.

Oh yes, 3.1459.

Well, that makes sense.

That's a simple test.

When we go to the bug programs,

we want data for which


we know the answer.

We want simple cases first.

We can ask for a more


complicated case.

Let's repeat that.

So it's saying, "Enter


the radius again."

Why don't we do 2.5?

In this case, it's 19.634937.

That's correct as well.

So that shows you


how not only to do

simple output which we've


studied in a few cases,

but also simple input.

The little secret there

is outside of using
a format string.
Where you want the input,

you have a percent with


the appropriate format.

In this case,
doubles, so it's lf.

If it was an integer,

it would be percent D. Then,

instead of using the


ordinary variable name,

we have to use the


address of that variable.

You might also like