ME 461 C review Session Fall 2009 S. Keres

Similar documents
UNIT- 3 Introduction to C++

A flow chart is a graphical or symbolic representation of a process.

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

Introduction to C Language

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Chapter 3: Operators, Expressions and Type Conversion

Information Science 1

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Unit 3. Operators. School of Science and Technology INTRODUCTION

Expressions and Precedence. Last updated 12/10/18

JAVA OPERATORS GENERAL

Operators. Java operators are classified into three categories:

Applied Computer Programming

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

C PROGRAMMING LANGUAGE. POINTERS, ARRAYS, OPERATORS AND LOOP. CAAM 519, CHAPTER5

Reserved Words and Identifiers

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

Course Outline Introduction to C-Programming

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

A First Program - Greeting.cpp

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

Review of the C Programming Language for Principles of Operating Systems

Part I Part 1 Expressions

The C Programming Language Guide for the Robot Course work Module

Writing Program in C Expressions and Control Structures (Selection Statements and Loops)

Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University

Overview of C. Basic Data Types Constants Variables Identifiers Keywords Basic I/O

Arithmetic and Bitwise Operations on Binary Data

Data Types and Variables in C language

Computers Programming Course 6. Iulian Năstac

C: How to Program. Week /Mar/05

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Informatics Ingeniería en Electrónica y Automática Industrial

EDIABAS BEST/2 LANGUAGE DESCRIPTION. VERSION 6b. Electronic Diagnostic Basic System EDIABAS - BEST/2 LANGUAGE DESCRIPTION

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

CS201 Some Important Definitions

Programming in C++ 5. Integral data types

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

Programming in C++ 6. Floating point data types

Unit-2 (Operators) ANAND KR.SRIVASTAVA

Variables and literals

DEPARTMENT OF MATHS, MJ COLLEGE

SECTION II: LANGUAGE BASICS

Programming. Elementary Concepts

ISA 563 : Fundamentals of Systems Programming

Preview from Notesale.co.uk Page 6 of 52

Room 3P16 Telephone: extension ~irjohnson/uqc146s1.html

Arithmetic Operators. Portability: Printing Numbers

Engineering Computing I

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

2/5/2018. Expressions are Used to Perform Calculations. ECE 220: Computer Systems & Programming. Our Class Focuses on Four Types of Operator in C

UNIT 3 OPERATORS. [Marks- 12]

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

Declaration and Memory

Fundamental of Programming (C)

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

Programming for Engineers Iteration

Basics of Programming

Lecture 3. More About C

Review of the C Programming Language

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

Chapter 4. Operations on Data

Operators in C. Staff Incharge: S.Sasirekha

Programming I Lecture 7

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

XC Specification. 1 Lexical Conventions. 1.1 Tokens. The specification given in this document describes version 1.0 of XC.

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Information Science 1

Lexical Considerations

Chapter 2 - Introduction to C Programming

Operators & Expressions

1 Lexical Considerations

Arithmetic and Bitwise Operations on Binary Data

I Internal Examination Sept Class: - BCA I Subject: - Principles of Programming Lang. (BCA 104) MM: 40 Set: A Time: 1 ½ Hrs.

Computer System and programming in C

Appendix. Grammar. A.1 Introduction. A.2 Keywords. There is no worse danger for a teacher than to teach words instead of things.

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

Number Systems. Binary Numbers. Appendix. Decimal notation represents numbers as powers of 10, for example

Department of Computer Science

Operations On Data CHAPTER 4. (Solutions to Odd-Numbered Problems) Review Questions

Fundamentals of Programming

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

1/31/2017. Expressions are Used to Perform Calculations. ECE 120: Introduction to Computing. Five Arithmetic Operators on Numeric Types

Operators. Lecture 3 COP 3014 Spring January 16, 2018

CHAPTER 3 Expressions, Functions, Output

9/2/2016. Expressions are Used to Perform Calculations. ECE 120: Introduction to Computing. Five Arithmetic Operators on Numeric Types

CENG 447/547 Embedded and Real-Time Systems. Review of C coding and Soft Eng Concepts

Bits, Words, and Integers

These are reserved words of the C language. For example int, float, if, else, for, while etc.

Computers Programming Course 5. Iulian Năstac

Transcription:

