BrainDump
  Home arrow BrainDump arrow C: Operators
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
BRAINDUMP

C: Operators
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 28
    2008-02-11

    Table of Contents:
  • C: Operators
  • Atoi Does Not Rhyme with All-Gooey
  • Relational Operators
  • Assignment Operators

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More BrainDump Articles
    More By James Payne


       · Hey, thanks for stopping by to read my article on C operators. In this one we...
     

    BRAINDUMP ARTICLES

    - Firefox on Windows Mobile
    - Microsoft`s Sneaky Firefox Add-On Installati...
    - Microsoft`s Answer to Google Search - Bing
    - Doing Statistical Analysis with MS Excel
    - Extracting Google-Indexed Web Site Pages Usi...
    - Advanced String Manipulation Using MS Excel
    - Using Goal Seek and Solver in MS Excel
    - Internet Explorer 8: A Hands-on View
    - Tools for Beginning Game Developers
    - XAMPP: Tips for Running an Apache/MySQL Serv...
    - Improving Your Visual Studio Workspace
    - A Look at Microsoft`s Mobile Operating System
    - Microsoft Internet Explorer 8: Mixed Reactio...
    - Configuring WSUS 3.0 on Windows Server 2008
    - Migrating to Windows Server 2008





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
    Stay green...Green IT