int i;main(){for(;i["]<i;++i){--i;}"];read('-'-'-',i+++"hell\
o, world!\\n",'/'/'/'));}read(j,i,p){write(j/p+p,i---j,i/i);}
-- Dishonorable mention, Obfuscated C Code Contest, 1984.
(Author requested anonymity.)
S07: The C Language
Required:
PM: Ch 6, pgs 63-80
PM: Ch 8.4, pgs 114-118
Recommended:
K&R, Chapters 1-4
C++ Compiler 6.5
Global Variables
Operators
Functions
C program
Variables
Expressions
Control Statements
Library Functions
CS 224
Chapter
Lab
Homework
L01: Data Types
L02: FSM
HW01
HW02
L03: Blinky
L04: Microarch
L05b: Traffic Light
L06a: Morse Code
HW03
HW04
HW05
HW06
L07b: Morse II
L08a: Life
L09b: Snake
L10a: Threads
HW07
HW08
HW09
HW10
S00: Introduction
Unit 1: Digital Logic
S01: Data Types
S02: Digital Logic
Unit 2: ISA
S03: ISA
S04: Microarchitecture
S05: Stacks / Interrupts
S06: Assembly
Unit 3: C
S07: C Language
S08: Pointers
S09: Structs
S10: Threads
S11: I/O
BYU CS 224
The C Language
2
Learning Objectives…
Learning Outcomes
After completing this section, you should
be able to
Discuss the advantages of using a
high level language.
Explain the difference between a
compiler and an interpreter.
Summarize the function of the C
preprocessor.
Describe the
compile/assembly/linker process.
List the main features of a C
Language program.
Describe how C stream I/O works.
BYU CS 224
The C Language
Topics
Compilers vs. Interpreters
C Program
Symbol Table
Variables & Operators
Scope
Expressions
Precedence
C Compilation
Frames
C / Assembler
Coding Practices
I/O Streams
Lab 7b: Morse II
3
Terms…
Activation Record – A block of memory on the stack that is created when a function is
called and contains all the local variables for a given invocation of a function.
Arithmetic Operator – Operator that returns a numerical value.
Associativity – The execution order of same precedence operators.
Bitwise Operator – Operator that performs bitwise logical operations.
Data type – Representation and valid operations of data object.
Expression – Combination of variables / operators that returns a single value.
Global (static) – Variable permanently assigned to a memory location.
Literal – An immutable data object.
Local (automatic) – Variable stored in a functions activation record.
Logical Operator – Operator that returns a logical (true/false) value.
Operator – Performs an operation on operand(s).
Scope - Extent of a variable/function’s availability in a program.
Precedence – The execution order of operators.
Variable - Symbolic name for a memory location that hold a value.
Variable Coercion – Forcing mixed data type variables to a common type.
Volatile – Variable modifier that prohibits optiminization by compiler.
BYU CS 224
The C Language
4
Levels of Abstraction
Problems
Algorithms
High Level Languages
Language
Assembly code
Machine (ISA) Architecture
Machine code
Microarchitecture
MSP430 Architecture
Circuits
Logic gates, multiplexers, memory, etc.
Devices
Transistors
BYU CS 224
The C Language
5
High Level Languages
High Level Languages
The closer a language is to your original specification, the
easier the program is to write.
Many, many programming languages
LISP - LISt Processing
PROLOG - logic programming
MATLAB - matrix and vector manipulations
BASIC – interpreter for small computers
APL – matrix and vectors
FORTRAN – formula translation
COBOL – business and accounting
PASCAL – procedural
Ada – DOD large systems
Java – Internet
C, C++ ….
BYU CS 224
The C Language
6
High Level Languages
High Level Languages
Allow us to use symbolic names for values
Hide low level details (ISA) from programmer
Portable software (works on different ISAs)
printf("Hello World!");
Provide expressiveness
numberOfDays = 30;
switch_A = ON;
Provide abstraction of underlying hardware
Programmer simply assigns each value a name
Allow us to ignore many memory details.
Express complex tasks with smaller amount of code
English-like and human constructs
Enhance code readability
Can read like a novel…
Easier to debug/maintain
BYU CS 224
main()
{
readInput();
checkForErrors();
doCalculation();
writeOutput();
}
The C Language
if(isCloudy)
get(umbrella);
else
get(sunglasses);
7
High Level Languages
High Level Languages
Provide safeguards against bugs
Rules can lead to well-formed programs
Compilers can generate checks
array bounds checking
data type checking
assert(accountBalance >= 0);
Many languages provide explicit support for assertions
structured programming (no GOTO statements)
something that should be true - if it isn’t, then error
High-level languages make complex programming
simpler, while low-level languages tend to produce more
efficient code
However, well-designed compilers frequently produce code
comparable in efficiency to what most low-level programmers
can produce by hand with better overall results
BYU CS 224
The C Language
8
Compilers vs Interpreters
temp=v[i];
v[i]=v[i+1];
v[i+1]=temp;
= Executable
BYU CS 224
MOV.B 0x0001(SP),R14
MOV.W SP,R15
INCD.W R15
ADD.W R15,R14
MOV.B @R14,0x0000(SP)
MOV.B 0x0001(SP),R14
INC.W R14
Object
code
415E 0001
410F
532F
5F0E
4EE1 0000
415E 0001
531E
Source
code
Application
Assembly
Interpreter
High-level
language
statements
Assembler
temp=v[i];
v[i]=v[i+1];
v[i+1]=temp;
Compiler
Compilers vs Interpreters
= Data Path
The C Language
9
The C Language
The C Programming Language
Developed between 1969 and 1973 by Dennis Ritchie at
Bell Labs.
C first developed for use in writing compilers and
operating systems (UNIX).
A low-level high-level language
Many variants of C
1989, the American National Standards Institute standardized C
(ANSI C, most commonly used C)
“The C Programming Language” by Kernighan and Ritchie is the C
“Bible” (Also called the “White Book”.)
C is one of the most popular programming languages of
all time – very few computer architectures exist for which
there is no C.
C is predecessor to most of today’s procedural languages
such as C++ and Java.
BYU CS 224
The C Language
10
Dennis Ritchie (1940-2011)
Dennis Ritchie, the software developer who brought the world
the C programming language and Unix operating system, has died
at the age of 70.
Ritchie (known by the username "dmr") was part of a dynamic
software development duo with Ken Thompson at Bell Labs,,
which they joined in 1967 and 1966, respectively. Ritchie created
the C programming language, which replaced the B programming
language Thompson invented.
Two years later in 1969, they created Unix, initially designed
for minicomputers. Unix was initially written in 1969 in
assembly language and later in C. Unix went on to become key
software for critical computing infrastructure around the world.
“UNIX is very simple, it just needs a genius to understand its simplicity.”
--Dennis Ritchie
BYU CS 224
The C Language
11
The C Language
Compiling a C Program
C/C++ Code
Assembler Code
Object Code
Machine Code
BYU CS 224
The C Language
12
The C Language
Compiling a C Program
Preprocessor
Text
C Source Code
C Compiler
C Preprocessor
2nd Pass
1st Pass
Source Code
Analysis
Symbol
Table
Preprocessed C
source code
Code
Generation
Assembly Code
Assembler
Library & Object
Files
Object Code
Linker
Executable
Image
BYU CS 224
The C Language
Machine Code
13
1st C Program
A First Program
Tells compiler to use all the definitions
found in the msp430.h library.
A .h file is called a header file and
//************************************
containsP1.0
definitions and declarations.
// blinky.c: Software Toggle
//************************************
All C programs must have a main() routine.
#include "msp430.h"
volatile unsigned int i;
//
void main(void)
{
WDTCTL = WDTPW | WDTHOLD; //
P4DIR |= 0x40;
//
for (;;)
//
{
P4OUT ^= 0x40;
//
while (--i);
//
}
Delay 65,536
}
BYU CS 224
no optimization
Stop WD w/Password
stop Allocate
watchdog
a RAM variable
P4.6 output
(.bss i,2)
loop
Set P4.6 as output
toggle P4.6
Loop forever
delay
The C Language
Toggle P4.6
14
C Program
A C Program
What is a C program?
Functions
Global variables
Variables are symbolic names (labels) for memory
locations that hold values
2 types of variables
Variable declarations are held in symbol table and include
Local (automatic) – allocated on stack at run-time.
Global (static) – allocated at compile time.
A symbolic name
Data type (int, char, double)
Scope (code region where the variable is defined)
Variables must be declared and in scope before they can be
used (referenced) by a program
Operators manipulate values
BYU CS 224
The C Language
15
C Style
Style
Use lots of comments
/* This is a comment */
// This is a single line comment
Indents
Each new scope is indented 2 spaces from previous
Put { on end of previous line, or start of next line
Line matching } up below
Style is something of a personal matter.
Everyone has their own opinion…
What is presented here is similar to that
in common use and a good place to
start...
BYU CS 224
Style 1
if(a < b) {
b = a;
a = 0;
}
else {
a = b;
b = 0;
}
The C Language
Style 2
if(a < b)
{
b = a;
a = 0;
}
else
{
a = b;
b = 0;
}
16
C Preprocessor
The C Preprocessor
#define symbol code
The preprocessor replaces symbol with code everywhere it appears in the
program below
#define NUMBER_OF_MONKEYS 259
#define MAX_LENGTH 80
#define PI 3.14159
#include filename.h
The preprocessor replaces the #include directive itself with the contents of
header file filename.h
#include <stdio.h>
#include "myheader.h"
/* a system header file */
/* a user header file */
Macros with Functional Substitution
#define add(x,y) x+=y
#define doLoop(x,y) do {x} while(y);
doLoop(add(z,2),z<10)
BYU CS 224
The C Language
do {z+=2} while(z<10);
17
Exercise 7.1
Expand the following C pre-processor macros:
#define MASK(b) (0x80 >> ((b)%8))
#define SET_CELL(a2d,r,c) a2d[r][(c)/8] |= MASK(c)
#define TEST_CELL(a1d,c) ((a1d[(c)/8] & MASK(c)) ? 1:0)
1. SET_CELL(life,row,col+1);
2. if (TEST_CELL(temp,col)) {...};
BYU CS 224
The C Language
18
Variables & Operators
MSP430 C Variable Data Types
Type
Size
Representation
Minimum
Maximum
char, signed char
8 bits
ASCII
-128
127
unsigned char
8 bits
ASCII
0
255
short, signed short
16 bits
2's complement
-32768
32767
unsigned short
16 bits
Binary
0
65535
int, signed int
16 bits
2's complement
-32768
32767
unsigned int
16 bits
Binary
0
65535
enum
16 bits
2's complement
-32768
32767
pointers, references
16 bits
Binary
0
0xFFFF
function pointers
16 bits
Binary
0
0xFFFF
long, signed long
32 bits
2's complement
-2,147,483,648
2,147,483,647
unsigned long
32 bits
Binary
0
4,294,967,295
float
32 bits
IEEE 32-bit
1.175495e-38
3.4028235e+38
double
32 bits
IEEE 32-bit
1.175495e-38
3.4028235e+38
long double
32 bits
IEEE 32-bit
1.175495e-38
3.4028235e+38
BYU CS 224
The C Language
19
Scope
Scope: Local versus Global
Extent of a variable/function’s availability in a program
Local Variables (automatic)
{
Declared at the beginning of a block
Stored in activation record on the stack
Scope is from point of declaration to the
end of the block
Un-initialized
// begin block
int chimp;
...
}
Global Variables (static)
Declared outside of a function
Stored in Global Data Section of memory
Scope is from point of declaration to the
end of the program
May be initialized to zero
BYU CS 224
The C Language
int chimp;
{
// begin block
...
}
20
Variables
Literals/ Constants
Literal Values
Constant Variables
Variable declarations prefixed with the const qualifier
Immutable named variables
const double pi = 3.14159;
Symbolic Values
Unnamed constant values used in programs
area = 3.14159 * radius * radius;
Created using preprocessor directive #define
#define PI 3.14159
How are the above the same?
How are the above different?
BYU CS 224
The C Language
21
enum
Enum
Enums in C are a way to map identifiers to integral values
(thus avoiding magic numbers in your code).
The advantage of enum over #define is that it has scope.
Two types of enum’s:
Only visible within the block it was declared.
Easier to change values – let the compiler do the work.
Named: enum greekType { ALPHA, BETA, GAMMA };
Unnamed: enum { HOMER, MARGE, BART, LISA };
Values start at zero, unless specified.
enum
enum
enum
enum
BYU CS 224
{ zero, one, two, three };
animals { cat=1, dog, cow=9, sheep, goat };
plants { grass, roses, cabbages, oaktree };
BOOLEAN { FALSE, TRUE };
The C Language
22
Variables
Variable Usage
Make your variable names meaningful
Common naming conventions
Hungarian notation (prefix hints)
UpperCamelCase / lowerCamelCase for most identifiers
#define TRUE 1
Names beginning with underscore are reserved for
compilers/libraries
last_variable_used, number_of_days
all-upper-case for constants
MyInputByte, buzzerCounter
Underscores
gVariable, hMyRoutine
__reserved, _Reserved
Encapsulate your variables
Avoid global variables - explicitly pass parameters to functions
Keep the scope as small as you can
BYU CS 224
The C Language
23
Operators
Operators and Expressions
Expressions are formed by combining variables with operators and
ALWAYS return a single value in C.
i = 5 * x + 100;
a = (a < b);
Operators
Assignment –
Arithmetic –
AND, OR, XOR, NOT, and shifts on Integers
equality, inequality, less-than, etc.
Logical –
C supports a rich set of
operators that allow the
programmer to
manipulate variables
Relational –
add, subtract, multiply, divide
Bitwise –
changes the values of variables
AND, OR, NOT on Booleans
Increment/Decrement
BYU CS 224
The C Language
24
Operators
The Assignment Operator
The operator symbol is the equal sign
The expression on the right-hand side is evaluated and
assigned to the left-hand variable
{
int x = 9;
sub.w #2,sp
mov.w #9,0(sp)
Stack
x = x + 4;
add.w #4,0(sp)
add.w #2,sp
sp
sp
}
BYU CS 224
The C Language
X
0x05fa
0x05fc
0x05fe
0x05f0
...
0x0600
25
Operators
Arithmetic / Relational Operators
Add (+), subtract (–), multiply (*), divide (/)
Integer; 5/3 = 1 (truncated to int)
Floating point : 5.0 / 3.0 = 1.66666666
Integer; remainder after integer division; 5 % 3 = 2
Relational operators return Boolean values:
x – y
x * y
x / y
Modulus (%)
x + y
Arithmetic Operators
x % y
0 if relation is FALSE
1 if relation is TRUE
Comparisons
x == y
x != y
x<y
x <= y
x>y
x >= y
BYU CS 224
equality
inequality
less-than
less-than-or-equal
greater-than
greater-than-or-equal
The C Language
26
Operators
Bitwise Operators
Perform bitwise logical operations across individual bits of a value.
AND
&
OR
|
XOR
^
NOT
~
(1’s complement)
Shifts are bitwise operators
SHIFT LEFT <<
SHIFT RIGHT >>
x << y
x >> y
BYU CS 224
x
y
x & y
x | y
x ^ y
~x
:
:
:
:
:
:
1
1
1
1
0
0
0
1
0
1
1
1
1
0
0
1
1
0
0
0
0
0
0
1
(binary)
(binary)
(binary)
(binary)
(binary)
(binary)
shift x y-places to the left (add zeros)
shift x y-places to the right (sign extend)
The C Language
27
Operators
Logical Operators
Logical operators evaluate to Boolean
AND
OR
NOT
&&
||
!
10 && 20 1
10 && 0 0
Don’t confuse with Bitwise operators
Operate on Boolean inputs and produce Boolean outputs
Boolean inputs (how values are interpreted):
Value not equal to zero TRUE
Value equal to zero FALSE
if( 'a' <= x <= 'z' ) statement; // wrong!
if(('a' <= x) && (x <= 'z')) statement;
if(!x) statement;
if(x == 0) statement;
if(x) statement;
if(x != 0) statement;
BYU CS 224
The C Language
Same
Same
28
Expressions
Order of Evaluation
Variable Coercion
When executing expressions of mixed types, C automatically
converts integer to floating point and back again as needed.
int x = 1;
x = x + 4.3;
x is declared an integer
integer + floating point ??
(result is x = 5)
Avoid the use of forced data conversion as operators may yield
unanticipated results.
Order of expression evaluation:
Precedence – higher precedence operators evaluate first.
Associativity – operators of same precedence evaluate left to right
(with a few exceptions).
Parentheses override all other evaluation rules.
BYU CS 224
The C Language
29
Expressions
Operator Precedence/Associativity
OPERATORS
( )
[ ]
->
.
!
~
++
-+
*
& (type) sizeof
*
/
%
+
<<
>>
Bitwise
<
<=
>
>=
Relational
Unary
==
!=
Relational
&
Bitwise
^
Bitwise
|
Bitwise
&&
Logical
||
Logical
?:
= += -= *= /= %= &= ^= |= <<= >>=
,
BYU CS 224
The C Language
ASSOCIATIVITY
left to right
right to left
left to right
left to right
left to right
left to associate
right
operators
right
lefttotoleft.
right
left to right
left to right
left to right
left to right
left to right
right to left
right to left
left to right
30
Expressions
Combined Assignment Operators
Arithmetic and bitwise operators can be combined with
the assignment operator.
x
x
x
x
x
x
x
x
x
x
BYU CS 224
+= y;
-= y;
*= y;
/= y;
%= y;
&= y;
|= y;
^= y;
<<= y;
>>= y;
x
x
x
x
x
x
x
x
x
x
=
=
=
=
=
=
=
=
=
=
x
x
x
x
x
x
x
x
x
x
+ (y);
– (y);
* (y);
/ (y);
% (y);
& (y);
| (y);
^ (y);
<< (y);
>> (y);
The C Language
Note: All of the
expression on the
right is considered
parenthesized.
32
Static Variables
Static Variables
Static functions are local to a file.
Static variables are “global local” variables.
May be declared inside a C function.
Like local variables, a static variable cannot be referred from
outside the function.
However, unlike a local variable, the value continues to exist
even after the function exits.
The value comes back into scope when the function is called
again (value retained).
A function can pass out a pointer to a static variable and dereference the value.
static int sum = 0;
BYU CS 224
The C Language
33
Static Variables
Static Function Variables
What does this function do?
Does it compile? Work as intended?
How would you fix it?
int next_Factorial(void)
{
int
current
= 1;
static
int current
= 1;
int
next
= 1;
static
int
next = 1;
current *= next++;
return current;
} // next_Factorial
printf("%d ", next_Factorial());
printf("%d ", next_Factorial());
...
BYU CS 224
The C Language
34
Expressions
Conditional Expressions
Conditional expression
C multiplexor operation
Format: <boolean> ? <true expression> : <false expression>
Example:
x ? y : z
y
z
0
1
x
x?y:z
This expression returns the value of y if x != 0, otherwise it
returns the value of z
printf("%d dog%s", dogs, (dogs == 1) ? "" : "s");
BYU CS 224
The C Language
35
Exercise 7.3
What is the output?
1. main()
{
int i = 5,j = 10;
i = i &= j && 10;
printf("%d %d",i,j);
}
2. main()
{
int i = 4,j = 7;
j = j || i++ && printf("Hello");
printf("%d %d", i, j);
}
BYU CS 224
The C Language
36
Compilation Examples
C to Assembly – Example 1
SP
{
int
int
int
x =
y =
x
y
z
x
x
=
=
=
+
+
10;
20;
30;
4;
y - z;
SP
}
0x8696:
0x869a:
0x86a0:
0x86a6:
0x86ac:
0x86b0:
0x86b4:
0x86b6:
0x86ba:
0x86be:
BYU CS 224
8031
40B1
40B1
40B1
52A1
411F
512F
811F
4F81
5031
0006
000A 0000
0014 0002
001E 0004
0000
0002
0004
0002
0006
SUB.W
MOV.W
MOV.W
MOV.W
ADD.W
MOV.W
ADD.W
SUB.W
MOV.W
ADD.W
x0600
x05fe
0x001e (z) x05fc
0x0014 (y) x05fa
0x000a (x) x05f8
x05f6
Stack 0x000e (x)
#0x0006,SP
#0x000a,0x0000(SP)
#0x0014,0x0002(SP)
#0x001e,0x0004(SP)
#4,0x0000(SP)
0x0002(SP),R15
@SP,R15
0x0004(SP),R15
R15,0x0002(SP)
#0x0006,SP
The C Language
37
Compilation Examples
C to Assembly – Example 2
int main(int argc, char** argv)
{
unsigned int x = 7;
unsigned int y = 5;
unsigned int z;
z = x * y;
return 0;
}
SP
SP
x0600
x05fe
ret adr
x05fc
z
0x0005 (y) x05fa
0x0007 (x) x05f8
argv (r13) x05f6
argc (r12) x05f4
Stack
BYU CS 224
0x8040:
0x8044:
0x8048:
0x804c:
0x8052:
0x8058:
0x805c:
0x8060:
0x8064:
0x8068:
0x806a:
0x806e:
main:
8031 000A
4D81 0002
4C81 0000
40B1 0007 0004
40B1 0005 0006
411C 0004
411D 0006
12B0 80DA
4C81 0008
430C
5031 000A
4130
__mpyi:
0x80da: 430E
mpyi_add_loop:
0x80dc: C312
0x80de: 100C
0x80e0: 2801
0x80e2: 5D0E
shift_test_mpyi:
0x80e4: 5D0D
0x80e6: 930C
0x80e8: 23F9
0x80ea: 4E0C
0x80ec: 4130
The C Language
SUB.W
MOV.W
MOV.W
MOV.W
MOV.W
MOV.W
MOV.W
CALL
MOV.W
CLR.W
ADD.W
RET
#0x000a,SP
R13,0x0002(SP)
R12,0x0000(SP)
#0x0007,0x0004(SP)
#0x0005,0x0006(SP)
0x0004(SP),R12
0x0006(SP),R13
#__mpyi
R12,0x0008(SP)
R12
#0x000a,SP
CLR.W
R14
CLRC
RRC
JLO
ADD.W
R12
(shift_test_mpyi)
R13,R14
RLA.W
TST.W
JNE
MOV.W
RET
R13
R12
(mpyi_add_loop)
R14,R12
38
Exercise 7.4
int inGlobal;
void main(void)
{
int outLocal;
int inLocalA;
int inLocalB;
outLocal = inGobal + inLocalA + inLocalB;
return;
}
Symbol
Table
Identifier
Type
main:
SUB.W
MOV.W
ADD.W
ADD.W
MOV.W
ADD.W
RET
#0x0006,SP
0x0002(SP),R15
&inGlobal,R15
0x0004(SP),R15
R15,0x0000(SP)
#0x0006,SP
Storage Class Offset
Scope
inGlobal
inLocalA
inLocalB
outLocal
BYU CS 224
The C Language
39
Review…
Activation Record – A block of memory on the stack that is created when a function is
called and contains all the local variables for a given invocation of a function.
Arithmetic Operator – Operator that returns a numerical value.
Associativity – The execution order of same precedence operators.
Bitwise Operator – Operator that performs bitwise logical operations.
Data type – Representation and valid operations of data object.
Expression – Combination of variables / operators that returns a single value.
Global (static) – Variable permanently assigned to a memory location.
Literal – An immutable data object.
Local (automatic) – Variable stored in a functions activation record.
Logical Operator – Operator that returns a logical (true/false) value.
Operator – Performs an operation on operand(s).
Scope - Extent of a variable/function’s availability in a program.
Precedence – The execution order of operators.
Variable - Symbolic name for a memory location that hold a value.
Variable Coercion – Forcing mixed data type variables to a common type.
Volatile – Variable modifier that prohibits optiminization by compiler.
BYU CS 224
The C Language
40
BYU CS 224
The C Language
41
Exercise 7.4
Fill in the resulting values for x, y, and z after evaluating the construct.
Assume for each row, x, y, and z are initialized to 10, 20, and 30 respectively.
x=10
1)
if (x = y) y = 100;
2)
3)
if (x < 10) y = 1;
else if (x < 20) y
else if (x < 30) y
switch ('a') {
case 'a': y++;
case 'b': --y;
}
4)
for (x=1; x<y; x++, y--) z = x + y;
5)
while (!z) {
z %= y;
}
6)
do {
y=20
z=30
= 5;
= 10;
z *= 5;
z /= 10;
x = --y;
z = x++;
}
while (z);
BYU CS 224
The C Language
42
Streaming I/O in C
C Stream I/O
C I/O
I/O facilities are not part of the C language itself
Most digital I/O handled directly by C program
#include "msp430.h"
SPR’s, Ports, A/D, transponder, switches, LED’s, etc
The ANSI standard defines a set of I/O library functions
for portability
Nonetheless, programs do interact with their environment!
Programs that confine their system interactions to facilities
provided by the standard library can be moved from one system
to another without change.
The properties of the C I/O library functions are specified
in header files
#include <stdio.h> (C standard library)
#include "RBX430_lcd.h"
BYU CS 224
The C Language
44
C Stream I/O
C Data Streams
C I/O is character based, using streams.
I/O streams must be opened / closed.
In standard C there are 3 streams automatically opened before
main() is called:
stdin is the input stream
stdout is the output stream
stderr stream for error messages
printf function outputs formatted values to stdout stream
The printf function requires a format string followed by optional
parameters:
printf( "format string...", parameters... );
The format string contains two object types:
BYU CS 224
Ordinary characters that are copied to the output stream
Conversion specifications which cause conversion and printing of the
next argument in the argument list.
The C Language
45
C Stream I/O
Printf Output in C
String literal
printf( format_string, parameters )
Decimal
Integer
printf("Hello World");
printf("\n%d plus %d is %d", x, y, x+y);
printf("\nIn hex it is %x", x+y);
printf("\nHello, I am %s. ", myname);
Hex
printf("\nIn ascii, 65 is %c. ", 65);
Integer
Output:
Hello world Newline
5 plus 6 is 11
In hex it is b
Hello, I am Bambi.
In ascii, 65 is A.
BYU CS 224
The C Language
Character
String
46
LCD I/O
RBX430_lcd.h Prototypes
uint8 lcd_init(void);
void lcd_clear(void);
void lcd_backlight(uint8 backlight);
void lcd_volume(uint8 volume);
uint16 lcd_mode(int16 mode);
uint8 lcd_cursor(uint16 x, uint16 y);
uint16 lcd_printf(const char* fmt, ...);
uint8 lcd_image(const uint8* image, int16 x, int16 y);
uint8 lcd_bitImage(const uint8* image,
int16 x, int16 y, uint8 flag);
uint8 lcd_wordImage(const uint16* image,
int16 x, int16 y, uint8 flag);
uint8 lcd_blank(int16 x, int16 y, uint16 w, uint16 h);
uint8 lcd_point(int16 x, int16 y, uint8 flag);
void lcd_circle(int16 x, int16 y, uint16 r, uint8 pen);
void lcd_rectangle(int16 x, int16 y,
uint16 w, uint16 h, uint8 pen);
BYU CS 224
The C Language
47
LCD I/O
LCD – 160 x 160 x 5 Pixels
Y (0-159)
lcd_init();
lcd_clear();
// 5 x 8 pixel Characters
lcd_cursor(40, 60);
lcd_printf("Hello World!");
BYU CS 224
Hello World!
X (0-159)
The C Language
48
BYU CS 224
The C Language
49
© Copyright 2026 Paperzz