0% found this document useful (0 votes)
6 views17 pages

Introduction to C Programming Language

The document provides an overview of basic elements in C/C++ programming, including programming languages, compilers, interpreters, and the structure of C++ programs. It covers key concepts such as data types, control statements (if, switch, loops), arrays, and functions, along with an introduction to Python programming and IoT integration using Adafruit IO. Additionally, it includes practical examples and steps for setting up a programming environment and creating a simple Python application.

Uploaded by

104240647
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)
6 views17 pages

Introduction to C Programming Language

The document provides an overview of basic elements in C/C++ programming, including programming languages, compilers, interpreters, and the structure of C++ programs. It covers key concepts such as data types, control statements (if, switch, loops), arrays, and functions, along with an introduction to Python programming and IoT integration using Adafruit IO. Additionally, it includes practical examples and steps for setting up a programming environment and creating a simple Python application.

Uploaded by

104240647
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/ 17

Created by Turbolearn AI

Basic Elements in C/C++

Introduction
A computer program is a set of instructions used to operate a computer to produce a specific result.

Writing computer programs is called computer programming. The languages used to create computer
programs are called programming languages.

Programming Language Generation


Machine languages are the lowest level of computer languages. Programs written in machine language consist of
1s and 0s.
Assembly languages perform the same tasks as machine languages, but use symbolic names for opcodes and
operands instead of 1s and 0s (e.g., ADD A,B).
High-level programming languages create computer programs using instructions that are much easier to
understand.

Compiler vs. Interpreter


Compiler: Translates all the instructions at once and then executes the translated code.
Interpreter: Translates and executes instructions one by one.

Programming IDE
IDE stands for Integrated Development Environment. It's essentially an environment for editing a program. An example
of an online IDE is http://cpp.sh/.

Here's an image of a C++ shell, an example of a programming IDE.

The main() Function


The main() function is a special function that runs automatically when a program first executes. All C++ programs must
include one main() function, and all other functions in a C++ program are executed from the main() function. The keyword
before main (e.g., int main()) is called the function header line and indicates the return type of the main function (in this
case, an integer).

Page 1
Created by Turbolearn AI

Output: cout Object


The cout object is an output object that sends data to the standard output display device. To send a message to the cout
object, use the following pattern:

cout << "Hello World";

printf is also an instruction to print something to the screen:

printf("Hello World");

In C, only printf is supported.

Data Types and Variables


Here are a couple of common datatypes:

int a; or int a = 10; (typically 2 or 4 bytes)


float b; or float b = 2.5; (4 bytes)

A Simple Program

Here is an image showing a C++ "Hello World" program in a C++ shell. Note the #include <iostream> which is a directive,
not an instruction.

Branch Statement
A branch statement allows us to select an action based on some conditions.

For example: If a user inputs a valid account number and PIN, then allow money withdrawal; otherwise, alert a
message.

An if statement works like "If condition is met, then execute the task".

Page 2
Created by Turbolearn AI

Simple IF

if(boolean_expression) {
// body of if
}

The boolean_expression returns true or false. A value of zero is false, while other values are true.

This image provides a flowchart that illustrates the structure of an if statement.

Single and Multiple Statements


Single statement: { and } are optional.
Multiple statements: { and } are mandatory.

If Else Statement

Page 3
Created by Turbolearn AI

Here is a visual representation of an if-else statement.

Multiple Conditions

Conditions are checked one by one. When one condition is true, the if is stopped.

Nested IF

Page 4
Created by Turbolearn AI

Switch case

Page 5
Created by Turbolearn AI

The expression inside switch must evaluate to an integer, character, or enumeration constant. The switch...case structure
only works with integral, character, or enumeration constants. The execution jumps directly to the right condition instead
of checking one by one as in an if else statement.

IF ELSE vs. SWITCH CASE

Feature if...else...if switch...case

Used when there are conditions instead of a Used when there is a list of choices from which you need
Conditions
list of choices. to take a decision.
Number of Choices are in the form of an integer, character, or
Suited for a few number of conditions.
Checks enumeration constant.
Further Enumerable declaration, state machine, deterministic
N/A
Reading finite automata

Looping Statements

Looping Statement: FOR

Page 6
Created by Turbolearn AI

Looping Statement: WHILE

Looping Statement: DO WHILE

Page 7
Created by Turbolearn AI

Nested Loop

Break Statement in Loop

Example: Check a Prime Number

Page 8
Created by Turbolearn AI

Question: Why do we need a break statement instead of putting it in the condition statement of the loop?

Continue in Loop

Example

Page 9
Created by Turbolearn AI

What is printed to the console?

Array and Function in C/C++

Array in C/C++

Page 10
Created by Turbolearn AI

Arrays in C – Declare, Initialize, and Access


Array is a collection - An array is a container that can hold a collection of data.
Array is finite - The collection of data in the array is always finite, which is determined prior to its use.
Array is sequential - An array stores a collection of data sequentially in memory.
Array contains homogeneous data - The collection of data in the array must share the same data type.

