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

Similar documents
UNIT- 3 Introduction to C++

Work relative to other classes

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

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

Engineering Computing I

Programming in C++ 5. Integral data types

DEPARTMENT OF MATHS, MJ COLLEGE

Expression and Operator

6.096 Introduction to C++ January (IAP) 2009

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

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

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

Lecture 02 C FUNDAMENTALS

Operators. Java operators are classified into three categories:

Chapter 3: Operators, Expressions and Type Conversion

CSC 1107: Structured Programming

Operators in C. Staff Incharge: S.Sasirekha

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

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

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

Types, Operators and Expressions

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

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:

Types, Operators and Expressions

Lecture 3. More About C

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

Unit 3. Operators. School of Science and Technology INTRODUCTION

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

Fundamentals of Programming

Course Outline Introduction to C-Programming

Language Fundamentals Summary

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

JAVA OPERATORS GENERAL

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

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

Fundamental of Programming (C)

Computers Programming Course 5. Iulian Năstac

Computer System and programming in C

Introduction to C Language

Operators in java Operator operands.

Fundamental of C programming. - Ompal Singh

SECTION II: LANGUAGE BASICS

Unit-2 (Operators) ANAND KR.SRIVASTAVA

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

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Review of the C Programming Language for Principles of Operating Systems

JAVA Programming Fundamentals

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

C - Basic Introduction

Operators and Expressions:

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

Programming. Elementary Concepts

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

COMPUTER SCIENCE HIGHER SECONDARY FIRST YEAR. VOLUME II - CHAPTER 10 PROBLEM SOLVING TECHNIQUES AND C PROGRAMMING 1,2,3 & 5 MARKS

Arithmetic Operators. Portability: Printing Numbers

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

ANSI C Programming Simple Programs

Java Notes. 10th ICSE. Saravanan Ganesh

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

C: How to Program. Week /Mar/05

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

Operators & Expressions

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

The Arithmetic Operators

Expressions and Precedence. Last updated 12/10/18

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

Chapter 1 & 2 Introduction to C Language

CS1100 Introduction to Programming

Department of Computer Science

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

File Handling in C. EECS 2031 Fall October 27, 2014

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

Language Reference Manual

Chapter 2 - Introduction to C Programming

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

OBJECT ORIENTED PROGRAMMING

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Review of the C Programming Language

Creating a C++ Program

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

A Shotgun Introduction to C

PART I. Part II Answer to all the questions 1. What is meant by a token? Name the token available in C++.

Unit-II Programming and Problem Solving (BE1/4 CSE-2)

Visual C# Instructor s Manual Table of Contents

Computers Programming Course 6. Iulian Năstac

Language Reference Manual simplicity

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

Reserved Words and Identifiers

C++ character set Letters:- A-Z, a-z Digits:- 0 to 9 Special Symbols:- space + - / ( ) [ ] =! = < >, $ # ; :? & White Spaces:- Blank Space, Horizontal

C++ Programming Basics

C Language, Token, Keywords, Constant, variable

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

ME 461 C review Session Fall 2009 S. Keres

CSCE 110 PROGRAMMING FUNDAMENTALS

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

A Java program contains at least one class definition.

CHAPTER 3 BASIC INSTRUCTION OF C++

Data Types and Variables in C language

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Transcription:

Mechatronics and Microcontrollers Szilárd Aradi PhD Refresh of C

About the C programming language The C programming language is developed by Dennis M Ritchie in the beginning of the 70s One of the most popular generic programming languages. And the base of many other languages 20/11/2018 Mechatronics and Microcomputers 2

Language structure The language is Case Sensitive Identifiers English alphabet ( a.. z, A.. Z ) Numbers ( 0.. 9 ) _ underscore character Must not start with a number Good: Variable, _calulate, _5,a_, Not good: változó, 32szam, 20/11/2018 Mechatronics and Microcomputers 3

Language structure Literals Given values, always has a type Character arrays e.g. string with a new line character\n Keywords and flow control Operators =, +, - stb. Other separators 20/11/2018 Mechatronics and Microcomputers 4

Program structure #include #define declarations: constants variables functions int main(void){} Function definitions 20/11/2018 Mechatronics and Microcomputers 5

Types char One byte 0..255 short, or short int int, or long int float double Type modifiers: The 16-bit short int data type -2 15..2 15-1 The 32-bit int data type -2 31..2 31-1 The float data type is the smallest of the three floating point types (32 bit) Double precision floating data type (64 bit) unsigned for example: unsigned int is :0.. 2 32-1 signed for example: signed char is :-128..127 short long for example long long int is 64 bit Important! Atmel AVR uses fixed-point arithmetic, thus integer types are preferred. 20/11/2018 Mechatronics and Microcomputers 6

