C-programming autumn 2007


Exercise 1

7.9 12:00-14:00 B119.

  1. Write into a file "Hello, world" program, compile and make a runnable program. Run the program.
    #include <stdio.h>
    
    main()
    {
    printf("Hello, world!\n");
    return 0;
    }
    
  2. In lecture notes there is a situation, where a program consists of modules (eka, toka, main etc.) Write codes in to files and compile them and make a runnable program from object files. Write also a makefile that does the same and try to use it.

  3. What the following codes of C do?
    a)while (*q++ = *p++);
    b)if ((c=fgetc(fileHandle)) == EOF)
    c)for (i=a, j=b; i<=j; i += 2, j += 2)
    
  4. Identify the errors and try to correct them.
    #include <stdio.h>
    int longest(char **p, char *r) {
    char *s,*t;
    int max=0; len;
    while(*p++) {
         if ((len=strlen(*p) > max )  {
              max==len;
              s=*p
         }
         p++;
     }
     t=s;
     whlie(*r++ == *s++);
     r=t;
     return s - t;
    }
    
    int main () {
    int pit;
    char  pisin_rivi(80);
    static char *tptr[5] ={
      "Ensimmäinen \
       rivi",
      "Toinen rivi",
      "Ja tässä kolmas rivi",
      "Neljäs on riveistä kaikkein pisin",
      "Viides ja viimeinen"
     };
    char *pp = pisin_rivi;
    pit= longest(tptr,pp);
    prinft("%d merkkiä: %s\n",pit, pp);
    return 0
    }
    

.