Вы находитесь на странице: 1из 3

C Course Day 2

Exercise1 a-d should be done in separate directories. Directory names


should be excercise1a ... exercise1d. Create one text file for all four
examples where comments will be written down.

Exercise1 - a
This exercise is created to demonstrates how extern works and what are the pitfalls
when using global variables.
1. Create a header named counter.h and declare extern g_counter.
2. Create new file named counter_handler_1.c:

Define and initialize g_ counter in it.

Create function cnt_increment_1(). It should increment g_ counter.

3. Create new file named counter_handler_2.c:

Create function cnt_increment_2(). It should increment g_ counter by 2.

Create function cnt_print(). It should print g_ counter stdout.

4. Both counter_handler_2.c and counter_handler_1.c should include counter.h


5. Create counter_main.c and call functions in following order: cnt_increment_1,
cnt_print, cnt_increment_2 and cnt_print.
6. Comment results.

Exercise1 - b
1. Create a header named counter.h and declare extern g_counter.
2. Create module counter_handler_1.c :

Define variable g_counter. Do not initialize variable.

Implement function init_counter(int val) that sets g_counter counter to


specified value.

3. Create module counter_handler_2.c:

Include counter.h.

Define variable g_counter. Do not initialize variable.

C Course Day 2

Implement function print_counter(). It should print g_counter variable to


standard out.

4. Create counter_main.c and


print_counter() respectively.

call

functions

init_counter(int

val)

and

5. Build and run application


6. Comment results.

Exercise 1 - c
1. Create new file named counter_handler_1.c:

Define static variable g_counter. Variable should not be initialized.

Implement function cnt_increment_1(). It should increment counter by


one and print it to standard out.

2. Create new file named counter_handler_2.c:

Define static variable g_counter. Variable should not be initialized.

Implement function cnt_increment_2(). It should increment counter by


two and print it to standard out.

3. Create file counter_main.c


cnt_increment_2 respectivly.

and

call

functions

cnt_increment_1

and

4. Build and run application.


5. Comment results.

Exercise 1 - d
1. Create new file named counter_handler_1.c:

Define static variable g_counter and initialize it to 10.

Implement function cnt_increment_1(). It should increment counter by


one and print it to standard out.

2. Create new file named counter_handler_2.c:

Define global (not static) variable g_counter and initialize it to 20.

C Course Day 2
Implement function cnt_increment_2(). It should increment counter by
two and print it to standard out.

3. Create file counter_main.c and


cnt_increment_2 respectively.

call

functions

cnt_increment_1

and

4. Build and run application.


5. Comment results.

Exercise 2
This exercise is created to show scope of variables. Build and run object_lifetime_e1.c
example. Comment behavior of variable x.
Modify object_lifetime_e1.c example so variable x is visible both in main() and foo()
functions. Remove foo() function parameter. Build and run example. Comment
behavior.

Exercise 3
Example demonstrates const qualifier usage in function parameter list. Comment lines
that are not allowed to be inside the functions in order to be able to build the
const_parameter.c code. Write down comments as function comments with in source
file.
modifyString1(const char *p)
modifyString1(char const *p)
modifyString1(char * const p)
modifyString1(const char const * p)
Explain why those lines can not be in these functions.

Exercise 4
Fix linkage error in linkage_error.c example.

Вам также может понравиться