INFOSTATION CORP
  • Home
  • Gallery
  • Contact
  • About
  • Digital Goods
  • Home
  • Gallery
  • Contact
  • About
  • Digital Goods
Search

A quick lesson in C programming

21/5/2019

Comments

 
Arduino uses the C/C++ programming language, but remove all the complexity in C. We will discuss some of the basic foundation of C.
​
Variable Declaration
   A variable is a named location in a memory where a program can manipulate data. Here is the declaration syntax in C.
      int i = 5;
  where i is the variable, int is the type of the variable, "=" is the equal operator and 5 is the value of the variable.

The value of the variable may get change in the program. A variable in C should have a data type like int or boolean.
See the table below for different C data types.
​ 
C Data Types
Type
Memory (bytes)
Range
Remarks
boolean
1
true or false (0 or 1)
char
1
-128  to +128
Used to represent an ASCII character code
byte
1
0 to 255
int
2
-32,768 to +32,767
unsigned int
2
0 to 65,536
arithmetic with ints may cause unexpected results.
long
4
-2,147,483,648 to 2,147,483,647
represents for long whole numbers.
unsigned long
4
0 to 4,294,967,295
Value
float
4
-3.4028235E+38 to + 3.4028235E+38
represents for decimal numbers.
double
4
as float
The same as float type.
Strings
   Strings in C is created with the following syntax:       
            char* text = "Sample Text";
   The char* is an equivalent to String type in other language (like Java)

Arithmetic Operation
   There are times that it needs to perform calculation in the code and assign to a variable. The following code contains the example of arithmetic operation.
Sample Arithmetic Operation

    
​Conditional Statements
   When a conditional statement is use, it means that it needs to decide what the next instruction to be executed based on the logical expression.  See below for an example:
Sample if-else statement

    
In a given sample, if the logical expression is true, that is, if value is greater than 2, the digitalWrite(13, HIGH) will be executed. But if the condition returns false, the statement in the "else" block will be executed, which is  digitalWrite(13, LOW)

We will discuss more and other coding in C as we explore doing more projects in Arduino.  See you on the next article.  

Comments

    Noel Anonas

    Author, coder, Innovator
    email:
    ​noelqanonas@gmail.com
    My Resume
    ​Couse Outline for Java

    Picture

    Archives

    July 2019
    June 2019
    May 2019
    June 2017

    Categories

    All

    RSS Feed

    View my profile on LinkedIn
  • Home
  • Gallery
  • Contact
  • About
  • Digital Goods