 2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Discussion 2 Number Systems and their importance.

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

Lecture 2 Introduction to C Programming
Introduction to C Programming
 2005 Pearson Education, Inc. All rights reserved Introduction.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
CS1061 C Programming Lecture 16: Formatted I/0 A. O’Riordan, 2004.
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
Software Development Method. Assignments Due – Homework 0, Warmup Reading – Chapter 2 –
C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX.
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
Introduction to C Programming CE Lecture 2 Basics of C programming.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Data types and variables
Chapter 2 Data Types, Declarations, and Displays
Introduction to C Programming
The printf Method The printf method is another way to format output. It is based on the printf function of the C language. System.out.printf(,,,..., );
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
Input/Output  Input/Output operations are performed using input/output functions  Common input/output functions are provided as part of C’s standard.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Outline Variables 1.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Streams Streams –Sequences of characters organized.
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.
1 IPC144 Session 11 The C Programming Language. 2 Objectives To format a #define statement correctly To use a #define statement in a C program To construct.
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
 Pearson Education, Inc. All rights reserved Formatted Output.
Sales person receive RM200/week plus 9% of their gross sales for that week. Write an algorithms to calculate the sales person’s earning from the input.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Numeric types of C Integers – Signed and unsigned – In a few different sizes – 209 Floating point – In different sizes –
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
CS115 FALL Senem KUMOVA-METİN1 The Fundamental Data Types CHAPTER 3.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types –
Module B - Computation1/61 Module-B-Computation Variables Basic Memory Operations Expressions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
+ Note On the Use of Different Data Types Use the data type that conserves memory and still accomplishes the desired purpose. For example, depending on.
Formatted I/O ä ä Standard Output ä ä printf() family of functions ä ä Standard Input ä ä scanf() family of functions.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
C Formatted Input/Output
CSE 220 – C Programming Bitwise Operators.
Variables Mr. Crone.
ECE Application Programming
ITEC113 Algorithms and Programming Techniques
TMF1414 Introduction to Programming
ICS103 Programming in C Lecture 3: Introduction to C (2)
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Data Representation Data Types Complements Fixed Point Representation
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
INPUT & OUTPUT scanf & printf.
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Conversion Check your class notes and given examples at class.
Chapter 2 - Introduction to C Programming
Variables in C Topics Naming Variables Declaring Variables
Introduction to C Programming
Variables in C Topics Naming Variables Declaring Variables
Presentation transcript:

 2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Discussion 2 Number Systems and their importance –Binary Binary two's complement negative representation –Octal –Hexadecimal Macros Types The format-control specifiers for the function printf()

 2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach The Binary Number System All data is stored as binary in a computer –Data is stored as a series of high and low voltages on a computer. –Transistors and logic gates enable arithmetic and logical operations to be performed on binary data.

 2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach The Binary Number System Simple Binary Arithmetic –addition –multiplication –complement

 2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Conversion from binary to decimal

 2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach The Binary Number System Negative Number Representation –Different methods exist sign-and-magnitude Binary two's complement –Which method is better?

 2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach The Octal Number System What is the octal number system? Why do we use the octal number system? –Conversion to and from binary to octal –Common concepts represented in octal

 2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach The Hexadecimal Number System What is the hexadecimal system? What is the hexadecimal system used for? –Memory Addresses –MAC Addresses –WEP Wireless Keys Conversion to and from binary to hexadecimal

 2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Base Conversions for Integers Find the decimal, octal, and hexadecimal values of the following binary numbers. –0b (decimal), 063 (octal), 0x33 (hexadecimal)‏ –0b (two's complement notation)‏ -5 (decimal), 0373, 0xFB

 2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Binary Representation for Integers Find the binary values of the following integers using 8 bits. –28 0b –-28 0b

 2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Macros What is a macro? How does a macro work? What do we use macros for and why? Some final tips regarding macros

 2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Macros What is a macro? –A macro is a “shortcut”. It allows a programmer to “macro” a number or variable. #define M_PI #define NUM_PPL 54

 2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Macros How does a macro work? –When a C program is executed, the execution engine will literally replace all occurrences of a macro with its defined value. #include #define DEFAULT_NUM 5 int main() { printf(“My default num is %d.\n”, DEFAULT_NUM); return 0; }

 2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Macros What do we use macros for and why? –To avoid “magic numbers” A magic number is a numeric constant that appears in code with no explanation. –To replace long variable names or numbers by meaningful shorter macros that are easier to type, dead, and change. Easy to develop and maintain a program.

 2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Macros Some final considerations –Macros are generally named in capital letters. –Macro names should be meaningful. –Macros cannot be changed during program execution, unlike normal variables which may change value during the execution.

 2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Types Variables in C must be “declared” before they may be used. Each variable in C is a certain “type”. Different types of variables are useful for different things, or can hold a different amount of information.

 2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach The format specifiers for printf() “%b” in Ch onlybinary number “%uhd”unsigned short “%ulld”unsigned long long “%u”unsigned int “%hd”short “%lf”double “%s”string “%f”float “%p”pointer “%lld”long “%d”int “%c”char Format-Control-StringArgument Type

 2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach The precision and field width for printf() specifiers Additional information may be provided to the specifiers to further refine output. %.10lf ← This tells printf() to print a double with 10 digits after the decimal point. %20.5lf ← this tells printf() to print a double that takes up to 20 character spaces. The left side of the number is padded with spaces to make up the difference.

 2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach printf() specifiers > printf("%20lf\n", 5.4) > printf("%5.10lf\n", 5.4) > printf("%.10lf\n", 5.4); > printf("%15.10lf\n", 5.4); > printf("%8.2lf\n", 5.4); 5.40