Constants Constants 1234 int constant 1234L long 1234UL unsigned long 0x1f2 hexa int 0x1f2ULhexa unsigned long 1234.5 double 1234.5f float c char szoveg char[] (string) Escape sequences \a bell \b backspace \f formfeed \n new line \r carriage return \t horizontal tab, HTAB \v vertical tab, VTAB \\ backlash \?? \' \" \ooo octal \xhh hexadecimal 20/11/2018 Mechatronics and Microcomputers 7

Variable declarations Declaration: int i; float f,g; char c; Initialization: char c=a; char s[]= string ; const int j=12; 20/11/2018 Mechatronics and Microcomputers 8

Assignment operator Assignment: i=2; Compound assignment operators perform an operation involving both the left and right operands, and then assign the resulting expression to the left operand. i=i+2; Compound assignment operators format: i+=2; Assignment has a return value 20/11/2018 Mechatronics and Microcomputers 9

Arithmetic Operators The binary arithmetic operators are +, -, *, /, and the modulus operator %. Integer division truncates any fractional part. The expression x % y produces the remainder when x is divided by y, and thus is zero when y divides x exactly. The % operator cannot be applied to float or double. The direction of truncation for / and the sign of the result for % are machine-dependent for negative operands, as is the action taken on overflow or underflow. The binary + and - operators have the same precedence, which is lower than the precedence of *, /, and %, which is in turn lower than unary + and -. Arithmetic operators associate left to right 20/11/2018 Mechatronics and Microcomputers 10

Relational and Logical Operators The relational operators are: >, >=, <, <=. They all have the same precedence. Just below them in precedence are the equality operators: ==,!= By definition, the numeric value of a relational or logical expression is 1 if the relation is true, and 0 if the relation is false Expressions connected by && or II are evaluated left to right, and evaluation stops as soon as the truth or falsehood of the result is known. The unary negation operator I converts a non-zero operand into 0, and a zero operand into 1 20/11/2018 Mechatronics and Microcomputers 11

Increment and Decrement Operators C provides two unusual operators for incrementing and decrementing variables. The increment operator ++ adds 1 to its operand, while the decrement operator -- subtracts 1. The unusual aspect is that ++ and -- may be used either as prefix operators (before the variable, as in ++n), or postfix (after the variable: n++). In both cases, the effect is to increment n. But the expression +-n increments n before its value is used, while n«+ increments n after its value has been used. This means that in a context where the value is being used, not just the effect, ++n and n++ are different. If n is 5, then x = n++; sets x to 5, but x = ++n; sets x to 6. In both cases, n becomes 6 20/11/2018 Mechatronics and Microcomputers 12

Bitwise Operators C provides six operators for bit manipulation; these may only be applied to integral operands, that is, char, short, int, and long, whether signed or unsigned & bitwise AND bitwise inclusive OR ^ bitwise exclusive OR << left shift >> right shift ~ one's complement 20/11/2018 Mechatronics and Microcomputers 13

If-else The else is associating with the closest previous elseless if: if (n>0) if (a>b) z=a; else z=b; 20/11/2018 Mechatronics and Microcomputers 14

Switch example char c; switch (c){ case 'a': case 'e': case 'i': case 'o': case 'u': printf("maganhangzo"); break; case ' ': printf("space"); break; default: printf ("Egyik sem"); } 20/11/2018 Mechatronics and Microcomputers 15

While example char c=0; while(++c<10) { printf("%d ",c); } Result: 1 2 3 4 5 6 7 8 9 20/11/2018 Mechatronics and Microcomputers 16

While example with continue char c=0; while(++c<10) { if (c==3) continue; printf("%d ",c); } Result: 1 2 4 5 6 7 8 9 20/11/2018 Mechatronics and Microcomputers 17

While example with break char c=0; while(++c<10) { if (c==3) continue; if (c==6) break; printf("%d ",c); } Result: 1 2 4 5 20/11/2018 Mechatronics and Microcomputers 18

The C Preprocessor C provides certain language facilities by means of a preprocessor, which is conceptually a separate first step in compilation: #include, to include the contents of a file during compilation #define, to replace a token by an arbitrary sequence of characters conditional compilation and macros with arguments 20/11/2018 Mechatronics and Microcomputers 19

Macro Substitution example #define EOS '\0' #define TRUE 1 #define boole int int main(void) { boole vege=0; char s[]="hello"; int i=0; do { if (s[++i]==eos) vege=true; } while (!vege); printf("%d",i); getchar(); } int main(void) { int vege=0; char s[]="hello"; int i=0; do { if (s[++i]== \0 ) vege=1; } while (!vege); printf("%d",i); getchar(); } 20/11/2018 Mechatronics and Microcomputers 20

Problems, Threats #define abs(x) ((x) < 0? (-(x)) : (x)) #define min(a, b) ((a) < (b)? (a) : (b)) int i=-5; int j=-6; printf("%d",min(abs(i),abs(j))); The previously defined macro uses the parameters twice, hence: min(i++,j++) substituted with: ((i++) < (j++)? (i++) : (j++)) therefore one of the variables increments twice 20/11/2018 Mechatronics and Microcomputers 21

