Outline. Welcome to C Syntax Variables Operations Function calls Control statement More data type Timer examples

Similar documents
C Programming C.1 INTRODUCTION

Introduction to C Programming

Lecture 3. More About C

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

The C language. Introductory course #1

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

The Arithmetic Operators

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

!"#$% &'($) *+!$ 0!'" 0+'&"$.&0-2$ 10.+3&2),&/3+, %&&/3+, C,-"!.&/+"*0.&('1 :2 %*10% *%7)/ 30'&. 0% /4%./

C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock)

C/Java Syntax. Lecture 02 Summary. Keywords Variable Declarations Data Types Operators Statements. Functions. if, switch, while, do-while, for

Fundamental of Programming (C)

OBJECT ORIENTED PROGRAMMING

Variables and literals

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

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

Lecture 02 Summary. C/Java Syntax 1/14/2009. Keywords Variable Declarations Data Types Operators Statements. Functions

CSE 351: The Hardware/Software Interface. Section 2 Integer representations, two s complement, and bitwise operators

Programming. Elementary Concepts

Introduction to C Language

C - Basics, Bitwise Operator. Zhaoguo Wang

JAVA OPERATORS GENERAL

Operators in C. Staff Incharge: S.Sasirekha

CS 241 Data Organization Binary

Chapter 12 Variables and Operators

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

CSC 1107: Structured Programming

Course Outline Introduction to C-Programming

Computers Programming Course 6. Iulian Năstac

Fundamentals of Programming

Introduction to Computer Science Midterm 3 Fall, Points

Work relative to other classes

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

UNIVERSITY OF LIMERICK OLLSCOIL LUIMNIGH COLLEGE OF INFORMATICS & ELECTRONICS DEPARTMENT OF ELECTRONIC & COMPUTER ENGINEERING

JAC444 - Lecture 1. Introduction to Java Programming Language Segment 4. Jordan Anastasiade Java Programming Language Course

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

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

Flowchart, Types, and Values

CPSC 213. Introduction to Computer Systems. Numbers and Memory. Unit 1a

Lecture 5: Multidimensional Arrays. Wednesday, 11 February 2009

Unit 3. Operators. School of Science and Technology INTRODUCTION

Expressions and Precedence. Last updated 12/10/18

Fundamentals of Programming Session 20

C-Programming. CSC209: Software Tools and Systems Programming. Paul Vrbik. University of Toronto Mississauga

Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 2

Programming in C and Data Structures [15PCD13/23] 1. PROGRAMMING IN C AND DATA STRUCTURES [As per Choice Based Credit System (CBCS) scheme]

UNIT- 3 Introduction to C++

EL2310 Scientific Programming

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

The Design of C: A Rational Reconstruction: Part 2

Goals for this Week. CSC 2400: Computer Systems. Bits, Bytes and Data Types. Binary number system. Finite representations of binary integers

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C

Beginning C Programming for Engineers

Lecture 5: C programming

CS313D: ADVANCED PROGRAMMING LANGUAGE

SECTION II: LANGUAGE BASICS

The C Programming Language. (with material from Dr. Bin Ren, William & Mary Computer Science)

Presented By : Gaurav Juneja

ISA 563 : Fundamentals of Systems Programming

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

CT 229. Java Syntax 26/09/2006 CT229

The C Programming Language Guide for the Robot Course work Module

Subject: Fundamental of Computer Programming 2068

CSE 303 Lecture 8. Intro to C programming

Continued from previous lecture

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value.

Reserved Words and Identifiers

Unit-2 (Operators) ANAND KR.SRIVASTAVA

Operators and Expressions:

Java Basic Programming Constructs

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

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

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

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

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

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

Programming in C - Part 2

CS C Primer. Tyler Szepesi. January 16, 2013

Learning C Language. For BEGINNERS. Remember! Practice will make you perfect!!! :D. The 6 th week / May 24 th, Su-Jin Oh

C++ is case sensitive language, meaning that the variable first_value, First_Value or FIRST_VALUE will be treated as different.

Quick Reference Guide

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

C Programming Language (Chapter 2 of K&R) Variables and Constants

Programming Project 2

2. C99 standard guarantees uniqueness of characters for internal names. A. 12 B. 26 C. 31 D. 48

Before Class Install SDCC Instructions in Installing_SiLabs-SDCC- Drivers document. Solutions to Number Systems Worksheet. Announcements.

CS 241 Data Organization Pointers and Arrays

BCA-105 C Language What is C? History of C

C Language Summary. Chris J Michael 28 August CSC 4103 Operating Systems Fall 2008 Lecture 2 C Summary

ECE2049 E17 Lecture 2: Data Representations & C Programming Basics

ME 461 C review Session Fall 2009 S. Keres

Guide for The C Programming Language Chapter 1. Q1. Explain the structure of a C program Answer: Structure of the C program is shown below:

CS 61c: Great Ideas in Computer Architecture

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

Principles of C and Memory Management

INTRODUCTION TO C-PROGRAMING LANGUAGE

Mechatronics and Microcontrollers. Szilárd Aradi PhD Refresh of C

Structured programming. Exercises 3

CSC 1214: Object-Oriented Programming

Chapter 3: Operators, Expressions and Type Conversion

Transcription:

C"for"PIC" E155"

Outline Welcome to C Syntax Variables Operations Function calls Control statement More data type Timer examples 2

Welcome to C Invented in 1973 by Dennis Ritchie of Bell Labs Its popularity stems from a number of factors including: Availability for a tremendous variety of platforms, from supercomputers down to embedded microcontrollers Relative ease of use, with a huge user base Moderate level of abstraction providing higher productivity than assembly language, yet giving the programmer a good understanding of how the code will be executed The C Programming Langage 2 nd Edition, B. Bernighan, D. Ritchie, Prentice Hall Software Series. 3

Welcome to C Suitability for generating high performance programs Ability to interact directly with the hardware In summary, C allows the programmer to directly access addresses in memory, illustrating the connection between hardware and software C is a practical language that all engineers and computer scientists should know. Its wide usages make proficiency in C a vital and marketable skill. 4

Simple Program C Code Example C.1. Simple C program // Writes "Hello world!" to the console #include <stdio.h> int main(void){ printf("hello world!\n"); Console Output Hello world! 5

Comments/Define // this is an example of a one-line comment. /* this is an example of a multi-line comment */ #define TRISD 0xFE000044 6

Primitive data types and sizes Type Size (bits) Minimum Maximum char 8-2 -7 = -128 2 7-1 = 127 unsigned char 8 0 2 8-1 = 255 short 16-2 15 = -32,768 2 15 1 = 32,767 unsigned short 16 0 2 16 1 = 65,535 long 32-2 31 = -2,147,483,648 2 31 1 = 2,147,483,647 unsigned long 32 0 2 32 1 = 4,294,967,295 long long 64-2 63 2 63 1 unsigned long long 64 0 2 64 1 int unsigned int machinedependent machinedependent float 32 ±2-126 ±2 127 double 64 ±2-1023 ±2 1022 C Code Example: Data Types // Examples of several data types and their binary representations unsigned char x = 42; // x = 00101010 short y = -10; // y = 11111111 11110110 unsigned long z = 0; // z = 00000000 00000000 00000000 00000000 7

Global vs Local Variable C Code Example: Global Variables // Use a global variable to find and print the maximum of 3 numbers int max; // global variable holding the maximum value void findmax(int a, int b, int c) { max = a; if (b > max) { if (c > b) max = c; else max = b; else if (c > max) max = c; void printmax(void) { printf("the maximum number is: %d\n", max); int main(void) { findmax(4, 3, 7); printmax(); 8

Global vs Local Variable C Code Example C.5. Local Variables // Uses local variables to find and print the maximum of 3 numbers int getmax(int a, int b, int c) { int result = a; // local variable holding the maximum value if (b > result) { if (c > b) result = c; else result = b; else if (c > result) result = c; return result; void printmax(int m) { printf("the maximum number is: %d\n", m); int main(void) { int max; max = getmax(4, 3, 7); printmax(max); 9

Operators Highest Lowest Category Operator Description Example Monadic ++ post-increment a++; // a = a+1 -- post-decrement x--; // x = x-1 & memory address of a variable x = &y; // x = the memory // address of y ~ bitwise NOT z = ~a;! Boolean NOT!x - negation y = -a; ++ pre-increment ++a; // a = a+1 -- pre-decrement --x; // x = x-1 (type) casts a variable to (type) sizeof() size of a variable in bytes Multiplicative * multiplication y = x * 12; / division z = 9 / 3; // z = 3 % modulo z = 5 % 2; // z = 1 Additive + addition y = a + 2; - subtraction y = a - 2; Bitwise Shift << bitshift left z = 5 << 2; // z = 0b0001 0100 >> bitshift right x = 9 >> 3; // x = 0b0000 0001 x = (int)c; // cast c to an // int and assign it to x long int y; x = sizeof(y); // x = 4 10

Operators Highest Lowest Category Operator Description Example Relational == equals y == 2!= not equals x!= 7 < less than y < 12 > greater than val > max <= less than or equal z <= 2 >= greater than or equal y >= 10 Bitwise & bitwise AND y = a & 15; bitwise OR y = a b; ^ bitwise XOR y = 2 ^ 3; Logical && Boolean AND x && y Boolean OR x y Ternary? : ternary operator y = x? a : b;// if x is TRUE, // y=a, else y=b 10

Operators Highest Lowest Category Operator Description Example Assignment = assignment x = 22; += addition and y += 3; // y = y + 3 assignment -= subtraction and z -= 10; // z = z 10 assignment *= multiplication and x *= 4; // x = x * 4 assignment /= division and assignment y /= 10; // y = y / 10 %= modulo and assignment x %= 4; // x = x % 4 >>= bitwise right-shift and x >>= 5; // x = x>>5 assignment <<= bitwise left-shift and x <<= 2; // x = x<<2 assignment &= bitwise AND and y &= 15; // y = y & 15 assignment = bitwise OR and x = y; // x = x y assignment ^= bitwise XOR and x ^= y; // x = x ^ y assignment 10

C Code Example: Operator Examples Expression Result Notes 44 / 14 3 Integer division truncates 44 % 14 2 44 mod 14 0x2C && 0xE //0b101100 && 1 Logical AND 0b1110 0x2C 0xE //0b101100 0b1110 1 Logical OR 0x2C & 0xE //0b101100 & 0xC (0b001100) Bitwise AND 0b1110 0x2C 0xE //0b101100 0b1110 0x2E (0b101110) Bitwise OR 0x2C ^ 0xE //0b101100 ^ 0b1110 0x22 (0b100010) Bitwise XOR 0xE << 2 //0b1110 << 2 0x38 (0b111000) Left shift by 2 0x2C >> 3 //0b101100 >> 3 0x5 (0b101) Right shift by 3 x = 14; x += 2; x=16 y = 0x2C; // y = 0b101100 y &= 0xF; // y &= 0b1111 y=0xc (0b001100) x = 14; y = 44; y = y + x++; x=15, y=58 Increment x after using it x = 14; y = 44; y = y + ++x; x=15, y=59 Increment x before using it y = (a > b)? a : b; if (a > b) y = a; else y = b; 11

Function Calls C Code Example C.8. sum3()function // Returns the sum of the three input variables int sum3(int a, int b, int c) { int result = a + b + c; return result; After the following call to sum3(), y holds the value 42. int y = sum3(10, 15, 17); // printprompt() prints a user prompt to the console. void printprompt(void) { printf("please enter a number from 1-3:\n"); 14

Function Prototype A function must be declared in the code before it is called by another function. This may be done by placing the called function earlier in the file. For this reason, main() is often placed at the end of the C file after all the functions it calls. Alternatively, a function prototype can be placed in the program before the function is defined. int sum3(int a, int b, int c); void printprompt(void); 15

Function Prototype // function prototypes int sum3(int a, int b, int c); void printprompt(void); int main(void) { int y = sum3(10, 15, 20); printf("sum3 result: %d\n", y); printprompt(); int sum3(int a, int b, int c) { int result = a+b+c; return result; void printprompt(void) { printf("please enter a number from 1-3:\n"); 16

Conditional Statements if/if else int dontfix = 0; if (aintbroke == 1) dontfix = 1; if (a > b) y = a; else y = b; 17

switch/case // assign amt depending on the value of option switch (option) { Conditional Statements case 1: case 2: case 3: case 4: default: amt = 100; break; amt = 50; break; amt = 20; break; amt = 10; break; printf("error: unknown option\n"); // assign amt depending on the value of option if (option == 1) amt = 100; else if (option == 2) amt = 50; else if (option == 3) amt = 20; else if (option == 4) amt = 10; else printf("error: unknown option\n"); 18

Conditional Statements: Loops // Compute 9! (the factorial of 9) int i = 1, fact = 1; // multiply the numbers from 1 to 9 while (i < 10) { // while loops check the condition first fact *= i; //fact = fact * i; i++; 19

Conditional Statements: loops // Query user to guess a number and check it against the correct number. #define MAXGUESSES 3 #define CORRECTNUM 7 int guess, numguesses = 0; do { printf("guess a number between 0 and 9. You have %d more guesses.\n", (MAXGUESSES-numGuesses)); scanf("%d", &guess); numguesses++; // read user input while ( (numguesses < MAXGUESSES) & (guess!= CORRECTNUM) ); // do loop checks the condition after the first iteration if (guess == CORRECTNUM) printf("you guessed the correct number!\n"); 20

Conditional Statements: loops /* * helloworld.c: simple test application */ #include <stdio.h> #include "platform.h" void print(char *str); int a[5]={100, 56, 20, 30, -5; int i, j; int tmp; int flag; int main() { init_platform(); print("hello World\n\r"); print("this is original array\n\r"); for (i=0; i<5; i++) printf("%10d\n", a[i]); 21

Conditional Statements: loops //bubble sort for (j=0; j<4; j++){ flag=0; for (i=0; i<4; i++){ if (a[i]>a[i+1]){ tmp=a[i]; a[i]=a[i+1]; a[i+1]=tmp; flag=1; if (flag=0) break; print("this is ordered array\n\r"); for (i=0; i<5; i++) printf("%10d\n", a[i]); cleanup_platform(); return 0; 22

Data Types Pointers // Example pointer manipulations int salary1, salary2; // 32-bit numbers int *ptr; // a pointer specifying the address of an int variable salary1 = 67500; // salary1 = $67,500 = 0x000107AC ptr = &salary1; // ptr = 0x0070, the address of salary1 salary2 = *ptr + 1000; /* dereference ptr to give the contents of address 70 = $67,500, then add $1,000 and set salary2 to $68,500 */ 23

Pointers 24 Memory Address (Byte #) Data Variable Name salary1 ptr salary2.. 67500 68500 0x70 Memory Address (Byte #) Data Variable Name salary1 ptr salary2.. 0x07 0xAC 0x00 0x01 0x94 0x0B 0x01 0x70 0x00 0x00 0x00 0x00 (a) (b) 0x71 0x70 0x73 0x72 0x74 0x75 0x76 0x78 0x77 0x7A 0x79 0x7B 0x71 0x70 0x73 0x72 0x74 0x75 0x76 0x78 0x77 0x7A 0x79 0x7B

Pointer C Code Example: Passing an input variable by reference // Quadruple the value pointed to by a. #include <stdio.h> void quadruple(int *a) { *a = *a * 4; int main(void) { int x = 5; printf("x before: %d\n", x); quadruple(&x); printf("x after: %d\n", x); return 0; Console Output x before: 5 x after: 20 25