LEC20080911 - Introduction to Computer Programming CSC180...

Info icon This preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
Introduction to Computer Programming September 11, 2008 CSC180 Fall 2008, University of Toronto
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
The C Programming Language C was created by Dennis Ritchie, born in 1941, between 1969 and 1973. C is a professional programming language. C is a general-purpose programming language. C is useful for writing operating systems, compilers, and major programs in many different domains. C is small, simple, expressive and efficient. CSC180 Fall 2008, University of Toronto 1
Image of page 2
The main function Programming in C consists mostly of writing functions and calling functions. Every C program must contain exactly one main function. Example: /* a program that does nothing */ int main() { return 0; } CSC180 Fall 2008, University of Toronto 2
Image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Write it in one line: /* a program that does nothing */ int main(){return 0;} The main function can have parameters: int main(int argc, char *argv[]) /* cmd.c -- read command-line information */ #include <stdio.h> /* print program’s name and number of options */ int main(int argc, char *argv[]) { printf(‘‘program %s has %d options\n’’, argv[0], argc-1); return 0; } CSC180 Fall 2008, University of Toronto 3
Image of page 4
Make an executable a.out from cmd.c . Try the following command-line inputs: ./a.out ./a.out cmd.c ./a.out cmd.c -o cmd What did you find? argc is the number of arguments we input in command-line, and argv[0] is the first argument. %s in the format string of printf() converts argv[0] as a string. CSC180 Fall 2008, University of Toronto 4
Image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
C comments Take a look at read.c . Nested comments, as shown below, are not allowed: /* /* read.c */ */ /* if (n == 0) /* no lines? */ read_ch2_twice(); if (n > 0 && n <= 1000) /* some lines? */ read_ch2_once(); if (n > 1000) /* enough lines? */ CSC180 Fall 2008, University of Toronto 5
Image of page 6
ignore_ch2(); */ How to comment out a block of code that contains comments? Conditional compilation. #if 0 if (n == 0) /* no lines? */ read_ch2_twice(); if (n > 0 && n <= 1000) /* some lines? */ read_ch2_once(); if (n > 1000) /* enough lines? */ ignore_ch2(); #endif CSC180 Fall 2008, University of Toronto 6
Image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Everything between #if 0 and #endif will be left out after preprocessing. Verify this using the command gcc -E read.c . #if 0 and #endif are preprocessor’s directives. 0 means false; therefore statements after #if 0 and before #endif will be ignored. CSC180 Fall 2008, University of Toronto 7
Image of page 8
User-defined functions Call a user defined function f ( r ) = πr 2 in main: /* area.c */ #include <stdio.h> #include <stdlib.h> /* for function atof() */ #define PI 3.14159265 /* macro definition */ /* compute circle area */ double f(double r) { return PI*r*r; } CSC180 Fall 2008, University of Toronto 8
Image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern