0% found this document useful (0 votes)
10 views71 pages

Arduino Part 1

Uploaded by

aneeqadimaporo12
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
10 views71 pages

Arduino Part 1

Uploaded by

aneeqadimaporo12
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 71

Fundamentals of C-Programming

 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.

 An IDE normally consists of a source code


editor, a compiler and/or interpreter,
build automation tools, and usually a
debugger.
 This window contains
the interface 'Projects'
which will in the
following text be
referred to as the
project view.

 This view show all the


projects opened in
CodeBlocks at a certain
time.

 The 'Symbols' tab of


the Management
window shows symbols
and variables.
 A part of
the
environme
nt where
we can
write the
codes
 It is a computer
program that
translates text
written in a
computer
language (the
source language)
into another
computer
language (the target
language)
 Itis a
computer
program that
is used to
test and
debug other
programs
Preprocessor directive

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.

 Statements are expressions,


assignments, function calls, or control
flow statements which make up C
programs.
#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;
}
Doing it the right way...
 Write one statement per line.
 Put spaces before and after each arithmetic operator, just like
you put spaces between words when you write.

/* Poor programming practice */


biggest=-l;first=0;count=57;init_key_words();
If (debug)open_log_files();table_size=parse_size+lexsize;

/* Better programming practice*/


biggest=-l;
first=0;
count=57;
init_key_words();
if(debug)
open_log_files();
table_size=parse_size + lex_size;
 Change a long, complex statement into several
smaller, simpler statements.

/* This is a big mess */


gain = (old_value - new_value) /(total_old - total_new) * 100.0;

/* 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 4 -2147483648 2147483647

float 4 1.17549435e-38 3.40282347e+38

double 8 2.2250738580720 1.7976931348623


14e-308 157e+308
Format Description Example
%c Single character A
%i or %d Integer or 1024
decimal Number
%f Floating point 3.145
number or real
Long range of floating value
%lf Double
%.2f Floating point 3.10
number with 2
decimal places
 Examples:

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).

 Useblank lines to enhance


readability.
 Usespaces around all arithmetic
and assignment operators.
◦ Examples:
feet = 6 * fathoms ;
a = b + c;
#include <stdio.h>
int main( )
{
float inches ; // number of inches deep
float feet ; // number of feet deep
float fathoms ; // number of fathoms deep

// Get the depth in fathoms from the user


printf (“Enter the depth in fathoms : ”) ;
scanf (“%f”, &fathoms) ;
// Convert the depth to inches
feet = 6 * fathoms ;
inches = 12 * feet ;
.
.
.
return 0;
}
 Assignment statement is
represented by an equal sign (=)

 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.

You might also like