Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
master-embedded-systems
c-exercise-solutions public
Commits
d6a9abef
Commit
d6a9abef
authored
Sep 12, 2017
by
Dominik Widhalm
Browse files
Some fixes/extensions of chapter 7 comments
parent
946e8688
Changes
5
Hide whitespace changes
Inline
Side-by-side
ch_7/task_04/main.c
View file @
d6a9abef
...
...
@@ -14,7 +14,6 @@
#include <stdio.h>
int
average
(
int
val1
,
int
val2
)
{
return
val1
+
val2
/
2
;
}
// function returns int, but result of calculation may be with decimal places
// additionally it is advisable to always provide function prototypes
int
main
(
int
argc
,
char
**
argv
)
{
// argc & argv are not used within this program
char
number1
=
15
;
...
...
ch_7/task_05/main.c
View file @
d6a9abef
...
...
@@ -20,6 +20,6 @@ int main (int argc, char** argv) {
scanf
(
"%c"
,
&
answer
);
if
(
answer
==
'n'
)
{
printf
(
"What do you want then? "
);
scanf
(
"%s"
,
maoam
);
scanf
(
"%s"
,
maoam
);
// scanf with "%s" implies to danger of buffer overflows
}
}
// main function has return type int, but return statement is missing
ch_7/task_06/main.c
View file @
d6a9abef
...
...
@@ -15,7 +15,7 @@
int
main
(
int
argc
,
char
**
argv
)
{
// argc & argv are not used within this program
char
animal
[]
=
'
frog
'
;
// strings are denoted with "", '' are only applicable for single character
int
count
=
sizeof
(
animal
);
int
count
=
sizeof
(
animal
);
// it is advisable to always take into account the size of the variable type in sizeof()
for
(
int
i
=
0
;
i
<=
count
;
i
++
);
// semicolon after loop head ends the loop ...
printf
(
"%d letter: %c
\n
"
,
i
,
*
animal
+
i
);
// ... therefore this statement is executed only once
// loop head run condition has an off-by-one error, since the maximum index of animal is (count-1) ...
...
...
ch_7/task_08/main.c
View file @
d6a9abef
...
...
@@ -13,7 +13,7 @@
// required includes are missing (i.e., <stdio.h>)
int
*
get_double
(
int
value
)
{
// it is advisable to always provide function prototypes
int
*
get_double
(
int
value
)
{
value
*=
2
;
return
&
value
;
// the memory location of value is automatically freed after leaving the function
// therefore its address is invalid afterwards
...
...
@@ -23,7 +23,7 @@ int main (int argc, char** argv) {
int
number
=
42
;
int
*
number2x
=
get_double
(
number
);
printf
(
"Double a value using an own function!
\n
"
);
printf
(
"The double of %d is %d!
\n
"
,
number
,
*
number2x
);
printf
(
"The double
amount
of %d is %d!
\n
"
,
number
,
*
number2x
);
return
0
;
}
ch_7/task_09/main.c
View file @
d6a9abef
...
...
@@ -15,7 +15,7 @@
int
check
=
0
;
void
check_input
(
int
value
,
int
guess
)
{
// it is advisable to always provide function prototypes
void
check_input
(
int
value
,
int
guess
)
{
int
check
=
value
==
guess
?
1
:
0
;
// declaration of local variable check shadows global variable
return
;
}
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment