Introduction to C Programming

Similar documents
Course Information and Introduction

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

CSE 303 Lecture 8. Intro to C programming

The C language. Introductory course #1

EC 413 Computer Organization

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

C - Basics, Bitwise Operator. Zhaoguo Wang

C Language, Token, Keywords, Constant, variable

Programming in C. What is C?... What is C?

Programming in C UVic SEng 265

Programming in C. What is C?... What is C?

EL2310 Scientific Programming

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

EL2310 Scientific Programming

COMP s1 Lecture 1

Programming. Data Structure

C Introduction. Comparison w/ Java, Memory Model, and Pointers

Work relative to other classes

Presented By : Gaurav Juneja

EL2310 Scientific Programming

mith College Computer Science CSC352 Week #7 Spring 2017 Introduction to C Dominique Thiébaut

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

IECD Institute for Entrepreneurship and Career Development Bharathidasan University, Tiruchirappalli 23.

Introduction to C An overview of the programming language C, syntax, data types and input/output

C Programming C.1 INTRODUCTION

COMP1917: Computing 1 1. Introduction

COMP1917: Computing 1 1. Introduction

PROGRAMMAZIONE I A.A. 2017/2018

CS133 C Programming. Instructor: Jialiang Lu Office: Information Center 703

In 1978, Brian Kernighan and Dennis Ritchie produced the first publicly available description of C, now known as the K&R standard.

1. Introduction. Course Web Site. COMP1917: Computing 1. Textbook. Occupational Health and Safety (OHS)

C LANGUAGE AND ITS DIFFERENT TYPES OF FUNCTIONS

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Programming refresher and intro to C programming

Saint Louis University. Intro to Linux and C. CSCI 2400/ ECE 3217: Computer Architecture. Instructors: David Ferry

mith College Computer Science CSC231 C Tutorials Fall 2017 Introduction to C Dominique Thiébaut

CSC231 C Tutorial Fall 2018 Introduction to C

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

Unit 1: Introduction to C Language. Saurabh Khatri Lecturer Department of Computer Technology VIT, Pune

XSEDE Scholars Program Introduction to C Programming. John Lockman III June 7 th, 2012

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

Introduction to Programming. Lecture 2: Introduction to C

CS240: Programming in C

CMPE-013/L. Introduction to C Programming

Computer Programming

Lecture 5: C programming

Course Outline Introduction to C-Programming

CS 253: Intro to Systems Programming 1/21

C Programming Primer 8/31/15 1

HISTORY OF C LANGUAGE. Facts about C. Why Use C?

Motivation was to facilitate development of systems software, especially OS development.

CMPT 115. C tutorial for students who took 111 in Java. University of Saskatchewan. Mark G. Eramian, Ian McQuillan CMPT 115 1/32

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

Topic 6: A Quick Intro To C

mith College Computer Science CSC231 Bash Labs Week #10, 11, 12 Spring 2017 Introduction to C Dominique Thiébaut

PRINCIPLES OF OPERATING SYSTEMS

Lecture 3. Review. CS 141 Lecture 3 By Ziad Kobti -Control Structures Examples -Built-in functions. Conditions: Loops: if( ) / else switch

Motivation was to facilitate development of systems software, especially OS development.

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

1. The Mac Environment in SIE 1222

Lecture 3. More About C

Chapter 1 & 2 Introduction to C Language

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Introduction to C Language

A Fast Review of C Essentials Part I

Lecture 2. Examples of Software. Programming and Data Structure. Programming Languages. Operating Systems. Sudeshna Sarkar

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

by Pearson Education, Inc. All Rights Reserved.

Deep C. Multifile projects Getting it running Data types Typecasting Memory management Pointers. CS-343 Operating Systems

ENCE Computer Organization and Architecture. Chapter 1. Software Perspective

Experiment 9: Using HI-TECH C Compiler in MPLAB

CSCI 2132: Software Development. Norbert Zeh. Faculty of Computer Science Dalhousie University. Introduction to C. Winter 2019

Running a C program Compilation Python and C Variables and types Data and addresses Functions Performance. John Edgar 2

Programming and Data Structure

Summary of Last Class. Processes. C vs. Java. C vs. Java (cont.) C vs. Java (cont.) Tevfik Ko!ar. CSC Systems Programming Fall 2008

Introduction Presentation A

C Programming Language Training. This instruction relies on the C language described in C++: The Complete Reference Third Edition By Herbert Schildt

introduction week 1 Ritsumeikan University College of Information Science and Engineering Ian Piumarta 1 / 20 imperative programming about the course

High Performance Programming Programming in C part 1

The Hyderabad Public School, Begumpet, Hyderabad, A.P

Part 1: Introduction to the C Language

Lecture 3: C Programm

Programming in C and C++

Introduction. C provides two styles of flow control:

Introduction Slide 1/20. Introduction. Fall Semester. Parallel Computing

1. The Mac Environment in Sierra Hall 1242

Tutorial 1: C-Language

Introduction to Supercomputing

EPITA SPE C Workshop - D0

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

6.S096 Lecture 1 Introduction to C

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

EE 209: Programming Structures for Electrical Engineering

Introduction to C programming. By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE

Physics 2660: Fundamentals of Scientific Computing. Lecture 5 Instructor: Prof. Chris Neu

Embedded Systems. Introduction. The C Language. Introduction. Why C instead ASM. Introduction to C Embedded Programming language

Algorithms, Data Structures, and Problem Solving

edunepal_info

Physics 306 Computing Lab 1: Hello, World!

Computer Programming: Skills & Concepts (CP) Variables and ints

Transcription:

Introduction to C Programming Digital Design and Computer Architecture David Money Harris and Sarah L. Harris 2-<1>

C Chapter :: Topics Introduction to C Why C? Example Program Compiling and running a C Program Variables and Data types Operators Control Statements Programming the PIC32 C-<2>

Introduction to C Developed by Dennis Ritchie and Brian Kernighan at Bell labs in 1972. Motivation: rewriting UNIX (used to be in assembly language). Many languages derived from C: C++, C#, Objective C. By many measures, C is the most widely used language in existence. C-<3>

Why C? Availability for variety of platforms (supercomputers down to embedded microcontrollers) Relative ease of use Huge user base Ability to directly interact with hardware Allows us to write at a high-level but still retain the power of assembly language (i.e., the programmer still has a good idea of how the code will be executed) C-<4>

C Code Example 1 #include <stdio.h> int main() { printf( Hello world!\n ); } Console Output Hello world! C-<5>

C Code Example 1 #include <stdio.h> int main() { printf("hello world!\n"); } Header: header file needed for using printf function Main: the main function must be included in all C programs Body: prints "Hello world!" to the screen C-<6>

Compiling and Running a C Program Many C compilers exist cc (C compiler) gcc (GNU C compiler) PIC32 C compiler (will use in lab) Create text file (i.e., hello.c ) At command prompt, type: gcc hello.c Executable is created: a.out (a.exe on Windows) At command prompt, type:./a.out (or./a.exe) C-<7>

Programmer support comments: // and /* */ Organization Readability Usability (by yourself and others) #define: enables organization, ease of modification, and the avoidance of magic numbers #include: gives access to common functions C-<8>

Comments Single-line comments: // // this is an example of a one-line comment. Multi-line comments: /* */ /* this is an example of a multi-line comment */ C-<9>

#define Format: #define identifier replacement All instances of identifier will be replaced by replacement before compilation Example: #define MAXGUESSES 3... if (num < MAXGUESSES)... C-<10>

#include Gives access to common functions: provided by built-in libraries written by you or others Examples: #include <stdio.h> #include mypath/myfile.h C-<11>

Variables Each variable has: Type Name Value Memory Location Note: variable names are case sensitive and cannot begin with a number or contain special characters (except underscore _). C-<12>

Variables: Example short y; // type = short, name = y y = 25; // value = 25 What about the memory location? C-<13>

