MPATE-GE 2618: C Programming for Music Technology. Unit 1.2

Similar documents
The programming language C. sws1 1

Useful Number Systems

Sources: On the Web: Slides will be available on:

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Pemrograman Dasar. Basic Elements Of Java

Informatica e Sistemi in Tempo Reale

Oct: 50 8 = 6 (r = 2) 6 8 = 0 (r = 6) Writing the remainders in reverse order we get: (50) 10 = (62) 8

So far we have considered only numeric processing, i.e. processing of numeric data represented

Computer Science 281 Binary and Hexadecimal Review

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

C++ Language Tutorial

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

Binary Numbers. Binary Octal Hexadecimal

Numeral Systems. The number twenty-five can be represented in many ways: Decimal system (base 10): 25 Roman numerals:

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

Chapter 4: Computer Codes

DNA Data and Program Representation. Alexandre David

CS101 Lecture 11: Number Systems and Binary Numbers. Aaron Stevens 14 February 2011

Base Conversion written by Cathy Saxton

Binary Representation

C / C++ and Unix Programming. Materials adapted from Dan Hood and Dianna Xu

2 Number Systems. Source: Foundations of Computer Science Cengage Learning. Objectives After studying this chapter, the student should be able to:

Chapter 2: Elements of Java

Simple C++ Programs. Engineering Problem Solving with C++, Etter/Ingber. Dev-C++ Dev-C++ Windows Friendly Exit. The C++ Programming Language

Caml Virtual Machine File & data formats Document version: 1.4

2011, The McGraw-Hill Companies, Inc. Chapter 3

As previously noted, a byte can contain a numeric value in the range Computers don't understand Latin, Cyrillic, Hindi, Arabic character sets!

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Fundamentals of Programming

Topics. Parts of a Java Program. Topics (2) CS 146. Introduction To Computers And Java Chapter Objectives To understand:

The string of digits in the binary number system represents the quantity

Number Representation

Levent EREN A-306 Office Phone: INTRODUCTION TO DIGITAL LOGIC

Chapter 2. Binary Values and Number Systems

2010/9/19. Binary number system. Binary numbers. Outline. Binary to decimal

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

Counting in base 10, 2 and 16

Binary Representation. Number Systems. Base 10, Base 2, Base 16. Positional Notation. Conversion of Any Base to Decimal.

About The Tutorial. Audience. Prerequisites. Copyright & Disclaimer

LSN 2 Number Systems. ECT 224 Digital Computer Fundamentals. Department of Engineering Technology

Section 1.4 Place Value Systems of Numeration in Other Bases

Digital System Design Prof. D Roychoudhry Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Variables, Constants, and Data Types

Lecture 2. Binary and Hexadecimal Numbers

PROGRAMMING IN C PROGRAMMING IN C CONTENT AT A GLANCE

Chapter One Introduction to Programming

Systems I: Computer Organization and Architecture

CSI 333 Lecture 1 Number Systems

Numbering Systems. InThisAppendix...

Unsigned Conversions from Decimal or to Decimal and other Number Systems

Goals. Unary Numbers. Decimal Numbers. 3,148 is s 100 s 10 s 1 s. Number Bases 1/12/2009. COMP370 Intro to Computer Architecture 1

Introduction to Java

Verilog - Representation of Number Literals

Introduction to Visual C++.NET Programming. Using.NET Environment

6.087 Lecture 2 January 12, 2010

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

EE 261 Introduction to Logic Circuits. Module #2 Number Systems

Today s topics. Digital Computers. More on binary. Binary Digits (Bits)

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions

CDA 3200 Digital Systems. Instructor: Dr. Janusz Zalewski Developed by: Dr. Dahai Guo Spring 2012

Everything you wanted to know about using Hexadecimal and Octal Numbers in Visual Basic 6

Java Basics: Data Types, Variables, and Loops

J a v a Quiz (Unit 3, Test 0 Practice)

Programming in C an introduction

Chapter 2 Elementary Programming

Solution for Homework 2

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

VB.NET Programming Fundamentals

The C Programming Language course syllabus associate level

