CMPE-013/L. Introduction to C Programming

Similar documents
Lecture 2: C Programming Basic

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

Syntax and Variables

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

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

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

Presented By : Gaurav Juneja

ET156 Introduction to C Programming

A Fast Review of C Essentials Part I

Procedures, Parameters, Values and Variables. Steven R. Bagley

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

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

Introduction to Computing Lecture 01: Introduction to C

BLM2031 Structured Programming. Zeyneb KURT

Lectures 5-6: Introduction to C

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

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

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

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

Programming in C++ 4. The lexical basis of C++

Chapter 2: Overview of C. Problem Solving & Program Design in C

The component base of C language. Nguyễn Dũng Faculty of IT Hue College of Science

Short Notes of CS201

Fundamentals of Programming

CS201 - Introduction to Programming Glossary By

EL6483: Brief Overview of C Programming Language

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

CSCI 2132 Software Development. Lecture 8: Introduction to C

MPLAB C1X Quick Reference Card

C: How to Program. Week /Mar/05

Variables in C. Variables in C. What Are Variables in C? CMSC 104, Fall 2012 John Y. Park

ET156 Introduction to C Programming

CS 61C: Great Ideas in Computer Architecture Introduction to C

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

Lectures 5-6: Introduction to C

Programming. Data Structure

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

6.096 Introduction to C++ January (IAP) 2009

C Overview Fall 2014 Jinkyu Jeong

INTRODUCTION 1 AND REVIEW

Chapter 2 - Introduction to C Programming

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

C - Basics, Bitwise Operator. Zhaoguo Wang

Week 1 / Lecture 2 8 March 2017 NWEN 241 C Fundamentals. Alvin Valera. School of Engineering and Computer Science Victoria University of Wellington

C Language, Token, Keywords, Constant, variable

Fundamental of Programming (C)

CS Programming In C

Technical Questions. Q 1) What are the key features in C programming language?

C Fundamentals & Formatted Input/Output. adopted from KNK C Programming : A Modern Approach

C Programming Review CSC 4320/6320

Programming. C++ Basics

DEPARTMENT OF MATHS, MJ COLLEGE

Variables in C. CMSC 104, Spring 2014 Christopher S. Marron. (thanks to John Park for slides) Tuesday, February 18, 14

Variables. Data Types.

Basic Types, Variables, Literals, Constants

Topic 6: A Quick Intro To C

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

.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar..

PROGRAMMAZIONE I A.A. 2018/2019

Lecture 3. Variables. Variables

Chapter 15 - C++ As A "Better C"

Data Types and Variables in C language

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

Part 1: Introduction to the C Language

Annotation Annotation or block comments Provide high-level description and documentation of section of code More detail than simple comments

Programming and Data Structures

Programming in C and C++

COMP322 - Introduction to C++

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

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

Programming in C UVic SEng 265

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

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

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

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and

Tokens, Expressions and Control Structures

from Appendix B: Some C Essentials

Programming in C and C++

CMSC 104 -Lecture 5 John Y. Park, adapted by C Grasso

Data Type Fall 2014 Jinkyu Jeong

DETAILED SYLLABUS INTRODUCTION TO C LANGUAGE

CS240: Programming in C

