lec08-cs110 computational engineering
DESCRIPTION
A keynote on Problem Solving using ComputersTRANSCRIPT
![Page 1: Lec08-CS110 Computational Engineering](https://reader033.vdocuments.us/reader033/viewer/2022052310/555e3736d8b42aad058b495c/html5/thumbnails/1.jpg)
CS110: Data Types in C
Lecture 8V. Kamakoti
20th January 2008
![Page 2: Lec08-CS110 Computational Engineering](https://reader033.vdocuments.us/reader033/viewer/2022052310/555e3736d8b42aad058b495c/html5/thumbnails/2.jpg)
Datatypes
• Types of Data you can use in C withoutexplicit definition by the user– Arithmetic
• Integer domain• Real domain
– Strings• Length one - character• Length greater than one - array of characters
– Sequence of characters
![Page 3: Lec08-CS110 Computational Engineering](https://reader033.vdocuments.us/reader033/viewer/2022052310/555e3736d8b42aad058b495c/html5/thumbnails/3.jpg)
• Built-in Types in C• You can define your own data types• Construct called typedef
• Later in this course• User defined data type
• Creative Problem
Today’s Lecture Contents
![Page 4: Lec08-CS110 Computational Engineering](https://reader033.vdocuments.us/reader033/viewer/2022052310/555e3736d8b42aad058b495c/html5/thumbnails/4.jpg)
Built-in Data types
• int• char• float• double• Sizes of each vary depending on
– Architecture and compiler
![Page 5: Lec08-CS110 Computational Engineering](https://reader033.vdocuments.us/reader033/viewer/2022052310/555e3736d8b42aad058b495c/html5/thumbnails/5.jpg)
Variations
• int– signed, unsigned, long, short
• float– single and double precision
• Size of “short int” less than or equal to just“int” which is less than or equal to “long int”
• The builtin function sizeof() shall return thesize of the types.
![Page 6: Lec08-CS110 Computational Engineering](https://reader033.vdocuments.us/reader033/viewer/2022052310/555e3736d8b42aad058b495c/html5/thumbnails/6.jpg)
Example Program• #include <stdio.h>• /* Program to print sizeof all datatypes */• main() {• printf("sizeof int is %d\n",sizeof(int));• printf("sizeof short int is %d\n",sizeof(short int));• printf("sizeof long int is %d\n",sizeof(long int));• printf("sizeof unsigned is %d\n",sizeof(unsigned));• printf("sizeof short unsigned is %d\n",sizeof(short unsigned));• printf("sizeof long unsigned is %d\n",sizeof(long unsigned));• printf("sizeof float is %d\n",sizeof(float));• printf("sizeof double is %d\n",sizeof(double));• }
![Page 7: Lec08-CS110 Computational Engineering](https://reader033.vdocuments.us/reader033/viewer/2022052310/555e3736d8b42aad058b495c/html5/thumbnails/7.jpg)
Output
• Apple I Mac system - Intel Core 2 DuoArchitecture
• Compiler gcc-4.0.1 versionsizeof int is 4sizeof short int is 2sizeof long int is 4sizeof unsigned is 4sizeof short unsigned is 2sizeof long unsigned is 4sizeof float is 4sizeof double is 8
![Page 8: Lec08-CS110 Computational Engineering](https://reader033.vdocuments.us/reader033/viewer/2022052310/555e3736d8b42aad058b495c/html5/thumbnails/8.jpg)
What does this mean?
• int i; // Allocated to memory 0x1000• It has 4 bytes
– B3,B2,B1,B0– B3 is most significant byte 0x1003– B0 is least significant byte stored in 0x1000– B1 is stored in 0x1001– B2 is stored in 0x1002
• This is called little-endian storage - Intelmachines use this
![Page 9: Lec08-CS110 Computational Engineering](https://reader033.vdocuments.us/reader033/viewer/2022052310/555e3736d8b42aad058b495c/html5/thumbnails/9.jpg)
What does this mean?
• int i; // Allocated to memory 0x1000• It has 4 bytes
– B3,B2,B1,B0– B3 is most significant byte 0x1000– B0 is least significant byte stored in 0x1003– B1 is stored in 0x1002– B2 is stored in 0x1001
• This is called big-endian storage - SUNprocessors - Sparc use this
![Page 10: Lec08-CS110 Computational Engineering](https://reader033.vdocuments.us/reader033/viewer/2022052310/555e3736d8b42aad058b495c/html5/thumbnails/10.jpg)
Constants in C• Remember the #define PI in the Area of the
circle problem?• That was a “real” constant
• 0, 1, 245, 623, 987 - are decimals• 0, 01, 0245, 0623 - are octals
• Begin with a “0”• So 125 is not equal to 0125• 0987 is illegal - why?
• 0x0, 0x1, 0xABCD - are hexadecimals• 0X0, 0X1, 0XABCD - are also correct way of
representing them.
![Page 11: Lec08-CS110 Computational Engineering](https://reader033.vdocuments.us/reader033/viewer/2022052310/555e3736d8b42aad058b495c/html5/thumbnails/11.jpg)
Wrong ways for integer constants
• 12,245– no comma to enhance readability
• 36.0– no dots //Not integer constant
• 10 20 30– no space in between
• 123-45-6789– no hyphen to enhance readability
• 0900– not decimal
![Page 12: Lec08-CS110 Computational Engineering](https://reader033.vdocuments.us/reader033/viewer/2022052310/555e3736d8b42aad058b495c/html5/thumbnails/12.jpg)
Wrong ways for octal constants
• 743– Should start with 0
• 05280– Illegal digit “8”
• 0777.77– No dots allowed for octal integers
![Page 13: Lec08-CS110 Computational Engineering](https://reader033.vdocuments.us/reader033/viewer/2022052310/555e3736d8b42aad058b495c/html5/thumbnails/13.jpg)
Unsigned and Long IntegerConstants
• 50000U - decimal unsigned• 123456789L - decimal long• 123456789UL - decimal unsigned long• 0123456L - octal long• 0777777U - octal unsigned• 0x5000ABCU - hexa unsigned• 0XFFFFFUL - hexa unsigned long
![Page 14: Lec08-CS110 Computational Engineering](https://reader033.vdocuments.us/reader033/viewer/2022052310/555e3736d8b42aad058b495c/html5/thumbnails/14.jpg)
Floating Point Constants
• 3 X 105
– 300000.– 3e5– 3e+5– 3E5– 3.0e+– .3e6– 0.3E6
• 3 X 105
– 30E4– 30.E+4– 300e3– ….
![Page 15: Lec08-CS110 Computational Engineering](https://reader033.vdocuments.us/reader033/viewer/2022052310/555e3736d8b42aad058b495c/html5/thumbnails/15.jpg)
Floating Point Constants
• 5.026 X 10-17
– 5.026E-17– .5026E-16– 50.26E-18– .0005026E-13
Beware: Underflows and Overflows because of
Finite precision arithmetic
![Page 16: Lec08-CS110 Computational Engineering](https://reader033.vdocuments.us/reader033/viewer/2022052310/555e3736d8b42aad058b495c/html5/thumbnails/16.jpg)
Character Constants
• ASCII code - introduced• ‘A’ is an example• ‘a1’ is wrong• sizeof(char) is 1 byte• Declared as char c;• Then assign as
– c = ‘A’ //Only single quotes
![Page 17: Lec08-CS110 Computational Engineering](https://reader033.vdocuments.us/reader033/viewer/2022052310/555e3736d8b42aad058b495c/html5/thumbnails/17.jpg)
Escape Sequences
• ‘\n’ - Line feed• ‘\t’ - Tab• ‘\r’ carriage return• Where do you use?• Get an apostrophe ‘\’’• Double quote ‘\”’• ‘\0’ is the null character
![Page 18: Lec08-CS110 Computational Engineering](https://reader033.vdocuments.us/reader033/viewer/2022052310/555e3736d8b42aad058b495c/html5/thumbnails/18.jpg)
ASCII values
char c; c = ‘A’; c = ‘\101’; //Octal of 65 c = ‘\x41’; //Hexa of 65 c = ‘\X41’;All are same. The ASCII value of
character c is decimal 65
![Page 19: Lec08-CS110 Computational Engineering](https://reader033.vdocuments.us/reader033/viewer/2022052310/555e3736d8b42aad058b495c/html5/thumbnails/19.jpg)
Symbolic Constants
Those that are defined using#define
Remember the PI in your Area of thecircle program.
![Page 20: Lec08-CS110 Computational Engineering](https://reader033.vdocuments.us/reader033/viewer/2022052310/555e3736d8b42aad058b495c/html5/thumbnails/20.jpg)
Statements
• Simple and compound statements• Expression and control statements• if (condition) S1 else S2 - Control• a = b+c; expression { pi = 3.141593; area = pi * radius * radius;} // A compound statement
![Page 21: Lec08-CS110 Computational Engineering](https://reader033.vdocuments.us/reader033/viewer/2022052310/555e3736d8b42aad058b495c/html5/thumbnails/21.jpg)
Test Your Understanding
• Which of the following are not valid identifiers– record1– 1record– $tax– file_3– File_3– return– Name_and_address– Name-and-address
![Page 22: Lec08-CS110 Computational Engineering](https://reader033.vdocuments.us/reader033/viewer/2022052310/555e3736d8b42aad058b495c/html5/thumbnails/22.jpg)
Test Your Understanding
• Which of the following are not valid identifiers– record1– 1record
– $tax– file_3– File_3– return– Name_and_address– Name-and-address
![Page 23: Lec08-CS110 Computational Engineering](https://reader033.vdocuments.us/reader033/viewer/2022052310/555e3736d8b42aad058b495c/html5/thumbnails/23.jpg)
Test Your Understanding
• Which of the following is not a ANSI Clanguage keyword?– void– enum– goto– function
![Page 24: Lec08-CS110 Computational Engineering](https://reader033.vdocuments.us/reader033/viewer/2022052310/555e3736d8b42aad058b495c/html5/thumbnails/24.jpg)
Test Your Understanding
• Which of the following is not a ANSI Clanguage keyword?– void– enum– goto– function
![Page 25: Lec08-CS110 Computational Engineering](https://reader033.vdocuments.us/reader033/viewer/2022052310/555e3736d8b42aad058b495c/html5/thumbnails/25.jpg)
Test Your Understanding
• What will be the result of the followingprogram if the inputs are 2 and 3
main() { float a,b; scanf(“%d %d”,&a,&b); printf(“%f %f”,a,b);}
![Page 26: Lec08-CS110 Computational Engineering](https://reader033.vdocuments.us/reader033/viewer/2022052310/555e3736d8b42aad058b495c/html5/thumbnails/26.jpg)
Test Your Understanding
main() { float a,b; scanf(“%d %d”,&a,&b); printf(“%f %f”,a,b);}
Ans: It compiles without any problem. It will begarbage. What does this tell?
![Page 27: Lec08-CS110 Computational Engineering](https://reader033.vdocuments.us/reader033/viewer/2022052310/555e3736d8b42aad058b495c/html5/thumbnails/27.jpg)
Test Your Understandingmain() { float a,b; scanf(“%d %d”,&a,&b); printf(“%f %f”,a,b);}
Ans: The compiler only catches some type of bugs. Youhave to take much care. Just because your programcompiled without errors/warning, you are notguaranteed of correct results.
![Page 28: Lec08-CS110 Computational Engineering](https://reader033.vdocuments.us/reader033/viewer/2022052310/555e3736d8b42aad058b495c/html5/thumbnails/28.jpg)
Test Your Understanding
• What will be the result of the followingprogram if the inputs are 2 and 3
main() { int a,b; scanf(“%d %d”,&a,&b); printf(“%f %f”,a,b);}
![Page 29: Lec08-CS110 Computational Engineering](https://reader033.vdocuments.us/reader033/viewer/2022052310/555e3736d8b42aad058b495c/html5/thumbnails/29.jpg)
Test Your Understanding
• Junk will be printed for this too. main() { int a,b; scanf(“%d %d”,&a,&b); printf(“%f %f”,a,b);}
![Page 30: Lec08-CS110 Computational Engineering](https://reader033.vdocuments.us/reader033/viewer/2022052310/555e3736d8b42aad058b495c/html5/thumbnails/30.jpg)
Test Your Understanding
• What will be the result of the followingprogram
main() { int a,b; a = 10; b = 20; printf(“%d ”,a,b);}
![Page 31: Lec08-CS110 Computational Engineering](https://reader033.vdocuments.us/reader033/viewer/2022052310/555e3736d8b42aad058b495c/html5/thumbnails/31.jpg)
Test Your Understanding
• It shall print 10main() { int a,b; a = 10; b = 20; printf(“%d ”,a,b);}
![Page 32: Lec08-CS110 Computational Engineering](https://reader033.vdocuments.us/reader033/viewer/2022052310/555e3736d8b42aad058b495c/html5/thumbnails/32.jpg)
Conclusion
• Compilers do not catch most of yourbugs
• Reasons are many– Attend the 5th semester Language
Translator BTech Core course in CSE Dept• You train yourself to be a CAREFUL
PROGRAMMER
![Page 33: Lec08-CS110 Computational Engineering](https://reader033.vdocuments.us/reader033/viewer/2022052310/555e3736d8b42aad058b495c/html5/thumbnails/33.jpg)
Creative Problem
• Suppose you are using a program thatreads in a large English text file as inputand processes it. The program for somereason does not like a set of words andwhenever it sees the same in your textfile it halts and outputs “Error”. It is soangry that it does not say what the wordis and which line it occurred or anyother info.
![Page 34: Lec08-CS110 Computational Engineering](https://reader033.vdocuments.us/reader033/viewer/2022052310/555e3736d8b42aad058b495c/html5/thumbnails/34.jpg)
Creative Problem
• You do not have a list of offendingwords. Devise a strategy to identify thefirst offending word in your text. Expressthe efficiency of your strategy in termsof the number of words in the input textfile.
![Page 35: Lec08-CS110 Computational Engineering](https://reader033.vdocuments.us/reader033/viewer/2022052310/555e3736d8b42aad058b495c/html5/thumbnails/35.jpg)
Thank You
• SAARANG Time :-)– Do not go home - it is your festival– No lab this week– People who have pending work to be done
can visit labs on Monday and Tuesday tofinish backlogs.
– Lab exercises 1 and 2• Grades to be finalized this week.