Docsity

C Programming Basics: Variables, Data Types, Control Flow, Functions, and Pointers - Prof., Assignments of Microcomputers

Partial preview of the text

Download C Programming Basics: Variables, Data Types, Control Flow, Functions, and Pointers - Prof. and more Assignments Microcomputers in PDF only on Docsity!

Chapter 5

C Language Programming

Introduction to C

-^

C has gradually replaced assembly language in many embedded applications.

-^

A summary of C language constructs that will be used in HCS12 programming.

-^

Books on C language^ –

Kernighan & Ritchie, “The C Programming Language”, Prentice Hall, 1988.

-^

Deitel & Deitel, “C: How to Program”, Prentice Hall, 1998.

-^

Kelly & Pohl, “A Book on C: Programming in C”, Addison-Wesley, 1998.

-^

A C program consists of functions and variables.

A function contains statements that specify the operations to be performed

-^

A function contains statements that specify the operations to be performed.

-^

Types of statements:

-^

Declaration

-^

Assignment

-^

Function call

-^

ControlControl

-^

Null

-^

A variable stores a value to be used during the computation.

-^

The

main ()

function is required in every C program.

-^

Data types–

C has five basic data types: void,

char, int, float,

and

double.

yp

,^

,^

,^

–^

The v

oid

type represents nothing and is mainly used with function.

–^

A variable of type

char

can hold a single byte of data.

–^

A variable of type

int

is an integer that is the natural size for a particular

hi machine.

-^

The type

float

refers to a 32-bit single-precision floating-point number.

–^

The type

double

refers to a 64-bit double-precision floating-point

numbernumber.

-^

Qualifiers

short

and

long

can be applied to integers. For GNU C

compiler,

short

is 16 bits and

long

is 32 bits.

–^

Qualifiers

signed

and

unsigned

may be applied to data types

char

and

i tinteger.

-^

Declarations–

A declaration specifies a

type,

and contains a list of one or more

variables of that typevariables of that type.

-^

Examples of declarations

int i j k;

-^

int i, j, k;

-^

char cx, cy;

-^

int m = 0;

-^

char echo = ‘y’; /* the ASCII code of letter

y

is assigned to variable

h^

echo. */

-^

Constants–

Types of constants: integers, characters, floating-point numbers, andstrings.

g

–^

A character constant is written as one character within single quotes,such as ‘x’.

-^

A character constant is represented by the ASCII code of the character.

-^

A string constant is a sequence of zero or more characters surroundedA string constant is a sequence of zero or more characters surroundedby double quotes, as “MC9S12DP256 is made by Motorola” or “”, whichrepresented an empty string. Each individual character in the string isrepresented by its ASCII code.

-^

An integer constant like 1234 is an

int.

A long constant is written with a

tege

co sta t

e^

s a

t^

o g co sta t s

tte

t^

a

terminal

l^

or L, as in 44332211L.

Bitwise Operators

(1 of 2)

•^

C provides six operators for bit manipulations; they may^ &

AND

|^

OR

^^

XOR

p

p

p

;^

y

y

only be applied to integral operands.^ ^^

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 // PTH is of type char */

| is often used to set one or more bits to 1

-^

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

/* sets bit 6 to 1 (PTB is of type char) */

-^

^^

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

/* toggles upper four bits (abc is of type

char) */char) */

•^

can be used to shift the involved operand to the right for the specified

Bitwise Operators

(2 of 2)

p^

g^

p

number of places.

xyz

= xyz >> 3;

-- shift right 3 places

•^

can be used to shift the involved operand to the left for the specified

number of places.

xyz

= xyz << 4;

-- shift left 4 places

•^

The assignment operator

is often combined with the operator. For example,

PTH

&= 0xBD;

PTH

&= 0xBD;

PTB

|= 0x40;

xyz

xyz

Relational and Logical Operators

(2 of 2)

  • Examples of relational and logical operators:

if (!(ATD0STAT0 & 0x80))

statement

;^1

/* if bit 7 is 0, then execute statement

*/ 1

if (i > 0 && i < 10)

(^

)

statement

;^2

/* if 0 < i < 10 then execute statement

*/ 2

if (a1 == a2)

statement

;^3

/* if a1 == a2 then execute statement

*/ 3

C

t^

l fl

-^

Control flow– The control-flow statements specify the order in

which computations are performed.

  • Semicolon is a statement terminator.– Braces

and

are used to group declarations and

statements together into a

compound statement,

or

block.

If-Else Statement

if (

expression

statement

1

else

-- The

else

part is optional.

statementstatement

2

Example

if (a != 0)

r = b; else

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

Switch Statement

Example

switch (expression) {

switch (i) {

case const_expr

case 1: printf(“*”);

statement

break;

break;

case 2: printf(“**”);

break;

case 2: printf(

case const_expr

break;

statement

case 3: printf(“***”);

break;

break;

i tf(“****”)

case 4: printf(“****”);

default:

break;

statement

;n

case 5: printf(“*****”);

}^

break;

}^

For-Loop Statement

for (

expr

;^ expr

;^ expr

statement; where

expr

and

expr

are assignments or function calls and

expr

is a

where,

expr

and

expr

are assignments or function calls and

expr

is a

relational

expression. Example

sum = 0;for (i = 1; i < 10; i++)for (i

1; i < 10; i++) sum += i * i; for (i = 1; i < 20; i++)

if (i % 2)

i tf(“%d “ i)

if (i % 2) printf(“%d “, i);

E amples

Input and Output

Examples

-^

Not part of the C language itself.

-^

Four I/O functions will be discussed. 1.

int

getchar

char

xch;

-- returns a character when called

xch = getchar ();

i t

t h

(i t)

t h

int

p

utchar

(i

nt).

putchar(‘a’);

outputs a character on a standardoutput device

int

p

uts

(const char *s).

puts (“Welcome to USA! \n”);

p^

(^

)^

p^

(^

-- outputs the string pointed by

s

on

a standard output device

int

printf

( formatting string, arg1, arg2, …).

converts formats and prints its arguments --

converts

, formats, and prints its arguments

on the standard output device

Formatting String for Printf

-^

The arguments of

printf

can be written as constants, single variable

The arguments of

printf

can

be written as constants, single variable

or array names, or more complex expressions.

-^

The formatting string is composed of individual groups of characters,with one character group associated with each output data item.^ –

The character group starts with

–^

The simplest form of a character group consists of the percent signfollowed by a

conversion character

indicating the type of the

corresponding data itemcorresponding data item.

-^

Multiple character groups can be contiguous, or they can be separatedby other characters, including whitespace characters. These “

other

characters

” are simply sent to the output device for display.

E

l

printf (“this is a challenging course! \n”);printf(%d %d %d”, x1, x2, x3);

/* outputs

x1, x2,

and

x

using minimal

number of digits with one space separating

-^

Examples

each value */

printf(“Today’s temperature is %4.1d\n”, temp);

Functions and Program Structure

-^

Every C program consists of one or more functions.Every C program consists of one or more functions.

-^

Definition of a function cannot be embedded within another function.

-^

A function will process information passed to it from the callingportion of the program and return a single value

return_type function_name (declarations of arguments){

portion of the program, and return a single value.

-^

Syntax of a function definition:

{

declarations and statements } ExampleExamplechar lower2upper (char cx){

if^

‘ ’ &&

‘ ’)

t^

(^

(‘ ’

‘A’))

if cx > ‘a’ && cx <= ‘z’) return (cx - (‘a’ - ‘A’));else return cx; }

Example 5.1 Write a function to test if an integer is a prime number.Solution: A number is a prime if it is indivisible by any integer between 2 andSolution: A number is a prime if it is indivisible by any integer between 2 andits half./* this function returns a 1 if

a

is prime. Otherwise, it returns a 0. */

char test_prime (int a){{

int i;if (a == 1) return 0;for (i = 2; i < a/2; i++)

if ((a % i) == 0) return 0;if ((a % i) == 0) return 0;

return 1; } • A function must be defined before it can be called• A function must be defined before it can be called.• Function prototype declaration allows us to call a function before it is defined.• Syntax of a function prototype declaration:

return_type function_name (declarations of arguments);