Unit2 - 2 - w6 - Assignment2 8
Unit2 - 2 - w6 - Assignment2 8
Unit2_2
Assignment 2
Exercise 1
#define PI_Long 3.1415926535897932384 Questions:
int main(){ 1. What are functions?
float PI=3.14, 05var, int;
2. What are valid identifiers?
3. What are variables?
const int yeardays= 365;
4. What are constants, string constants, character
printf(“The value of PI is:”); constants?
printf(“ %f ”, PI); 5. How many block in this program?
printf(“\n”); 6. What are types?
printf(“PI error: %f\n”, PI_Long-PI); 7. What are keywords?
return 0;
8. What are expressions?
9. What are operators?
}
Exercise 2
1. Write a function to print an integer number as a binary number. Then use
this print function for all the below programs.
2. Then, write a program to get an input number a. Then check whether
(a>>1) is the same with (a/2). Print out the results to compare the results
using the function in (1)
3. Write another program that is similar to (2) to check whether (a<<1) is
the same with (a*2).
4. Write program to check the operations in (2) and (3) when a<0. Print out
the results when a is shifted with a larger number of bits (eg., >>5,
>>10,..).
5. Find cases that the above results, (2) and (3), are not the same and prove
them with examples.
6. Write another program to check the binary representations of a and –a.
Exercise 3
• With these declaration
• int a =-1;
Explain why these prints provide different results
printf(“%d \n”,i);
printf(“%u \n”,i);
Exercise 4
• With these declaration
• int a =-10, b=1;
Explain why these prints provide different results
30,40
Help for Exercise 5
• getch() function will return a key code when push a key on the
keyboard
• For example
• The unsigned char ch = getch() will return the code of the
keyboard
A program to check keyboard code
#include <conio.h>
#include <stdio.h>
main()
{
unsigned ch=0;
while (ch!=27)//27 is code for ESC key
{
printf("press a key ");
ch = getch();
printf(". Its code is %d\n",ch);
}
}
Exercise 6: extension of exercise 5
• Use the left, right, up, down keys instead of ‘w’, ’a’, ’s’, ’d’