SlideShare ist ein Scribd-Unternehmen logo
1 von 35
Downloaden Sie, um offline zu lesen
CS110: Data Types in C
Lecture 8
V. Kamakoti
20th January 2008
Datatypes
• Types of Data you can use in C without
explicit definition by the user
– Arithmetic
• Integer domain
• Real domain

– Strings
• Length one - character
• Length greater than one - array of characters
– Sequence of characters
Today’s Lecture Contents

• 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
Built-in Data types
•
•
•
•
•

int
char
float
double
Sizes of each vary depending on
– Architecture and compiler
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 the
size of the types.
Example Program
•
•
•
•
•
•
•
•
•
•
•
•

#include <stdio.h>
/* Program to print sizeof all datatypes */
main() {
printf("sizeof int is %dn",sizeof(int));
printf("sizeof short int is %dn",sizeof(short int));
printf("sizeof long int is %dn",sizeof(long int));
printf("sizeof unsigned is %dn",sizeof(unsigned));
printf("sizeof short unsigned is %dn",sizeof(short unsigned));
printf("sizeof long unsigned is %dn",sizeof(long unsigned));
printf("sizeof float is %dn",sizeof(float));
printf("sizeof double is %dn",sizeof(double));
}
Output
• Apple I Mac system - Intel Core 2 Duo
Architecture
• Compiler gcc-4.0.1 version
sizeof int is 4
sizeof short int is 2
sizeof long int is 4
sizeof unsigned is 4
sizeof short unsigned is 2
sizeof long unsigned is 4
sizeof float is 4
sizeof double is 8
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 - Intel
machines use this
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 - SUN
processors - Sparc use this
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.
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
Wrong ways for octal constants
• 743
– Should start with 0

• 05280
– Illegal digit “8”

• 0777.77
– No dots allowed for octal integers
Unsigned and Long Integer
Constants
•
•
•
•
•
•
•

50000U - decimal unsigned
123456789L - decimal long
123456789UL - decimal unsigned long
0123456L - octal long
0777777U - octal unsigned
0x5000ABCU - hexa unsigned
0XFFFFFUL - hexa unsigned long
Floating Point Constants
• 3X

105

– 300000.
– 3e5
– 3e+5
– 3E5
– 3.0e+
– .3e6
– 0.3E6

• 3 X 105
– 30E4
– 30.E+4
– 300e3
– ….
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
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
Escape Sequences
•
•
•
•
•
•
•

‘n’ - Line feed
‘t’ - Tab
‘r’ carriage return
Where do you use?
Get an apostrophe ‘’’
Double quote ‘”’
‘0’ is the null character
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
Symbolic Constants
Those that are defined using
#define
Remember the PI in your Area of the
circle program.
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
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
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
Test Your Understanding
• Which of the following is not a ANSI C
language keyword?
– void
– enum
– goto
– function
Test Your Understanding
• Which of the following is not a ANSI C
language keyword?
– void
– enum
– goto
– function
Test Your Understanding
• What will be the result of the following
program if the inputs are 2 and 3
main() {
float a,b;
scanf(“%d %d”,&a,&b);
printf(“%f %f”,a,b);
}
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 be
garbage. What does this tell?
Test Your Understanding
main() {
float a,b;
scanf(“%d %d”,&a,&b);
printf(“%f %f”,a,b);
}
Ans: The compiler only catches some type of bugs. You
have to take much care. Just because your program
compiled without errors/warning, you are not
guaranteed of correct results.
Test Your Understanding
• What will be the result of the following
program if the inputs are 2 and 3
main() {
int a,b;
scanf(“%d %d”,&a,&b);
printf(“%f %f”,a,b);
}
Test Your Understanding
• Junk will be printed for this too.
main() {
int a,b;
scanf(“%d %d”,&a,&b);
printf(“%f %f”,a,b);
}
Test Your Understanding
• What will be the result of the following
program
main() {
int a,b;
a = 10; b = 20;
printf(“%d ”,a,b);
}
Test Your Understanding
• It shall print 10
main() {
int a,b;
a = 10; b = 20;
printf(“%d ”,a,b);
}
Conclusion
• Compilers do not catch most of your
bugs
• Reasons are many
– Attend the 5th semester Language
Translator BTech Core course in CSE Dept

