0% found this document useful (0 votes)
2 views9 pages

Unit2 - 2 - w6 - Assignment2 8

The document outlines a C programming assignment consisting of multiple exercises focused on functions, variables, and bitwise operations. It includes questions about programming concepts and tasks such as writing functions to print binary numbers and control character movement on the screen. Additionally, it provides examples of code and explanations for expected outputs in various scenarios.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views9 pages

Unit2 - 2 - w6 - Assignment2 8

The document outlines a C programming assignment consisting of multiple exercises focused on functions, variables, and bitwise operations. It includes questions about programming concepts and tasks such as writing functions to print binary numbers and control character movement on the screen. Additionally, it provides examples of code and explanations for expected outputs in various scenarios.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

C Programming

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

printf(“%d \n”, a&b); printf(“%d \n”, a|b);


printf(“%u \n”, a&&b); printf(“%u \n”, a||b);
Exercise 5
• Write a program to control the character ‘$’ move one position when
the some keys are pressed.
• Limit the terminal screen size by 30x40 characters.
• Use ‘w’ for up, ‘a’ for left, ‘s’ for down, and ‘d’ for right.
0,0

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’

You might also like