Jump to content

Anyone Have Good Online Tutorials For Learning C?


OptikaliLLusion

Recommended Posts

  • 2 weeks later...
http://www.lysator.liu.se/c/bwk-tutor.html

 

Search google for "C tutorial" and look through them I suppose? I think you might be better off with a good book though..!

548672[/snapback]

 

Disclaimer: This ``tutorial'' is presented as a historical document, not as a tutorial.  Although it has lost little of its didactic value, it describes a language that C compilers today do no longer understand: the C of 1974, four years before Kernighan and Ritchie published the first edition of ``The C Programming Language''.

 

Might want to skip this tutorial, as it will not teach you anything you can use. Get the book quoted above, and remember to get the companion workbook that goes with it, you will need it. And of course, you need a compiler.

Share this post


Link to post
Share on other sites

2.  A Simple C Program

 

      main( ) {

              printf("hello, world");

      }

 

A C program consists of one or more functions, which are similar to the functions and subroutines of a Fortran program or the procedures of PL/I, and perhaps some external data definitions.  main is such a function, and in fact all C programs must have a main.  Execution of the program begins at the first statement of main.  main will usually invoke other functions to perform its job, some coming from the same program, and others from libraries.

 

One method of communicating data between functions is by arguments.  The parentheses following the function name surround the argument list; here main is a function of no arguments, indicated by ( ).  The {} enclose the statements of the function.  Individual statements end with a semicolon but are otherwise free-format.

 

printf is a library function which will format and print output on the terminal (unless some other destination is specified).  In this case it prints

 

      hello, world

 

A function is invoked by naming it, followed by a list of arguments in parentheses.  There is no CALL statement as in Fortran or PL/I.

 

3.  A Working C Program; Variables; Types and Type Declarations

Here's a bigger program that adds three integers and prints their sum.

 

      main( ) {

              int a, b, c, sum;

              a = 1;  b = 2;  c = 3;

              sum = a + b + c;

              printf("sum is %d", sum);

      }

 

Arithmetic and the assignment statements are much the same as in Fortran (except for the semicolons) or PL/I.  The format of C programs is quite free.  We can put several statements on a line if we want, or we can split a statement among several lines if it seems desirable.  The split may be between any of the operators or variables, but not in the middle of a name or operator.  As a matter of style, spaces, tabs, and newlines should be used freely to enhance readability.

 

C has four fundamental types of variables:

 

    * int  integer (PDP-11: 16 bits; H6070: 36 bits; IBM360: 32 bits)

    * char  one byte character (PDP-11, IBM360: 8 bits; H6070: 9 bits)

    * float  single-precision floating point

    * double  double-precision floating point

 

There are also arrays and structures of these basic types, pointers to them and functions that return them, all of which we will meet shortly.

 

All variables in a C program must be declared, although this can sometimes be done implicitly by context.  Declarations must precede executable statements.  The declaration

 

      int a, b, c, sum;

 

declares a, b, c, and sum to be integers.

 

Variable names have one to eight characters, chosen from A-Z, a-z, 0-9, and _, and start with a non-digit.  Stylistically, it's much better to use only a single case and give functions and external variables names that are unique in the first six characters.  (Function and external variable names are used by various assemblers, some of which are limited in the size and case of identifiers they can handle.) Furthermore, keywords and library functions may only be recognized in one case.

 

4.  Constants

We have already seen decimal integer constants in the previous example-- 1, 2, and 3.  Since C is often used for system programming and bit-manipulation, octal numbers are an important part of the language.  In C, any number that begins with 0 (zero!) is an octal integer (and hence can't have any 8's or 9's in it).  Thus 0777 is an octal constant, with decimal value 511.

 

A ``character'' is one byte (an inherently machine-dependent concept).  Most often this is expressed as a character constant, which is one character enclosed in single quotes.  However, it may be any quantity that fits in a byte, as in flags below:

 

      char quest, newline, flags;

      quest = '?';

      newline = '\n';

      flags = 077;

 

The sequence `\n' is C notation for ``newline character'', which, when printed, skips the terminal to the beginning of the next line.  Notice that `\n' represents only a single character.  There are several other ``escapes'' like `\n' for representing hard-to-get or invisible characters, such as `\t' for tab, `\b' for backspace, `\0' for end of file, and `\\' for the backslash itself.

 

float and double constants are discussed in section 26.

 

5.  Simple I/O -- getchar, putchar, printf

 

      main( ) {

              char c;

              c = getchar( );

              putchar

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...