LEC20080911 - Introduction to Computer Programming CSC180...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Introduction to Computer Programming September 11, 2008 CSC180 Fall 2008, University of Toronto 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 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 • 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 • 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 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 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 • Everything between #if 0 and #endif will be left out after preprocessing. Verify this using the command gcc -E read.c . #if and #endif are preprocessor’s directives. means false; therefore statements after #if 0 and before #endif will be ignored....
View Full Document

This note was uploaded on 01/10/2011 for the course CSC 180 taught by Professor Na during the Fall '01 term at University of Toronto.

Page1 / 29

LEC20080911 - Introduction to Computer Programming CSC180...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online