1 Lecture 7  Fundamental data types in C  Data type conversion:  Automatic  Casting  Character processing  getchar()  putchar()  Macros on ctype.h.

Slides:



Advertisements
Similar presentations
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Advertisements

©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Lecture 071 CS 192 Lecture 7 Winter 2003 December 15-16, 2003 Dr. Shafay Shamail.
1 Chapter 6 The Fundamental Data Types. 2 Outline  Declarations and expressions  Fundamental data types  Characters and the data type char  The Data.
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
1 Review of Class on Oct Outline of Chapter 4  How to write a function?  Function Prototypes  Function Invocation  Function Definition  The.
1 Fundamental Data Types. 2 Declaration All variables must be declared before being used. –Tells the compiler to set aside an appropriate amount of space.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
CS1061 C Programming Lecture 4: Indentifiers and Integers A.O’Riordan, 2004.
1 Character Processing How characters can be treated as small integers? How characters are stored and manipulated in a machine? How use is made of certain.
Chapter 2 Data Types, Declarations, and Displays
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
Representation and Conversion of Numeric Types 4 We have seen multiple data types that C provides for numbers: int and double 4 What differences are there.
1 Review of Chapter 6: The Fundamental Data Types.
Data Type. A data type defines a set of values that a variable can store along with a set of operations that can be performed on that variable. Common.
Basic Elements of C++ Chapter 2.
C Programming. Chapter – 1 Introduction Study Book for one month – 25% Learning rate Use Compiler for one month – 60%
C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
CHAPTER 8 CHARACTER AND STRINGS
Input & Output: Console
CS2311 Computer Programming Dr. Yang, Qingxiong (with slides borrowed from Dr. Xu, Henry) Lecture 2: Basic Syntax – Part I: Variables and Constants.
BBS514 Structured Programming (Yapısal Programlama)1 Character Processing, Strings and Pointers,
C Tokens Identifiers Keywords Constants Operators Special symbols.
Week 1 Algorithmization and Programming Languages.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
Fundamental Data Types, Operators and Expressions Kernighan/Ritchie: Kelley/Pohl: Chapter 2 Chapter 2, 3.
Data Type. Syntax Rules Recap keywords breakdoubleifsizeofvoid caseelseintstatic..... Identifiers not#me123th scanfprintf _idso_am_igedd007 Constant ‘a’‘+’
CS115 FALL Senem KUMOVA-METİN1 The Fundamental Data Types CHAPTER 3.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Chapter-4 Managing input and Output operation.  Reading, processing and writing of data are three essential functions of a computer program.  Most programs.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types –
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 1.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
CSC Programming for Science Lecture 8: Character Functions.
Module B - Computation1/61 Module-B-Computation Variables Basic Memory Operations Expressions.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
Gator Engineering Project 1 Grades released Re-grading –Within one week –TA: Fardad, or office hours: MW 2:00 – 4:00 PM TA Huiyuan’s office hour.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 2 : August 28 webpage:
Basic Data Types & Memory & Representation. Basic data types Primitive data types are similar to JAVA: char int short long float double Unlike in JAVA,
7. BASIC TYPES. Systems of numeration Numeric Types C’s basic types include integer types and floating types. Integer types can be either signed or unsigned.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 2.
Chapter 1 slides1 What is C? A high-level language that is extremely useful for engineering computations. A computer language that has endured for almost.
1Object-Oriented Program Development Using C++ Built-in Data Types Data type –Range of values –Set of operations on those values Literal: refers to acceptable.
Basic Data Types & Memory & Representation
Character Processing How characters can be treated as small integers?
Programming Fundamentals
Tokens in C Keywords Identifiers Constants
7. BASIC TYPES.
TMF1414 Introduction to Programming
Introduction to C CSE 2031 Fall /3/ :33 AM.
Fundamental Data Types
DATA HANDLING.
Lectures on Numerical Methods
Basic Types Chapter 7 Copyright © 2008 W. W. Norton & Company.
Character Processing How characters can be treated as small integers?
Fundamental Data Types
Homework Finishing Chapter 2 of K&R. We will go through Chapter 3 very quickly. Not a lot is new. Questions?
Introduction to C EECS May 2019.
Data Type.
DATA TYPES There are four basic data types associated with variables:
Expressions An Expression is a sequence of operands and operators that reduces to a single value. An operator is a language-specific syntactical token.
Introduction to C CSE 2031 Fall /15/2019 8:26 AM.
INTRODUCTION TO C.
Presentation transcript:

