Professional Documents
Culture Documents
1-Progarmming in C
1-Progarmming in C
MICROCONTROLLER
INTRODUCTION
History of C language
It is a structured, high-level, machine independent
language.
I see,
I remember!
The information contained between the parentheses is called the
argument of the function.
This argument of the first Printf function is I see, /n’’ and the
second is I remember!”.
These argument are simply strings of character to be printed out.
printf contains a combination of two characters \ and n at the end
of the string.
\n collectively called the newline character and instruct the
computer to go to the next (new) line.
No space is allowed between \ and n.
Contd…
Possible to produce two or more lines of output by one printf
statement with the use of newline character at appropriate
places. e.g. printf(“I see,\n I remember !”);
Will print
I see,
I remember !
while the statement
printf(“I\n.. See, \n… …. ….I\n… … … …
remember !”);
will print
I
.. See,
………I
… … … … remember !
Contd…
Some authors recommend the inclusion of the statement
#include<stdio.h>
At the beginning of all programs that use any input/output
library function
This is not necessary for the function printf and scanf
which have been defined as a part of C language.
• C makes a distinction between uppercase and lowercase
letters.
• printf and PRINTF are not the same.
• In C everything is written in lowercase letter.
• Uppercase letters are used for symbolic names
representing constants.
Contd..
General format of simple program and all C program
need a main function.
The main function()
C permits different forms of main statement.
Following forms are allowed
main()
int main()
void main()
main(void)
void main(void)
int main(void)
The empty pair of parenthesis indicates that the function has no arguments.
Keyword void means that the function does not return any information to
the operating system
int means that the function returns an integer value to the operating system
When int is specified, the last statement in the program must be “return 0”
C standard library
C program using the if statement
C program using the if statement
C program using loop
C program for implementing conditional loop
While(1)
For(;;);
Output
100
106.10
Contd…
number and amount are variable name and what types
of data they hold.
int number;
float amount;
Tells the compiler that number is an integer(int) and
amount is a floating(float) point number.
The word int and float are called the keywords and
can not used as a variable name.
number=100;
amount=30.75+75.35;
are called the assignment statements and have a
semicolon at the end.
Contd…
Output statement that prints the value of number.
The print statement
printf(“%d\n”,number);
Contains two argument. The first argument “%d” tells
the compiler that the value of second argument
number should be printed as decimal integer.
Printf(“%5.2f”,amount); prints out the value of amount
in floating point format.%5.2f tells the compiler that
the output must be in floating point with five places in
all and two places to the right of the decimal point.
Contd…
A # define instructions defines value to a symbolic constant for use
in the program.
Whenever a symbolic name is encountered, the compiler
substitutes the value associated with the name automatically.
To change the value, we have to simply change the definition.
These values remain constant throughout the execution of the
program.
A # define lines should not end with a semicolon.
A # define instructions are usually placed at the beginning before
the main() function.
The defined constants are not variables.
We may not change their values within the program by using an
assignment statement.
Ex. PRINCIPAL= 10000.00;
Is illegal
Contd…
The mul function multiplies the values of x and y and the result is
returned to the main() function when it is called in the
statement.
c=mul(a, b);
The mul() has two argument x and y that are declared as integer.
The value a and b are passed on to x and y respectively when the
function mul() is called.
The #include Directive
C programs are divided into modules or functions.
some function are written by user and store in the C
library.
Library functions are grouped category wise and stored in
different files known as header files.
To access the functions stored in library, necessary to tell
the compiler about the files to be accessed.
#include<filename>
• file name is the name of library file that contains the
required function definition.
• Preprocessor directives are placed at the beginning of a
program.
BASIC STRUCTURE OF C PROGRAMS
Any C program is consists of 6 main
sections.
C program can be viewed as a group of
building blocks called functions.
A function is a subroutine that may include
one or more statements designed to perform
a specific task.
Contd…
Contd…
Contd…
Various section and Use
Documentation: Consists of comments, some
description of the program, programmer name and
any other useful points that can be referenced later.
Link: Consists of the header files of the functions
that are used in the program. Provides instruction to
the compiler to link function from the library
function.
Definition: Consists of symbolic constants or macros
Global declaration: Can be used anywhere in the
program. Consists of function declaration and global
variables.
Contd…
main( ){ }: Every C program must have a main()
function which is the starting point of the program
execution and contains two parts, declaration and
executable part. The declaration part declares all the
variables that are used in executable part. Must be
written in between the opening and closing braces end
with a semicolon (;).
variable declaration: In C programming we cannot
create variable anywhere like c++ or java so need to
declare variable before body of program.
Subprograms or function: User defined functions
that are used to perform a specific task. These user
defined functions are called in the main() function.
Contd…
Different sections of the above code
Documentation:/*This section contains a multi line
comment or name of program. description: program to
call function add(); */
In C, we can create single line comment using two forward
slash // and we can create multi line comment using /*
*/. Comments are ignored by the compiler and is used to
write notes and document code.
Link:This section includes header file.
#include <stdio.h>
#include<conio.h>
We are including the stdio.h input/output header file from
the C library.
Contd…
Definition: This section contains constant.
#define max 10
In the above code we have created a constant max and
assigned 10 to it.
The #define is a preprocessor compiler directive which is used
to create constants. We generally use uppercase letters to
create constants.
The #define is not a statement and must not end with a ;
semicolon.
Global Declaration: This section contains function
declaration.
void add();
int x=10;
We have declared an add function which print Hello add .
main( ) function
Contd…
This section contains the main() function.
int main(){
int a=10;
printf("Hello Main");
return 0;
}
This is the main() function of the code. Inside this function we
have created a integer variable a and assigned 10 to it.
Then we have called the printf() function to print Hello Main.
Subprograms or Function
This section contains a subprogram or function i.e add( ) or
any no of user defined function .
void add() {
void add(){
printf(" Hello add ") ;
}
Character set of C
Character:-It denotes any alphabet, digit or special symbol
used to represent information.
Use:-These characters can be combined to form variables. C
uses constants, variables, operators, keywords and
expressions as building blocks to form a Basic c program
Character set:-The character set is the fundamental raw
material of any language and they are used to represent
information.
Contd…
The characters in C are grouped into the
following two categories:
1. Source character set
a. Alphabets
b. Digits
c. Special Characters
d. White Spaces
2. Execution character set
a. Escape Sequence
Source character set
1. ALPHABETS
Uppercase letters A-Z
Lowercase letters a-z
2. DIGITS 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Contd…
3.Special Characters
~ tilde
%percent sign
| vertical bar
@ at symbol
+ plus sign
< less than
& ampersand
$ dollar sign
* asterisk \ back slash ,etc.
Contd…
4.White space Characters.
\b blank space
\t horizontal tab
\v vertical tab
\r carriage return
\f form feed
\n new line
\\ Back slash
\’ Single quote
\" Double quote
\? Question mark
\0 Null
\a Alarm (Audible Note)
Contd…
Execution Character Set:
Execution characters set are always represented by a
backslash (\) followed by a character.
They are used in output statements. Escape sequence
usually consists of a backslash and a letter or a
combination of digits.
Each one of character constants represents one
character, although they consist of two characters.
These characters combinations are called as escape
sequence.
Contd…
Character ASCII value Escape Sequence Result
Alarm (bell) 007 \a Beep Sound
Back space 008 \b Moves previous position
Horizontal tab 009 \t Moves next horizontal tab
New line 010 \n Moves next Line
Vertical tab 011 \v Moves next vertical tab
Form feed 012 \f Moves initial position of next page
Carriage return 013 \r Moves beginning of the line
Double quote 034 \" Present Double quotes
Single quote 039 \' Present Apostrophe
Question mark 063 \? Present Question Mark
Back slash 092 \\ Present back slash
C program to print all the characters of C character Set
#include<stdio.h>
#include<conio.h>
int main()
{
int i;
clrscr();
printf("ASCII ==> Character\n");
for(i = -128; i <= 127; i++)
printf("%d ==> %c\n", i, i);
getch();
return 0;
}
Tokens in C
In a C program smallest individual units are known as C tokens
A C program consists of various tokens
Token is either a keyword, an identifier, a constant, a string literal, or a
symbol.
For example, the following C statement consists of six type of tokens −
C TOKENS EXAMPLE PROGRAM:
int main()
{
int x, y, total;
x = 10, y = 20;
total = x + y;
printf ("Total = %d \n", total);
}
where,
main – identifier
{,}, (,) – delimiter
int – keyword
x, y, total – identifier
main, {, }, (, ), int, x, y, total – tokens
Keywords:
Keyword and Every c word is classified as either a keyword or an identifier.
Identifiers
All keywords have fixed meanings and these meaning can not be
changed.
Keywords are predefined, reserved words used in programming that have
special meanings to the compiler.
Keywords serve as basic building block for program statements.
All keywords must be written in lowercase.
Underscore character is also an identifier and used as a link between two
words in long identifiers.
ANSI C Keyword
Contd…
Identifier:
A C identifier is a name used to identify a variable, function,
or any other user-defined item.
An identifier starts with a letter A to Z, a to z, or an
underscore '_' followed by zero or more letters, and digits (0
to 9).
C does not allow punctuation characters such as @, $, and %
within identifiers.
C is a case-sensitive programming language. Thus, Manpower
and manpower are two different identifiers in C.
Here are some examples of acceptable identifiers −
mohd zara abc move_name
myname50 _temp j a23b9
retVal a_123
Constants:
Constant in c refer to fixed values that do not change
during execution of a program.
Constants are like a variable
Value remains the same during the entire execution of
the program.
Constants are also called literals.
Constants can be any of the data types.
It is considered best practice to define constants using
only upper-case names.
Syntax:
const type constant_name;
Basic types of C constant:
Primary Constants
Numeric Constants
Integer Constants
Real Constants
Character Constants
Single Character Constants
String Constants
Backslash Character Constants
Contd…
Contd…
Given below is the list of special characters and their
purpose.
By “const”
HOW keyword:INwhen
TO USE CONSTANTS you try to change constant values after defining
A C PROGRAM? in C
program, it will through error.
EXAMPLE PROGRAM USING CONST KEYWORD IN C:
#include<stdio.h>
OUTPUT:
void main() value of height : 100
{ value of number : 3.140000
value of letter : A
const int height = 100; /*int constant*/ value of letter_sequence : ABC
const float number = 3.14; /*Real constant*/ value of backslash_char : ?
const char letter = 'A'; /*char constant*/
const char letter_sequence[10] = "ABC"; /*string constant*/
const char backslash_char = '\?'; /*special char const*/
printf("value of height :%d \n", height );
printf("value of number : %f \n", number );
printf("value of letter : %c \n", letter );
printf("value of letter_sequence : %s \n", letter_sequence);
printf("value of backslash_char : %c \n", backslash_char);
}
#include <stdio.h>
EXAMPLE PROGRAM USING #DEFINE PREPROCESSOR DIRECTIVE IN C:
First_tag Valid
In above program first increase the value of i and then used value of i into
expression.
Contd…post-increment
In post-increment first(variable ++)
value of variable is used in the expression
(initialize into another variable) and then increment the value of
variable.
Syntax: variable ++;
#include<stdio.h>
#include<conio.h> Output:
int main() x: 10
{ i: 11
int x,i;
i=10;
x=i++;
printf("x: %d\n",x);
printf("i: %d\n",i);
getch();
}
In above program first used the value of i into expression then increase value of i
by 1.
Decrement Operator
Type of Decrement Operator
pre-decrement
post-decrement
Pre-decrement (-- variable)
In pre-decrement first decrement the value of variable and then used inside the
expression (initialize into another variable).
Syntax: -- variable;
#include<stdio.h>
#include<conio.h> Output:
int main() x: 9
{ i: 9
int x,i;
i=10;
x=--i;
printf("x: %d\n",x);
printf("i: %d\n",i);
getch();
}
In above program first decrease the value of i and then value of i used in expression.
Contd…post-decrement (variable --)
In Post-decrement first value of variable is used in the expression
(initialize into another variable) and then decrement the value of
variable.
Syntax: variable --;
#include<stdio.h>
Output:
#include<conio.h> x: 10
int main() i: 9
{
int x,i;
i=10;
x=i--;
printf("x: %d\n",x);
printf("i: %d\n",i);
getch();
}
In above program first used the value of x in expression then decrease value of i
by 1.
#include<stdio.h>
Example of increment and decrement operator
#include<conio.h>
int main() Output:
x: 0
{
int x,a,b,c;
a = 2;
b = 4;
c = 5;
x = a-- + b++ - ++c;
printf("x: %d",x);
getch();
}
Programming 8051 microcontroller in C
Programming Languages
C Programming
Data types in C
Software compiler for microcontroller
Use of Keil Microvision
IDE of Keil Microvision
First C Program
Using 8051 I/O ports
Create a project in keil microvision for 8051 microcontroller
Create a project in keil microvision for 8051 microcontroller
First we create a new file:
Create a project in keil microvision for 8051 microcontroller