Thursday, January 3, 2008

CSE2031 Lecture 1

Course Information

Prof: Gordon Turpin

Email: gordon@cse.yorku.ca

    -only send mail from prism account

    -use a subject header "2031: …"


 

Hello World


 

#include <stdio.h>

int main(void)

{

printf("Hello World!"\n");

return 0;

}

  • #include <stdio.h>
    • This file is replaced in the header in compile time
    • stdio.h does not contain executable code
      • only definition of constants
      • include statements
      • and function prototypes
      • 'printf'
        • A function call
        • (method is a function, method is a java term)
        • When it encounters a function call like 'printf'
          • It wants the function prototype of printf
  • int main(void)
    • 'void' means it takes no parameters
    • 'main' returns an 'int'
  • return 0;
    • indicates success
    • unspecified types are assumed to be 'int'
  • printf
    • most basic print type
    • '\n' sends a new line after the text
  • This program doesn't return a '0' because printing is not the same as returning


 

Compiling C

  • Simplest way to compile

    cc hello.c

    ls a.out

    cat hello.c

  • 'cat' prints out the code for hello.c in console
  • cat is called from /bin/cat
  • cc is the compile command
    • cc created a.out
      • to create a different file name
        • use cc –o hello hello.c
          • 'hello' is the filename
          • -o is the 'flag' to create a different name
        • cc –o hello –Wall hello.c
          • –Wall will allow the compiler point out errors in code
        • cc –o hello –Wall -pedantic hello.c
          • allows you to see if code will run on older versions of C
        • cc –o hello –Wall -pedantic –c hello.c
          • stops compilation when the object module is produced
          • Produces the file 'hello.o'
          • nm hello.o
            • shows the function 'main' in the T (text)
            • and there is a call for a function 'printf'
        • cc –o hello –Wall –pedantic –c -E hello.c
          • stops the compilation after the preprocessor phase
          • Replaces #define with code supplied for that function


             

  • echo $PATH
    • returns the paths where the shell will look for the commands you are passing
      • separated by ':'
    • the last place bash looks in is the current directory
    • ./a.out will find a.out within your path
    • PATH=$PATH:..
      • Adds ' ..' to the $PATH
  • a.out only needs executable permission to run
  • if you write a shell script you would need execute and read permission


 

chmod

  • 9 permission bits
    • ---------
      • User
    • ---------
      • Group
    • ---------
      • Owner    
    • Read = 4
    • Write = 2
    • Execute = 1
    • Non = 0
  • 'chmod 751 a.out 'gives
    • user
      • r/w/e
    • group
      • r/e
    • owner
      • e

Comments

  • /*comment in here */


     

No comments: