Você está na página 1de 6

EEE3017W - Laboratory 5

C Programming
1. Introduction
This lab will help you get familiar with the C programming language. Please show the tutor the last
three exercises and the solutions to the theory questions when you have completed the lab.

2. Theory Questions

Suppose that you have a “signed char” type variable.

a) Explain what size and representation are used for the variable
b) Write a C statement to set the least significant bit without affecting any other bit in the variable
c) Write a C statement to wait in a loop for the most significant bit to go high.
d) Suppose that the variable had the number 64 in it.
i. What happens if a left shift operator is executed?
ii. What result comes out of that operation?
e) Write the C statement to perform the operation in (d)

3. C Programming Environment
We will use the free Dev-Cpp environment from Bloodshed Software. This software is started by
clicking Start, Programs and then Dev-Cpp. When you start the software you may be asked to select a
few display options. Choose the defaults and then click File->New->Project->Console application.
Give your project a name in the text box at the bottom of the dialog. The screen will then show you the
skeleton of a C program. Start a new project for each of the following exercises.

4. Exercises
a) In the main function enter the line (without the quotes)

printf (“Hello world\n”);

Press the F9 key to compile and run your first program. You will notice that the computer flashes
your output at the end of the run and does not wait for you to see if it was correct. In order to fix
this do the following: include the “stdlib” library by using the following line at the top of
your code:
#include <stdlib.h>

1
Ask the system to wait for a key press before exiting by putting this line in at the end of your
code, just before the closing brace on main():

system("PAUSE");

b) Write a program to output the ASCII table on the screen. Do this by incrementing a “char” type
variable from 0 to 255 and printing it out using printf. Use a for loop.

c) Modify the above program to put a space between each character and to put 12 characters per line.
You can do this by using an “if” statement on the loop counter.

d) Write a while-loop based program to output all the powers of 3 up to 10 billion (thousand million).
You will need to write the number 10 billion in scientific notation for the compiler. This is done by
writing “1e10”. Watch out for the ranges of your data types.

e) Write a C function which raises one number to the power of another number. Your function must
take in two “double” parameters and return a double. Use your function in a program which
calculates the powers of 4 up to one million million.

Student Name: _____________________________

Tutor’s Signature: _____________________________

Date: _____________________________

2
3
EEE3017W - Laboratory 5
C Programming
Memo

2. Theory Questions

Suppose that you have a “signed char” type variable.

a) The variable is represented by a 2’s complement 8-bit number. It can represent numbers in the
range -128 to +127.

b) temp=temp|0x01 alternatively temp|=0x01;

c) while ((temp&0x80)==0);

d) 6410 = 010000002 therefore a left shift operation will result in temp equalling 10000000 which is -
128 because this is a signed 2 complement character
e) temp = temp<<1;Write the C statement to perform the operation in (d)

4. Exercises
a)
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
printf("Hello World\n");
system("PAUSE");
}

4
b)

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
unsigned char c;

for(c=0;c<255;c++)
{
printf("%i %c\n",c,c);
}
system("PAUSE");
}

c)

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
unsigned char c;

for(c=0;c<255;c++)
{
if ((c%12)>0)
{
printf("%i %c ",c,c);
}
else
{
printf("%i %c\n",c,c);
}
}
system("PAUSE");
}

5
d)

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
double f,g;
f=1;

while (g<=1e10)
{
g = f*f*f;
printf("%f,%f\n",f,g);
f++;
}
system("PAUSE");
}

e)

#include <stdio.h>
#include <stdlib.h>

double mypow(double b, double p);

int main(void)
{
double base, answer;
int i;
base = 4;
i=0;
while(answer<=1e11)
{
answer = mypow(4, i);
printf("%i - %f\n",i, answer);
i++;
}
system("PAUSE");
}
double mypow(double b, double p)
{
double a,i;
a=1;
i=1;
while(i<p)
{
a = b*a;
i++;
}

return a;
}

Você também pode gostar