1 Lecture 7  Fundamental data types in C  Data type conversion:  Automatic  Casting  Character processing  getchar()  putchar()  Macros on ctype.h  Readings: Chapter 6, Section 1-12, Chapter 5

2 Fundamental data types  There are 5 basic data types in C:  char : characters  int : integers  float, double : floating values  void : empty value  char and int are collectively known as integral types  Variants of these types can be formed by adding type modifiers: long, short, signed, unsigned

3 short and long  Affect size of storage and hence range of values  Use short to conserve space  Use long to have larger range  Exact effect depends on compilers  Applicable to int: short int, int, long int  long is applicable to double too; thus the various floating point types are: float, double, long double

4 signed and unsigned  Applicable to char: char, signed char, unsigned char  Applicable to integers:  Integers are signed by default, thus signed int is the same as int  unsigned types can only store non-negative integers  Can be used with other modifiers. E.g., unsigned short int, unsigned long int

5 Fundamental data types (summary)  Long form charsigned charunsigned char signed short intsigned intsigned long int unsigned short intunsigned intunsigned long int floatdoublelong double  Short form charcharunsigned char short intlong unsigned shortunsignedunsigned long floatdoublelong double

6 The sizeof operator  Used to find the number of bytes needed to store an object (which can be a variable or a data type)  Its result is typically returned as an unsigned integer  It is called a compile-time operator because its result depends on compiler  E.g. int len1, len2; float x; len1 = sizeof(int); len2 = sizeof(x);

7 Automatic Type Conversions  Automatic type conversions are performed when:  a value is assigned to a variable of a different type  a value is passed to a function parameter of a different type  a value is returned from a function of a different type  evaluating an expression with operands of different type  When evaluating an expression, the operands are promoted to the “ highest ” type in the expression.

8 Hierarchy of Built-in Data Types  long double  double  float  unsigned long int  long int  unsigned int  int  unsigned short int  short int  unsigned char  char

9 Casts  Explicit type conversions called casts is supported in C  If x is an int, then (double) x is an expression with value x and type double. Type and value of x itself remain unchanged.  Casts can be applied to expressions, e.g, int x=4, y=3; double z; Z = (double)x/y;

10 The data type char  A character is stored as an 8-bit integer using the ASCII encoding.  Example: charc = ’a’; Internally, c is stored as the following bit pattern which is equivalent to a decimal 97  Variables of any integral type can be used to store characters. int i=’a’; printf(“%d\n”,i); /* 97 */

11 The data type char (cont’d)  Any integral expression can be printed as a character or an integer E.g.: printf(“%c\n”,95+2); /* ‘a’ */ printf(“%d\n”,95+2); /* 97 */  Depending on the compiler, the type char is equivalent to either signed char or unsigned char  signed char and unsigned char are used less often than char in typical C programs

12 The data type char (cont’d)  note that ASCII value of ‘7’ is 55 but not 7

13 Nonprinting & hard-to-print characters  Preceded by the escape character backslash \ as it is used to “escape” the usual meaning of the character that follows it, e.g., /*print “ABC” */ printf (“\”ABC\””);

14 The use of getchar () & putchar ()  Input and output of character can be done by using getchar () and putchar () which are defined in  getchar () reads a character from the keyboard. It returns the character read if successful, and EOF otherwise.  The function prototype is: int getchar(void);  putchar () writes a character to the screen. It returns the character printed if successful, and EOF otherwise.  The function prototype is: int putchar(int);

15 Example: lower- to upper-case conversion #include /* for I/O handling */ #include /* for character handling */ void main(void) { int c; while ((c = getchar()) != EOF) if (islower(c)) putchar(toupper(c)); else putchar(c); }

16 Macros and functions in  The header file ctype.h contains a set of macros and functions for character handling  The macros usually takes an argument of type int and return an int value that is either nonzero (true) or zero (false)

17 Macros and functions in (cont’d)  Note that the value of c stored in memory is not changed after the execution of the functions and macros defined in ctype.h