💾 Archived View for spam.works › mirrors › textfiles › programming › c-easy.txt captured on 2023-06-16 at 20:08:58.

View Raw

More Information

-=-=-=-=-=-=-



[50/50]: C Lesson #4 (REPOST)
From  : Predat0r #1 @5211
Date  : Fri Jul 26 04:38:09 1991
Origin: Blitzkrieg [502-499-8933]
        [Louisville, Kentucky]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The Not #9 @5211
Tue Jan 15 23:30:35 1991
??????????????????????????????????????????????????????????????????????????????
?                           C Made Easier                                    ?
?      Brought to you by: The Not c/o Blitzkrieg (502/499-8933)              ?
?                                                                            ?
?                 I can be reached at the following:                         ?
?                   Internet  UK03981@ukpr.uky.edu                           ?
?                     and:                                                   ?
?                    WWIVNET  THE NOT@5211                                   ?
??????????????????????????????????????????????????????????????????????????????
?                        Lesson #4:  A Review                                ?
??????????????????????????????????????????????????????????????????????????????

A Review of the "printf()" Function
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Nearly every program example that does console output will use the
"printf()" function.  You have already seen several examples in the
preceding programs.  Let's take a more formal look at "printf()".

The general form of "printf()" is:

                printf("control string", argument list);

In the "printf()" function, the control string contains either
characters that will be displayed on screen, format commands that tell
"printf()" how to display the rest of the arguments, or both.  The
format codes that you have learned are shown here:

        Code            Meaning
        ~~~~            ~~~~~~~
         %d     Display an integer in decimal format
         %f     Display an float in decimal format
         %c     Display a character
         %s     Display a string

There are several other format codes, which will be explained later.

you may embed format-control commands anywhere in the first string of
characters.  When you call "printf()", it scans the control string.  The
"printf()" function simply prints on screen all regular characters as
they appear.  When encountering a format command, "printf()" remembers
it and uses it when printing the appropriate argument.  The function
matched up format commands and arguments left to right.  The number of
format commands in the control string tells "printf()" how many
subsequent arguments to expect.

The following examples will show you the "printf()" function in action.

        printf("%s %d","this is a string ", 100);

                displays: this is a string 100

        printf("this is a string %d", 100);

                displays: this is a string 100

        printf("number %d is decimal, %f is float.", 10,110789);

                displays: number 10 is decimal 110.789 is float.

        printf("%s", "HELLO\n");

                displays: HELLO

You MUST have the same number of arguments as you do format commands in
the control string.  If you do not, the screen will display either
garbage or no information at all.



A Review of the "scanf()" Function
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The "scanf()" function is one of C's input functions.  Although you can
use it to read virtually and type of data entered at the keyboard, in
the first few lessons you will be using it only to input integers or
floats.

The general form of "scanf()" is:

                scanf("control string", argument list);

For now, assume that the control string may only contain format codes.
The two codes that you will need are "%d" and "%f", which tell "scanf()"
to read an integer and a floating-point number.  The argument list must
contain exactly the same number of arguments as the number of format
codes in the control string.  If this is not the case, various problems
could occur - including a program crash.

In the argument list, an & (ampersand) must precede the variable that
will receive the value from the keyboard.  For now we'll say that the &
lets "scanf()" place a value into the argument.



Semicolons, Braces, and Comments
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
You may have been wondering why so many statements in C are terminated
with a semicolon.  In C, the semicolon is a statement terminator; thus,
you must end each individual statement with a semicolon.  The semicolon
indicates the end of one logical entity.  (If you are familiar with
Pascal, be careful.  The semicolon in Pascal is a statement separator
in C, it is a statement terminator.)

In C, a block is a set of logically connected statements, which is
inside opening and closing braces.  If you consider a block as a group
of statements with a semicolon after each statement, it makes sense that
the block is not followed by a semicolon.

Unlike BASIC, C does not recognize the end of the line as a terminator.
This mean C does not limit position, which makes it easier to group
statements together visually for clarity, as shown by these two
equivalent code fragments.

  x = y;

  y = y + 1;

  mul(x.y);

is the same as

  x = y;  y = y + 1;  mul(x,y);

You may place comments in C anywhere in a program.  Comments are
enclosed between two markers.  The starting comment marker is "/*" and
the ending comment is "*/".  In ANSI standard C, you may not nest
comments, as shown the following comment within a comment, which
will generate a compile-time error.

  /* this is /* an error */ */



Indentation Practices
~~~~~~~~~~~~~~~~~~~~~
As you may notice, the earlier examples indented certain statements.
The C language is free-form because C does not care where you place
statements in relation to each other on a line.

However, over the years, a common and accepted indentation style has
developed that creates readable programs.  These lessons will follow
that style and I recommend that you do so as well in your coding.  Using
this style you indent one level after each opening brace and back one
level at each closing brace.  There are certain statements that
encourage some additional indenting and these will be covered later.

Sometimes, in a particular complex routine, the indentation is so great
that the lines of code begin to wrap around.  To avoid wrapping, you can
break a statement into parts and put them on separate lines.  For
example, this statement is perfectly valid.

  count = 10 * unit /
          amount_left;

In general, you can break a line wherever you can place a space.
However you should break lines only when necessary because it can
confuse anyone reading the code.




C Libraries
~~~~~~~~~~~
The C library and its functions have been mentioned frequently in this
study of C.  Now it's time for you to learn about them.  All C compilers
have libraries that provide functions to perform most commonly needed
tasks.

When you use a function that is not part of your program you wrote, C
"remembers" its name.  When the linker takes over, it finds the missing
function and adds it to your object code.  The linker is a program that
combines your program with the necessary library functions.  The
functions in the library are in relocatable format.  This means that the
functions do not define the absolute memory address for the various
machine-code instructions, but rather keep only offset information.
When you program links with the functions in the standard library, C
uses these memory offsets to create the actual addresses used.  Several
technical manuals and books explain this process in more details.
However, you do not need any further explanation of the actual
relocation process in order to program in C.



C Keywords
~~~~~~~~~~
Like all other programming languages, C consists of keywords and syntax
rules that apply to each keyword.  A keyword is essentially a command
and, to a great extent, the keywords of a language define what can be
done and how it will be done.

All C keywords are in lowercase.  As stated earlier C, is
case-sensitive; hence, "else" is a keyword while "ELSE" is not.  You
may not use a keyword for any other purpose in a C program.  For
example, you cannot use a keyword as the name of a variable.



A Review of Terms
~~~~~~~~~~~~~~~~~
Compile-time -=- The events that occur while your program is being
        compiled.  A common compile-time occurrence is a syntax error.

Library -=- The file containing the standard functions that your program
        may use.  These function include all I/O operations, as well
        as other useful routines.

Linker -=- A program that links separately compiled functions together
        into one program; used to combine the functions in the standard
        C library with the code that you write.

Object code -=- The code that the computer can read and execute directly,
        and that has been translated from the source code of a program.

Run-time -=- The events that occur while your program is executing.

Source code -=- The text of a program that a user can read; commonly
        thought of as "the program."

                            End Of Lesson #4
                            ****************


Time left: 00:00:50

Current sub: [[@5211] Beginning & Intermediate C]
[Msg #50 of 50] ?=help, <ENTER>=next: