Question

In: Computer Science

Edit question Miscellaneous C knowledge. (Put all answers in the same q5.txt file, they’re all short.)...

Edit question

Miscellaneous C knowledge. (Put all answers in the same q5.txt file, they’re all short.)

(a) [2 marks] On an old 16-bit computer system and its C compiler,sizeof(double)=8andsizeof(int)=2. Given the two type definitions below, what weresizeof(s)andsizeof(lingling)on that system? Assume that ins, there is no gap between the twofields.

typedef struct s {

double r[5];int a[5];

} s;

typedef union lingling {

double r[5];int a[5];

} lingling;

(b) [2 marks] Given the following declarations, two questions: What is the type ofq? Whatis the type ofy?double* p, q;typedef

double *t;t x, y;

(c) [1 mark] To read a character from stdin, what’s wrong with “c = getchar();”, ifchasbeen declared with “char c;”?

(d) [3 marks] A beginner in C has coded up an attempt to determine whether stdin isempty:

#include <stdio.h>

int main(void){

if (feof(stdin)) {

printf ("empty\n");

   else {

printf ("not empty\n");

}

return 0;

}

This is run with stdin redirected from an empty file. It mistakenly reports “not empty”.Help the beginner by briefly answering: Why is this approach ineffective? And what isa correct approach?

Solutions

Expert Solution

Question a)
Given:
sizeof(double)=8 and sizeof(int)=2

typedef struct s {
double r[5];int a[5];
} s;

typedef union lingling {
double r[5];int a[5];
} lingling;

To find size of s and size of lingling

For s:
There are 5 double values=>5*8=40
There are 5 int values=>10
sizeof(s)=40+10=50

sizeof(s)=40

For lingling:

union is also like structure but it returns the max size among the variables used
There are 5 double values=>5*8=40
There are 5 int values=>10
sizeof(lingling)=max(40,10)=40

sizeof(lingling)=40


Question b)

Given

double* p, q;
typedef double *t;t x, y;

To find the type of q and type of y.

q is of type double.
y is of type t which is an alias for a pointer for type double

So y is pointer of double type.

Question c)

To read a character from stdin, what’s wrong with “c = getchar();”, if c has been declared with “char c;”?

For reading character from stdin there has to be an input.if not get char will return EOF.

Question d)

The EOF returns a boolean value in C.

If the value is 0 then there is no data.

Otherwise The data will be read.

SO the obvious code is

#include <stdio.h>
int main(void){
if (feof(stdin)==0) 
{
        printf ("empty\n");
}
else 
{
        printf ("not empty\n");
}
return 0;
}

This will print correctly as empty or not empty


Related Solutions

C++ Question: we need to read speech from .txt file. Steve Jobs delivered a touching and...
C++ Question: we need to read speech from .txt file. Steve Jobs delivered a touching and inspiring speech at Stanford's 2005 commencement. The transcript of this speech is attached at the end of this homework description. In this homework, you are going to write a program to find out all the unique tokens (or words) used in this speech and their corresponding frequencies, where the frequency of a word w is the total number of times that w appears in...
QUESTION : Read from a file that contains a paragraph of words. Put all the words...
QUESTION : Read from a file that contains a paragraph of words. Put all the words in an array, put the valid words (words that have only letters) in a second array, and put the invalid words in a third array. Sort the array of valid words using Selection Sort. Create a GUI to display the arrays using a GridLayout with one row and three columns. The input file Each line of the input file will contain a sentence with...
Using C Programming. Put all of these 4 things in one source file and attach to...
Using C Programming. Put all of these 4 things in one source file and attach to this question. Put #1 in main. Put all the others into separate function functions but in the same file.   1)   Put this code in main. You are writing a program for Bowl Me Over, a local bowling alley. The program will allow staff to enter any number of bowling scores. Scores in a standard game range between 0 to 300, with being a perfect...
An individual is presented with three different glasses of cola, labeled C, D, and P. He is asked to taste all three and then list them in order of preference. Suppose the same cola has actually been put into all three glasses.
An individual is presented with three different glasses of cola, labeled C, D, and P. He is asked to taste all three and then list them in order of preference. Suppose the same cola has actually been put into all three glasses. (a) What are the simple events in this ranking experiment, and what probability would you assign to each one? (b) What is the probability that C is ranked first? (c) What is the probability that C is ranked...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT