ZFGC

Resources => Coding => Topic started by: gm112 on August 12, 2006, 10:03:40 pm

Title: [C] Simple adding program.
Post by: gm112 on August 12, 2006, 10:03:40 pm
Meh.. I decided to cook up a simple user-based input example. I've decided to do a simple addition program, since it is a good way for people thats learning C to learn how to do user input stuff.

All code commented.
Code: [Select]
#include <stdio.h> //Include our files.

int main() //Startup our main program.
{
    int a,b,answer; //Set a,b,and answer as our integers
 printf ("Enter a number:\n"); //Print Enter a number. \n makes a new line

 scanf("%d",&a); //Get user input, %d will read our set integer.
 scanf("%d",&b); //same as abovee
   
     answer = a+b; //Declare what answer is, then add a+b
   
      printf("%d + %d = %d\n",a,b,answer); //print our problem then, print the answer and create a new line.

    scanf("%d"); //Wait for user input to end.
  return 0; //End everything! =P
}

edit: forgot to tell...

// is a comment
/*
 This is a block
*/

printf prints the user defined text
scanf read's the defined integer.
---------------------
%'s now, this was confusing at first, really this is freaking convinent!
%d decimal integer.
%c single character
%i integer
%e, %f, and %g all are float values
%o octal number
%s cmon! A string =D
%x hexidecimal number
%p I smell pointers =P
%n integer that is equal to the number of characters read, so far...
%u unsinged integer
%[] a set of characters


------------------------------------
\n Makes a new line

------------------------------------

There ya go!
Title: Re: [C] Simple adding program.
Post by: AoDC on August 13, 2006, 03:27:16 am
Interesting. Though, VERY simple :P

Contact Us | Legal | Advertise Here
2013 © ZFGC, All Rights Reserved