How to Declare an Array?

DATA_TYPE array_name[SIZE];

DATA_TYPE is a valid C data type.


array_name is the name given to the array.
SIZE is a constant value.

Example:

int marks[5];

How to Initialize an Array

int marks[5] = {90, 86, 89, 76, 91};


int marks[] = {90, 86, 89, 76, 91};
marks[0] = 90;
marks[1] = 86;
marks[4] = 91;

Page 11
Created by Turbolearn AI

Function and Recursive in C

Introduction to Function
A function is a collection of statements grouped together to do some specific tasks. printf(), scanf(), and main() are
examples of functions.

Why Functions Are Used?


Divide code for all tasks into separate functions.
From main(), these functions are called to execute the tasks.

Advantages of Functions
Reusability of code: Functions once defined can be used several times.
Modular design of code: Modular programming leads to better code readability, maintenance, and reusability.
It is easier to write programs using functions. You can write code for separate tasks individually in a separate
function.
Code maintenance and debugging are easier: In case of errors in a function, you only need to debug that particular
function instead of debugging the entire program.

Function Declaration

return_type function_name(parameter_list);

return type: int, double, float,


or void.
function_name:A valid C identifier.
parameter_list: Input data.

Arguments – Call by Value


Function definition and function call

Parameter value is copied and passed to the formal parameter.

C code snippet illustrating a function to swap two integer values

Argument – Call by Reference


C code snippet designed to swap the values of two integers

Page 12
Created by Turbolearn AI

void swap(int *num1, int* num2){


int temp = *num1;
*num1 = *num2;
*num2 = temp;
}

void main(){
int n1 = 1, n2 = 2;
swap(&n1, &n2);
return;
}

Get Started with Python

Introduction
Python is a popular programming language created by Guido van Rossum and released in 1991.

Python is often used for:

Web development (server-side)


Software development
Industrial 4.0 applications (IoT, AI, Data Analytics)

More information can be found at https://www.w3schools.com/python/python_intro.asp.

Python IDE for Programming

Page 13
Created by Turbolearn AI

Windows Subsystem for Linux (WSL)


Visual Studio Code
Pycharm
Notepad + Terminal
Online Python editor: https://www.programiz.com/python-programming/online-compiler/

Python Indentation

if 5 > 2:
print("Five is greater than two!")

If Statement

FOR Loop

a = [1, 2, 3, 4, 5]
N = len(a)
for i in range (0, N):
print(a[i])

Arrays in Python

a = [1, 2, 3, 4, 5]
print(a[0])

WHILE Loop

Function

def sum_numbers(a, b):


result = a + b
return result

c = 4
print(sum_numbers(c, 3))

Internet of Things with Python

Page 14
Created by Turbolearn AI

More information can be found at https://io.adafruit.com/.

Adafruit IO Account Setup


To start, each group should create an account on Adafruit IO. This account can be shared among group members, but it's
crucial to keep your key private and avoid sharing it publicly, especially on platforms like GitHub.

Feeds and Dashboard


Feeds: This is the backend of the server where your data is stored.
Dashboard: This is the frontend of the server where you can visualize your data.

The Adafruit IO Feeds page allows you to set up the backend for your IoT project. In the image, you can see options for
creating new feeds and groups. A table lists existing feeds with their names, keys, and last values.

Step 1: Create Feeds in AIO


When creating feeds in Adafruit IO, ensure the feed names are:

In lowercase.
Without special characters.

Create the following feeds:

Two for buttons: button1, button2


One for AI: ai
Three for sensor data: sensor1, sensor2, sensor3

Step 2: Create a Dashboard in Adafruit IO

Page 15
Created by Turbolearn AI

The Adafruit IO dashboard displays real-time information and controls. In the image, you can see visualizations for
temperature, humidity, electrical conductivity, and controls for systems like lights and pumps.

Python File Setup

Step 3: Create and Run a Python File


1. Create a new Python file named link_mqtt.py.
2. Run the file. As a simple test, the script should print a string to the console to ensure the Python editor and
interpreter are functioning correctly.

Step 4: Install the Adafruit-IO Package


Install the necessary Adafruit IO package using pip:

pip install adafruit-io

Sample Source Code

Step 5: Integrate Sample Code


Here's a sample Python code snippet to get you started. Remember to replace the placeholder values with your actual
Adafruit IO username and key.

Page 16
Created by Turbolearn AI

import sys
from Adafruit_IO import MQTTClient
import random
import time

AIO_USERNAME = "" # Replace with your Adafruit IO username


AIO_KEY = "" # Replace with your Adafruit IO key

client = MQTTClient(AIO_USERNAME , AIO_KEY)


client.connect()
client.loop_background()

while True:
time.sleep(10)
print("Your Code Here")
value1 = random.randint(20, 80)
client.publish("sensor1", value1)

Page 17

You might also like