-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0c3e4c6
commit cbff2a5
Showing
11 changed files
with
388 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
You will get this error if for example, you write code like this:: | ||
|
||
```c | ||
int a; | ||
a = square; | ||
``` | ||
|
||
when you wanted to do this: | ||
|
||
```c | ||
int a; | ||
a = square(5); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
You are not permitted to assign arrays in C. | ||
|
||
You can NOT do this: | ||
|
||
```c | ||
#define ARRAY_SIZE 10 | ||
|
||
int array1[ARRAY_SIZE] = {0,1,2,3,4,5,6,7,8,9}; | ||
int array2[ARRAY_SIZE]; | ||
|
||
array2 = array1; | ||
``` | ||
You can instead use a loop to copy each array element individually. | ||
```c | ||
#define ARRAY_SIZE 10 | ||
int array1[ARRAY_SIZE] = {0,1,2,3,4,5,6,7,8,9}; | ||
int array2[ARRAY_SIZE]; | ||
for (int i = 0; i < 10; i++) { | ||
array2[ARRAY_SIZE] = array1[ARRAY_SIZE]; | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
Accidentally printing the special **EOF** value returned by the functions _getchar_, _getc_ and _fgetc_. | ||
|
||
For example, this program prints the **EOF** value before the loop exits: | ||
|
||
```c | ||
#include <stdio.h> | ||
|
||
int main(void) { | ||
int c = 0; | ||
while (c != EOF) { | ||
int c = getchar(); | ||
putchar(c); | ||
} | ||
return 0; | ||
} | ||
``` | ||
The special **EOF** value typically is defined to be `-1` (in `<stdio.h>`) | ||
and when printed is invisible. So the program appears to work. | ||
```console | ||
$ dcc cat.c | ||
$ echo cat | ./a.out | ||
cat | ||
$ | ||
``` | ||
|
||
But the program will fail automated testing because it is printing an extra byte. | ||
|
||
This is a program that output the same bytes as the above example. | ||
|
||
```c | ||
#include <stdio.h> | ||
|
||
int main(void) { | ||
|
||
putchar('c'); | ||
putchar('a'); | ||
putchar('t'); | ||
putchar('\n'); | ||
putchar(EOF); | ||
|
||
return 0; | ||
} | ||
``` | ||
This is a program which doesn't print the EOF value: | ||
```c | ||
#include <stdio.h> | ||
int main(void) { | ||
int c = getchar(); | ||
while (c != EOF) { | ||
putchar(c); | ||
c = getchar(); | ||
} | ||
return 0; | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
|
||
If you forget a curly bracket, e.g. | ||
```c | ||
int sum(int x, int y) { | ||
if (x > y) { | ||
return x + y; | ||
} else { | ||
return x - y; | ||
// <-- missing closing curly bracket | ||
} | ||
|
||
int f(int x) { | ||
return sum(x, x); | ||
} | ||
``` | ||
The compiler will give an error when the next function definition starts. | ||
You can fix by adding the missing curly bracket (brace): | ||
```c | ||
int sum(int x, int y) { | ||
if (x > y) { | ||
return x + y; | ||
} else { | ||
return x - y; | ||
} | ||
} | ||
int f(int x) { | ||
return sum(x, x); | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
It is legal to have local variable with the same name as a function, but you can't then call the function | ||
because the local variable declaration hides the function. | ||
|
||
For example: | ||
|
||
```c | ||
int sum(int x, int y) { | ||
return x + y; | ||
} | ||
|
||
int f(int sum) { | ||
int square = sum * sum; | ||
|
||
// error sum because sum is a variable | ||
return sum(square, square); | ||
} | ||
``` | ||
You can fix the name clash by changing the name of the variable: | ||
```c | ||
int sum(int x, int y) { | ||
return x + y; | ||
} | ||
int f(int a) { | ||
int square = a * a; | ||
// error sum because sum is a variable | ||
return sum(square, square); | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
Forgetting to print a newline is a common mistake. | ||
|
||
For example, this program doesn't print a newline. | ||
|
||
```c | ||
int main(void) { | ||
int answer = 7 * 6; | ||
printf("%d", answer); | ||
} | ||
|
||
``` | ||
So when its compiled and run you'll see something like this: | ||
```console | ||
$ dcc answer.c | ||
$ ./a.out | ||
42$ | ||
``` | ||
|
||
If you add a **\n** to the _printf_ like this: | ||
|
||
```c | ||
int main(void) { | ||
int answer = 7 * 6; | ||
printf("%d\n", answer); | ||
} | ||
``` | ||
It will fix the problem: | ||
```console | ||
$ dcc answer.c | ||
$ ./a.out | ||
42 | ||
$ | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
A C function can not return a pointer to a local variable. | ||
|
||
A local variable variable does not exist after the function returns. | ||
|
||
For example, you can NOT do this: | ||
|
||
```c | ||
struct node { | ||
struct node *next; | ||
int data; | ||
}; | ||
|
||
struct node *create_node(int i) { | ||
struct node n; | ||
|
||
n.data = i; | ||
n.next = NULL; | ||
|
||
return &n; | ||
} | ||
``` | ||
A function can return a pointer provided by malloc: | ||
For example, you can do this: | ||
```c | ||
struct node { | ||
struct node *next; | ||
int data; | ||
}; | ||
struct node *create_node(int i) { | ||
struct node *p; | ||
p = malloc(sizeof (struct node)); | ||
p->data = i; | ||
p->next = NULL; | ||
return p; | ||
} | ||
``` |
Oops, something went wrong.