Skip to content
Parallax Learn (Stage)

Parallax Learn (Stage)

This is the staging site. Please see https://learn.parallax.com for the official site.

  • Welcome
  • Tutorials
        • Tutorial Series head tag

          Tutorial Series
        • Tutorial Series

          The special, classroom-ready series pages are organized collections of tutorials for our most popular hardware and/or languages. The tutorials for each topic are conveniently accessible from a single page, shown in the order it is recommended that they be completed.
        • Robotics Series Head tag

          Robotics Series
        • Robotics Series

          • Artificial Intelligence Tutorial Series
          • Cybersecurity: Radio Data tutorialCybersecurity Tutorial Series
          • cyber:bot Tutorial Series
          • Boe-Bot Tutorial SeriesBoe-Bot Tutorial Series
          • Arduino Shield-Bot Tutorial Series
          • ActivityBot with C TutorialsActivityBot with C Tutorials
          • ActivityBot with BlocklyProp Tutorial SeriesActivityBot with BlocklyProp Tutorial Series
          • Scribbler 3 Tutorial SeriesScribbler 3 Tutorial Series
        • Electronics & Programming Series Head tag

          Electronics & Programming Series
          • BS2 Board of Education Tutorial SeriesBS2 Board of Education Tutorial Series
          • Propeller C-Language BasicsPropeller C Basics and Projects
          • FLiP Try-It Kit C Tutorial SeriesFLiP Try-It Kit C Tutorial Series
          • FLiP Try-It Kit BlocklyProp TutorialsFLiP Try-It Kit BlocklyProp Tutorials
          • Badge WX Tutorial SeriesBadge WX Tutorial Series
          • Propeller BlocklyProp Basics and ProjectsPropeller BlocklyProp Basics and Projects
          • View All Tutorial Series »
        • Browse Tutorials
        • Browse Tutorials

          Individual tutorials sorted by robot or kit, and language.
        • By Robot or Kit
          • ActivityBot
          • SumoBot WX
          • Boe-Bot
          • Shield-Bot
          • cyber:bot
          • Badge WX
          • ELEV-8
          • ARLO
        • By Language
        • By Language

          • Propeller C
          • Arduino
          • BlocklyProp
          • PBASIC
          • Python
          • View All Tutorials »
  • Educators
  • Reference
  • Downloads
  • Home
  • All Courses
  • Propeller C – Start Simple

Propeller C – Start Simple

Variables and Math

Variables are named sections of memory that make it possible for your program to “remember” values.  Your program can also use them in math expressions to perform calculations.

This example program, Variables and Calculations.c, declares variables named a, b, and c.  It stores 25 in a, 17 in b, and the result of a + b in the variable named c.

  • Click SimpleIDE’s Open Project button.
  • If you’re not already there, navigate to …SimpleIDELearnExamplesC IntroBasics.
  • Open Variables and Calculations.side. 
  • Examine Variables and Calculations.c, and try to predict what SimpleIDE Terminal will display.
  • Set the power switch to position 1 if it isn’t already (if applicable for your board).
  • Run the Program (Click the Run with Terminal button), and compare the actual output to your predicted output.
/*
  Variables and Calculations.c
 
  Add two integer values together and display the result.
*/

#include "simpletools.h"                      // Include simpletools

int main()                                    // main function
{
  int a = 25;                                 // Initialize a variable to 25
  int b = 17;                                 // Initialize b variable to 17
  int c = a + b;                              // Initialize c variable to a + b
  print("c = %d ", c);                        // Display decimal value of c
}

 

How Variables and Calculations.c Works

Variables and Calculations.c declares an integer variable named a and assigns it the value 25 with int a = 25.  Then, it declares a second variable named b and initializes it to 17 with int b = 17.  The last integer variable it declares is named c, and stores the result of a + b in it. 

Finally, it displays the value of c with print(“c = %d”, c).  This variation on print displays a sequence of characters called a string, followed by a variable.  The %d is called a format placeholder, and it tells print how to display the value stored in that variable as a decimal number, 42 in this case.

 


Did You Know?

The term + is a binary operator, meaning that it performs an operation on two inputs.  Examples of binary operators include:

+          Add
–          Subtract
*          Multiply
/           Divide
%         Modulus (remainder of a division calculation)


 

Try This

Here is a modified version of the main routine that displays “a = , b = “with their values, and then “a + b = ” and the value of c on a new line.  Then, it repeats for a – b. 

Notice that the second time it calculates the value of c, we don’t need to declare it with int.  It’s just c = a – b.  Notice also that print allows you to display more than one numeric value within your string.  All it takes is two format placeholders in the string and two values, separated by commas, after the string.

  • Click Save As Project button, and name it Test Binary Operators.
  • Modify the main function as shown below.
  • Run the program and verify the output.

 

Your Turn

  • Expand Test Binary Operators.c so that it goes through testing all five binary operators in the Did You Know section.

PRO TIP: Displaying % with print
To display the output of the Modulus operator, use (“a mod b = …”) or (“a  %% b = …) in the print function.   Since % has another purpose in print strings, just saying (“a % b = …) will give unexpected results.

  • Try changing a to 17 and b to 25, then re-run.
  • Try declaring int variables of y, m, and b, and then use them to calculate and display y = m*x + b.

 


Printer-friendly version
Index Array Variables
Prev
Variable Values and Addresses
Next

DISCUSSION FORUMS | PARALLAX INC. STORE

About | Terms of Use | Feedback: learn@parallax.com | Copyright©Parallax Inc. 2024

© 2025 Parallax Learn (Stage) • Built with GeneratePress