0% found this document useful (0 votes)
7 views24 pages

Introduction To C Programming - 4

The document introduces C programming for embedded systems, focusing on structured programming and nested control structures. It includes a problem statement for counting student exam results, along with pseudocode and C code examples for implementation. Additionally, it covers assignment operators, increment and decrement operators, and formatted input/output in C.

Uploaded by

u20150540
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views24 pages

Introduction To C Programming - 4

The document introduces C programming for embedded systems, focusing on structured programming and nested control structures. It includes a problem statement for counting student exam results, along with pseudocode and C code examples for implementation. Additionally, it covers assignment operators, increment and decrement operators, and formatted input/output in C.

Uploaded by

u20150540
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

INTRODUCTION TO C

PROGRAMMING FOR EMBEDDED


SYSTEMS
INTRODUCTION TO EMBEDDED SYSTEMS

 Objectives
 Introduce structured programming
INTRODUCTION TO C PROGRAMMING FOR EMBEDDED SYSTEMS
INTRODUCTION TO C PROGRAMMING FOR EMBEDDED SYSTEMS

 Program Design 3: Nested Control Structures


 Assignment Operators
 Increment and Decrement Operators
 Formattted Input/Output
INTRODUCTION TO C PROGRAMMING FOR EMBEDDED SYSTEMS

Nested Control Structures I

Problem statement
Develop a program that would count and display the number of
students that have passed and the number of students that have
failed from a list of exam results for 10 students. If more than 8
students have passed, display "Bonus to instructor!"

Top-level pseudocode
Analyse exam results and decide if instructor should receive a
bonus
INTRODUCTION TO C PROGRAMMING FOR EMBEDDED SYSTEMS

Nested Control Structures I

First refinement
Initialise variables
Input the 10 exam grades and count passes and failures
Display a summary of the exam results and decide if instructor
should receive a bonus
INTRODUCTION TO C PROGRAMMING FOR EMBEDDED SYSTEMS

Nested Control Structures I

Second refinement
Initialise passes to 0
Initialise failures to 0
Initialise student counter to 1
While student counter is less than or equal to 10
Input the next exam result
If the student passed
Add 1 to passes
else
Add 1 to failures
Add 1 to student counter
INTRODUCTION TO C PROGRAMMING FOR EMBEDDED SYSTEMS

Nested Control Structures I

Second refinement (cont’d. . . )


Display the number of passes
Display the number of failures
If more than 8 students passed
Display "Bonus to instructor!"
INTRODUCTION TO C PROGRAMMING FOR EMBEDDED SYSTEMS

Nested Control Structures I

/* Nested Control Structures


* Copied from Deitel & Deitel Fig. 3.10
*/
#include <stdio.h>
int main( void )
{
// initialise variables in definitions
int passes = 0; // number of passes
int failures = 0; // number of failures
int student = 1; // student counter
int result; // one exam result
INTRODUCTION TO C PROGRAMMING FOR EMBEDDED SYSTEMS

C code (cont’d. . . )
// process 10 students using counter-controlled loop
while ( student <= 10 ) {
// prompt user for input and obtain value from user
printf( "Enter result for student %d (1=pass;2=fail): ", student);
scanf( "%d", &result );
if ( result == 1 ) { // if result is 1, increment passes
passes = passes + 1;
} // end if
else { // otherwise, increment failures
failures = failures + 1;
} // end else
student = student + 1; // increment student counter
}
INTRODUCTION TO C PROGRAMMING FOR EMBEDDED SYSTEMS

Nested Control Structures I

C code (cont’d. . . )

// termination phase; display number of passes and failures


printf( "Passed %d\n", passes );
printf( "Failed %d\n", failures );
// if more than 8 students passed, display "Bonus to instructor!"
if ( passes > 8 ) {
printf( "Bonus to instructor!\n" );
} // end if
return 0; // indicate program ended successfully
} // end main function
INTRODUCTION TO C PROGRAMMING FOR EMBEDDED SYSTEMS

Nested Control Structures I


Output
Enter result for student 1 (1=pass;2=fail): 1
Enter result for student 2 (1=pass;2=fail): 2
Enter result for student 3 (1=pass;2=fail): 2
Enter result for student 4 (1=pass;2=fail): 1
Enter result for student 5 (1=pass;2=fail): 1
Enter result for student 6 (1=pass;2=fail): 1
Enter result for student 7 (1=pass;2=fail): 2
Enter result for student 8 (1=pass;2=fail): 1
Enter result for student 9 (1=pass;2=fail): 1
Enter result for student 10 (1=pass;2=fail): 2
Passed 6
Failed 4
INTRODUCTION TO C PROGRAMMING FOR EMBEDDED SYSTEMS

Assignment Operators I

 Assignment operators abbreviate assignment expressions


c = c + 3;
can be abbreviated as c += 3; using the addition assignment
operator
 Statements of the form
variable = variable operator expression;
can be rewritten as
variable operator= expression;
INTRODUCTION TO C PROGRAMMING FOR EMBEDDED SYSTEMS

Assignment Operators I

 Examples of other assignment operators:


d = d - 4 ⟹ d -= 4
e = e * 5 ⟹ e *= 5
f=f/3 ⟹ f /= 3
g = g % 9 ⟹ g %= 9
INTRODUCTION TO C PROGRAMMING FOR EMBEDDED SYSTEMS

Increment and Decrement Operators I

 Increment operator (++)


Can be used instead of c += 1
c = c + 1 ⟹ c += 1 ⟹ c++

 Decrement operator (--)


Can be used instead of c -= 1
c = c - 1 ⟹ c -= 1 ⟹ c--
INTRODUCTION TO C PROGRAMMING FOR EMBEDDED SYSTEMS

Increment and Decrement Operators I

 Preincrement

Operator is used before the variable (++c or --c)


Variable is changed before the expression it is in is evaluated

 Postincrement
Operator is used after the variable (c++ or c--)
Expression executes before the variable is changed
INTRODUCTION TO C PROGRAMMING FOR EMBEDDED SYSTEMS

Increment and Decrement Operators I

 The increment and decrement operators


INTRODUCTION TO C PROGRAMMING FOR EMBEDDED SYSTEMS

Increment and Decrement Operators I

Variable in an expression:
int j;
j = 5;
printf( "%d", ++j );
 prints 6
int j;
j = 5;
printf( "%d", j++ );
 Print 5
INTRODUCTION TO C PROGRAMMING FOR EMBEDDED SYSTEMS

Increment and Decrement Operators I

 When variable not in an expression:


Preincrementing and postincrementing have the same effect
INTRODUCTION TO C PROGRAMMING FOR EMBEDDED SYSTEMS

Formatted Input/Ouput I

 printf statement using different types


INTRODUCTION TO C PROGRAMMING FOR EMBEDDED SYSTEMS

Formatted Input/Ouput I

 Printing integers with field with Field width specifies the minimum
space that the displayed integer should occupy
C code:
printf( "%4d\n", 1 );
printf( "%4d\n", 1234 );
printf( "%4d\n", 12345 );
Output:
1
1234
12345
INTRODUCTION TO C PROGRAMMING FOR EMBEDDED SYSTEMS

Formatted Input/Ouput I
Printing floating-point values with field width and precision
 Field width specifies the minimum space that the displayed floating
point number should occupy
 Precision specifies the number of digits to appear after the decimal
point
C code:
printf( "%.3f\n", 3.8663 );
printf( "%9f\n", 3.8663 );
printf( "%9.3f\n", 3.8663 );
Output:
3.866
3.866300
3.866
INTRODUCTION TO C PROGRAMMING FOR EMBEDDED SYSTEMS

Formatted Input/Ouput I

 scanf statement using different types


INTRODUCTION TO C PROGRAMMING FOR EMBEDDED SYSTEMS

Formatted Input/Ouput I

 Today
Structured Program Development III
Program design 3: nested control structures
Assignment, increment and decrement operators
Formatted input/output
 Next lecture
Program Control
‘for’ repetition structure

You might also like