Memory C views memory as a group of consecutive bytes. Each byte has a unique number (the address) Address (Byte #).. 4 3 2 1 0 Data 1 byte Memory C-<14>

Memory The variable type indicates: How many bytes the variable occupies How to interpret the bytes The address of a variable occupying multiple bytes is the lowest numbered byte. Address (Byte #).. 4 3 2 1 0 Data 1 byte Memory C-<15>

C s Data Types and sizes C-<16>

C Code Example 2 unsigned char x = 42; // x = 00101010 short y = -10; // y = 11111111 11110110 unsigned long z = 0; // z = 00000000 00000000 // 00000000 00000000 Address (Byte #). 7 6 5 4 3 2 1 0 Data 1 byte 00000000 00000000 00000000 00000000 11111111 11110110 00101010 Variable Name z = 0 y = -10 x = 42 Memory C-<17>

Operators: Part 1 C-<18>

Operators: Part 2 C-<19>

Operators: Part 3 C-<20>

Example 3 #include <stdio.h> main() { int count = 0, loop; loop = ++count; /* same as count = count + 1; loop = count; */ printf("loop = %d, count = %d\n", loop, count); loop = count++; /* same as loop = count; count = count + 1; */ printf("loop = %d, count = %d\n", loop, count); } Sample Program Output loop = 1, count = 1 loop = 1; count = 2 C-<21>

C Code Example 4 int x = 14; int y = 43; int z; // x = 0b1110, y = 0b101011 printf( x = %d, y = %d\n, x, y); z = y / x; printf( y/x = %d/%d = %d\n, y, x, z); z = y % x; printf("y %% x = %d %% %d = %d\n", y, x, z); z = x && y; printf( x && y = %d && %d = %d\n, x, y, z); z = x && 0; printf( x && 0 = %d && 0 = %d\n, x, z); z = x y; printf( x y = %d %d = %d\n, x, y, z); z = x 0; printf( x 0 = %d 0 = %d\n, x, z); z = x & y; printf( x & y = %d & %d = %d\n, x, y, z); z = x y; printf( x y = %d %d = %d\n, x, y, z); z = x ^ y; printf( x XOR y = %d XOR %d = %d\n, x, y, z); z = x << 2; printf( x << 2 = %d << 2 = %d\n, x, z); z = y >> 3; printf( y >> 3 = %d >> 3 = %d\n, y, z); x += 2; y &= 15; printf( x += 2 = %d\n, x); printf( y &= 15 = %d\n, y); C-<22>

C Code Example 3: Console Output x = 14, y = 43 y/x = 43/14 = 3 y % x = 43 % 14 = 1 x && y = 14 && 43 = 1 x && 0 = 14 && 0 = 0 x y = 14 43 = 1 x 0 = 14 0 = 1 x & y = 14 & 43 = 10 x y = 14 43 = 47 x XOR y = 14 XOR 43 = 37 x << 2 = 14 << 2 = 56 y >> 3 = 43 >> 3 = 5 C-<23>

Control Statements Enables a block of code to execute only when a condition is met Conditional Statements if statement if/else statement switch/case statement Loops while loop do/while loop for loop C-<24>

if Statement The if block executes only if the condition is true (in this case, when input is equal to 1). if (input == 1) result = data; // if block x = 2; C-<25>

if/else Statement When the condition is true, the if block executes. Otherwise, the else block executes. if (a > b) return a; // if block else return b; // else block C-<26>

switch/case Statement Depending on the value of the switch variable, one of the blocks of code executes. int amount, color = 3; switch (color) { } case 1: amount = 100; break; case 2: amount = 50; break; case 3: amount = 20; break; case 4: amount = 10; break; default: printf( Error!\n ); C-<27>

switch/case Statement Equivalent to a nested if/else statement int amount, color = 3; switch (color) { } case 1: amount = 100; break; case 2: amount = 50; break; case 3: amount = 20; break; case 4: amount = 10; break; default: printf( Error!\n ); int amount, color = 3; if (color == 1) amount = 100; else if (color == 2) amount = 50; else if (color == 3) amount = 20; else if (color == 4) amount = 10; else printf( Error!\n ); C-<28>

Control Statements Enables a block of code to execute only when a condition is met Conditional Statements if statement if/else statement switch/case statement Loops while loop do/while loop for loop C-<29>

while Loop The code within the while loop executes while the condition is true. // this code computes the factorial of 9 int i = 1, fact = 1; while (i < 10) { } fact = fact * i; i++; C-<30>

do/while Loop The do block executes while the condition is true. The condition is checked only after the do block is run once. // this code computes the factorial of 9 int i = 1, fact = 1; do { fact = fact * i; // do block i++; } while (i < 10); C-<31>

for Loop Similar function as while and do/while but gives support for loop variable (in previous code, the variable i). General format: for (initialization; condition; loop operation) C-<32>

for Loop // This code computes the factorial of 9 int i, fact = 1; for (i=1; i<10; i++) fact *= i; C-<33>

Loops Loops while loop do/while loop for loop int i = 1, fact = 1; while (i < 10) { } fact = fact * i; i++; int i = 1, fact = 1; do { fact = fact * i; i++; } while (i < 10); int i, fact = 1; for (i=1; i<10; i++) fact *= i; C-<34>

C Chapter :: Topics Introduction to C Why C? Example Program Compiling and running a C Program Variables and Data types Operators Control Statements Programming the PIC32 C-<35>

Programming the PIC32 Physical pin connections Setting up (initializing) the pins Reading/Writing the pins C-<36>

Programming the PIC32 Input from switches Input analog D[8] D[9] D[10] D[11] B[11] PIC32 D[0] D[1] D[2] D[3] D[4] D[5] D[6] D[7] Output to LEDs C-<37>

Programming the PIC32 Included libraries define ports (groups of pins) and allow writing and reading from them #include <p32xxxx.h> #include <plib.h> #include <sys/appio.h> Note: int and unsigned int are both 32 bits C-<38>

Predefined variables for accessing pins TRISD: for setting up port D as input or output PORTD: for reading from port D LATD: for writing to port D C-<39>

Setting up the pins on PIC32 Pre-defined variable for setting the pins as outputs or inputs: TRISD: tristate of port D 0: output 1: input Example 1: setting PORTD as inputs TRISD = 0xFFFFFFFF; Example 2: setting PORTD as outputs TRISD = 0x00000000; Example 3: D[7:0] as outputs, D[11:8] as inputs TRISD = 0xFFFFFF00; C-<40>

Reading the pins on PIC32 Pre-defined variable used to read port D PORTD Example 1: Reading PORTD int val = PORTD; Example 2: Reading only D[3:0] int val = PORTD & 0x000F; Example 3: Reading only D[11:8] int val = (PORTD >> 8) & 0x000F; C-<41>

Writing the pins on PIC32 Pre-defined variable used to write port D LATD Example 1: writing PORTD int val = 10; LATD = val; Example 2: Writing only D[7:0] int val = 10; LATD = val & 0x00FF; C-<42>

Extras How to create a loop that executes forever: while (1) {... } Generating random numbers: #include <stdlib.h> int x, y; x = rand(); // x = a random integer y = rand() % 10; // y = rand integer from 0 to 9 C-<43>