ME 461 C review Session Fall 2009 S. Keres DISCLAIMER: These notes are in no way intended to be a complete reference for the C programming material you will need for the class. They are intended to help focus your review efforts toward the concepts that will be the most useful. Digital Representations You are already familiar with decimal (base 10) representation. Binary (base 2) is similar to decimal, except that the digits represent ones, twos, fours, eights, 2 4 s, 2 5 s, etc, instead of ones, tens, hundreds, 10 3 s, 10 4 s, etc. Hexadecimal (base 16) is the same, except the digits represent ones, sixteens, 256s, 16 3 s, 16 4 s, etc. Numbers 10 16 are represented by the letters A F. Dec Bin Hex Dec Bin Hex 1 10 1 2 1 16 9 10 1001 2 9 16 2 10 10 2 2 16 10 10 1010 2 A 16 3 10 11 2 3 16 11 10 1011 2 B 16 4 10 100 2 4 16 12 10 1100 2 C 16 5 10 101 2 5 16 13 10 1101 2 D 16 6 10 110 2 6 16 14 10 1110 2 E 16 7 10 111 2 7 16 15 10 1111 2 F 16 8 10 1000 2 8 16 16 10 10000 2 10 16 Hexadecimal is very convenient when working directly with memory and registers (as we will be) because each nibble (group of 4 binary digits) can be represented with a single hexadecimal digit. In C, we cannot write directly in binary, but we can write in hex. Hexadecimal numbers are prefixed by 0x in C. a = 0x52 is the same as a = 82, which is the same as binary 101 0010. It is important to note that C does not distinguish between representations (everything is binary to a computer). The ability to write numbers using different representations is a facility provided for our convenience. Operators Arithmetic: + Addition ++ increment - Subraction -- decrement

* Multiplication += assignment by addition / Division -= assignment by subtraction % Modulus (remainder) *= assignment by multiplication %= assignment by modulus /= assignment by division The increment and decrement operators do just that; the timing depends on whether they are postfixed or prefixed to the operand. a = b++; assigns b s value to a, then increments b. a = ++b; increments b, then assigns the value to a. Any of the arithmetic operators besides increment and decrement may be combined with the assignment operator to act as shorthand as in the following example. Assignment by addition: a += b is the same as a = a + b. Likewise, a *= b is the same as a = a * b, and so on. Logical: a && b a b AND true if neither operand is zero OR true if either operand is nonzero! a NOT true if operand is zero a == b a!= b a > b a < b a >= b a <= b Bitwise: a b a & b a ^ b equality true if operands are the same number inequality true if operands are not the same number greater than true if a is larger than b less than true if a is less than b greater than or equal to less than or equal to bitwise OR bitwise AND bitwise XOR exclusive OR ~ a bitwise complement flips bits from 1 to 0 and 0 to 1 a << b a >> b left shift shifts the bits of a left and inserts zeros to the right right shift shifts the bits of a right and inserts leading zeros All of the bitwise operators except the complement can be combined with the assignment operator to act as shorthand as in the following example. a = b is the same as a = a b. Likewise a &= b is the same as a = a & b, and so on. Examples: 0x15 0x98 0x15 0001 0101 0x98 1001 1000 1001 1101 = 0x9D

~ 0xA6 0xA6 1010 0110 complement 0101 1001 = 0x59 5 ^ 3 5 0101 ^ 3 0011 0110 = 6 9 << 2 9 1001 left shift 10 0100 = 0x24 = 36 Application: setting/clearing bits in registers Set the 5 th and 6 th bits in an 8 bit register called P1OUT without changing the others or knowing their previous state P1OUT = 0x30 Before P1OUT = xxxx xxxx 0x30 0011 0000 After P1OUT = xx11 xxxx Clear the 1 st and 7 th bits in P1OUT P1OUT &= ~0x41 Before P1OUT = xxxx xxxx & ~0x41 1011 1110 After P1OUT = x0xx xxx0 Can you think of an operation we can use to flip (1 0 and 0 1) bits of our choosing without affecting the others or knowing their values? Other: a = b a[b] & a assignment array subscripting (indexing) accesses the b th element of array a reference returns the memory address of a, i.e. constructs a pointer to a * a dereference accesses the value stored at location a in memory, the thing that a points to The ability to work with pointers is essential to the creation of a sophisticated program. Our most common use of pointers will involve passing variables by reference to functions. Basic Data Types Integer types: char usually 8 bits long, may be used to store a single character int usually 16 bits long, but more generally has more precision than a char Basic type modifiers: long, short, signed, unsigned

Floating point types: float IEEE 32 bit floating point number double double precision floating point number (64 bits) Other: void valueless, carries no data * when postfixed to a type specifier constructs a pointer to a variable of that type Variable declarations Variables are declared like type var_name = value; where type is a valid type preceded by one or more modifiers, var_name is a name for the variable, and value is an optional initial value for the variable. Arrays are declared like unsigned int counter1 = 0; float error = 0.0; char letter = a ; type my_arr[dim1][dim2] [dimn]; and may be initialized like int my_2by2[2][2] = {{1, 2,{5, 9; Variables declared outside of functions are global and carry their values throughout the program. Variables declared inside functions are local and carry their values only through a call to the function. Type Conversion Explicit A variable s type may be changed (cast) to another temporarily during an operation. A type cast is performed by prefixing the new type in parentheses to the quantity of interest. int wholenumber = 0; float z = 5.0; wholenumber = (int)(z/2.0) + 5; Implicit Variables are promoted according to C conversion rules during operations between variables of different types In the following example, the integer is promoted to a float due to the floating point divisor and there is no loss of precision; the result is z = 2.5. int wholenumber = 5; float z = 0; z = wholenumber/2.0; If instead of 2.0 we had just used 2, the result would have been z = 2.0 due to an integer division. Take some time to familiarize yourself with C s conversion rules using your C reference. Also, if you re not already proficient, review discrete math fundamentals.

Functions Functions are created like type function_name(argument list) { variable list function body return statement float my_avg(int a, int b, int c) { function header int sum = 0; sum = a + b + c; return sum/3.0; local variables function body return statement This function takes three integers as inputs and returns their average. A call to the function would look like float z = 0.0; z = my_avg(3, 4, 11); The function call itself can be treated as any variable of the same type as the output of the function. It can be added, subtracted, multiplied, divided, type cast, etc. Note that we can create void functions that have no output. In this case the return statement is optional. In the same way, we can create functions that have no input arguments. We can also create functions that have neither inputs nor outputs. Init_PWM(); is a complete line of code assuming the function has neither inputs nor outputs. Functions must be prototyped by stating the header information in the global variable area before they are used if their definition occurs later in the file or in a different c file. Program Control Statements If Else An if else statement allows the programmer to execute code depending on the result of a conditional statement. if(condition) { elseif(condition) {

else { char up = 1; float a = 0.0; if(up) { a += 0.1; if(a >= 10) up = 0; else { a -= 0.1; if(a <= -10) up = 1; The elseif statement allows the programmer to allocate code blocks to several mutually exclusive alternatives. While The while statement executes a block of code repeatedly as long as the conditional is true. while(condition) { Do while Same as the while, except the code block executes once regardless of the conditional. do { while(condition); For Executes a block of code repeatedly as long as the conditional (usually) based on the loop variable is true. for(initialization; conditional; operation) { At the beginning of the loop, the initialization is performed. After each execution of the statement block, the operation is performed. When the conditional is false, the program continues execution after the for statement (after the closing bracket). int i, j = 0, my_arr[10];

for(i = 0; i < 10; i++) { my_arr[i] = j; j += 5; Can you write out the contents of the array my_arr? Switch Case Executes a based on the value of a variable. switch(control variable) { case constant1: break; case constant2: break; default: break; The code block associated with the case matching the control variable s value is executed once. If no match is found among the cases, the code associated with the default statement is executed. If the break statements are omitted, code execution will continue through every case once a match is found. This is rarely desirable. As with all of the control statements, nesting switch statements is allowed and often very useful. Preprocessor Directives #include used to tell the compiler what libraries to look in for function declarations, variables, macros, etc. #include <msp430x22x2.h> Use <> around the file name when it is in the search path (usually defined in a properties section) and double quotes around the file name when it is in the project directory #define used as a dumb text find & replace macro #define PI 3.141592654 This causes all instances of the text PI in the c file to be replaced by the constant 3.141592654.

#define is very useful for parameterizing your programs. Use the #define macro whenever you will use a special constant throughout your program for clarity and ease of modification. #define can also be made to accept input arguments. #define AVG(A,B) (A+B/2.0) This is useful for creating very simple in line functions. Remember, #define is a simple text replace. Careful use of parentheses is required! #pragma directive used to specify compiler specific instructions. #pragma vector=timera0_vector The vector pragma is used before a function definition to tell the compiler that the function should be placed in the interrupt vector address. #if, #ifdef, #ifndef, #elif, #else, #endif conditional compilation. These will see limited, if any, use in our class. C References In lab: Schildt, Teach Yourself C (1997). Kernighan, Ritchie, The C Programming Language (1988). Harbison, Steele, C: A Reference Manual (2002).