C: Operators (Page 1 of 4 )
In our last article we left off discussing user input and variables in C. In this episode we will cover some of the operators available to you and how to manipulate data with them.
Mathematical Operators
You have been using most of the mathematical operators available in C since you began school. They consist of the +,-,*,/,and %. Below is a table describing the function of each one:
Operator | What it Does | Example |
+ | Used for addition/Unary Plus | 1+2 = 3 |
- | Used for subtraction/Unary Minus | 2-1 = 1 |
* | Used for Multiplication | 2*2 = 4 |
/ | Used for Division | 4/4 = 0 |
% | Used for Modulus (Returns the remainder from a division) | 4%3= -1 |
Here is how you would use them in code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int number1, number2, add, subtract, mul, div, mod;
printf("Enter two numbers separated by a space: ");
scanf_s ("%d %d", &number1, &number2);
add = number1+number2;
printf("n number1 + number2 equals %d", add);
subtract = number1-number2;
printf("n number1 - number2 equals %d", subtract);
mul = number1*number2;
printf("n number1 times number2 equals %d", mul);
div = number1/number2;
printf("n number1 divided by number2 equals %d", div);
mod = number1%number2;
printf("n The remainder of number1 divided by number2 is %d", mod);
}
In the above code you will notice a new include, the #include <stdlib.h>. We insert this when we are doing math.
The program works by creating two variables (number1 and number2) to hold the values a user will input, and then creates the add, subtract, mul, div, and mod variables to hold the results of our mathematical operations.
The program asks the user to enter two numbers, separated by a space. It then uses the scanf_s to retrieve the key strokes and store the numbers in number1 and number2 respectively. It then takes the two numbers and adds them, storing the result in the variable “add.” Then it prints out some text and the value that resides in “add.” It continues doing this for the rest of the examples. Let's say the user had input 5. The result would be:
Enter two numbers separated by a space: 5 3
number1 + number2 equals 8
number1-number2 equals 2
number1 times number2 equals 15
number1 divided by number2 equals 1
The remainder of number1 divided by number2 is -2
If we wanted to work with floating points we could do the following:
##include <stdio.h>
#include <stdlib.h>
int main()
{
float example;
float nonfloat;
float add;
printf("Please enter a decimal number and a non-decimal number: ");
scanf_s("%f %f", &example, &nonfloat);
add=example+nonfloat;
printf("n %.2f plus %.2f equals %.2f", example, nonfloat, add);
}
This program creates three variables. It then asks the user to enter a decimal number and a non-decimal number separated by a space. When the user enters the information, the first number is stored in the “example” variable and the second number is stored in the “nonfloat” variable. A calculation is then performed on both of these values and stored in the variable “add.” Finally, we print some text and append the result at the end of it. If the user has entered 1.2 and 3 as their text, then we would get the following result:
1.20 plus 3.00 equals 4.20
You will notice that we used %.2f for our placeholders. This formats the floating points to have 2 decimal places. If we had done %.3f, it would have shown three decimal places and so forth.
Next: Atoi Does Not Rhyme with All-Gooey >>
More BrainDump Articles
More By James Payne