Lecture2 Introduction C Langugue
Lecture2 Introduction C Langugue
CPU: Week
ARM Cortex-M 4
Curriculum Memory
and Interfaces
Week
5-6
Real-time Week
Operating systems 10-12
Project Week
Laboratory for Smart Integrated Systems 15
Objectives
• Most C programs begin with a few statements that start with a # symbol
– #include: File Inclusion
– #define: Macro Substition
– #if : Conditional Inclusion
#include <stdio.h>
#define PI 3.14159
• #include “filename”
– to include the contents of other files during compilation
– #include “filename” is replaced by the contents of the file “filename”
• Often a line or two of this form appears at the beginning of each source file.
int main()
{
typedef enum {FALSE, TRUE} Boolean;
int buff[SIZE] = {1,3,5,2,4};
int i;
Boolean flag;
do {
flag = FALSE;
for (i=0; i<SIZE; ++i) {
if ( buff[i-1] < buff[i] ) {
swap(buff[i-1], buff[i]);
flag = TRUE;
}
}
} while ( flag == TRUE );
for (i=0; i<SIZE; i++)
printf(" %i,", buff[i]);
printf("\n");
}
#ifndef _COND_INCLUDE_H
#endif /* _COND_INCLUDE_H */
• The braces { and } are used to group declarations and statements together into a
compound statement or block, so they are syntactically equivalent to a single
statement.
• Braces around multiple statements after an if, else, while or for are another.
• Variables can actually be declared inside any block; but they are usually grouped
together at the start of the function.
• There is never a semicolon after the right brace that ends a block.
main function
Comment
function body
• By default variables are considered as signed unless the unsigned keyword is used with the exception of char.
• intx_t and uintx_t (ISO C99 standard) defined in stdint.h
int main()
{
int a;
char buff[80];
int sum;
sum = a + buff[0];
int main()
{
int a = 0;
float b = 0.0f;
int sum;
a = b; /* variable assignment */
a = 6; /* constant assignment */
• Constants and variables can be combined with arithmetic, logical, and relational
operators to form compound statements.
a = a + 1;
a = b * 10 + 2;
/* function prototype */
int a(void);
int a(void)
{
static int i = 10;
i++;
return(i);
}
int main()
{
int p, n;
int checkspace(f,c)
register int f;
register char c;
{
if (c == ' ')
f++;
return(f);
}
int main()
{
char buff[MAXLINE];
register int i;
int n,j=0;
n = getline(buff, MAXLINE);
for (i=0; i<n; i++)
j = checkspace(j,buff[i]);
printf("Number of spaces: %i\n", j);
}
n n
n+1 x main’s variables n+1 x
Pointer
n+2 y n+2 y
n+3 n+3
• a • a = n+1
function’s local
• b • b = n+2
copies
• •
int main() {
int x[8];
int i;
int *p; /* p is pointer to an int */
• An optional name called a structure tag may follow the word struct (as with
clock here). The tag can be used to refer to the structure.
struct { . . . } x, y, z;
is syntactically analogous to
int x, y, z;
timer.sec = 60;
• typedef is often used to define shorthand type names, for example, Keil often
include the header <stdint.h> in their programs, here is fragment:
/* exact-width signed integer types */
typedef signed char int8_t;
typedef signed short int int16_t;
typedef signed int int32_t;
typedef signed long long int int64_t;
• To make type identifiers stand out in a program the convention that names are
terminated with _t is sometimes adopted (t == type)
int main() {
typedef struct {
int hours;
int mins;
int secs;
} time;
time clock;
time *ptime; /* pointer to time struct */
http://dics.voicecontrol.ro/dicsEE-IP/chapter/Setting%20GPIO%20manually
int main(void)
{
unsigned char var1;
while(1)
{
var1 = var1 | 0x10; /* Set bit 4 (5th bit) of var1 */
delay();
var1 = var1 & 0xEF; /* Clear bit 4 (5th bit) of var1 */
}
return 0;
}
The following program toggles only bit 4 of var1 continuously without disturbing the rest of
the bits?
int main(void)
{
unsigned char var1;
while(1)
{
var1 = var1^0x10; /* Toggle bit 4 (5th bit) of var1 */
delay();
}
return 0;
}
The following program toggles only bit 4 of var1 continuously without disturbing the rest of
the bits?
while(1)
{
if (var1 & 0x20) /* check bit 5 (6th bit) of var1 */
var2 = 0x55; /* this statement is executed if bit 5 is a 1 */
else
var2 = 0xAA; /* this statement is executed if bit 5 is a 0 */
}
Line 24 sets bit 6 of the Reset and Clock Control APB2ENR register
(leaving the other bits unchanged).
#include <stdio.h>
int main()
{
int a;
#include <stdio.h>
int main()
{
int a;
int main()
{
unsigned int a;
#include <stdio.h>
int main()
{
int a;
unsigned int b;
int main()
{
int a;
unsigned int b;
do {
printf("Enter Positive Number: ");
scanf("%i", &a);
}
while (a < 0);
b = a;
}
int main()
{
char buff[SIZE];
unsigned int idx;
printf("%s\n", buff);
}
• The inner while loop starts at the last character of line (recall --n (pre)
decrements n before using the value), and scans backwards looking for the first
character that is not a space, tab or newline. The loop is broken when one is
found, or when n becomes negative (i.e. when the entire line has been scanned).
• The same is true of variables in other functions; for example the variables in
getline are completely unrelated to those in main.
• Each local variable comes into existence when the function is called, and
disappears when the function exits; for this reason the variables are known as
automatic variables.
/* globals */
char line[MAXLINE]; /* input line */
int main()
{
int len;
extern char line[]; /* extern declaration not strictly
necessary, but good practice */
• A minimal function is
– void dummy(void) { }
• Function definitions can occur in any order within the source file or be split over
multiple files, so long as no individual function is split.
int main()
{ … etc.
swap(&x,&y);
temp = *a; printf("x = %i, y = %i\n", x, y);
*a = *b; }
*b = temp;
}
– A tutorial introduction.
• http://www.deitel.com/Books/C/CHowtoProgram7e/tabid/3635/Default.aspx
– ‘C’ Resources
• http://www.deitel.com/ResourceCenters/Programming/C/tabid/199/Default.aspx