CPS 125
Alexander Ferworn
Week 2
Getting Started Programming in C
Main Topics
Starting
You must install a C compiler on your computer or use one
that is already installed. We suggest the use of the free Integrated
Development Environment (IDE) “
http://www.codecutter.net/tools/quincy/
and install it on your computer. Start it up by clicking on the icon.
Your First Program...producing output
Copy and paste the program below into the window.
If you typed in everything correctly you can now “Build”. You can fix it if it doesn’t compile. When it does compile use “Execute” to run your program.
/* This is my first program it says hello! */
#include <stdio.h>
void main()
{
printf("Hello\n");
}
But Alex...what does it all mean?
Notes:
Your second program...intro to variables
Create a second file called second.c and enter the following program
/* Boy this stuff is easy so here goes program # 2 */
#include <stdio.h>
void main()
{
int num;
num = 1;
printf("I am a simple computer ");
printf("program.");
printf("My favorite number is %d because it is first.\n",num);
}
But Alex...what does it mean?
Notes: Type this thing in and see what the output looks like.
Your third program...getting input
notes:
A simple program...learning some rules
/* constants, variables and integers */
#define CONSTANT 35
#include <stdio.h>
void main()
{
short int variable = -35;
printf("This is a constant: %d\n",CONSTANT);
printf("This is a variable: %d\n",variable);
}
Before we go on lets talk about variables and constants
Two important concepts in C;
Names
variables are generally named with all lowercase characters. Constants are name with all UPPERCASE characters (this is not a rule but is good to do).
Constants
One easy way to achieve a constant in C is to use the preprocessor directive #define. for example
#define NUM 34
#include <stdio.h>
void main()
{
printf("The number is %d. \n",NUM);
}
The #define directive simply tells the preprocessor everywhere you see NUM replace it with the number 34. You cannot change the value of NUM.
Variables
A variable is something that is stored somewhere in memory at a particular address. Memory is like a Canada Post super mailbox which has had individual slots in it-each with its own address.
So when you say something like;
num = 35;
this really means store 35 at the address represented by num.
If it does not have an address then it is not a variable (with one exception) so you cannot assign something to it.
Before we go on, a bit about integers
Integers in C are whole numbers, meaning there is no fractional part e.g. 3, 7, -46
.
We will start with the integer type int
for example
int num;
this set aside between 2 and 4 bytes (The ANSI standard does not indicated what size an int must be so it varies between compiler makers).
To be sure of how long it is we use long and short to modify int.
for example
long int num; /* num is an integer variable that is 4 bytes long */
short int short_num; /* short_num is an integer variable that is 2 bytes long */
Representing negative and positive integers
So far we have only talked about positive integers--we call these unsigned integers.But how do we represent negative numbers in binary?
Lets take the following declaration
short int num; /* a 2 byte integer */
this means we have the following bits set aside for num;
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
suppose we had the following assignment statement,
num = 9;
this gets stored as
0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1
now suppose we have the following statement,
num = -9;
We use two's complement to store the number. There is a simple rule to remember...figure out what the positive number is in binary then flip the bits and add 1...in our example;
9 as a short int = 00000000 00001001 in binary
flip the bits = 11111111 1110110
add one = 11111111 11110111 = -9
to check it out do a simple math trick what is 9 + -9 (hint: it should be zero)
00000000 00001001
+11111111 11110111
00000000 00000000
Now you know!