0% found this document useful (0 votes)
16 views

data types, functions

Uploaded by

Athulya B.S
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)
16 views

data types, functions

Uploaded by

Athulya B.S
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/ 6

Introduction to Data Types

• Definition: Data types define the type of data a variable can hold.

• Importance in embedded systems: Efficient use of memory and optimized performance.

Categories of Data Types

• Primary Data Types: char, int, float, double

• Derived Data Types: Arrays, Pointers, Structures

• Enumerated Types: enum

• Void: For functions with no return value.

Integer Types:

• int: Signed integer.

• unsigned int: Unsigned integer.

• Sizes: Typically 16 or 32 bits depending on architecture.

• Example: int count = 100;

Character Types:

• char: Represents a single character (ASCII).

• Size: 8 bits (1 byte).

• Example: char grade = 'A';

Floating-Point Types:

• float: Single precision floating-point.

• double: Double precision floating-point.

• Example: float pi = 3.14;

Integer Data Types with Modifiers:

Modifiers allow customization of the range and size of data types.

• Signed vs Unsigned:

o signed int: Can store both negative and positive values.

o unsigned int: Stores only positive values.

• Short and Long Modifiers:

o short int: Smaller range (usually 16 bits).

o long int: Larger range (usually 32 or 64 bits).

Example:

unsigned short int value = 65535;


long int distance = 100000L;

3. Floating-Point Precision:

• Float: Typically 4 bytes (32 bits).

• Double: Typically 8 bytes (64 bits).

• Use of floating-point numbers is often avoided in embedded systems for performance


reasons.

Example:

double largeNum = 123456.789;

Page 2 - Side 1: Derived and User-Defined Data Types

4. Derived Data Types:

• Arrays: Collection of elements of the same type.


Example: int numbers[10];

• Pointers: Variables that store memory addresses.


Example:

int x = 10;

int *ptr = &x;

• Structures: User-defined data types that group variables of different types.


Example:

struct Person {

char name[50];

int age;

};

• Unions: Memory-efficient data type that allows storing different data types in the same
memory location.
Example:

union Data {

int i;

float f;

};

Page 2 - Side 2: Enumerated Types and Special Types

5. Enumerated Types (Enum):


Defines named integer constants.
Example:

enum Day { MON, TUE, WED, THU, FRI };

6. Typedef and Void:

• Typedef: Allows creating new names (aliases) for existing types.


Example:

typedef unsigned int uint;

uint score = 100;

• Void: Used to indicate no value or no return type for functions.


Example:

void display();

7. Volatile and Const:

• Volatile: Tells the compiler that a variable can change at any time.
Example:

volatile int timer;

• Const: Indicates that a variable's value cannot be changed after initialization.


Example:

const int MAX = 100;

Functions in C

Introduction to Functions in Embedded C

1. What are Functions?

Functions are blocks of code designed to perform a specific task. They help in code reusability,
modularity, and readability.

2. Advantages of Using Functions in Embedded C:

• Modularity: Break complex programs into simpler, manageable parts.

• Reusability: Write once, use multiple times.

• Easier Debugging: Isolate and test individual functions.

• Memory Efficiency: Functions can be optimized for better memory usage.

3. Types of Functions:

• Standard Library Functions: Provided by C libraries (printf(), scanf(), etc.).

• User-Defined Functions: Created by the programmer to meet specific needs.


Page 1 - Side 2: Function Components and Declaration

4. Components of a Function:

• Return Type: The data type of the value the function returns. (void, int, char, etc.)

• Function Name: A unique identifier for the function.

• Parameters (Arguments): Inputs passed to the function.

• Body: The block of code that defines the function’s task.

Syntax:

return_type function_name(parameter_list) {

// Function body

Example:

int add(int a, int b) {

return a + b;

Page 2 - Side 1: Function Categories and Parameter Passing

5. Categories of Functions:

• Void Functions: Do not return a value.


Example:

void blinkLED() {

// Code to blink an LED

• Functions with Return Values: Return a result to the caller.


Example:

int multiply(int x, int y) {

return x * y;

6. Parameter Passing Methods:

• Pass by Value: Copies the actual value of the argument. Changes do not affect the original
variable.
Example:

void display(int num) {


printf("%d", num);

• Pass by Reference: Passes the address of the argument, allowing the function to modify the
original variable.
Example:

void increment(int *num) {

(*num)++;

Page 2 - Side 2: Recursive Functions and Best Practices

7. Recursive Functions:

A function that calls itself to solve smaller instances of a problem. Useful for tasks like factorial
calculation, Fibonacci series, etc.

Example:

int factorial(int n) {

if (n == 0)

return 1;

else

return n * factorial(n - 1);

8. Inline Functions (Keyword inline):

These functions are expanded at the point of call, reducing overhead but increasing code size. Useful
for small, frequently called functions.

Example:

inline int square(int x) {

return x * x;

9. Best Practices for Functions in Embedded C:

• Keep Functions Short and Focused: Each function should perform a single task.

• Use Descriptive Names: Helps in understanding code.

• Minimize Global Variables: Use parameters to pass data instead.


• Optimize for Performance: Avoid recursion and floating-point operations where possible.

• Error Handling: Use return values or status flags to indicate errors.

You might also like