Thursday, January 10, 2008

CSE 2031 Lecture 2

Course Information

15% on assignments

30 % on midterm

Rest on exam


 

Recap from day 1

  • -Wall
    • Shows any errors the compiler wouldn't typically catch
  • -o
    • Allows you to change the executable files filename
  • -c
    • Stops compilation after the object module is produced
  • -E
    • Stops the compilation after the preprocessor phase
    • Textual manipulation of the source code


 

#include <stdio.h>

int main(void)

{

    int n, m;

    int product = 1; /* will eventually hold n!*/

    scanf("%d, &n);

    m=n;

    while ( m > 0)

        product *= m--;

    printf("%d! = %d\n", n, product);

    return 0;

}


 

#include <stdio.h>


 

int factorial(int) /* must be declared here in case user inputs a non-int*/

int main(void)

{

int n;

scanf("%d", &n)

printf("%d! = %d\n", n, factorial(n));

return 0;

int factorial(int x)

{

    /* base case*/

if (x<2) return 1;

    /*recursive calls*/

return x*factorial(x-1);

}

/*returns 5! = 120*/

}

If you want to compile with two files linking eachother use

  • cc -0 printFactorial factorial.c printfactorial2.c
  • this will link the code used in printFactorial from factorial.c so that printFactorial will work
    • this is more explicit than java because there isn't an 'import' feature
    • instead the feature is found in the compiler
  • when you compile a program that uses multiple files you have to name all the files on the command line
  • utility 'make'
    • merges files together
    • looks for a 'makefile'
      • in a make file there are dependency lines or action lines
        • action lines MUST be tabbed over
      • dependency lines describe the files in use
      • action lines tell console what to do to generate the file described in the dependency file
    • when 'make' is typed in console it runs the commands found in makefile
    • read makefile on course website, when it is available
      • console: make clean
      • runs the clean command found in 'makefile'
      • all warning flags are put in 'makefile' such as –Wall
  • Compiler
    • –l makes the console look in the library for functions used that aren't in stdio.h
    • –lm looks in the m (math) library


 

#include <stdio.h>

#include <math.h>

int main(void)

{

    double x;

    

    printf("Enter value for x: ");

    scanf("%1f", &x);

    

    printf("x = %f sqrt(x) = %1f\n", x, sqrt(x));

    return 0;

}

s

No comments: