0% found this document useful (0 votes)
40 views9 pages

Esc101: Fundamentals of Computing Esc101: Fundamentals of Computing

This document contains a summary of lecture 32 of the ESc101 fundamentals of computing course. It discusses recaps of command line arguments, switch statements, and copying files using functions. It also covers typecasting, unsigned types, conditional operators, and answers what switch statements are not good for. The lecture was given by Dheeraj Sanghi on October 24, 2011.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views9 pages

Esc101: Fundamentals of Computing Esc101: Fundamentals of Computing

This document contains a summary of lecture 32 of the ESc101 fundamentals of computing course. It discusses recaps of command line arguments, switch statements, and copying files using functions. It also covers typecasting, unsigned types, conditional operators, and answers what switch statements are not good for. The lecture was given by Dheeraj Sanghi on October 24, 2011.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

ESc101: Fundamentals of Computing

2011-12-Monsoon Semester Lecture #32, October 24, 2011

Please switch off your mobile phones.

Announcements
Lab 11 starts from Friday, 28th October. Thursday section will do lab on 29th October (Saturday) y ( y) Lab exam in the week of 14th to 18th November Labs 11 and 12 will not be pre-announced to give you practice End-sem exam is on 25th November, 8:00 AM t afternoon. Copies can be seen on 28th afternoon

Lec-32

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Recap
Command line arguments
main ( ) function has arguments int agrc, char * argv[ ] argc refers to number of arguments argv points on all those arguments

Switch statement
needed when there are multiple else if clauses based on different values of same expression diff t l f i use of case and default keywords use of break

Lec-32

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Recap: Copying a file: simulating cp


#include <stdio.h> void filecopy (FILE *fp1, FILE *fp2) { char c; while ((c = getc (fp1)) != EOF) // read till end of file putc (c, fp2); } int main (int argc, char * argv[ ]) { FILE *fp1, *fp2; if (argc != 3) // error handling { printf (Program requires two file arguments\n); exit ( ); } fp1 = fopen (argv[1], r); fp2 = fopen (argv[2], w); filecopy (fp1, fp2); fclose (fp1); fclose (fp2); }
Lec-32 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 3

Recap: Switch statement: Syntax


switch (expression) { case value_1: statements_1; break; case value_2: statements_2; break; case value_n: statements_n; break; default: t t d f lt statements_d; break; t d b k }

Lec-32

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Recap: Switch statement


All values must be constants and not expressions or variables No value can appear twice default is executed when expression evaluates to a value not present It is not necessary that default case be present
If it is not there, and expression evaluates to a non-existent value, then program leaves the switch statement

values can appear in any order


Lec-32 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 5

Recap: Switch statement


Important: Without break, next case is also executed.
switch (x) { case a: printf (0); case b: printf (1); default: printf (2); }

When x is a, all of 0, 1 and 2 are printed When x is b, both 1 and 2 are printed When x is neither, only 2 is printed
Lec-32 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 6

What is switch not good for?


Count the number of digits and letters? int i, ndigit[10], nletters[26]; char c; ; for (i = 0; i < 10; i++) ndigit[i] = 0; for (i = 0; i < 26; i++) nletters[i ] = 0; while (( c = getchar ( )) != EOF) switch ( c ) { case 0: case 9: ndigit [c 0]++; break;
Lec-32 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 7

case a: case b: case z: nletter [ c a]++; break; case A: case B: case Z: nletter [ c A]++; break; Z: A ]++; }

Lec-32

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

What is switch not good for


The previous program was too tedious
One had to write 62 cases They could have been combined into three if clauses, which would have been easier to write and understand. if ((c >= 0) && (c <= 9)) ndigit[c 0]++; else if ((c >= a) && (c <= z)) a) z )) nletter[c a]++; else if ((c >= A) && (c <= Z)) nletter[c A]++;
Lec-32 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 9

Typecasting
We have seen so far
implicit type casting
int i; float f; i = f;

explicit type casting


allocating space using malloc ( )

Lec-32

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

10

Typecasting
Explicit type casting can be used anywhere
int i; int j; float f; f = i / j; // will result in integer division f = (float) i / (float) j ; // will result in floating point division

Lec-32

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

11

Typecasting
int i; short s; s = 300; i=s*s; i = (int) (s * s) ; i = (int) s * s; (i t) // will result in overflow // will still result in overflow // by force converting first instance of s b f ti fi t i t f // system will convert second one also, and // the result will also be stored in an int

Lec-32

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

12

Lec-32

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

13

unsigned type
We can tell system that we will only store non negative non-negative numbers by attaching keyword unsigned to declaration Example:
unsigned short int unsigned int unsigned long int s; i; l;

Lec-32

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

14

Conditional Operator
Requires three operands:
condition ? expression_1 : expression_2

Meaning:
If condition is true then evaluate (and set value of) expression 1 If condition is false then evaluate (and set value of) expression 2

Example:
int z, a, b; z = (a > b) ? a : b
Lec-32

// set z to max of a and b


Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 15

Conditional Operator
z = (a > b) ? a : b

This is equivalent to the following code:


if (a > b) z = a; else z = b;

Lec-32

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

16

Any Questions?

Lec-32

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

17

You might also like