01.11.2014 Views

Lesson 1.1: Hello, world

Lesson 1.1: Hello, world

Lesson 1.1: Hello, world

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Lesson</strong> <strong>1.1</strong>: <strong>Hello</strong>, <strong>world</strong><br />

Programming in C<br />

Prof. Dr. Eike Best<br />

Dr. Elke Wilkeit<br />

September 28, 2002<br />

1 #include <br />

2<br />

3 main()<br />

4 {<br />

5 printf("hello, <strong>world</strong>\n");<br />

6 }<br />

Exercise 1-1 (my first C program) (a) Run the ‘‘hello-<strong>world</strong>” program on<br />

your system. Experiment with leaving out parts of the program to see what error<br />

messagees you get. In particular, leave out the newline-escape-sequence.<br />

Exercise 1-2a (escape sequences) Experiment to find out what happens when<br />

printf’s argument string contains \c , where c is some character different from n.<br />

1-2b Write a program that prints the meaning of all escape sequences. Try \\ in<br />

order to print a single \ (this is also an escape sequence).<br />

Exercise 1-1b (hello, <strong>world</strong> — revisited) Write a program that prints the<br />

string ‘‘hello, <strong>world</strong>! ” in the first line, then ‘‘ello, <strong>world</strong>! h” , in the second line,<br />

‘‘llo, <strong>world</strong>! he” in the third line, and so on, moving the first character to the end<br />

of the string, until the original string is reached again in the last line.


<strong>Lesson</strong>s 1.2, 1.3 and 1.4:<br />

Fahrenheit and Celsius<br />

Programming in C<br />

Prof. Dr. Eike Best<br />

Dr. Elke Wilkeit<br />

September 28, 2002<br />

1 #include <br />

2<br />

3 /* print Fahrenheit-Celsius table<br />

4 for fahr = 0, 20, ..., 300 */<br />

5 main()<br />

6 {<br />

7 int fahr, celsius;<br />

8 int lower, upper, step;<br />

9<br />

10 lower = 0; /* lower limit of temperature table */<br />

11 upper = 300; /* upper limit of temperature table */<br />

12 step = 20; /* step size */<br />

13<br />

14 fahr = lower;<br />

15 while (fahr


Exercise 1-3. (heading) Modify the temperature conversion program to print<br />

a heading above the table. The first column of the table should be four characters<br />

wide and have no decimal point and no fraction digits. The second column should<br />

be eight characters wide and should have two fraction digits.<br />

Exercise 1-4 (conversion) Write a program to print the correponding Celsius<br />

to Fahrenheit table. Choose another layout than in Exercise 1-3.<br />

1 #include <br />

2<br />

3 main()<br />

4 {<br />

5 int fahr;<br />

6<br />

7 for (fahr = 0; fahr


<strong>Lesson</strong> 1.5:<br />

Character input and output (1/3)<br />

Programming in C<br />

Prof. Dr. Eike Best<br />

Dr. Elke Wilkeit<br />

September 28, 2002<br />

1 #include <br />

2<br />

3 /* copy input to output; 1st version */<br />

4 main()<br />

5 {<br />

6 int c;<br />

7<br />

8 c = getchar();<br />

9 while (c != EOF) {<br />

10 putchar(c);<br />

11 c = getchar();<br />

12 }<br />

13 }<br />

1 #include <br />

2<br />

3 /* copy input to output; 2nd version */<br />

4 main()<br />

5 {<br />

6 int c;<br />

7<br />

8 while ((c =getchar()) != EOF)<br />

9 putchar(c);<br />

10 }<br />

Exercise 1-6. (EOF) Verify that the expression getchar() != EOF is 0 or 1.<br />

Exercise 1-7. (EOF again)<br />

Write a program to print the value of EOF.


<strong>Lesson</strong> 1.5:<br />

Character input and output (2/3)<br />

Programming in C<br />

Prof. Dr. Eike Best<br />

Dr. Elke Wilkeit<br />

September 28, 2002<br />

1 #include <br />

2<br />

3 /* count characters in input; 1st version */<br />

4 main()<br />

5 {<br />

6 long nc;<br />

7<br />

8 nc = 0;<br />

9 while (getchar() != EOF)<br />

10 ++nc;<br />

11 printf("%ld\n", nc);<br />

12 }<br />

1 #include <br />

2<br />

3 /* count characters in input; 2nd version */<br />

4 main()<br />

5 {<br />

6 double nc;<br />

7<br />

8 for (nc = 0; getchar() != EOF; ++nc)<br />

9 ;<br />

10 printf("%.0f\n", nc);<br />

11 }<br />

1 #include <br />

2<br />

3 /* count lines in input */<br />

4 main()<br />

5 {<br />

6 int c, nl;<br />

7<br />

8 nl = 0;<br />

9 while ((c = getchar()) != EOF)<br />

10 if (c == ’\n’)<br />

11 ++nl;<br />

12 printf("%d\n", nl);<br />

13 }<br />

Exercise 1-8. (Counting)<br />

Write a program to count blanks, tabs, and newlines.<br />

Exercise 1-9. (Copying and replacing) Write a program to copy its input to<br />

its output, replacing each string of one or more blanks by a single blank.


<strong>Lesson</strong> 1.5:<br />

Character input and output (3/3)<br />

Programming in C<br />

Prof. Dr. Eike Best<br />

Dr. Elke Wilkeit<br />

September 28, 2002<br />

Exercise 1-10. (More copying and replacing) Write a program to copy its<br />

input to its output, replacing each tab by \t, each backspace by \b, and each<br />

backslash by \\. This makes tabs and backspaces visible in an unambiguous way.<br />

1 #include <br />

2<br />

3 #define IN 1 /* inside a word */<br />

4 #define OUT 0 /* outside a word */<br />

5<br />

6 /* count lines, words, and characters in input */<br />

7 main()<br />

8 {<br />

9 int c, nl, nw, nc, state;<br />

10<br />

11 state = OUT;<br />

12 nl = nw = nc = 0;<br />

13 while ((c = getchar()) != EOF) {<br />

14 ++nc;<br />

15 if (c == ’\n’)<br />

16 ++nl;<br />

17 if (c == ’ ’ || c == ’\n’ || c == ’\t’)<br />

18 state = OUT;<br />

19 else if (state == OUT) {<br />

20 state = IN;<br />

21 ++nw;<br />

22 }<br />

23 }<br />

24 printf("%d %d %d\n", nl, nw, nc);<br />

25 }<br />

Exercise 1-11. (Testing) How would you test the word count program? What<br />

kinds of inputs are most likely to uncover bugs if there are any?<br />

Exercise 1-12. (Reformatting)<br />

per line.<br />

Write a program that prints its input one word


<strong>Lesson</strong> 1.6:<br />

Arrays<br />

Programming in C<br />

Prof. Dr. Eike Best<br />

Dr. Elke Wilkeit<br />

September 28, 2002<br />

1 #include <br />

2<br />

3 /* count digits, white space, others */<br />

4 main()<br />

5 {<br />

6 int c, i, nwhite, nother;<br />

7 int ndigit[10];<br />

8<br />

9 nwhite = nother = 0;<br />

10 for (i = 0; i < 10; ++i)<br />

11 ndigit[i] = 0;<br />

12<br />

13 while ((c = getchar()) != EOF)<br />

14 if (c >= ’0’ && c


<strong>Lesson</strong> 1.7:<br />

Functions<br />

Programming in C<br />

Prof. Dr. Eike Best<br />

Dr. Elke Wilkeit<br />

September 28, 2002<br />

1 #include <br />

2<br />

3 int power(int m, int n);<br />

4<br />

5 /* test power function */<br />

6 main()<br />

7 {<br />

8 int i;<br />

9<br />

10 for (i = 0; i < 10; ++i)<br />

11 printf("%d %d %d\n", i, power(2,i), power(-3,i));<br />

12 return 0;<br />

13 }<br />

14<br />

15 /* power: raise base to n-th power; n>=0 */<br />

16 int power(int base, int n)<br />

17 {<br />

18 int i,p;<br />

19<br />

20 p = 1;<br />

21 for (i = 1; i


<strong>Lesson</strong> 1.8:<br />

Arguments are call-by-value<br />

Programming in C<br />

Prof. Dr. Eike Best<br />

Dr. Elke Wilkeit<br />

September 28, 2002<br />

1 /* power: raise base to n-th power; n>=0; version 2 */<br />

2 int power(int base, int n)<br />

3 {<br />

4 int p;<br />

5<br />

6 for (p = 1; n > 0; --n)<br />

7 p = p * base;<br />

8 return p;<br />

9 }


<strong>Lesson</strong> 1.9:<br />

Character arrays (1/2)<br />

Programming in C<br />

Prof. Dr. Eike Best<br />

Dr. Elke Wilkeit<br />

September 28, 2002<br />

1 while ( there is another line )<br />

2 if ( it is longer than the previous longest )<br />

3 save it ;<br />

4 save its length ;<br />

5 print longest line<br />

Exercise 1-16. (Error-tolerant longest-line program) Revise the main routine<br />

of the longest-line program so it will correctly print the length of arbitrarily<br />

long lines, and as much as possible of the text.<br />

Exercise 1-17. (Printing lines longer than 80 characters)<br />

to print all input lines that are longer than 80 characters.<br />

Write a program<br />

Exercise 1-18. (Removing trailing invisibles) Write a program to remove<br />

trailing blanks and tabs from each line of input, and to delete entirely blank lines.<br />

Exercise 1-19. (Reversing strings) Write a function reverse(s) that reverses<br />

the character string s. Use it to write a program that reverses its input a line at a<br />

time.


1 #include <br />

2 #define MAXLINE 1000 /* maximum input line size */<br />

3<br />

4 int getline(char line[], int maxline);<br />

5 void copy(char to[], char from[]);<br />

6<br />

7 /* print longest input line */<br />

8 main()<br />

9 {<br />

10 int cur_len; /* current line length */<br />

11 int cur_max; /* maximum seen so far */<br />

12 char cur_line[MAXLINE]; /* current input line */<br />

13 char longest[MAXLINE]; /* longest line saved here */<br />

14<br />

15 cur_max = 0;<br />

16 while ((cur_len = getline(cur_line, MAXLINE)) > 0)<br />

17 if (cur_len > cur_max) {<br />

18 cur_max = cur_len;<br />

19 copy(longest, cur_line);<br />

20 }<br />

21 if (cur_max > 0) /* there was a line */<br />

22 printf("%s", longest);<br />

23 return 0;<br />

24 }<br />

25<br />

26 /* getline: read a line into s, return length */<br />

27 int getline(char s[], int limit)<br />

28 {<br />

29 int c, i;<br />

30<br />

31 for (i=0; i


<strong>Lesson</strong> <strong>1.1</strong>0:<br />

External variables and scopes (1/2)<br />

Programming in C<br />

Prof. Dr. Eike Best<br />

Dr. Elke Wilkeit<br />

September 28, 2002<br />

1 #include <br />

2 #define MAXLINE 1000 /* maximum input line size */<br />

3<br />

4 int cur_max; /* maximum length seen so far */<br />

5 char cur_line[MAXLINE]; /* current input line */<br />

6 char longest[MAXLINE]; /* longest line saved here */<br />

7<br />

8 int getline(void);<br />

9 void copy(void);<br />

10<br />

11 /* print longest input line; specialised version */<br />

12 main()<br />

13 {<br />

14 int cur_len; /* current line length */<br />

15 extern int cur_max;<br />

16 extern char cur_line[];<br />

17 extern char longest[];<br />

18<br />

19 cur_max = 0;<br />

20 while ((cur_len = getline()) > 0)<br />

21 if (cur_len > cur_max) {<br />

22 cur_max = cur_len;<br />

23 copy();<br />

24 }<br />

25 if (cur_max > 0) /* there was a line */<br />

26 printf("%s", longest);<br />

27 return 0;<br />

28 }<br />

29<br />

30 /* getline: specialised version */<br />

31 int getline(void)<br />

32 {<br />

33 int c, i;<br />

34 extern char cur_line[];<br />

35<br />

36 for (i=0; i


43 return i;<br />

44 }<br />

45<br />

46 /* copy: specialised version */<br />

47 void copy(void)<br />

48 {<br />

49 int i;<br />

50 extern char cur_line[], longest[];<br />

51<br />

52 i = 0;<br />

53 while ((longest[i] = cur_line[i]) != ’\0’)<br />

54 ++i;<br />

55 }<br />

Exercise 1-20. (Expanding tabs) Write a program detab that replaces tabs in<br />

the input with the proper number of blanks to space to the next tab stop. Assume a<br />

fixed number of tab stops, say every n columns. Should n be a variable or a symbolic<br />

parameter?<br />

Exercise 1-21. (Inserting tabs) Write a program entab that replaces strings<br />

of blanks by the minimum number of tabs and blanks to achieve the same spacing.<br />

Use the same tab stops as for detab. When either a tab or a single blank would<br />

suffice to reach a tab stop, which should be given preference?<br />

Exercise 1-22. (Breaking lines) Write a program to ‘fold’ long input lines into<br />

two or more shorter lines after the last non-blank character that occurs before the<br />

nth column of input. Make sure your program does something intelligent with very<br />

long lines, and if there are no blanks or tabs before the specified column.<br />

Exercise 1-23. (Removing comments) Write a program to remove all comments<br />

from a C program. Don’t forget to handle quoted strings and character<br />

constants properly. C comments do not nest.<br />

Exercise 1-24. (Syntax checking) Write a program to check a C program for<br />

rudimentary syntax errors like unbalanced parenthese, brackets and braces. Don’t<br />

forget about quotes, both single and double, escape sequences, and comments. (This<br />

program is hard if you do it in full generality.)<br />

<strong>Lesson</strong> <strong>1.1</strong>0, 2nd page

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!