The Answer to the 14 Most Frequently Asked Modbus Questions

Decimal to Binary Conversion

Example of a Java program

Objective-C Tutorial

C++ Programming Language

Chapter Binary, Octal, Decimal, and Hexadecimal Calculations

5 Arrays and Pointers

How To Write Portable Programs In C

Chapter 1: Digital Systems and Binary Numbers

Data Storage: Each time you create a variable in memory, a certain amount of memory is allocated for that variable based on its data type (or class).

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Chapter 3. Input and output. 3.1 The System class

JavaScript: Introduction to Scripting Pearson Education, Inc. All rights reserved.

This section describes how LabVIEW stores data in memory for controls, indicators, wires, and other objects.

MS ACCESS DATABASE DATA TYPES

Outline. Conditional Statements. Logical Data in C. Logical Expressions. Relational Examples. Relational Operators

Lecture 11: Number Systems

Positional Numbering System

IC4 Programmer s Manual

Outline Basic concepts of Python language

The New IoT Standard: Any App for Any Device Using Any Data Format. Mike Weiner Product Manager, Omega DevCloud KORE Telematics

C Programming Tutorial

Object Oriented Software Design

NUMBER SYSTEMS. 1.1 Introduction

CS1020 Data Structures and Algorithms I Lecture Note #1. Introduction to Java

Memory is implemented as an array of electronic switches

Selection Statements

An Introduction to the C Programming Language and Software Design. Tim Bailey

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

Transcription:

MPATE-GE 2618: C Programming for Music Technology Unit 1.2

Binary = base 2 Decimal = base 10 Octal = base 8 Hexadecimal = base 16 Examples Base 10 357 = (10 2 * 3) + (10 1 * 5) + (10 0 * 7) Binary notation Base 2 1011 = (2 3 * 1) + (2 2 * 0) + (2 1 * 1) + (2 0 * 1) Base 16 A = 10; B = 11; C = 12; D = 13; E = 14; F = 15 0xFA13 = (16 3 * 15) + (16 2 * 10) + (16 1 * 1) + (16 0 + 3) Binary Octal Decimal Hex 0000 0001 01 01 01 0000 0010 02 02 02 0000 0011 03 03 03 0000 0100 04 04 04 0000 0101 05 05 05 0000 0110 06 06 06 0000 0111 07 07 07 0000 1000 10 08 08 0000 1001 11 09 09 0000 1010 12 10 0A 0000 1011 13 11 0B 0000 1100 14 12 0C 0000 1101 15 13 0D 0000 1110 16 14 0E 0000 1111 17 15 0F 0001 0000 20 16 10 0001 0001 21 17 11

Data representation: Bits and bytes Number of Bits Value 1 2 2 4 3 8 4 16 5 32 6 64 7 128 8 (one byte, char) 256 9 512 10 1024 16 (short int) Approx. 65K 24 Approx. 16.7M 32 (int) Approx. 4B

ASCII encoding ASCII = American Standard Code for Information Exchange An industry standard which assigns an 8-bit number to letters, numbers and characters. Original standard without special symbols consisted of 7 bits (128 possibilities). e.g. the letter A = 65, B = 66, etc. Convert the letters A (decimal 65) and m (decimal 109) to binary and hexadecimal: A = decimal 65 = binary 0100 0001 = hex 0x41 m = decimal 109 = binary 0110 1101 = hex 0x6D

Note on C99 C99 is the latest standard for the C programming language ere is a newer one that was published very recently (in 2011), C11. Older C standards Initial development of C took place in the late 60s/early 70s K&R C (1978) ANSI C aka Standard C or C89 (1989) ISO C aka C90 (1990) mostly the same as ANSI C C99 (2000) Some compilers support many C99 features, others don t (Microsoft) gcc has most C99 features implemented

Another type of comment Original C comments /* This is a comment */ C++ comments are part of C99 // A comment that only affects a single line

