Intro to C
All example programs and handouts, and many many more, are in a directory which should be accesible; ~kwalsh/vesta/Classes/T695 . Feel free to snoop around and see what you find there.
Hello World
The most basic functioning program, simply prints "Hello World" to the screen.
#include <stdio.h
main()
{
printf("hello, world\n");
}
How to compile:
We have two main compiliers; cc and gcc . Specialized programs might use something else.
>cc prog1.c
>gcc prog1.c
If it compiles OK, the executable will be a.out . Try to run it..
To name the executable something else (advisable typically) try this, and make sure it sends it to prog1 .
>cc -o prog1 prog1.c
Important/necessary parts of the program:
#include -- In this case stdio.h is a necessary header file for running a program with input or output. NOTE, if you ever use the math.h library, you must link when you compile, with a -lm so..
cc -o prog1 prog1.c -lm
Another good precaution with the gcc compiler is the -Wall flag, this turns on all warnings, a good habit to get into.
gcc -Wall -o prog1 prog1.c -lm
main() -- This is the main function of the program, wherein the heart of the programming typically lies in the following curly brackets. However, all functions are signified similarly.
printf(" ") -- Very basic output. All lines inside a function are followed by a semicolon.
\n Escape sequence signifying a newline... try putting more in and see what happens.
Variable types
Now a look at various types of variables that you will encounter as soon as you start trying to do math in your programs.
#include <stdio.h
int main(void)
{
int i = 0;
short j = 1;
long k = 2;
float x = 0.0;
double y = 1.0;
char c = 'm';
(void) printf("the value of i is %d\n", i);
(void) printf("the value of j is %d\n", j);
(void) printf("the value of k is %ld\n", k);
(void) printf("the value of x is %f\n", x);
(void) printf("the value of y is %lf\n", y);
(void) printf("the value of c is %c\n\n\n", c);
i = 123456;
j = 12345;
k = 1234567890;
x = 123.456;
y = 5.43e21;
c = 'q';
(void) printf("the value of i is %d\n", i);
(void) printf("the value of j is %d\n", j);
(void) printf("the value of k is %ld\n", k);
(void) printf("the value of x is %f\n", x);
(void) printf("the value of y is %lf\n", y);
(void) printf("the value of c is %c\n\n\n", c);
return 0;
}
Notice, the main(void) is preceded by int. The main function is in itself an actual function, and we look at functions down the road, this will be more important, but for now just make sure that return 0; is included at the end of the all the commands.
int -- Integer; 0,1,2,3,-1,-2,-3... on so on.
short and long -- More integers, but more memory allowed for a larger range of numbers.
float -- A real number 0,0.1,1e-12 and so on. A double again has a larger range.
char -- A character; a,b,r,y...
print("value is %d\n". i); -- The text is in quotes, with an escape character %d to signify the placement of the integer, i, which comes after a comma.
%d,%ld -- Escape sequence for integers, and long integers.
%f,%lf -- Escape sequence for floats, and doubles.
%c -- Escape sequence for chars.
Math stuff
Now lets take this same program and add some math in there to make sure we can manipulate numbers as we will want to do. Try these changes...
i = 1 + 2;
j = 9 / 3;
k = 43 - 1000;
x = 1.0 + 2.0;
y = 9.0 / 3.0;
Pretty straight forward. Try makeing changing it up, and testing limits and integer math....
If statements
Pretty useful stuff.
int i = 5;
if (i == 5) {
printf("i is equal to five.\n");
} else {
printf("i is not equal to five.\n");
}
if (i > 6) {
printf("i is greater than six.\n");
} else {
printf("i is not greater than six.\n");
}
Notice the == means equal to, instead of =. Similarly != is, not equal to.
Both examples have an else statement. This is not necessary, likewise, you can have many in a row.
For statements
int i;
double x = 1.0;
for (i = 0; i < 10; i++) {
x *= 2.0;
(void) printf("%d %lf\n", i, x);
}
Reading in values
#include <stdio.h
#include <stdlib.h
int main(int argv, char **argc)
{
/* this is a comment */
int i;
int j;
i = atoi(argc[1]);
j = atoi(argc[2]);
(void) printf("i = %d, j = %d\n", i, j);
return 0;
}
argv -- The number of command line values given when running the program
argc -- An array of full of the strings of those inputs.
atoi -- Function that takes the input string and makes them into integers. Could be changed into floats with atof.
/* blah */ -- A comment is always set off between these markers, it will not be considered by the compiler.
Arrays
int i;
double x[10];
for (i = 0; i < 10; i++) {
x[i] = i*50.1;
(void) printf("%d %lf\n", i, x[i]);
}
double x[10]; -- This means there are 10 doubles in x. You can reference indivual ones with x[1] . The indexes start at 0. Trying to refence the 20th x values will cause errors.
More next week and HELP.
Next week Zoe? will take this and run with it and get into functions and other useful stuff that you will want to use at some point.
Who to ask for help?
man pages. Always try a man page. Try man -a.
Google. 10 minutes on Google would find everything listed on this page probably.
A book. There are many around the dept. Kernighan and Ritchie wrote a great one, there is also Numerical Recipes, which has lots of code and algorithms.
Old programs, friends programs, and my example programs. The easiest way to figure out how to write a C program, is to have an old one in front of you so as to make sure the syntax is right and little stuff like that.
An older grad student, lots of us use C frequently, and might have a better reference.
Prof.? This is a tough one, usually reserved for the very difficult questions. Some of them know C very very well, but ALL of them are busy. Try the other options first.
Kevin Walsh
Last modified: Mon Sep 27 16:53:07 EDT 2004