2011-10-15
Programming Methods and Techniques I
C Introduction
MSc. Wojciech Szmuc / PhD Lucjan Janowski
Verified: Jakub Palider
Attention !!
Any program that will be written during this course has to be properly formatted i.e. you should comment
the code, use proper indentation etc. (see the examples in the instructions). The comments can be entered
between /* and */. You should add comments regularly in the code. The comments should inform what is
the commented code functionality unless it is self explanatory.
Task 1. The first program
1. Create a new directory where you will keep all programs made in this exercise. Call it zajecia3.
2. Create a file
witaj.c
with the following content:
/* The program prints "Witaj!" on the screen */
#include <stdio.h> /* adding a library */
int main() /* The main program function */
{ /* a block of functions beginning */
printf("Witaj!"); /* printing a "Witaj!" text */
printf("\n"); /* printing a new line sight */
return 0; /* exit function with return value 0 */
} /* the end of a block of functions */
Note that indentations make the code much more clear. We use indentation inside a separate block of
functions denoted by
{
and
}
.
3. Try to compile the created file using:
gcc witaj.c
4. Print the directory content. Did something change?
5. Run the created file typing
./a.out
6. Compile the program using the following syntax:
gcc witaj.c o witaj
.
Do we have a new file?
Execute the new file.
7. Try to put comments removing some parts of the code (for example the line containing
stdio.h)
.
What was the output?
8. Try to compile the code with option
nodefaultlibs
Exercise
Write a program printing out:
__ __ ________
| | / / |__ __|
| |/ / | |
| |\ \ | |
|__| \__\ |__|
Task 2. Variables and arithmetic operations
1. Write a program including:
float rzeczywista1=2/3, rzeczywista2=2.3; /* Float variables definition */
/* and initialization */
printf("rzeczywista1=%f", rzeczywista1); /* "rzeczywista1" printing */
printf("\n");
printf("rzeczywista2=%5.2f", rzeczywista2); /* Formatted output, e.g 12345.67*/
printf("\n");
printf("rzeczywista1+rzeczywista2=%f", rzeczywista1+rzeczywista2);
Do not forget to add different elements needed to compile the program properly.
Check the program output.
Co należałoby zmienić aby program działał poprawnie?
2. Check if other arithmetic operations work similarly.
Task 3. Address
A variable address is a number denoting the first byte in the computer memory where the variable is
kept. We cannot change the variable address since the memory management is out of the program
management.
1. Write a code:
short krotka1=12345, krotka2=54321; /* an integer values with small range of
values */
printf("Zmienna \"krotka1\" value: %hi, address: %p", krotka1, &krotka1);
printf("\n");
printf("Zmienna \"krotka2\" wartosc: %hi, address: %p", krotka2, &krotka2);
printf("\n");
Why does this program work like that?
2. Based on address offset find size of variables.
3. Write a code that computes size of other type(s) of data..
4. Write with adding necessary headings etc:
float rzeczywista;
printf("Wpisz liczbe: ");
scanf("%f", &rzeczywista); /* passing a variable address as */
/* an argument of scanning function */
printf("The square of the entered number is: %f\n", rzeczywista*rzeczywista);
compile and run.
5. Write a program that makes an arithmetic operation on two numbers entered by a user.
Task 4. Pointers
A pointer is a variable being an address of a variable. Since it is a variable we can change its value so it
will point other variable or element.
1. Add to the previous program a line that is a pointer definition:
float *wRzeczywista=&rzeczywista; /* a pointer definition and “rzeczywista” */
/* address mapping */
Note that in this case the mapped value is the address itself not a number, therefore we do not use the
conversion as in the example mentioned in Task 3.
Next change the entering variable name definition into:
scanf("%f", wRzeczywista);
Is program working correctly?
2. Change the program in such a way that multiplication works for pointers, not the variables
themselves.
An exercise
Write a program where a user enters the classes beginning, the classes duration and the current time.
On the basis of the entered data the program should compute how much time is left till the end of the
classes. The amount of time should be printed out in percentage and the number of minutes.