[Instructions] [Search] [Current] [News] [Syllabus] [Glance] [Links] [Handouts] [Outlines] [Labs] [Assignments] [Exams] [Examples] [Project]
Back to Background. On to Types: Typedefs, Enums, Structs, Unions, and Arrays.
Held Wednesday, February 2, 2000
Overview
Today, we go ``back to the basics'' for the C programming language. I will assume that you know some Java in this overview.
Notes
Contents
Summary
/*
Print a greeting. A standard first program.
Author: Too many programmers.
Version: 1.1 of 2 February 2000
*/
/*
Since we're doing output, we include the header that
defines the standard I/O functions.
*/
#include <stdio.h>
/*
Programs start with main.
*/
main()
{
printf("Hello world!\n");
} /* main */
/* and end with */.
\n.
type name(parameters) {
body
}
int.
main method in a slightly different
form.
main(int argc, char *argv[])
argc is the number of command line arguments
argv is an array of the arguments
atoi, for
``atom to integer''.
printf, you read
input with scanf.
scanf is
scanf(pattern, address-of-variable);
int i;
scanf("%d", &i);
char str[128];
scanf("%s", str);
hello.c.
% gcc hello.c
% gcc -c hello.c
% gcc -o hello hello.o
% gcc -o goodbye hello.o
-o flag do?
% hello > junk
#include from the beginning of hello.c
greet(char *name), to
hello.c. The function should print a simple
greeting.
main to use the function. Use some
default name, such as your own.
greet to a separate file,
helloextras.c.
hello.c with
% gcc hello.c
hello.c wtih
% gcc -o hello.c
helloextras.c with
% gcc -c helloextras.c
% gcc -o newhello hello.o helloextras.o
% gcc -o hello hello.o
main function to helloextras.c.
% gcc -o newhello hello.o helloextras.o
greet, whose behavior is given
by the following examples
% greet SamR Hi SamR! % greet Vivek Hi Vivek!
arguments, which prints out
the arguments preceded by the argument number. Here's some
sample output
% arguments Hello "S. Rebelsky" 0: arguments 1: Hello 2: S. Rebelsky
factorial that computes the factorial
of a value given on the command line.
silly, that asks for a name and
a number, and prints out some pithy saying that uses the name
and the number.
Thursday, 20 January 2000
Wednesday, 2 January 2000
Back to Background. On to Types: Typedefs, Enums, Structs, Unions, and Arrays.
[Instructions] [Search] [Current] [News] [Syllabus] [Glance] [Links] [Handouts] [Outlines] [Labs] [Assignments] [Exams] [Examples] [Project]
Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.
This page may be found at http://www.math.grin.edu/~rebelsky/Courses/CS364/2000S/Outlines/outline.05.html
Source text last modified Wed Feb 2 13:39:04 2000.
This page generated on Fri Feb 4 13:52:26 2000 by Siteweaver. Validate this page's HTML.
Contact our webmaster at rebelsky@grinnell.edu