Question

In: Computer Science

1) If c is type char and i type int, what is assigned to c and...

1) If c is type char and i type int, what is assigned to c and i?
    Give answers in hex. Assume c is one byte, and i is four 
    bytes. Explain.

    c = 240;
    i = c;

2) If c is type unsigned char and i type int, what is assigned to 
    c and i? Give answers in hex. Explain

    c = 240;
    i = c;

3) If c is type signed char and i type int, what is assigned to 
    c and i? Give answers in hex. Explain

    c = 240;
    i = c;
     

Solutions

Expert Solution

explanation:-

as we know

Type Storage size Value range
char 1 byte -128 to 127 or 0 to 255
unsigned char 1 byte 0 to 255
signed char 1 byte -128 to 127

The first bit is the sign-bit, leaving us with 11110000. Per two's complement, if we complement it (getting 00001111), and then add one, we get 00010000 -- this is 16. As our starting point, 240 = F0 in hex, and 11110000 in binary.

When it converts the signed char to signed int, it doesn't say "this would be 240 if it's unsigned", it says "this is -16, make it an int of -16", for which the 32-bit two's complement representation is FFFFFFF0, because it must be the negative value for the full size of int (this is called sign-extension):

+36 as a 32-bit value: 000000000000000000000000 11110000
complement:            111111111111111111111111 00001111
add one:               111111111111111111111111 00010000

Or, in hex, FFFFFFF0.

This is why you must be careful with printf("%x", c); (and relatives) -- if you intend to just get a two-digit value, and chars are signed, you may wind up with eight digits instead. Always specify "unsigned char" if you need it to be unsigned

#include <stdio.h>
int main()
{
   char c=240;
   int i=c;
  
   printf("c : HEX: = %X",c);
   printf("\ni : HEX: = %X",i);
  
   return 0;
}

output:

c :HEX: =fffffff0

c :HEX: =fffffff0

#include <stdio.h>
int main()
{
unsigned char c=240;
   int i=c;
  
   printf("c : HEX: = %X",c);
   printf("\ni : HEX: = %X",i);
  
   return 0;
}

output:

c :HEX: =f0

c :HEX: =f0

#include <stdio.h>
int main()
{
signed char c=240;
   int i=c;
  
   printf("c : HEX: = %X",c);
   printf("\ni : HEX: = %X",i);
  
   return 0;
}

output:

c :HEX: =fffffff0

c :HEX: =fffffff0


Related Solutions

