next next up down toc toc Src mail

all, section 3.1.

3.1.  The First Step

 1      /*
 2       *
 3       *      Mon Jan 12 15:39:10 EST 1998
 4       *      (c) Hans-Peter Bischof
 5       *      SUNY/Oswego
 6       *
 7       *      Module:         3_first.c
 8       *      Description:    the first C programm
 9       */
10      
11      
12      #include <stdio.h>      /* cpp(1)               */
13      
14      int main(void)
15      {
16              printf("Hello World!\n");
17              exit(0);
18      }

 1      /*
 2       *
 3       *      Module:         3_include.c
 4       *      Description:    declare before use
 5       */
 6      
 7      #ifdef ADD_DEC
 8              float div(float first, float second);
 9      #endif
10      
11      int main(void)
12      {
13          printf (" %d / %d  = %d \n", 5, 2, 5/2);
14          printf (" %g / %g  = %g \n", 5.0, 2.0, div ( 5.0, 2.0) );
15      }
16      
17      float div( float a, float b )
18      {
19              return a / b;
20      }
21              

 1      /*
 2       *      Module:         3_cpp.c
 3       */
 4      
 5      
 6      #include <stdio.h>      /* cpp(1): global       */
 7      #include "stdio.h"      /* cpp(1): local        */
 8      
 9      int main(void)
10      {
11      int four = 4;
12      int five = 5;
13              
14              if ( four < five )
15                      swap(four, five );
16              exit(0);
17      }

 1      /*
 2       *      Module:         stdio.h
 3       */
 4      
 5      #ifndef STDIO_H
 6      #       define STDIO_H
 7      #       define swap(a, b )      { int x; x = a; a = b; b = x; }
 8      #endif

[picture]

 1      /*
 2       *    Module:        3_pointer.c
 3       *    Description:    a pointer example
 4       */
 5      
 6      #include <stdlib.h>
 7      #include <stdio.h>
 8      long random(void);
 9      
10      
11      #define    MAX    3
12      #define    TYPE    long
13      
14      void main(void)
15      {
16      TYPE * numbers;
17      int index;
18      
19          if ( ( numbers = calloc(MAX, sizeof(TYPE) ) ) == NULL ) {
20              perror("Not enough space for the numbers!");
21              exit(10);
22          }
23      
24          for ( index = 0; index < MAX; index ++ )
25              numbers[ index ] = (long)random();
26      
27          for ( index = 0; index < MAX; index ++ )
28              printf("numbers + %d = %ld\n",
29                     index, *(numbers + index ) );
30      }
31      

% 3_pointer
numbers + 0 = 2078917053
numbers + 1 = 143302914
numbers + 2 = 1027100827

Please browse through man2 (introduction to system calls and error numbers) and man3 (introduction to functions and libraries).


back next up down toc toc Src mail


Created by unroff & hp-tools. © by Hans-Peter Bischof. All Rights Reserved (1998).

Last modified: 08/May/98 (11:53)