printf! printf is function that prints a formatted string (a series of text characters) and zero or more variables to stdout (the terminal). Include the header file stdio.h to use printf in your program.! Calling printf:!!printf(formatstring, variables...); e variables are printed in the order they are passed to printf and interpreted as the type specified in the string. e character % always precedes a type specifier (also known as a conversion specifier). (If you want to print an actual percent sign, use %% )

Some basic syntax: statements, variables, types Statements are followed by a semicolon (;) A variable declaration is one type of statement syntax: <type> <variablename>; int number; number = 5; // assignment You can initialize them on declaration too int number = 5; Some rules on naming variables: Must begin with a letter or underscore (_) not a number $ is not a valid character Can t have spaces Can t use reserved words like int Basic types in C int, float, double, char, bool (_Bool)

Types: int When printing an int in decimal format use either %d or %i If you assign a value starting with a leading zero, it will be interpreted as an octal value int n = 0177; // equivalent to decimal value 12 printf( An octal number: %#o, n); // format string If you assign a value preceded by 0x, it will be interpreted as a hexadecimal value int n = 0x6D; // equivalent to decimal value 109 printf( A hex number: %#x, n); // format string Type specifiers: long (%li), long long (%lli), short (%hi), unsigned (%u) NOTE: long long is only available in C99

Types: float and double floats are used to store values with decimal places doubles are larger than floats and more precise printf format specifiers for doubles and floats: %f, %e, %g %.5f, for example, displays 5 decimal places (Never test floats and doubles for equality)

Types: char and _Bool Char is a single character and is denoted by single quotes (double quotes are used for strings); use %c to print char ch = c ; char ch2 = \n ; // newline character _Bools have only two states, 0 or 1 e type bool as well as true and false are defined in the library <stdbool.h> bool condition = false; Print them as you would integers (using %i or %d) NOTE: bool is only available in C99

Reading in input To read from standard input, use scanf (for now) e following reads into variable n an integer entered by the user: int n; scanf( %i, &n); is reads in two floats into f1 and f2: float f1, f2; scanf( %f %f, &f1, &f2);

Basic types: review Four basic types: int float/double char bool (make sure to add #include <stdbool.h>)

More types: long, short, unsigned NOTE: size of integers depends on the platform; C leaves features of integer types implementation-defined, meaning that it is up to the particular compiler to determine the exact specifications. You can add you can put long, short, and unsigned before the int type specifier or leave out the int (it s assumed by default): short int i; short i; /* equivalent to the line above*/ long int i; long i; /* equivalent to the line above*/ long long int i; long long i; unsigned short int i; unsigned short i; /* equivalent to the line above*/

What s wrong with this? #include <stdio.h>! int main (void)! {! short unsigned i = -2;! printf("%d\n", i);! }! return 0;!

Operators Arithmetic operators: + for addition, - for subtraction, * for multiplication, / for division Precedence: some operators have higher priority a * b + c * d is the same as (a * b) + (c * d) When in doubt, use parentheses White space not necessary but recommended for readability Associativity: expressions containing operators of the same precedence are evaluated either left to right or vice versa depending on the operator. For example, for the assignment operator (= ): int x = 0, y = 5, z = 3; x = y = z; /* x has the value 3, not 5 */!

Operators continued e unary minus ( ) operator negates a value it precedes and has higher precedence than all the arithmetic operators (example: c = -a * b; ) e modulus operator % gives the remainder of the first value divided by the second value int a, b, c; a = 10; b = 4; c = a % b; // results in c being 2

Integer and floating point conversions and casting floats are truncated when assigned to ints (beware of this) float f1 = 123.125, f2; int i1, i2 = -150; i1 = f1; // i1 set to 123 f1 = i2; // f1 set to 150.000000 f1 = i2 / 100; // f1 set to 1.000000 f2 = i2 / 100.0; // f2 set to 1.500000 f2 = (float)i2 / 100; // f2 set to 1.500000 e last line above is an example of casting an integer to a float

Relational operators Relational operators: == equal to!= not equal to < less than <= less than or equal to > greater than >= greater than or equal to

Boolean expressions Expressions can be true or false int a = 4, b = 10; a == b is false a!= b is true a > b is false b > a is true b - a > a * 2 is false