C Lecture 4

[Previous Lecture] [Lecture Index] [Next Lecture]

Function Prototypes

Syntax:
return-type func-name(arg-1-type arg-1-name,
	arg-2-type arg-2-name, etc );
(i.e., same as function preamble, but semi-colon instead of function body)

Example:

#include <stdio.h>

char *my_fgets(char *b, int bsize, FILE *fp);

int
main()
{
    char buf[1024];
    while (fgets(buf, sizeof(buf), stdin))
	printf("%s\n", buf);
    return 0;
}

char *
my_fgets(char *b, int bsize, FILE *fp)
{
    ...
}

Character classification routines (ctype.h)

#include <ctype.h>
#include <ctype.h>

int
atoi(char *str)
{
    char *p;
    int num;
    int sign = 1;

    for (p = str; isspace(*p); p++)
	;
    if (*p == '-') {
	sign = -1;
	p++;
    }
    for (num = 0; isdigit(*p); p++)
	num = num * 10 + (*p - '0');
    return sign * num;
}

Command Line Arguments

Example:
#include <stdio.h>

int
main(int argc, char *argv[])
{
    int i;

    for (i = 1; i < argc; i++) {
	if (i > 1)
	    printf(" ");
	printf("%s", argv[i]);
    }
    printf("\n");
    return 0;
}

[Previous Lecture] [Lecture Index] [Next Lecture]