Src
|
|
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 }
% gcc -o 3_first 3_first.c % 3_first Hello World!
% cat makefile
#
# makefile fopr 445
#
# Mon Jan 12 15:44:57 EST 1998
CC=gcc
T=3_first
3_first: 3_first.o
$(CC) -o $@ 3_first.o
nuke:
rm $T
% make
gcc -o 3_first 3_first.o
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
% gcc -o 3_include 3_include.c && 3_include 3_include.c:18: warning: type mismatch with previous external decl 3_include.c:14: warning: previous external decl of `div' 3_include.c:18: warning: type mismatch with previous implicit declaration 3_include.c:14: warning: previous implicit declaration of `div' 3_include.c:18: warning: `div' was previously implicitly declared to return `int' 5 / 2 = 2 5 / 2 = 5 % gcc -DADD_DEC -o 3_include 3_include.c && 3_include 5 / 2 = 2 5 / 2 = 2.5
... /* * SVID & X/Open */ #define M_E 2.7182818284590452354 #define M_LN10 2.30258509299404568402 #define M_PI 3.14159265358979323846 #define M_SQRT1_2 0.70710678118654752440 ... extern double acos (double); extern double cosh (double);
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
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).
Src
|
|
Last modified: 08/May/98 (11:53)