EMBEDDED SYSTEM
NETTI VINAY 21nu1a0479
Day1:-05/06/2024
C-LANGUAGE:
1. C is a general-purpose, procedural, high-level
programming language used in the development
of computer software and applications, system
programming, games, and more.
2. C language was developed by Dennis M.
Ritchie at the Bell Telephone Laboratories
in 1972.
3. It is a powerful and flexible language which
was first developed for the programming of
the UNIX operating System.
4. C is one of the most widely used
programming languages.
TOKENS IN C: A token in C can be defined as the
smallest individual element of the C programming
language that is meaningful to the compiler. It is the
basic component of a C program.
TYPES OF TOKENS IN C: The tokens of C language can
be classified into six types based on the functions
they are used to perform. The types of C tokens are
as follows:
1. Keywords
2. Identifiers
3. Constants
4. Strings
5. Special Symbols
6. Operators
KEYWORDS:
1. The keywords are pre-
defined or reserved words in a programming
language. Each keyword is meant to perform a
specific function in a program.
2. Since keywords are referred
names for a compiler, they can’t be used as
variable names because by doing so, we are
trying to assign a new meaning to the keyword
which is not allowed.
3. You cannot redefine
keywords. However, you can specify the text to
be substituted for keywords before compilation
by using C preprocessor directives.
C language supports 32 keywords which are given
below:
auto double int
struct
break else long
switch
case enum register
typedef
char extern return
union
const float short
unsigned
continue for signed
void
default goto sizeof
volatile
do if static
while
1. auto: This keyword declares an automatic
variable with a local scope.
Syntax: auto data _ type variable _name;
2. break: It is used to terminate the execution of
a loop or switch statement.
Syntax: break;
3. case: It is used in a switch
statement to define different cases.
Syntax: case constant _expression;
4. char: This keyword is used to declare a character
data type.
Syntax: char variable_ name;
5.const: It is used to declare constants, whose
values cannot be modified.
Syntax: const data _type constant _name = value;
6.continue: It is used to skip the remaining
statements in a loop and continue with the next
iteration.
Syntax: continue;
7.default: It is used in a switch statement as
the default case when no other cases match.
Syntax: default:
8.do: It is used to create a do-while loop, which
executes a block of code repeatedly until
a condition is met.
Syntax: do {
// code to be executed
} while (condition);
9.double: This keyword is used to declare
a double-precision floating-point data type.
Syntax: double variable_ name;
10.else: It is used in an if statement to specify
the block of code to be executed when the condition
is false.
Syntax:
if (condition) {
// code to be executed if the condition is true
} else {
// code to be executed if the condition is false
}
11.enum: It is used to define
an enumeration, which is a set of named
values.
Syntax: enum enum_name
Value1,
Value2,
//….
}
12.extern: It is used to declare
a variable or function that is defined in another
file or external to the current scope.
Syntax: extern data _type variable _name;
13.float: This keyword is used to declare a single-
precision floating-point data type.
Syntax: float variable_name;
14.for: It is used to create a for loop, which
repeatedly executes a block of code based on a
specified condition.
Syntax:
for (initialization; condition; increment/decrement) {
// code to be executed
}
15. goto: It is used to transfer control to a labeled
statement within the same function.
Syntax :goto label_name;
16. if: It is used to create an if statement to perform
a certain action based on a condition.
Syntax: if (condition) {
// code to be executed if the condition i
s true
}
17. int: This keyword is used to declare an integer
data type.
Syntax: int variable_name;
18. long: This keyword is used to declare a long
integer data type.
Syntax: long variable_name;
19.register: It is used to declare a register variable,
which suggests the compiler to store the variable
in a register for faster access.
Syntax: register data_type variable
20.return: It is used to exit a function and return a
value (if any) to the calling code.
Syntax:return expression;
21.short: This keyword is used to declare a short
integer data type.
Syntax:short variable_name;
22.signed: It is used to declare a signed data type,
which can represent both positive and negative
values.
Syntax:signed data_type variable_name;
23. sizeof: It is used to determine the size in
bytes of a data type or variable.
Syntax:sizeof(data_type); or sizeof(variable)
;
24. static: It is used to declare
a variable or function that retains its value or scope
even after the block in which it is defined has exited.
Syntax:static data_type variable_name;
static return_type function_name(ar
guments);
25.struct: It is used to define a user-defined data
type called a structure, which can hold multiple
variables of different data types.
Syntax:struct struct_name {
data_type member1;
data_type member2;
//...
};
26.switch: It is used to create a switch statement,
which allows multiple execution paths based on
different cases.
Syntax:
switch (expression) {
case constant1:
// code to be executed if expression matches c
onstant1
break;
case constant2:
// code to be executed if expression matches c
onstant2
break;
//...
default:
// code to be executed if expression do
es not match any constant
}
27.typedef: It is used to create a new name
(alias) for an existing data type.
Syntax:typedef existing_data_type new_data_type
28.union: It is used to define a user-defined data
type called a union, which can hold different data
types but only one member at a time.
29.unsigned: It is used to declare an unsigned data
type, which can represent only positive
Syntax:unsigned data_type variable_name;
30.void: This keyword is used to indicate
the absence of a specific type or to define functions
that do not return a value.
Syntax:For function return type: void function_name(
arguments);
As a data type: void variable_name;
31.volatile: It is used to declare a variable that can
be modified externally and should not be optimized
by the compiler.
Syntax:volatile data_type variable_name;
32.while: It It is used to create a while loop, which
repeatedly executes a block of code based on
a specified condition.
is uSyntax:
while (condition) {
// code to be executed
}
2. C Token – Identifiers:
1. Identifiers are used as the general
terminology for the naming of variables,
functions, and arrays.
2. These are user-defined names consisting of
an arbitrarily long sequence of letters and digits
with either a letter or the underscore(_) as a first
character.
3. Identifier names must differ in spelling and
case from any keywords.
4. You cannot use keywords as identifiers; they
are reserved for special use. Once declared, you
can use the identifier in later program
statements to refer to the associated value.
5. A special identifier called a statement label
can be used in goto statements.
Rules for Naming Identifiers
Certain rules should be followed while naming c
identifiers which are as follows:
They must begin with a letter or underscore(_).
They must consist of only letters, digits, or
underscore. No other special character is
allowed.
It should not be a keyword.
It must not contain white space.
It should be up to 31 characters long as only
the first 31 characters are significant.
Note: Identifiers are case-sensitive.
3. Constants:
The constants refer to the variables with
fixed values.
4.Strings:
Strings are nothing but an array of characters ended
with a null character (‘\0’).This null character
indicates char the end of the string.
Examples of String
string[20] = {‘g’, ’e’, ‘e’, ‘k’, ‘s’, ‘f’, ‘o’, ‘r’, ‘g’, ’e’, ‘e’,
‘k’, ‘s’, ‘\0’};
char string[20] = “geeks for geeks”;
char string [] = “geeks for geeks”;
C Token – Special Symbol
Parentheses (): These special symbols are used
to indicate function calls and function
parameters.
Braces {}: These opening and ending curly
braces mark the start and end of a block of
code containing more than one executable
statement.
Comma ( , ): It is used to separate more than
one statement like for separating parameters
in function calls.
Colon (:): It is an operator that essentially
invokes something called an initialization list.
Semicolon (;): It is known as a statement
terminator. It indicates the end of one logical
entity. That’s why each individual statement
must be ended with a semicolon.
Asterisk (*): It is used to create a pointer
variable and for the multiplication of variables.
Assignment operator (=): It is used to assign
values and for logical operation validation.
Pre-processor (#): The preprocessor is a macro
processor that is used automatically by the
compiler to transform your program before
actual compilation.
Period (.): Used to access members of a
structure or union.
Tilde (~): Bitwise One’s Complement Operator
Operators in C: Operators are the symbols that
perform a specific operation to be performed on the
operands. C supports various operators like
arithmetic, relational, logical, assignment, etc.
Types of operators:
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Bitwise Operators
5. Assignment Operators
6. Other Operators
1.Arithmetic Operators :
Arithmetic Operators are the type of operators in C
that are used to perform mathematical operations in
a C program. They can be used in programs to define
expressions and mathematical formulas.
Operator Name of the Arithmetic Syntax
Operator Operation
+ Addition Add two x+y
operands.
– Subtraction Subtract x–y
the second
operand
from the
first
operand.
* Multiplication Multiply x*y
two
operands.
/ Division Divide the x/y
first
operand
by the
second
operand.
% Modulus Calculate x%y
the
remainder
when the
first
operand is
divided by
the second
operand.
2.Relational Operators:
1. In C, relational operators are the symbols
that are used for comparison between two
values to understand the type of relationship a
pair of numbers shares.
2. The result that we get after the relational
operation is a boolean value, that tells whether
the comparison is true or false.
3. Relational operators are mainly used in
conditional statements and loops to check the
conditions in C programming.
3.Logical Operators:
Logical operators in C are used to combine multiple
conditions/constraints. Logical Operators returns
either 0 or 1, it depends on whether the expression
result is true or false. In C programming for decision-
making, we use logical operators.
We have 3 logical operators in the C language:
1. Logical AND ( && )
2. Logical OR ( || )
3. Logical NOT ( ! )
1.Logical AND Operator (&&):If both operands are
non zero then the condition becomes true.
Otherwise, the result has a value of 0. The return
type of the result is int. Below is the truth table for
the logical AND operator.
Y X &&
X Y
1 1 1
1 0 0
0 1 0
0 0 0
2. Logical OR Operator ( || ):The condition becomes
true if any one of them is non-zero. Otherwise, it
returns false i.e., 0 as the value. Below is the truth
table for the logical OR operator.
Y X ||
X Y
1 1
1
1 0
1
0 1
1
0 0
0
3. Logical NOT Operator ( ! )If the condition is true
then the logical NOT operator will make it false and
vice-versa. Below is the truth table for the logical
NOT operator.
!X
X
0 1
X !X
1 0
4. Assignment Operators in C:
Assignment operators are used to assign value to a
variable. The left side operand of the assignment
operator is a variable and the right side operand of
the assignment operator is a value. The value on the
right side must be of the same data type as the
variable on the left side otherwise the compiler will
raise an error.
S. No. Symbol Operator Description Syntax
1 = Simple Assign the a = b
Assignment value of the
right
operand to
the left
operand.
2 += Plus and Add the a += b
assign right
operand
and left
operand
and assign
this value to
the left
operand.
3 -= Minus and Subtract a -= b
assign the right
operand
and left
operand
and assign
this value to
the left
operand.
Bitwise Operators in C:
In C, the following 6 operators are bitwise operators
(also known as bit operators as they work at the bit-
level). They are used to perform bitwise operations in
C.
1.The & (bitwise AND) in C takes two numbers as
operands and does AND on every bit of two
numbers. The result of AND is 1 only if both bits are
1.
2.The | (bitwise OR) in C takes two numbers as
operands and does OR on every bit of two numbers.
The result of OR is 1 if any of the two bits is 1.
3.The ^ (bitwise XOR) in C takes two numbers as
operands and does XOR on every bit of two numbers.
The result of XOR is 1 if the two bits are different.
4.The ~ (bitwise NOT) in C takes one number and
inverts all bits of it.
VARIABLES:
A variable in C is a memory location with some name
that helps store some form of data and retrieves it
when required. We can store different types of data
in the variable and reuse the same variable for
storing some other data any number of times.
Variable Syntax:
data_type variable_name =
value;
There are 3 aspects of defining a variable:
1.Variable Declaration
2.Variable Definition
3.Variable Initialization
1. Variable Declaration:
Variable declaration in C tells the compiler about the
existence of the variable with the given name and
data type.When the variable is declared compiler
automatically allocates the memory for it.
2. Variable Definition:
In the definition of a C variable, the compiler
allocates some memory and some value to it. A
defined variable will contain some random garbage
value till it is not initialized.
3. Variable Initialization:
Initialization of a variable is the process where the
user assigns some meaningful value to the variable.
Variable Types:
The C variables can be classified into the following
types:
1. Local Variables
2. Global Variables
3. Static Variables
4. Automatic Variables
5. Extern Variables
6. Register Variables
1. Local Variables :
A Local variable in C is a variable that is declared
inside a function or a block of code. Its scope is
limited to the block or function in which it is
declared.
2. Global Variables :
A Global variable in C is a variable that is declared
outside the function or a block of code. Its scope is
the whole program i.e. we can access the global
variable anywhere in the C program after it is
declared.
3. Static Variables :
A static variable in C is a variable that is defined
using the static keyword. It can be defined only once
in a C program and its scope depends upon the
region where it is declared (can be global or local).
Syntax of Static Variable :
static data_type variable_name = initial_value;
4. Automatic Variables :
All the local variables are automatic variables by
default. They are also known as auto variables.
Syntax of Auto Variables:
auto data_type variable_name;
5. External Variables :
External variables in C can be shared between
multiple C files. We can declare an external variable
using the extern keyword.
Syntax of Extern Variables :
extern data_type variable_name;
6. Register Variables in C:
Register variables in C are those variables that are
stored in the CPU register instead of the
conventional storage place like RAM. Their scope is
local and exists till the end of the block or a
function.
These variables are declared using the register
keyword.
The default value of register variables is a garbage
value.
Syntax of Register Variables:
register data_type variable_name =
initial_value;
C PROGRAMS:
1.WRITE A C PROGRAM TO PRINT A MESSAGE.
#include <stdio.h>
int main()
{
printf("Embedded systems");
return 0;
}
OUTPUT:
Embedded systems
2.WRITE A C PROGRAM TO TAKE TWO VALUES FROM
USER ADD THE TWO VALUES.
#include <stdio.h>
int main()
{
int a,b;
scanf("%d %d",&a,&b);
printf("%d",a+b);
return 0;
}
OUTPUT:
6
7
13
3.WRITE A C PROGRAM TO CALCULATE PERCENTAGE
OF 5 SUBJECTS,EACH SUBJECT MARKS 100.
#include <stdio.h>
int main() {
float a,b,c,d,e,total_marks,percentage;
scanf("%f %f %f %f %f",&a,&b,&c,&d,&e);
total_marks=a+b+c+d+e;
percentage=((total_marks)/500)*100;
printf("%.2f",percentage);
return 0;
}
OUTPUT:
60 70 80 90 70
74.00.
4.WRITE C PROGRAM FOR INTEREST OF 1LAKH FOR
2YEARS,2 PERCENTAGE.
#include <stdio.h>
int main() {
int p,t,r,total;
scanf("%d %d %d",&p,&t,&r);
total=(p*t*r)/100;
printf("%d",total);
return 0;
}
OUTPUT:
100000 24 2
48000
5.WRITE A C PROGRAM TO CONVERT TEMPERATURE
FROM CELCIUS TO FAHRENHEIT,PRINT THE VALUE.
#include <stdio.h>
int main() {
int c,f;
scanf("%d",&c);
f=((9*c)/5)+32;
printf("%d",f);
return 0;
}
OUTPUT:
25
77
6.WRITE C PROGRAM TO CALCULATE THE RECTANGLE
AREA.
#include <stdio.h>
int main() {
int L,b,a;
scanf("%d %d",&L,&b);
a=L*b;
printf("%d",a);
return 0;
}
OUTPUT:
42
8
7.WRITE A C PROGRAM SWAPPING OF 2 NUMBERS.
#include <stdio.h>
int main() {
int n,m,temp;
scanf("%d %d",&n,&m);
temp=n;
n=m;
m=temp;
printf("%d %d",n,m);
return 0;
}
OUTPUT:
45
54
8.WRITE A C PROGRAM TO CHECK EVEN OR ODD.
#include <stdio.h>
int main() {
int a;
scanf("%d",&a);
if(a%2==0)
{
printf("even");
}
else
{
printf("odd");
}
}
OUTPUT:
Even
9.Write a c program to find out person age into the
following categories if it is
1. 0 to 12 child,
2. 13 to 19 teeneger,
3. 20 to 59 adult
4. more than 60 senior.
#include <stdio.h>
int main() {
int n;
printf("Enter the age:");
scanf("%d",&n);
if(n>=0&&n<=12)
{
printf("child");
}
if(n>=13&&n<=19)
{
printf("teenager");
}
if(n>=20&&n<=59)
{
printf("adult");
}
if(n>60)
{
printf("senior");
}
}
OUTPUT:
Enter the age:71
Senior
10. Write a C program to find out the weather
condition the following categories
1. Below 0 – freezing
2. 0 to 10 – very cold
3. 11 to 20 – cold
4. 21 to 30 – warm
5. Above 30- hot
#include <stdio.h>
int main() {
int n;
printf("Enter the number:");
scanf("%d",&n);
if(n<0)
{
printf("freezing");
}
if(n>=0&&n<=10)
{
printf("very cold");
}
if(n>=11&&n<=20)
{
printf("cold");
}
if(n>=21&&n<=30)
{
printf("warm");
}
if(n>30)
{
printf("hot");
}
}
OUTPUT:
Enter the number:28
warm
Data Types in C
Each variable in C has an associated data type. It
specifies the type of data that the variable can store
like integer, character, floating, double, etc. Each
data type requires different amounts of memory and
has some specific operations which can be
performed over it. The data type is a collection of
data with values having fixed values, meaning as well
as its characteristics.
Size Format
Data (bytes Specifie
Type ) Range r
short
2 -32,768 to 32,767 %hd
int
unsign
ed
2 0 to 65,535 %hu
short
int
Size Format
Data (bytes Specifie
Type ) Range r
unsign
4 0 to 4,294,967,295 %u
ed int
-2,147,483,648 to
int 4 %d
2,147,483,647
long -2,147,483,648 to
4 %ld
int 2,147,483,647
unsign
ed long 4 0 to 4,294,967,295 %lu
int
long
long 8 -(2^63) to (2^63)-1 %lld
int
Size Format
Data (bytes Specifie
Type ) Range r
unsign
0 to
ed long
8 18,446,744,073,709,551, %llu
long
615
int
signed
1 -128 to 127 %c
char
unsign
ed 1 0 to 255 %c
char
float 4 %f
1.2E-38 to 3.4E+38
double 8 %lf
1.7E-308 to 1.7E+308
long 16 3.4E-4932 to 1.1E+4932 %Lf
Size Format
Data (bytes Specifie
Type ) Range r
double
1. if in C:
The if statement is the most simple decision-making
statement. It is used to decide whether a certain
statement or block of statements will be executed or
not i.e if a certain condition is true then a block of
statements is executed otherwise not.
Syntax of if Statement:
if(condition)
{
// Statements to execute if
// condition is true
2. if-else in C:
The if statement alone tells us that if a condition is
true it will execute a block of statements and if the
condition is false it won’t. But what if we want to do
something else when the condition is false? Here
comes the C else statement. We can use
the else statement with the if statement to execute
a block of code when the condition is false. The if-
else statement consists of two blocks, one for false
expression and one for true expression.
Syntax of if else in C:
if (condition)
{
// Executes this block if
// condition is true
}
else
{
// Executes this block if
// condition is false
}
3. Nested if-else in C:
A nested if in C is an if statement that is the target of
another if statement. Nested if statements mean an
if statement inside another if statement. Yes, C allow
us to nested if statements within if statements, i.e,
we can place an if statement inside another if
statement.
Syntax of Nested if-else
if (condition1)
{
// Executes when condition1 is true
if (condition2)
{
// Executes when condition2 is true
}
else
{
// Executes when condition2 is false
}
4. if-else-if Ladder in C:
The if else if statements are used when the user has
to decide among multiple options. The C if
statements are executed from the top down. As
soon as one of the conditions controlling the if is
true, the statement associated with that if is
executed, and the rest of the C else-if ladder is
bypassed. If none of the conditions is true, then the
final else statement will be executed. if-else-if ladder
is similar to the switch statement.
Syntax of if-else-if Ladder:
if (condition)
statement;
else if (condition)
statement;
.
.
else
statement;
5. switch Statement in C:
The switch case statement is an alternative to the if
else if ladder that can be used to execute the
conditional code based on the value of the variable
specified in the switch statement. The switch block
consists of cases to be executed based on the value
of the switch variable.
Syntax of switch
switch (expression) {
case value1:
statements;
case value2:
statements;
....
....
....
default:
statements;
}
for Loop :
for loop in C programming is a repetition control
structure that allows programmers to write a loop
that will be executed a specific number of times. for
loop enables programmers to perform n number of
steps together in a single line.
Syntax:
for (initialize expression; test
expression; update expression)
{
//
// body of for loop
//
}