Wrong usage Further examples for wrong usage: #define MARGIN 2 #define WIDTH 8 #define HEIGHT 10 #define FULLWIDTH WIDTH+MARGIN #define AREA FULLWIDTH*HEIGHT Wrong result, can be fixed with proper bracketing. also, #define square(x) x*x square(i+1); substituted with: i+1*i+1 20/11/2018 Mechatronics and Microcomputers 22

4.11.2. Fix Fixing with parenthesis: #define MARGIN 2 #define WIDTH 8 #define HEIGHT 10 #define FULLWIDTH (WIDTH+MARGIN) #define AREA FULLWIDTH*HEIGHT also, #define square(x) (x)*(x) square(i+1); substituted with: (i+1)*(i+1) 20/11/2018 Mechatronics and Microcomputers 23

Arrays int t[10]; // without intialization int t[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; // giving values to all elements int t[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; // same, the number of elements define array length int t[10] = {1, 2, 3, 4}; // only the first 4 elements are initialized 20/11/2018 Mechatronics and Microcomputers 24

5.7 Multi-dimensional Arrays int t[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11}; int t[3][4] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11}; int t[3][4] = {{0, 1, 2, 3}, {4, 5, 6, 7}, {8, 9,10,11}}; 20/11/2018 Mechatronics and Microcomputers 25

Character Arrays char s[200] = "Hello"; char s[200] = {'H', 'e', 'l', 'l', 'o', '\0'}; The two definitions are the same Strings are terminated with the 0 byte ( ( '\0' ) charater char s[] = "Hello"; Array length will be set to 5+1 because of the 0 20/11/2018 Mechatronics and Microcomputers 26

Basics of Structures struct pont{ int x; int y; }; This struct declaration defines a type, where the point is the structure tag and x and y are the members. Example for shorthand usage: struct point pt; Defines a point type variable pt. Initialization acan be done by listing member values: struct point pt={ 320, 200 }; printf("%d, %d", pt.x, pt.y); 20/11/2018 Mechatronics and Microcomputers 27

Functions Introduction After a while, the single main(){} program will be too large and complex. There are code sections and algorithms that need to be used at multiple code locations. Too complex algorithms reduce the readability of the program There is a need to structure our program Functions break large computing tasks into smaller ones, and enable people to build on what others have done instead of starting over from scratch. Appropriate functions hide details of operation from parts of the program that don't need to know about them, thus clarifying the whole, and easing the pain of making changes 20/11/2018 Mechatronics and Microcomputers 28

Pointers int i=10; int *ip; ip=&i; printf("%d\n",*ip); i++; printf("%d\n",*ip); Console: 10 11 Memory Variable Value (int) address (byte num) 1000? 1004 i 10 11 1008 ip? 1004 1012? ---- *ip? 10 11 &i 1004 20/11/2018 Mechatronics and Microcomputers 29

Function example int add(int a, int b) { return a+b; } 1 2 3 int main(int argc, char *argv[]) { int x=1,y=2; printf("x+y=%d",add(x,y)); return 0; } 3 1 2 20/11/2018 Mechatronics and Microcomputers 30

Call by value example void swap(int a, int b) { 1 2 int c; c=a;a=b;b=c; return; } int main(int argc, char *argv[]) { int x=1,y=2; swap(x,y); 1 2 printf("x:%d y:%d",x,y); return 0; } a=2, b=1, just for the copied variables x:1 y:2 swap has no effect outside 20/11/2018 Mechatronics and Microcomputers 31

Pointers and Function Arguments Because of call by value, swap can't affect the arguments a and b in the routine that called it. The function above only swaps copies of a and b. The way to obtain the desired effect is for the calling program to pass pointers to the values to be changed: swap(&a, &b); void swap(int *px, int *py) /* Right */ { int temp=*px; *px = *py; *py = temp; } 20/11/2018 Mechatronics and Microcomputers 32

5.3. Pointers and Arrays The notation a[i] refers to the i-th element of the array. If pa is a pointer to an integer, declared as int *pa; then the assignment pa = &a[0] sets pa to point to element zero of a; that is, pa contains the address of a[0]. Now the assignment x=*pa; will copy the contents of a[0] into x. 20/11/2018 Mechatronics and Microcomputers 33

5.3. Pointers and Arrays Thus, if pa points to a[0], *(pa+1) refers to the contents of a[1], pa+i is the address of a[i], and *(pa+i) is the contents of a[i]. 20/11/2018 Mechatronics and Microcomputers 34

Argument passing with arrays Same as with pointers. hamu void confuse(char s[]) { s[0]='m'; return; mamu } int main(int argc, char *argv[]) { char s[]="hamu"; printf("%s\n",s); hamu confuse(s); printf("%s\n",s); mamu return; } 20/11/2018 Mechatronics and Microcomputers 35