span.SPELLE {mso-spl-e:yes;} span.GRAME {mso-gram-e:yes;} @list L5:level2 {mso-bidi-font-family:"Times New Roman";}

CPS 125

Alexander Ferworn

Week 2

Getting Started Programming in C

Main Topics

  • reams of example C programs
  • The Example program Intro.c that is discussed in class can be found here.

 

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) “Quincy” (Named after the author’s daughter’s cat). You can get Quincy from the web site

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

Quincy will start you off in an editor but you will be editing a file with no name or one called “Text1”. You can work with this if you like and save it with the file name “first.c” or you can go on to the next sentence and start from scratch. Create a file called first.c by clicking on the “File” menu item and selecting “New”. You will be given a choice of source files you can make. We are only interested in C so select “C Source File”. Again go to the File menu and select “Save”. Save the file as “first.c”.

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:

  • a semi-colon (;) ends the only executeable line of code (printf). Basically this is always true in C.
  • The block of code is indented. This makes it easier to read...you will see more later.

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:

  • There are many I/O functions in C but these are very common.
  • scanf() and printf() act on data coming from standard input which is usually your keyboard and standard output which is usually your screen.
  • lets talk a bit about integers and variables

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;

  • constant: never changes value
  • variable: can change value

Names

  • Variable and constant (and all other) names can be as long as you like
  • with both alphabetic and numeric characters and the underscore character
  • starting with an alphabetic character.
  • There cannot be any spaces in the name and
  • you cannot use C keywords as names
  • the c language is case sensitive.

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

.

  • Each slot is capable of holding 1 byte
  • As we have seen, one byte can hold 256 different numbers (0-255)...this is not much of a range for integers
  • C puts bytes together to make bigger integers...just like you might get more than one mail box if you get tons of mail.
  • There are lots of different types of integers which are differentiated by the number of bytes it takes to store them and the range of numbers they can represent

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!