CS402: Worksheet 1 Introduction to C 1 Hello, World!

advertisement
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 < stdio .h >
int main ( int argc , char * argv []) {
printf ( " Hello , world !\ n " ) ;
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 < stdio .h >
int factorial ( int n ) {
int i = 0;
int fac = 1;
for ( i = 1; i <= n ; i ++) {
fac = fac * i ;
}
return fac ;
}
int main ( int argc , char * argv []) {
int fac_five = factorial (5) ;
printf ( " 5! is % d \ n " , fac_five ) ;
}
Task 1
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 & to get the memory address of a variable, so that it can be assigned to a pointer.
# include < stdio .h >
int main ( int argc , char * argv []) {
int * pntr ;
int my_int = 6;
// Make pntr point to the memory address of my_int
pntr = & my_int ;
// % d prints a digit .
// % p prints a pointer address .
printf ( " The value of my_int is %d ,
, my_int , pntr ) ;
the value of pntr is % p \ n "
// Dereference the pointer with *.
printf ( " Pntr is pointing at % d \ n " , * pntr ) ;
return 0;
}
Task 2
Write a swap function that swaps the values of two variables using pointers. Feel free to base
your code on the following template:
# include < stdio .h >
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
Download