Tuesday, 25 September 2012

Global Variables

Code:
#include<stdio.h>
static int anotherNum = 200;
int main()
{
    extern int anotherNum;
    printf("Value of anotherNum is %d\n", anotherNum);
    anotherNum += 50;
    printf("Value is now %d\n", anotherNum);
    extern int num;
    printf("Value of num is %d\n", num += 50);
    printf("Value of num is now %d", num);
    return 0;
}


From another file...
int num = 100;

Output:
Value of anotherNum is 200
Value is now 250
Value of num is 150
Value of num is now 150
 
I learned how to:
  • Create global variables and use them
  • Use global variables from another file
  •  
     
     

Enhanced Calculator

Below is a MULTIPLICATION calculator, but it will ONLY let you enter a number between 1 and 100...

#include <stdio.h>
int getUserInput();
int main()
{
 int works = 0,
     first = 0,
     second = 0,
     result = 0;
 // printf("first is %d and second is %d", first, second);
 while (works == 0)
 {
      int input = getUserInput();
      if (input != 0)
      {
        if (first == 0)
        {
          first = input;
          // printf("first is now %d", first);
        } else
        {
          second = input;
          works = 1;
        }
      }
 }
  // printf("first is %d and second is %d\n", first, second);
  result = first * second;
  printf("%d", result);
  return 0;
}
int getUserInput()
{
 int x;
 printf("Enter a number between 1 and 100: ");
 scanf("%d", &x);
 if (x >= 1 && x <= 100)
 {
  return x;
 }
 else
 {
  return 0;
 }
}

Output:
Enter a number between 1 and 100: 6
Enter a number between 1 and 100: 7
42
 
Output - 2:
Enter a number between 1 and 100: 312321
Enter a number between 1 and 100: 312
Enter a number between 1 and 100: 6
Enter a number between 1 and 100: 34
204
 - Note that the program multiplied only 6 and 34 together since the first two numbers were too big.

Wednesday, 19 September 2012

A very basic multiplication calculator

Original Code:
#include <stdio.h>
int main()
{
 int x, y;
 printf("Enter a number: ");
 scanf("%d", &x);
 printf("Enter another number: ");
 scanf("%d", &y);
 printf("%d times %d equals %d", x, y, x * y);
 return 0;
}
 
Output:
Enter a number: 7 
Enter another number: 5
7 times 5 equals 35 
 A highlighted number denotes an input.

My first C program!!!

O.M.G.!! My first C Program!!!!!!!!!

As you can see, it is extremely complicated and sophisticated!!

Original Code:
#include <stdio.h>
int main()
{
        printf("Aloha!\n");
        printf("What is 1 + 1?\n");
        printf("2");
        return 0;
}
Soo... C's syntax is in some ways very similar to Java's - both have the printf() function/method, and both have a main() function/method. I just need to remember to include that pesky #include <stdio.h> at the top and the return 0; at the end!

Tuesday, 11 September 2012

Binary, Hexadecimal, and Stuff

E.g. covert 255 to binary.
255 = 11111111

MING YIN in binary is 01001101 01101001 01101110 01100111 00100000 01011001 01101001 01101110


In the C language, a word is most often called an integer, abbreviated int. An integer can be used to represent numbers that range from negative to positive values, or numbers that have only positive values. In other words, an integer can be signed or unsigned. A signed integer can have either positive or negative values. An unsigned integer can only be positive. An unsigned 16-bit integer can have values from 0 through 65535. It is often abbreviated simply as unsigned.

SBit 15 is used as a sign bit for signed integers. If it is on, the number is negative. If it is off, it is positive. Positive values can range from 0 to 32767. Negative values can range from -1 to -32768. Some examples are shown below. Notice that the signed version is equal to -1 * (65536 - unsigned version). For example, to get the signed number from the unsigned value 49151, signed = -1 * (65536 - 49151) = -16385. 
HEX 8000 BFFF FFFE FFFF 0000 3FFF 7FFE 7FFF
Signed -32768 -16385 -0002 -0001 00000 16383 32766 32767
Unsigned 32768 49151 65534 65535 00000 16383 32766 32767

MY FIRST POST!!!

Hello World!!!!!!!!!