Computer Components. Software{ User Programs. Operating System. Hardware

Introduction to the C Programming Language

COP 3275: Chapter 02. Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

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

VARIABLES AND CONSTANTS

Chapter 1 & 2 Introduction to C Language

UEE1302 (1102) F10: Introduction to Computers and Programming

Language Design COMS W4115. Prof. Stephen A. Edwards Spring 2003 Columbia University Department of Computer Science

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

CSC 1600 Memory Layout for Unix Processes"

PRINCIPLES OF OPERATING SYSTEMS

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

Introduction to C++ Systems Programming

Chapter IV Introduction to C for Java programmers

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

In this session we will cover the following sub-topics: 1.Identifiers 2.Variables 3.Keywords 4.Statements 5.Comments 6.Whitespaces 7.Syntax 8.

Transcription:

CMPE-013/L Introduction to C Programming Bryant Wenborg Mairs Spring 2014

What we will cover in 13/L Embedded C on a microcontroller Specific issues with microcontrollers Peripheral usage Reading documentation Testing and Debugging Commenting Unit testing Integration testing Incremental development Issues with embedded debugging

Key things we ll enforce Modularity and decomposition Code is segmented cleanly by functionality Proper use of.h and.c files Good use of functions for clean implementation Incremental build and test Write small amounts of code and test often Write unit tests for separable functions Use pseudo-code and diagrams for planning

Key things we ll enforce Well documented code Self-documenting code does not exist! Good variable names and comments are req d Clean and clear style More important to adhere to established guidelines than use the right style We ll use a (modified) K&R style guide

C Programming C Source Files.c C Compiler Assembly Source Files (.asm or.s).s Assembly Source Files (.asm or.s).s Assembler Object File Libraries (Archives) (.lib or.a) Archiver.a.o Linker Object Files Executable.hex.map Memory Map Linker Script (.lkr or.gld).gld.cof MPLAB X Debug Tool COFF Debug File

Development Tools Data Flow C Compiler C Source File.c Preprocessor.h C Header File Compiler Assembly Source File.s

Compilation Source Code Analysis front end parses programs to identify its pieces variables, expressions, statements, functions, etc. depends on language (not on target machine) Code Generation back end generates machine code from analyzed source may optimize machine code to make it run more efficiently dependent on target architecture Symbol Table map between symbolic names and items like assembler, but more kinds of information

Fundamentals of C A Simple C Program Preprocessor Directives Header File #include <stdio.h> #define PI 3.14159 int main(void) { float area; Constant Declaration (Text Substitution Macro) Variable Declaration Function } // Calculate area of circle float radius = 12.0; area = PI * radius * radius; printf("area = %f\n", area); Comment Variable Initialization

The C Runtime The C runtime is the backend of C: Allocates space for the stack Initializes the stack pointer Allocates space for the heap Copies values from Flash/ROM to variables in RAM that were declared with initial values Otherwise clears uninitialized RAM Calls main()

C Runtime Environment Runtime environment setup code is automatically linked into a program by the XC32 compiler Code comes from: XC32: crt0.o User modifiable if absolutely necessary

Stack/Heap Heap grows from top down Stack grows from bottom up

Hello World The only way to learn a new programming language is by writing programs in it. The first program to write is the same for all languages: Print the words Hello, world!

Generic C Code #include <stdio.h> int main(void) { printf("hello, world!\n"); // Uses the I/O library to print } return 0;

Embedded C Code #include <stdio.h> int main(void) { printf("hello, world!\n"); } while (1); // Loop forever and never return

Embedded C Code int main(void) { while (1) { // Read inputs // Perform calculations } } // Update outputs

Exercise 1 A Simple C Program Preprocessor Directives Header File #include <stdio.h> #define PI 3.14159 int main(void) { float area; Constant Declaration (Text Substitution Macro) Variable Declaration Function } // Calculate area of circle float radius = 12.0; area = PI * radius * radius; printf("area = %f\n", area); Comment Variable Initialization

Comments Definition Comments are used to document a program's functionality and to explain what a particular block or line of code does. Comments are ignored by the compiler, so you can type anything you want into them. Two kinds of comments may be used: Block Comment /* This is a comment */ Single Line Comment // This is also a comment

Comments Using Block Comments Block comments: Begin with /* and end with */ May span multiple lines /******************************************************** * Program: hello.c * Author: R. Ostapiuk ********************************************************/ #include <stdio.h> /* Function: main() */ int main(void) { printf( Hello, world!\n ); /* Display Hello, world! */ }

Comments Using Single Line Comments Single line comments: Begin with // and run to the end of the line //======================================================= // Program: hello.c // Author: R. Ostapiuk //======================================================= #include <stdio.h> // Function: main() int main(void) { // Display greeting printf( Hello, world!\n ); }

Comments Nesting Comments Block comments may not be nested within other delimited comments Single line comments may be nested Example: Single line comment within a delimited comment. /* code here // Comment within a comment */ Example: Delimited comment within a delimited comment. /* */ Delimiters don t match up as intended! code here /* Comment within a comment */ code here /* Comment within a oops! */ Dangling delimiter causes compile error

Comments Best Practices/Doxygen /** * @file * @author R. Ostapiuk * @section DESCRIPTION * This is an example Hello World program */ #include <stdio.h> /** * Main, the entrypoint for this C program. * @return A success code, where non-zero values indicate failure */ int main(void) { int i; /// Loop counter variable char *p; /// Pointer to text string } // Display greeting printf( Hello, world!\n );

Variables and Data Types A Simple C Program Example Data Types #include <stdio.h> #define PI 3.14159 int main(void) { float area; Variable Declarations } // Calculate area of circle radius = 12.0; area = PI * radius * radius; printf("area = %f\n", area); Variables in use

Variables Definition A variable is a name that represents one or more memory locations used to hold program data. A variable may be thought of as a container that can hold data used in a program int myvariable; myvariable = 5; 5

Variables Variables are names for storage locations in memory 31 Data Memory (RAM) 0 int warpfactor; char firstletter; long double length; 41 16 5.74532370373175 10-44 A

Variables Variable declarations consist of a unique identifier 31 Data Memory (RAM) 0 int warpfactor; char firstletter; long double length; 41 16 5.74532370373175 10-44 A

Variables and a data type Determines size Determines how values are interpreted 31 Data Memory (RAM) 0 int warpfactor; 41 16 char firstletter; A long double length; 5.74532370373175 10-44

Example of Identifiers in a Program Identifiers Names given to program elements: Variables, Functions, Arrays, Other elements #include <stdio.h> #define PI 3.14159 int main(void) { float radius, area; } // Calculate area of circle radius = 12.0; area = PI * radius * radius; printf("area = %f\n", area);

Identifiers Valid characters in identifiers: First Character _ (underscore) A to Z a to z Case sensitive! I d e n t i f i e r Remaining Characters _ (underscore) A to Z a to z 0 to 9 Only first 31 characters significant*

ANSI C Keywords auto break case char const continue default do double else enum extern float for goto if int long register return short signed sizeof static struct switch typedef union unsigned void volatile while Some compiler implementations may define additional keywords

Data Types Fundamental Types Type Description Bits char int float long double single character integer single precision floating point number double precision floating point number The size of an int varies from compiler to compiler. XC16 int as 16-bits XC32 defines int as 32-bits 8 16 32 64 If you need precise length variable types, use stdint.h uint8_t is unsigned 8 bits int16_t is signed 16 bits, etc.

Data Type Qualifiers Modified Integer Types Qualifiers: unsigned, signed, short and long Qualified Type Min Max Bits unsigned char char, signed char unsigned short int short int, signed short int unsigned int int, signed int unsigned long int long int, signed long int unsigned long long int long long int, signed long long int 0-128 0-32768 0-2 31 0-2 31 0-2 63 255 127 65535 32767 2 32-1 32767 2 32-1 2 31-1 2 64-1 2 63-1 8 8 16 16 32 32 32 32 64 64

Data Type Qualifiers Modified Floating Point Types Qualified Type Absolute Min Absolute Max Bits float double long double ± ~10-44.85 ± ~10 38.53 32 ± ~10-44.85 ± ~10 38.53 32 ± ~10-323.3 ± ~10 308.3 64 MPLAB-X XC32: Uses the IEEE-754 Floating Point Format

Variables How to Declare a Variable Syntax type identifier 1, identifier 2,, identifier n ; A variable must be declared before it can be used The compiler needs to know how much space to allocate and how the values should be handled Example int x, y, z; float warpfactor; char textbuffer[10]; unsigned index;

Syntax Variables How to Declare a Variable Variables may be declared in a few ways: One declaration on a line type identifier; One declaration on a line with an initial value type identifier = InitialValue; Multiple declarations of the same type on a line type identifier 1, identifier 2, identifier 3 ; Multiple declarations of the same type on a line with initial values type identifier 1 = Value 1, identifier 2 = Value 2 ;

Variables How to Declare a Variable Examples unsigned int x; unsigned y = 12; int a, b, c; long int myvar = 0x12345678; long z; char first = 'a', second, third = 'c'; float bignumber = 6.02e+23; It is customary for variable names to be spelled using "camel case", where the initial letter is lower case. If the name is made up of multiple words, all words after the first will start with an upper case letter (e.g. mylongvarname).