Arduino Part 1
Arduino Part 1
Introduction to C Programming
Integrated Development Environment (IDE)
Structure of C Programming
Elements of Style
Variables in C
Control Structure
Loop Structure
Computer Programming -- is
often called coding. It is the
process of writing, testing,
debugging and maintaining
the source code of
computer programs.
Computer Program – is
known as software program.
It is a structured collection
of instructions to the
computer.
Computer Programmer
– is a professional
who writes programs.
It is a software application that provides
comprehensive facilities to computer
programmers for software development.
main()
Head
{
Variables Body
Statements
}
Preprocessor Commands
Functions
Variables
Statements & Expressions
Comments
These commands tells the
compiler to do preprocessing
before doing actual compilation.
#include <stdio.h>
int main()
{
double C, F;
/*read in value for C*/
printf(“Enter Celsius temp: “);
scanf(“%lf”,&C);
/* Calculate F */
F = (9/5) * C + 32;
/* Display the value of F */
printf(“The equivalent Fahrenheit is %lf \n“, F);
return 0;
}
are main
building blocks
of any C
Program
#include <stdio.h>
int main()
{
double C, F;
/*read in value for C*/
printf(“Enter Celsius temp: “);
scanf(“%lf”,&C);
/* Calculate F */
F = (9/5) * C + 32;
/* Display the value of F */
printf(“The equivalent Fahrenheit is %lf \n“, F);
return 0;
}
are used to hold numbers,
strings and complex data
for manipulation
#include <stdio.h>
int main()
{
double C, F;
/*read in value for C*/
printf(“Enter Celsius temp: “);
scanf(“%lf”,&C);
/* Calculate F */
F = (9/5) * C + 32;
/* Display the value of F */
printf(“The equivalent Fahrenheit is %lf \n“, F);
return 0;
}
Expressions combine variables and
constants to create new values.
/* Good practice */
delta_value = (old_value - new_value);
delta_total = (total_old - total_new);
gain = delta_value / delta_total * 100.0;
Declaration and Data Types
Naming Variables
Declaring Variables
Using Variables
The Assignment Statement
Variables
– use represent
some unknown value
Example:
Perimeter = 2L + 2W;
Variablesin programming can
be represented containing
multiple characters
Example:
full_name = first_name +
last_name;
Variable names in C
◦ May only consist of letters, digits,
and underscores
◦ May be as long as you like, but only
the first 31 characters are significant
◦ May not begin with a number
◦ May not be a C reserved word
(keyword)
auto break int long
case char register
const continue return
default do short signed
double else
sizeof static
enum extern
float for struct
switch
goto if
typedef union
unsigned void
volatile while
Begin variable names with lowercase letters
Use meaningful identifiers
Separate “words” within identifiers with
underscores or mixed upper and lower case.
◦ Examples: surfaceArea surface_Area
surface_area
Be consistent!
Use all uppercase for symbolic constants (used
in #define preprocessor directives).
Examples:
#define PI 3.14159
#define AGE 52
C is case sensitive
◦ Example:
area
Area
AREA
ArEa
All are seen as different variables by
the compiler.
Syntax : <data_type> variable_name;
Example:
int num_of_meatballs;
float area;
Integers (whole numbers)
◦ int, long int, short int, unsigned int
Floating point (real numbers)
◦ float, double
Characters
◦ char
Data Number Minimum Value Maximum Value
Type of Bytes
char 1 -128 127
int length = 7 ;
float diameter = 5.9 ;
char initial = „A‟ ;
double cash = 1058635.5285
Good Programming Practice
◦ a comment is always a good idea
◦ Example:
int height = 4; // rectangle
height
int width = 6 ; // rectangle
width
int area ; // rectangle
area
Place each variable declaration on its own line
with a descriptive comment.
Example :
float inches ; // number of inches deep
Place a comment before each logical
“chunk” of code describing what it
does.
Example :
/* Get the depth in fathoms from the user*/
printf(“Enter the depth in fathoms: ”);
Donot place a comment on the
same line as code (with the
exception of variable declarations).
Examples:
diameter = 5.9 ;
area = length * width ;
#include <stdio.h>
int main( )
{
int inches, feet, fathoms ;
fathoms = 7 ;
feet = 6 * fathoms ;
inches = 12 * feet ;
.
.
.
It is a statement for
choosing two alternative
action
Basic Syntax: Conditional T
Expression Execute S_1
if (conditional expr)
statement_1; F
else
Execute S_2
statement_2;
Write a program that reads in an integer and
then display whether the number is positive
or negative.
Write a program that reads weight in pounds
and output the weight in kilograms.
Moreover, if the weight in kilogram exceeds
60 kilos, then display the message “GO ON
DIET”, else “YOU ARE IN GOOD SHAPE”
Used to check the Basic Syntax:
value of a certain if (conditional expr)
item that may have statement_1;
more than one else if (conditional expr)
outcome
statement_2;
else if (conditional expr)
statement_3;
…
else if (conditional expr)
statement_n;
Write a program that reads in an integer and
then display whether the number is positive
or negative or neither when the user enters 0.
Write a program that reads the student’s
average grade in Programming and then
output his grade standing according to the
following scheme
AVERAGE GRADE GRADE STANDING
Below 75 F
At least 75 but below 85 C
At least 85 but below 95 B
At least 95 A
Write a program to display a menu system for an
elementary arithmetic operation and then ask the
user to select the operation he or she wants
Elementary Arithmetic Operations
1. Addition
2. Subtraction
3. Multiplication
4. Division
Input your choice (1-4):
The user then asked to type two integers and the correct
answer. Your program should also inform the user
whether the answer is correct or not.
Basic Syntax:
switch (expression) {
case constant 1;
statement 1;
statement 2;
Allows to execute a …
statement n;
series of statement break;
based on the value of case constant 2;
an expression statement 1;
statement 2;
…
statement n;
break;
case constant n;
statement 1;
statement 2;
…
statement n;
break;
default:
statement;
}
Create a Number Base converter program
that allows the user to select from the four
options given
NUMBER BASE CONVERTER
1. Decimal to Hexadecimal
2. Hexadecimal to Decimal
3. Decimal to Octal
4. Octal to Decimal
Input your choice (1-4):
Write a program that reads the student’s
average grade in Programming and then
output his grade standing according to the
following scheme
AVERAGE GRADE GRADE STANDING
Below 75 F
At least 75 but below 85 C
At least 85 but below 95 B
At least 95 A
It does not The general from of while
use a counter
variable to initialize loop counter;
loop a while (test loop counter using a condition)
number of {
times statement_1;
statement_2;
increment loop counter;
}
Write a program that reads in an integer and
then calculate the sum of the positive
integers from 1 to N.
Write a program that reads in an integer and
then count the number of positive and
negative numbers
The The general from of do…while
conditional
expression is do {
placed at the
statement_1;
end of the
statement_2;
loop
…
statement_2;
} while (conditional expression);
Write a program that output all even numbers
between 1 to 15.
Write a program that reads in 10 positive
integers and then calculate its sum.
It lets you The general from of for loop
execute a
group of for( initialization; test counter; increment counter)
statements {
a certain statement_1;
number of statement_2;
times …
statement_n;
}
Create a program that displays a
multiplication table of 5.
Sample Output
5*1=5
5*2=10
5*3=15
…
5*10=50
Modify the sample problem so that it will ask
the user for the number to multiply and the
number of terms.