# CS402: Worksheet 1 Introduction to C 1 Hello, World! ```CS402: Worksheet 1
Introduction to C
1
Hello, World!
First things first, lets get up and running with the ubiquitous ”Hello, world!” program. Open
up your favourite text editor (maybe vim or emacs), and type in the following program, saving
it as hello.c:
# include &lt; stdio .h &gt;
int main ( int argc , char * argv []) {
printf ( &quot; Hello , world !\ n &quot; ) ;
return 0;
}
To compile this program, open up a terminal window and change to the directory containing the
program. Then use the GCC compiler like so:
gcc -o helloworld hello.c
You will notice a few differences between C and Java, but overall the structure of the programs
is mostly the same. One thing to notice is that you need to put special characters like the line
break (\n) at the end of strings you want to print.
Run this program with:
./helloworld
1
2
Programming Practice
This next program calculates factorials, demonstrating the use of functions and for loops in C.
In C, it is important that you define your functions before you use them. You can do this just by
keeping them further up the file, or you can use prototype definitions that just state the return
type, name, and arguments of the function. The prototype of the factorial function would
look like this:
int factorial ( int n ) ;
Below is the factorial program:
# include &lt; stdio .h &gt;
int factorial ( int n ) {
int i = 0;
int fac = 1;
for ( i = 1; i &lt;= n ; i ++) {
fac = fac * i ;
}
return fac ;
}
int main ( int argc , char * argv []) {
int fac_five = factorial (5) ;
printf ( &quot; 5! is % d \ n &quot; , fac_five ) ;
}
Write a program that uses the formula ◦ C = 59 (◦ F − 32) to print a table of Fahrenheit temperatures, from 0 to 300 in steps of 20◦ , with the corresponding temperatures in Celsius. The
example output would be something like:
0
20
40
-17
-6
4
.
.
.
300 148
2
3
Pointers
Pointers are one of the key differences between C and Java. A pointer is a variable that stores a
memory address; it points to a location in memory.
Use * to “dereference” a pointer, and get the value stored at the memory address it points at.
Use &amp; to get the memory address of a variable, so that it can be assigned to a pointer.
# include &lt; stdio .h &gt;
int main ( int argc , char * argv []) {
int * pntr ;
int my_int = 6;
// Make pntr point to the memory address of my_int
pntr = &amp; my_int ;
// % d prints a digit .
// % p prints a pointer address .
printf ( &quot; The value of my_int is %d ,
, my_int , pntr ) ;
the value of pntr is % p \ n &quot;
// Dereference the pointer with *.
printf ( &quot; Pntr is pointing at % d \ n &quot; , * pntr ) ;
return 0;
}
Write a swap function that swaps the values of two variables using pointers. Feel free to base
your code on the following template:
# include &lt; stdio .h &gt;
void swap ( int * a , int * b ) {
/* insert code here */
}
main ( int argc , char * argv []) {
int value_a = 50;
int value_b = 5;
/* use swap here , then print the values */
}
3
```