1. In the following program, what would be the final output? Explain in
detail.
v oid c o p y a r r ( ch a r ∗p1 , ch a r p2 [ ] ) {
memcpy ( p2 , p1 , s i z e o f ( p1 ) ) ;
memcpy ( p2 , ‘ ‘ABCD’ ’ , 4 ) ;
}
i n t main ( ) {
ch a r a r r 1 [ 1 0 0 ] ;
ch a r a r r 2 [ 1 0 0 ] ;
p r i n t f ( ‘ ‘ Enter a s t r i n g : ’ ’ ) ;
s c a n f ( ‘ ‘%[ ˆ\n ] s ’ ’ , a r r 1 ) ;
c o p y a r r ( a r r 1 , a r r 2 ) ;
p r i n t f ( ‘ ‘ \ n %s ’ ’ , a r r 2 ) ;
r e t u r n 0 ;
}
2. Consider the following program:
i n t main ( v oid ) {
i n t a = 2 , ∗b = NULL;
b = &a ;
p r i n t f ( ‘ ‘%p ’ ’ , b + 1 ) ;
p r i n t f ( ‘ ‘%p ’ ’ , ( ch a r ∗) b + 1 ) ;
p r i n t f ( ‘ ‘%p ’ ’ , ( v oid ∗) b + 1 ) ;
}
Suppose the address of a is equal to 0x1000. What is the output of the
above program? Explain in detail.
What to submit:
• Write-up describing an explanation for the output (i.e. why you observe
what you observe), for both the cases.
Let us say that you have a program that uses SCHED_FIFO scheduling. How
would the vruntime variable for such a program be computed or updated ?
How is this different from how vruntime would be updated for SCHED_RR and
SCHED_NORMAL ?
What would be the output of the following program? Explain.
i n t main ( )
{
p i d t pid 1 ;
p r i n t f ( ‘ ‘ b e f o r e f o r k ( ) ’ ’ ) ;
i f ( ( pid 1=f o r k ()) >0)
{
w ai t pi d ( pid1 , NULL, 0 ) ;
}
e l s e i f ( pid 1 == 0 ) {
e x e c l ( ‘ ‘ / u s r / bin / bash ’ ’ , ‘ ‘ bash ’ ’ ,NULL ) ;
p r i n t f ( ‘ ‘ done l a u n c hi n g the s h e l l ’ ’ ) ;
}
e l s e {
p e r r o r ( ‘ ‘ f o r k ( ) ’ ’ ) ;
}
}
What to submit:
• Write-up describing an explanation for the output (i.e. why you observe
what you observe).
Two code snippets are being presented to you.
1. mov rax, 0x1234567812345678
xor ax, 0x11
mov rdi, ax
call printf
xor rax, 0x11
mov rdi, rax
call printf
2. int x=-2;
unsigned int y = -33;
int z;
z = x + y;
printf(‘‘%u %u %u’’, x,y,z);
printf(‘‘%d %d %d’’, x,y,z);
In both the parts (1) and (2), explain what the two printf function calls
result in. Explain the reasons for any differences in the two cases.
Try to compile the following code snippet (no need to link).
char add(float a,float b)
{
return (int)(round (a)+round (b)) ;
}
Would the compilation of this code snippet result in errors or warnings?
Would this code, if compiled and linked with a full fledged program result in
any logical errors? Answer both parts accurately.
What to submit:
• Command (with appropriate arguments) that were used to compile the
snippet.
• A writeup showing all the compilation errors and warnings that you may
have encountered and their possible explanations, along with the logical
errors (if any if you think there are, i.e.).
write a C code to add two polynomials having n number of unknown variables
3. Using the online compiler:-
• Enter the code from the file bitwise_demo.c
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
byte_t result;
byte_t second_byte;
byte_t first_byte;
int shift;
// Reading the first byte. Integer is entered then cast to byte_t.
printf("Enter the value of the first_byte (0-255): ");
scanf("%d", &i);
first_byte = (byte_t)i;
// Reading the second byte. Integer is entered then cast to byte_t.
printf("Enter the value of the second_byte (0-255): ");
scanf("%d", &i);
second_byte = (byte_t)i;
printf("\n\n");
// The results will be printed out in hexadecimal.
// The %X placeholder is for hexadecimal output.
// This can be extended to %02X, i.e. a hexadecimal
// character, 2 characters wide, with leading blanks
// filled-in as zeros.
// Bitwise inversion
result = ~first_byte;
printf("The value of ~(%02X) is %02X\n", first_byte, result);
result = ~second_byte;
printf("The value of ~(%02X) is %02X\n\n\n", second_byte, result);
// Bitwise AND, OR, XOR
result = first_byte & second_byte;
printf("The value of (%02X) & (%02X) is %02X\n",
first_byte, second_byte, result);
result = first_byte | second_byte;
printf("The value of (%02X) | (%02X) is %02X\n",
first_byte, second_byte, result);
result = first_byte ^ second_byte;
printf("The value of (%02X) ^ (%02X) is %02X\n",
first_byte, second_byte, result);
system("pause");
return 0;
}
• Add this source code to your logbook.
• Run the program. You will be prompted to enter two values between 0 and 255. Initially enter 234 and 37.
• Add the output of this program to your logbook.
• Repeat this with other values of your choice.
• Write a short reflective account of the code concentrating on its functionality and comparing the outputs against the source code.
2. Using the online compiler
• Enter the code from the file bit_shift_demo.c
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
byte_t first_byte = 128;
byte_t second_byte = 1;
int shift;
printf("Enter the number of left/right shifts: ");
scanf("%d", &shift);
printf("\n\n");
// Right-shift first_byte
first_byte = first_byte >> shift;
// Left-shift second byte
// This is the equivalent of second_byte = second_byte << shift;
second_byte <<= shift;
// Display the results
printf("128 >> %d = %d\n", shift, first_byte);
printf("1 << %d = %d\n", shift, second_byte);
system("pause");
return 0;
}
Run the program. You will be prompted to enter a value for the number of shifts. Initially enter the value of zero
• Add the output of this program to your logbook.
• Repeat the previous two steps with values of 1, 2, 3, 4, 5, 6, 7, and 8.
• Write a short reflective account of the code concentrating on its functionality and comparing the outputs against the source code.
1. Using the online compiler (or Dev C++):-
• Enter the code from the file operators.c https://vle.dmu.ac.uk/bbcswebdav/pid-5463722-dt-content-rid-10886002_1/courses/ENGD1025_2122_520/operators%281%29.c
• Add this source code to your logbook.
• Run the program. You will be prompted to enter integer values for a and b. Enter 4 for a, and 5 for b.
• Add the output of this program to your logbook
• Write a short reflective account of the code concentrating on its functionality and comparing the outputs against the source code.
3. Write a C program using an online C compiler that implements the following:-
• Declare a string; upon initialization populate the string with a phrase of your choice.
• Declare a second-string; upon initialization populate that string with a different phrase of your choice.
• Display both strings using a single printf() function call.