Docsity

Introduction to C Programming - Lecture Slides | ECE 4510, Assignments of Microcomputers

Partial preview of the text

Download Introduction to C Programming - Lecture Slides | ECE 4510 and more Assignments Microcomputers in PDF only on Docsity!

©^ Copyright: Massood Atashbar

Introduction to Microprocessors Introduction to C Programming

©^ Copyright: Massood Atashbar

Introduction to C Programming^ „^ A C program consists of functions and variables.^ „

A function contains statements that specify theoperations to be performed.^ „^

Types of statements:^ „^ Declaration^ „^ Assignment^ „^ Function call^ „^ Control^ „^ Null

„^ A variable stores a value to be used during thecomputation. „ The^ main ()

function is required in every C program.

©^ Copyright: Massood Atashbar

Declarations^ „^ Declarations^ „

A declaration specifies a

type,

and contains a list of one or

more variables of that type. „^ Examples of declarations^ „

int i, j, k; „ char cx, cy; „ int m = 0; „ char echo = ‘y’; /* the ASCII code of letter

y^ is assigned to

variable

echo. */

©^ Copyright: Massood Atashbar

Constants „ Constants^ „^ Types of constants: integers, characters, floating-pointnumbers, and strings.^ „^ A character constant is written as one character within singlequotes, such as ‘x’.^ „^ A character constant is represented by the ASCII code of thecharacter.^ „^ A string constant is a sequence of zero or more characterssurrounded by double quotes, as “MC9S12DP256 is madeby Motorola” or “”, which represented an empty string. Eachindividual character in the string is represented by its ASCIIcode.^ „^ An integer constant like 1234 is an

int.^ A long constant is

written with as 44332211.

©^ Copyright: Massood Atashbar

Bitwise Operators „ C provides six operators for bit manipulations; they may only beapplied to integral operands. „ &^ AND „ | OR „ ^ XOR „ ~^ NOT „ >>^ right shift „ <<^ left shift „ &^ is often used to clear one or more bits of an integral variable to 0. „ PTH = PTH & 0xBD

Æ^

clears bit 6 and bit 1 of PTH to 0

„^ | is often used to set one or more bits to 1. „^ PTB = PTB | 0x40;

/* sets bit 6 to 1

„^ ^^

can be used to toggle a bit. „^ abc = abc ^ 0xF0;

/* toggles upper four bits

©^ Copyright: Massood Atashbar

Bitwise Operators^ „^ >>

can be used to shift the involved operand to the right for thespecified number of places. „^ xyz

= xyz >> 3

Æ^ shift right 3 places

„^ <<

can be used to shift the involved operand to the left for thespecified number of places. „^ xyz

= xyz << 4

Æ^ shift left 4 places

„^ The assignment operator

=^ is often combined with the operator.

For example, „ PTH &= 0xBD; „ PTB |= 0x40; „ xyz^

„^ xyz

©^ Copyright: Massood Atashbar

Control Flow^ „^ Control flow^ „

The control-flow statements specify the order in whichcomputations are performed. „ Semicolon is a statement terminator. „ Braces

{^ and

}^ are used to group declarations and statements together into a

compound statement,

or^ block.

©^ Copyright: Massood Atashbar

If-Else^ „^ if (

expression

„^

statement „^ else

-- The

else

part is optional.

„^

statement „^ Example

„^ if (a != 0) „^

r = b; „^ else „^

r = c; „^ A more concise way for this statement is „^ r = (a != 0)? b : c;

©^ Copyright: Massood Atashbar

Switch Statement^ „^ switch (expression) {^ „^

case const_expr1:

statement1;break;

„^

case const_expr2:

statement2;break;

„^

Default:

statementn;break;

„^

©^ Copyright: Massood Atashbar

Switch Statement^ „^ switch (i) {^ „^

case 1: printf(“*”);break; „^

case 2: printf(“**”);break; „^

case 3: printf(“***”);break; „^

case 4: printf(“****”);break; „^

default:break;

©^ Copyright: Massood Atashbar

While Statement „^ while (

expression

„^

statement „^ Example „^ int_cnt = 5; „^ while (int_cnt); /* do nothing while the variable int_cnt

≠^ 0 */

„^ Do-While Statement „^ do „^

statement „^ while (

expression

„^ Example „^ Int digit = 9; „^ do „^

printf(“%d “, digit--); „^ while (digit >= 1);

©^ Copyright: Massood Atashbar

Functions and Program Structures^ „^ Every C program consists of one or more functions^ „^ Definition of a function cannot be embedded within anotherfunction^ „^ A function can be called from several different places within aprogram^ „^ A function will process information passed to it from the callingportion of the program, and return a single value^ „^ Information is passed to the function via special identifiers calledarguments and returned via return statement^ „^ Some functions, however, accpet information but do not returnanything

©^ Copyright: Massood Atashbar

Functions and Program Structures^ „^ A function cannot be called before it has been defined^ „^ The dilemma is solved by using the function prototype statement^ „^ Syntax for the function prototype statement:^ „^ Return_type function_name (declaration of arguments)^ „^ Ex:^ „^ Char test_prime(int a);^ „^ Declaring the function prototype before main solves the problem

©^ Copyright: Massood Atashbar

Pointers and Addresses^ „^ Ability to work with memory addresses is an important feature ofthe C language^ „^ Feature allows programmer the freedom to perform operationssimilar to assembly language^ „^ Array elements can be more efficiently reached through pointersthan by sub-scripting^ „^ Addresses that can be stored and changed are called pointers^ „^ Pointer is just a variable that contains an address^ „^ Pointers can be used to reach objects in memory