• You train yourself to be a CAREFUL
PROGRAMMER
Creative Problem
• Suppose you are using a program that
reads in a large English text file as input
and processes it. The program for some
reason does not like a set of words and
whenever it sees the same in your text
file it halts and outputs “Error”. It is so
angry that it does not say what the word
is and which line it occurred or any
other info.
Creative Problem
• You do not have a list of offending
words. Devise a strategy to identify the
first offending word in your text. Express
the efficiency of your strategy in terms
of the number of words in the input text
file.
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 to
finish backlogs.
– Lab exercises 1 and 2
• Grades to be finalized this week.

Weitere ähnliche Inhalte

Was ist angesagt?

Code generator
Code generatorCode generator
Code generator
Tech_MX
 

Was ist angesagt? (20)

C++ Tokens
C++ TokensC++ Tokens
C++ Tokens
 
c++
 c++  c++
c++
 
Code generator
Code generatorCode generator
Code generator
 
Basics of c++ Programming Language
Basics of c++ Programming LanguageBasics of c++ Programming Language
Basics of c++ Programming Language
 
Programming for Problem Solving Unit 2
Programming for Problem Solving Unit 2Programming for Problem Solving Unit 2
Programming for Problem Solving Unit 2
 
Getting started with c++
Getting started with c++Getting started with c++
Getting started with c++
 
Intermediate code
Intermediate codeIntermediate code
Intermediate code
 
Intermediate code generation
Intermediate code generationIntermediate code generation
Intermediate code generation
 
CLASS VIII COMPUTERS FLOW CHART AND ALGORITHM
CLASS VIII COMPUTERS FLOW CHART AND ALGORITHMCLASS VIII COMPUTERS FLOW CHART AND ALGORITHM
CLASS VIII COMPUTERS FLOW CHART AND ALGORITHM
 
Cse115 lecture08repetitionstructures part02
Cse115 lecture08repetitionstructures part02Cse115 lecture08repetitionstructures part02
Cse115 lecture08repetitionstructures part02
 
Vhdl introduction
Vhdl introductionVhdl introduction
Vhdl introduction
 
Constants and variables in c programming
Constants and variables in c programmingConstants and variables in c programming
Constants and variables in c programming
 
Programming for Problem Solving Unit 1
Programming for Problem Solving Unit 1Programming for Problem Solving Unit 1
Programming for Problem Solving Unit 1
 
Constant, variables, data types
Constant, variables, data typesConstant, variables, data types
Constant, variables, data types
 
Basics of c++
Basics of c++Basics of c++
Basics of c++
 
C++ Basics
C++ BasicsC++ Basics
C++ Basics
 
Lecture 12 intermediate code generation
Lecture 12 intermediate code generationLecture 12 intermediate code generation
Lecture 12 intermediate code generation
 
Pointer and string(Programming fundamental)
Pointer and string(Programming fundamental)Pointer and string(Programming fundamental)
Pointer and string(Programming fundamental)
 
Unit 2. Elements of C
Unit 2. Elements of CUnit 2. Elements of C
Unit 2. Elements of C
 
Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design)   Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design)
 

Andere mochten auch

Code Analysis-run time error prediction
Code Analysis-run time error predictionCode Analysis-run time error prediction
Code Analysis-run time error prediction
NIKHIL NAWATHE
 
Floating point units
Floating point unitsFloating point units
Floating point units
dipugovind
 
floating point multiplier
floating point multiplierfloating point multiplier
floating point multiplier
Bipin Likhar
 

Andere mochten auch (12)

Code Analysis-run time error prediction
Code Analysis-run time error predictionCode Analysis-run time error prediction
Code Analysis-run time error prediction
 
&lt;marquee>html title testfsdjk34254&lt;/marquee>
&lt;marquee>html title testfsdjk34254&lt;/marquee>&lt;marquee>html title testfsdjk34254&lt;/marquee>
&lt;marquee>html title testfsdjk34254&lt;/marquee>
 