Implement stack in C Struct: struct patients{ int id; int severity; char *firstName; char *lastName; char...
Implement stack in C Struct: struct patients{ int id; int severity; char *firstName; char *lastName; char *state; int time_spent; }; Given Array: struct patients* patientsArray[4] = {&p1, &p2, &p3, &p4}; Create two functions one for pushing this array to the stack and one for popping the variables such as p1 p2 p3 p4 by its ID
Write the MIPS assembly version of this C code: int weird(char[] s, int x) { int...
Write the MIPS assembly version of this C code: int weird(char[] s, int x) { int i; for (i = 0; i < x; i++) { if (s[i] == ‘A’) { return power(10, i); } } return -1; } int power(int base, int i) { int j = 0; while (j < base) { base = base * base; j++; } return base; }
int main(int argc, char *argv[]){ int fd1, fd2; char buffer[100]; long int n1; if(((fd1 = open(argv[1],...
int main(int argc, char *argv[]){ int fd1, fd2; char buffer[100]; long int n1; if(((fd1 = open(argv[1], O_RDONLY)) == -1) || ((fd2 = open(argv[2], O_CREAT|O_WRONLY|O_TRUNC,0700)) == -1)){ perror("file problem "); exit(1); } while((n1=read(fd1, buffer, 512) > 0)) if(write(fd2, buffer, n1) != n1){ perror("writing problem "); exit(3); } // Case of an error exit from the loop if(n1 == -1){ perror("Reading problem "); exit(2); } close(fd2); exit(0); } Could anyone fix the two issues in the while loop, so the ouput can...
I want Algorithim of this c++ code #include<iostream> using namespace std; int main() { char repeat...
I want Algorithim of this c++ code #include<iostream> using namespace std; int main() { char repeat = 'y'; for (;repeat == 'y';){ char emplyeename[35]; float basic_Salary,EPF, Dearness_Allow, tax, Net_Salary , emplyee_id; cout << "Enter Basic Salary : "; cin >> basic_Salary; Dearness_Allow = 0.40 * basic_Salary; switch (01) {case 1: if (basic_Salary <= 2,20,00) EPF = 0; case 2: if (basic_Salary > 28000 && basic_Salary <= 60000) EPF = 0.08*basic_Salary; case 3: if (basic_Salary > 60000 && basic_Salary <= 200000)...
*Answer in C program* #include <stdio.h> int main() {      FILE *fp1;      char c;     ...
*Answer in C program* #include <stdio.h> int main() {      FILE *fp1;      char c;      fp1= fopen ("C:\\myfiles\\newfile.txt", "r");      while(1)      {         c = fgetc(fp1);         if(c==EOF)             break;         else             printf("%c", c);      }      fclose(fp1);      return 0; } In the program above which statement is functioning for opening a file Write the syntax for opening a file What mode that being used in the program. Give the example from the program Referring to...
public class StringTools {    public static int count(String a, char c) {          ...
public class StringTools {    public static int count(String a, char c) {           }
#include #include #include int main(void) { int feof(FILE *stdin); int i, num; int binary[10]; char input[10];...
#include #include #include int main(void) { int feof(FILE *stdin); int i, num; int binary[10]; char input[10]; printf("Starting the CPSC 1011 Decimal to Binary Converter!\n"); while(1) {    i=0;    printf("\nPlease enter a positive whole number (or EOF to quit): ");    scanf("%s", input); // user inputs value as a string for separate values    if(strcmp(input,"")==0) {        printf("\n\tThank you for using the CPSC 1011 Decimal to Binary Generator.\nGoodbye!\n\n");    return(0); } num=atoi(input); if (num<=0) {    printf("\n\tSorry, that was...
1.) Answer the following programs a.) Let f be the following function: int f(char *s, char...
1.) Answer the following programs a.) Let f be the following function: int f(char *s, char *t) {     char *p1, *p2;     for(p1 = s, p2 = t; *p1 != '\0'&& *p2 != '\0'; p1++, p2++){               if (*p1 == *p2) break;     }     return p1 -s; } What is the return value of f(“accd”, “dacd”)? b.) What will be the output of the below program? #include <stdio.h> int main()   {    char x[]="ProgramDesign", y[]="ProgramDesign";   if(x==y){   printf("Strings are...
#include int main() {      FILE *fp1;      char c;      fp1= fopen ("C:\\myfiles\\newfile.txt", "r");     ...
#include int main() {      FILE *fp1;      char c;      fp1= fopen ("C:\\myfiles\\newfile.txt", "r");      while(1)      {         c = fgetc(fp1);         if(c==EOF)             break;         else             printf("%c", c);      }      fclose(fp1);      return 0; } In the program above which statement is functioning for opening a file Write the syntax for opening a file What mode that being used in the program. Give the example from the program Referring to the program above what has...
convert this string type   program into interger data type #include "stdafx.h" #include #include unsigned int putIntoHashTable(char...
convert this string type   program into interger data type #include "stdafx.h" #include #include unsigned int putIntoHashTable(char *ptrInputData, unsigned int bufferLength); // function to add to hash table unsigned int getFromHashTable(char *ptrOutputData, unsigned int bufferLength); // function to retrieve data from hash table #define INPUT_BUFFER_SIZE 200 // local buffer used for adding data to the hash table (there is no reason in this assignment to change this value) #define HASH_SIZE 100 // size of hash table to be used (for testing...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT