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



Similar documents
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

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

Informatica e Sistemi in Tempo Reale

Object Oriented Software Design

Keil C51 Cross Compiler

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

Introduction to Java

Programing the Microprocessor in C Microprocessor System Design and Interfacing ECE 362

Comp151. Definitions & Declarations

Tutorial on C Language Programming

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

The programming language C. sws1 1

Why using ATmega16? University of Wollongong Australia. 7.1 Overview of ATmega16. Overview of ATmega16

The C Programming Language course syllabus associate level

Example of a Java program

AP Computer Science Java Subset

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

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

DNA Data and Program Representation. Alexandre David

Operator Overloading. Lecture 8. Operator Overloading. Running Example: Complex Numbers. Syntax. What can be overloaded. Syntax -- First Example

Lecture 2 Notes: Flow of Control

Chapter 5. Selection 5-1

ELEG3924 Microprocessor Ch.7 Programming In C

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

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

Object Oriented Software Design

C++FA 5.1 PRACTICE MID-TERM EXAM

An Incomplete C++ Primer. University of Wyoming MA 5310

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

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

C++ Programming Language

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

9 Control Statements. 9.1 Introduction. 9.2 Objectives. 9.3 Statements

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Pemrograman Dasar. Basic Elements Of Java

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

C++ Language Tutorial

VB.NET Programming Fundamentals

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

Network Working Group. October 1990

Lecture 22: C Programming 4 Embedded Systems

Java (12 Weeks) Introduction to Java Programming Language

Statements and Control Flow

Fundamentals of Programming

13 Classes & Objects with Constructors/Destructors

C++ program structure Variable Declarations

Passing 1D arrays to functions.

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

Moving from CS 61A Scheme to CS 61B Java

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

5 Arrays and Pointers

Chapter One Introduction to Programming

COS 217: Introduction to Programming Systems

CpSc212 Goddard Notes Chapter 6. Yet More on Classes. We discuss the problems of comparing, copying, passing, outputting, and destructing

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint)

Java Interview Questions and Answers

C Programming. for Embedded Microcontrollers. Warwick A. Smith. Postbus 11. Elektor International Media BV. 6114ZG Susteren The Netherlands

Chapter 7D The Java Virtual Machine

1 Abstract Data Types Information Hiding

1.00/ Session 2 Fall Basic Java Data Types, Control Structures. Java Data Types. 8 primitive or built-in data types

Chapter 2 Introduction to Java programming

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

Lecture 3. Arrays. Name of array. c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] Position number of the element within array c

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

Crash Course in Java

An Introduction to Assembly Programming with the ARM 32-bit Processor Family

Binary storage of graphs and related data

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Basic Programming and PC Skills: Basic Programming and PC Skills:

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

Conditionals (with solutions)

Chapter 8 Selection 8-1

Member Functions of the istream Class

Chapter 3 Operators and Control Flow

About The Tutorial. Audience. Prerequisites. Copyright & Disclaimer

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

Objective-C Tutorial

3/13/2012. Writing Simple C Programs. ESc101: Decision making using if-else and switch statements. Writing Simple C Programs

PIC 10A. Lecture 7: Graphics II and intro to the if statement

COMPUTER SCIENCE 1999 (Delhi Board)

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

Calling the Function. Two Function Declarations Here is a function declared as pass by value. Why use Pass By Reference?

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

Binary Division. Decimal Division. Hardware for Binary Division. Simple 16-bit Divider Circuit

Conditions & Boolean Expressions

Common Beginner C++ Programming Mistakes

Introduction to Computer Programming, Spring Term 2014 Practice Assignment 3 Discussion

Embedded C Programming

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

6. Control Structures

CS 106 Introduction to Computer Science I

Visual Basic Programming. An Introduction

Debugging. Common Semantic Errors ESE112. Java Library. It is highly unlikely that you will write code that will work on the first go

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

How To Write Portable Programs In C

Part I. Multiple Choice Questions (2 points each):

Transcription:

Phys4051: C Lecture 2 & 3 Functions (Review) Comment Statements Variables & Operators Branching Instructions Comment Statements! Method 1: /* */! Method 2: // /* Single Line */ //Single Line /* This comment spans more than one line. */ // This comment // spans more than // one line. int x; //variable x 2 C Data Types! No Return Value: (For functions & pointers only!)! Integers:! Real (floating point):! void! char! short! int! long! float! double 3 1

Data Types: Signed / Unsigned! All integer data type declarations can be signed (default) or unsigned.! Example:! unsigned int ival = 134;! signed int sval = -107; 4 Global / Local Variables! All variables declared outside of a function are GLOBAL.! Global variables have global scope, i.e., they are seen in ALL the functions.! All variables declared within a function are LOCAL to that function.! Local variables have local scope. They "exist" only within the function that declares them. 5 Variable Declarations! Position in Program: Global vs. Local Always declare variables before any statements or before main! 6 2

Variable Initialization! Global Variables:! Always initialized to 0! Local Variables:! Never initialized! Random 7 Variable Declaration & Initialization! Example 1a:! Example 1b:! short x; short y; short z;! short x = 3, y =3, z; x = 3; y = 4; 8 C-Operators -Mathematical -Relational - Assignment -Logical -Bitwise 3

C-Operators! Are always executed:! 1: Left to right! 2: By order of precedent 10 Mathematical C-Operators! By order of precedence:! * Multiplication! / Division! % Remainder: ex 13%4 = 1! + Addition! - Subtraction 11 Mathematical C-Operators:! Accuracy:! When applying an operator to variables having different levels of accuracy, then the calculation is carried out using the level of accuracy as specified by the variable with the higher level of accuracy.! Example: 12 4

Mathematical C-Operators: Accuracy Example short k, m = 2, n = 3; float a, b = 2.0, c = 3.0; k = m / n; k = b / n; a = m / n; a = 1.0 * m / n; a = (float) m / n; //type casting a = b / c; k =? k =? a =? a =? a =? a =? 13 Mathematical C-Operators: Shortcut Notations Equivalent Notations: x = x + 1; x = x - 1; x++; x--; ++x; --x; a = a + 3.14; a = a - 3.14; a = a * 3.14; a = a / 3.14; a + = 3.14; a - = 3.14; a * = 3.14; a / = 3.14; 14 C-Operators: Shortcut Notations Example! Example 1:! Example 2 int y, x = 4051; y = ++x; printf("%d", y); int y, x = 4051; y = x++; printf("%d", y);! Output:! Output: 15 5

Branching Instructions: Simple if-statement! Example1:! if ( logic condition ) statement(s);! if ( x > 1 ) x = 0; printf( Hello ); 16 Logic Condition! TRUE: Anything, except 0 or FALSE! FALSE: Only 0 or FALSE! Example: if( 3 ) printf( always true );! Example: if( 0 ) printf( never true ); 17 if-else Statement! if( logic condition ) //executed only if logic statement(s); //condition is TRUE else //executed only if logic statement(s); //condition is FALSE 18 6

if-statement Shortcut Notation (1)! If the if-statement contains only ONE statement, braces can be omitted: if( x ) printf( TRUE ); else printf( FALSE ) Equivalent program segment: if( x ) printf( TRUE); else printf( FALSE); 19 if-statement Shortcut Notation (1) WARNING!!! if( x ) printf( TRUE ); else printf( FALSE ); printf( AGAIN ); printf( DONE ) if( x ) printf( TRUE ); else printf( FALSE ); printf( AGAIN ); printf( DONE ); 20 Relational Operators (1) < <= > >= Less Than Less Than and Equal Greater Than Greater Than and Equal ==!= Equal Not Equal 21 7

Relational Operators (2) WARNING: Equal Operator! Example 1:! Example 2: short x = 5; if( x = 3 ) printf( x is 3 ); Output: short x = 5; if( x == 3) printf( x is 3 ); Output: 22 Relational Operators (3) The Not (!) Operator What is the difference between the following two program segments? if(!x ) if( x == 0 ) 23 Logic Operators: AND & OR (1) The following operators act on the entire variable or expression as a whole and return either TRUE (1) or FALSE (0) && AND OR (shift \ key) 24 8

Logic Operators: (Bytewise) AND & OR (2)! The AND and OR operators are often used to link relational operators.! Example: How do you write 3 < x < 10 in C? 25 Logic Operators: Bitwise Operators (1) << >> ~ & ^ Left Shift Right Shift Bitwise NOT Bitwise AND Bitwise XOR Bitwise OR! Bitwise operators operate on a variable or expression bit by bit. 26 Bytewise and Bitwise Operators (2)! Example 1: Bytewise short a = 5, b = 10, y; y = a && b;! Example 2: Bitwise short a = 5, b = 10, y; y = a & b; y =??? y =??? 27 9

Logic Operators: Bitwise Operators (3)! Bitwise operators are often used in hardware control applications! Example 1: Check one specific bit in a byte (BIT MASKING)! Example 2: Toggle a specific bit in a byte 28 10

Typical C-Function Syntax Function Declaration double Thdeg( float x, float y) ; Function Header double Thdeg( float x, float y) Local float fthetadeg, fthetarad ; Variable float fpi = 3.1415 ; Declarations double ftan ; Statements ftan = y / x ; and fthetarad = atan ( ftan ) ; Function Calls fthetadeg = ftheta * 180.0/fPi ; return ( fthetadeg );

Data Type Range Bytes Signed Declaration Range Unsigned Declaration 1 char -128 to 127 unsigned char 2 short -32768 to (or: int) 32767 4 long -2147483648 to 2147483647 4 float ±3.4 E±38 8 double ±1.7 E±308 unsigned short (unsigned int) Range 0 to 255 0 to 65635 0 to 4294967295

Program Global & Local Variables #include <ansi_c.h> void F1( short x); void F2( short x); short s1; main() short sm1; short sm2 = 123; //Non-intialized values printf("part 1: Non-Initialized Variables\n"); printf("s1: %d\n", s1); printf("sm1: %d\n\n", sm1); printf("part 2: Passing a Variable to a Function\n"); printf("sm2: %d\n", sm2); F1( sm2 ); //passing a variable by value printf("sm2: %d\n\n", sm2); printf("global vs. Local Variables\n"); F2( sm2 ); //global vs. local variables printf("sm2: %d\n", sm2); printf("s1: %d\n\n", s1); void F1( short x) printf("before: x: %d\t", x); x = 888; printf("after: x: %d\n", x); void F2( short x) short sm2 = -111; s1 = -1234;

Output: Part 1: Non-Initialized Variables s1: sm1: Part 2: Passing a Variable to a Function sm2: before: x: after: x: sm2: Global vs. Local Variables sm2: s1:

1) ANSI-C / No GUI //Program MyMaxNoGui #include <ansi_c.h> float MyMax(float x, float y); main() float u = 123.0, v = -543.2; float res; res = MyMax( u, v); printf("maximum is: %f\n", res); float MyMax(float x, float y) float max; if( x > y) max = x; else max = y; return max;

2) LabWindows / With GUI 2a) LabWindows Generated Code: #include <cvirte.h> #include <userint.h> #include "MyMaxGui.h" static int panelhandle; int main (int argc, char *argv[]) if (InitCVIRTE (0, argv, 0) == 0) return -1; /* out of memory */ if ((panelhandle = LoadPanel (0, "MyMaxGui.uir", PANEL)) < 0) return -1; DisplayPanel (panelhandle); RunUserInterface (); return 0; int CVICALLBACK Quit (int panel, int control, int event, void *callbackdata, int eventdata1, int eventdata2) switch (event) case EVENT_COMMIT: QuitUserInterface (0); break; return 0; int CVICALLBACK CalcMax (int panel, int control, int event, void *callbackdata, int eventdata1, int eventdata2) switch (event) case EVENT_COMMIT: return 0; break;

2b) Code for MyMax function added (in Bold): #include <ansi_c.h> #include <cvirte.h> #include <userint.h> #include "MyMaxGui.h" static int panelhandle; float MyMax(float x, float y); int main (int argc, char *argv[]) if (InitCVIRTE (0, argv, 0) == 0) return -1; /* out of memory */ if ((panelhandle = LoadPanel (0, "MyMaxGui.uir", PANEL)) < 0) return -1; DisplayPanel (panelhandle); RunUserInterface (); return 0; int CVICALLBACK Quit (int panel, int control, int event, void *callbackdata, int eventdata1, int eventdata2) switch (event) case EVENT_COMMIT: QuitUserInterface (0); break; return 0; int CVICALLBACK CalcMax (int panel, int control, int event, void *callbackdata, int eventdata1, int eventdata2) float u = 123.0, v = -543.2; float res; switch (event) case EVENT_COMMIT: res = MyMax( u, v); printf("maximum is: %f\n", res); return 0; break;

float MyMax(float x, float y) float max; if( x > y) max = x; else max = y; return max; 2c) Code Added to Read Input and to Display the Result: (Changes from previous version are shown in bold) #include <ansi_c.h> #include <cvirte.h> /* Needed if linking in external compiler; harmless otherwise */ #include <userint.h> #include "MyMaxGui.h" static int panelhandle; float MyMax(float x, float y); int main (int argc, char *argv[]) if (InitCVIRTE (0, argv, 0) == 0) /* Needed if linking in external compiler; harmless otherwise */ return -1; /* out of memory */ if ((panelhandle = LoadPanel (0, "MyMaxGui.uir", PANEL)) < 0) return -1; DisplayPanel (panelhandle); RunUserInterface ();

return 0; int CVICALLBACK Quit (int panel, int control, int event, void *callbackdata, int eventdata1, int eventdata2) switch (event) case EVENT_COMMIT: QuitUserInterface (0); break; return 0; int CVICALLBACK CalcMax (int panel, int control, int event, void *callbackdata, int eventdata1, int eventdata2) float u, v; float res; switch (event) case EVENT_COMMIT: GetCtrlVal (panelhandle, PANEL_XINPUT, &u); GetCtrlVal (panelhandle, PANEL_XINPUT, &v); res = MyMax( u, v); return 0; SetCtrlVal (panelhandle, PANEL_RESULT, res); break; float MyMax(float x, float y) float max; if( x > y) max = x; else max = y; return max;