Memory management of datatypes
Memory management of datatypesMemory management of datatypes
Memory management of datatypes
 
IEEE Floating Point
IEEE Floating PointIEEE Floating Point
IEEE Floating Point
 
XP - eXtreme Programming - 2010
XP - eXtreme Programming - 2010XP - eXtreme Programming - 2010
XP - eXtreme Programming - 2010
 
Fixed point and floating-point numbers
Fixed point and  floating-point numbersFixed point and  floating-point numbers
Fixed point and floating-point numbers
 
Floating Point Numbers
Floating Point NumbersFloating Point Numbers
Floating Point Numbers
 
Floating point units
Floating point unitsFloating point units
Floating point units
 
Quick tutorial on IEEE 754 FLOATING POINT representation
Quick tutorial on IEEE 754 FLOATING POINT representationQuick tutorial on IEEE 754 FLOATING POINT representation
Quick tutorial on IEEE 754 FLOATING POINT representation
 
floating point multiplier
floating point multiplierfloating point multiplier
floating point multiplier
 
Floating point ALU using VHDL implemented on FPGA
Floating point ALU using VHDL implemented on FPGAFloating point ALU using VHDL implemented on FPGA
Floating point ALU using VHDL implemented on FPGA
 
Designing of 8 BIT Arithmetic and Logical Unit and implementing on Xilinx Ver...
Designing of 8 BIT Arithmetic and Logical Unit and implementing on Xilinx Ver...Designing of 8 BIT Arithmetic and Logical Unit and implementing on Xilinx Ver...
Designing of 8 BIT Arithmetic and Logical Unit and implementing on Xilinx Ver...
 

Ähnlich wie Lec08-CS110 Computational Engineering

Ähnlich wie Lec08-CS110 Computational Engineering (20)

Lec-1c.pdf
Lec-1c.pdfLec-1c.pdf
Lec-1c.pdf
 
C introduction by thooyavan
C introduction by  thooyavanC introduction by  thooyavan
C introduction by thooyavan
 
Learn C LANGUAGE at ASIT
Learn C LANGUAGE at ASITLearn C LANGUAGE at ASIT
Learn C LANGUAGE at ASIT
 
C PROGRAMING.pptx
C PROGRAMING.pptxC PROGRAMING.pptx
C PROGRAMING.pptx
 
INTRODUCTION TO C PROGRAMMING in basic c language
INTRODUCTION TO C PROGRAMMING in basic c languageINTRODUCTION TO C PROGRAMMING in basic c language
INTRODUCTION TO C PROGRAMMING in basic c language
 
c-introduction.pptx
c-introduction.pptxc-introduction.pptx
c-introduction.pptx
 
Introduction to C
Introduction to CIntroduction to C
Introduction to C
 
C language
C languageC language
C language
 
Chapter1.pptx
Chapter1.pptxChapter1.pptx
Chapter1.pptx
 
C programming language tutorial
C programming language tutorialC programming language tutorial
C programming language tutorial
 
Lecture1
Lecture1Lecture1
Lecture1
 
C
CC
C
 
C programming language
C programming languageC programming language
C programming language
 
College1
College1College1
College1
 
Introduction to C programming
Introduction to C programmingIntroduction to C programming
Introduction to C programming
 
M.Florence Dayana / Basics of C Language
M.Florence Dayana / Basics of C LanguageM.Florence Dayana / Basics of C Language
M.Florence Dayana / Basics of C Language
 
Learn c++ Programming Language
Learn c++ Programming LanguageLearn c++ Programming Language
Learn c++ Programming Language
 
Chp1(c 2 c++)
Chp1(c 2 c++)Chp1(c 2 c++)
Chp1(c 2 c++)
 
Basic c
Basic cBasic c
Basic c
 
C tutorial
C tutorialC tutorial
C tutorial
 

Mehr von Sri Harsha Pamu

Mehr von Sri Harsha Pamu (20)

Lec23-CS110 Computational Engineering
Lec23-CS110 Computational EngineeringLec23-CS110 Computational Engineering
Lec23-CS110 Computational Engineering
 
Lec21-CS110 Computational Engineering
Lec21-CS110 Computational EngineeringLec21-CS110 Computational Engineering
Lec21-CS110 Computational Engineering
 
Lec19-CS110 Computational Engineering
Lec19-CS110 Computational EngineeringLec19-CS110 Computational Engineering
Lec19-CS110 Computational Engineering
 
Lec16-CS110 Computational Engineering
Lec16-CS110 Computational EngineeringLec16-CS110 Computational Engineering
Lec16-CS110 Computational Engineering
 
Lec15-CS110 Computational Engineering
Lec15-CS110 Computational EngineeringLec15-CS110 Computational Engineering
Lec15-CS110 Computational Engineering
 
Lec14-CS110 Computational Engineering
Lec14-CS110 Computational EngineeringLec14-CS110 Computational Engineering
Lec14-CS110 Computational Engineering
 
Lec13
Lec13Lec13
Lec13
 
Lec12-CS110 Computational Engineering
Lec12-CS110 Computational EngineeringLec12-CS110 Computational Engineering
Lec12-CS110 Computational Engineering
 
Lec10-CS110 Computational Engineering
Lec10-CS110 Computational EngineeringLec10-CS110 Computational Engineering
Lec10-CS110 Computational Engineering
 
Lec09-CS110 Computational Engineering
Lec09-CS110 Computational EngineeringLec09-CS110 Computational Engineering
Lec09-CS110 Computational Engineering
 
Lec07-CS110 Computational Engineering
Lec07-CS110 Computational EngineeringLec07-CS110 Computational Engineering
Lec07-CS110 Computational Engineering
 
Lec06-CS110 Computational Engineering
Lec06-CS110 Computational EngineeringLec06-CS110 Computational Engineering
Lec06-CS110 Computational Engineering
 
Lec04-CS110 Computational Engineering
Lec04-CS110 Computational EngineeringLec04-CS110 Computational Engineering
Lec04-CS110 Computational Engineering
 
Lec03-CS110 Computational Engineering
Lec03-CS110 Computational EngineeringLec03-CS110 Computational Engineering
Lec03-CS110 Computational Engineering
 
Lec02-CS110 Computational Engineering
Lec02-CS110 Computational EngineeringLec02-CS110 Computational Engineering
Lec02-CS110 Computational Engineering
 
Lec01-CS110 Computational Engineering
Lec01-CS110 Computational EngineeringLec01-CS110 Computational Engineering
Lec01-CS110 Computational Engineering
 
Lec1- CS110 Computational Engineering
Lec1- CS110 Computational EngineeringLec1- CS110 Computational Engineering
Lec1- CS110 Computational Engineering
 
Lec25-CS110 Computational Engineering
Lec25-CS110 Computational EngineeringLec25-CS110 Computational Engineering
Lec25-CS110 Computational Engineering
 
Android..imp google
Android..imp googleAndroid..imp google
Android..imp google
 
Android vulnerability study
Android vulnerability studyAndroid vulnerability study
Android vulnerability study
 

Kürzlich hochgeladen

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 

Kürzlich hochgeladen (20)

ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 

Lec08-CS110 Computational Engineering

  • 1. CS110: Data Types in C Lecture 8 V. Kamakoti 20th January 2008
  • 2. Datatypes • Types of Data you can use in C without explicit definition by the user – Arithmetic • Integer domain • Real domain – Strings • Length one - character • Length greater than one - array of characters – Sequence of characters
  • 3. Today’s Lecture Contents • 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
  • 4. Built-in Data types • • • • • int char float double Sizes of each vary depending on – Architecture and compiler
  • 5. 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 the size of the types.
  • 6. Example Program • • • • • • • • • • • • #include <stdio.h> /* Program to print sizeof all datatypes */ main() { printf("sizeof int is %dn",sizeof(int)); printf("sizeof short int is %dn",sizeof(short int)); printf("sizeof long int is %dn",sizeof(long int)); printf("sizeof unsigned is %dn",sizeof(unsigned)); printf("sizeof short unsigned is %dn",sizeof(short unsigned)); printf("sizeof long unsigned is %dn",sizeof(long unsigned)); printf("sizeof float is %dn",sizeof(float)); printf("sizeof double is %dn",sizeof(double)); }
  • 7. Output • Apple I Mac system - Intel Core 2 Duo Architecture • Compiler gcc-4.0.1 version sizeof int is 4 sizeof short int is 2 sizeof long int is 4 sizeof unsigned is 4 sizeof short unsigned is 2 sizeof long unsigned is 4 sizeof float is 4 sizeof double is 8
  • 8. 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 - Intel machines use this
  • 9. 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 - SUN processors - Sparc use this
  • 10. 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.
  • 11. 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
  • 12. Wrong ways for octal constants • 743 – Should start with 0 • 05280 – Illegal digit “8” • 0777.77 – No dots allowed for octal integers
  • 13. Unsigned and Long Integer Constants • • • • • • • 50000U - decimal unsigned 123456789L - decimal long 123456789UL - decimal unsigned long 0123456L - octal long 0777777U - octal unsigned 0x5000ABCU - hexa unsigned 0XFFFFFUL - hexa unsigned long
  • 14. Floating Point Constants • 3X 105 – 300000. – 3e5 – 3e+5 – 3E5 – 3.0e+ – .3e6 – 0.3E6 • 3 X 105 – 30E4 – 30.E+4 – 300e3 – ….
  • 15. 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
  • 16. 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
  • 17. Escape Sequences • • • • • • • ‘n’ - Line feed ‘t’ - Tab ‘r’ carriage return Where do you use? Get an apostrophe ‘’’ Double quote ‘”’ ‘0’ is the null character
  • 18. 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
  • 19. Symbolic Constants Those that are defined using #define Remember the PI in your Area of the circle program.
  • 20. 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
  • 21. 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
  • 22. 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
  • 23. Test Your Understanding • Which of the following is not a ANSI C language keyword? – void – enum – goto – function
  • 24. Test Your Understanding • Which of the following is not a ANSI C language keyword? – void – enum – goto – function
  • 25. Test Your Understanding • What will be the result of the following program if the inputs are 2 and 3 main() { float a,b; scanf(“%d %d”,&a,&b); printf(“%f %f”,a,b); }
  • 26. 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 be garbage. What does this tell?
  • 27. Test Your Understanding main() { float a,b; scanf(“%d %d”,&a,&b); printf(“%f %f”,a,b); } Ans: The compiler only catches some type of bugs. You have to take much care. Just because your program compiled without errors/warning, you are not guaranteed of correct results.
  • 28. Test Your Understanding • What will be the result of the following program if the inputs are 2 and 3 main() { int a,b; scanf(“%d %d”,&a,&b); printf(“%f %f”,a,b); }
  • 29. Test Your Understanding • Junk will be printed for this too. main() { int a,b; scanf(“%d %d”,&a,&b); printf(“%f %f”,a,b); }
  • 30. Test Your Understanding • What will be the result of the following program main() { int a,b; a = 10; b = 20; printf(“%d ”,a,b); }
  • 31. Test Your Understanding • It shall print 10 main() { int a,b; a = 10; b = 20; printf(“%d ”,a,b); }
  • 32. Conclusion • Compilers do not catch most of your bugs • Reasons are many – Attend the 5th semester Language Translator BTech Core course in CSE Dept • You train yourself to be a CAREFUL PROGRAMMER
  • 33. Creative Problem • Suppose you are using a program that reads in a large English text file as input and processes it. The program for some reason does not like a set of words and whenever it sees the same in your text file it halts and outputs “Error”. It is so angry that it does not say what the word is and which line it occurred or any other info.
  • 34. Creative Problem • You do not have a list of offending words. Devise a strategy to identify the first offending word in your text. Express the efficiency of your strategy in terms of the number of words in the input text file.
  • 35. 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 to finish backlogs. – Lab exercises 1 and 2 • Grades to be finalized this week.