Question:
I need help with some C programming. Please!?
Goku Uzumaki
2013-04-14 13:35:41 UTC
Before anyone says "You need to work on your homework on your own", please believe me when I say this - I have been working on these for 3 days now. Can't get the answer!! There were 15 of these. I got 7, but need help with the remaining 8. Please help! It's due tomorrow (April 15, 2013) by midnight! Any kind of help is highly appreciated! Thank you so much!!

1) Suppose that you are using getchar() and putchar() to write a C program that copies from an input file to an output file, one character at a time. Should you copy the EOF character to the output file? Why or why not?

2) Write both "pre-ANSI C" and "ANSI C" style function declarations (prototype) for a function magnitude() that takes a single argument of data type "integer" and returns an int.

3) Write both "pre-ANSI C" and "ANSI C" style function declarations (prototype) for a function smallest() that takes two arguments of data type "double" and returns a double.

4) Write both "pre-ANSI C" and "ANSI C" style function declarations (prototypes) for the function prtbox() takes no arguments and returns nothing.

5) Why can an actual argument be any expression with an appropriate type of value, but a formal argument can only be a variable?

6) In your own words, describe what a pointer variable is and identify at least one way they are used in the C language.

7) Declare a pointer variable called "size" that is a pointer to an integer variable.

8) Declare a pointer variable called "water_termperature" that is a pointer to a float variable.
Four answers:
Jonathan
2013-04-14 21:21:38 UTC
(1) No, you don't copy the EOF marker. You close the file and leave it to the operating system and compiler library system to work out the details of how that is handled.



(2) Pre-ANSI, there was no standard for prototypes. Some pre-ANSI C compilers, including the ones from AT&T that I used in the 1970's, didn't even have prototypes, at all. Later, they were added. For example, Microsoft C compilers operating under DOS did have them. But there is "pre-ANSI" standard method for prototypes. Just varying ways. Some accepted no parameter types in the prototype, at all, simply instead doing the "usual conversions" for parameters based upon the expressions and rules for parameter passing (no float, only double; etc.) Some accepted parameter typing. Some didn't even accept prototypes, at all, as I said. Pre-ANSI where prototypes did exist almost always interpreted an untyped return value as int, though.



For example,

int magnitude( int a );     /* ANSI and some pre-ANSI */

int magnitude( int );           /* ANSI and some pre-ANSI */

magnitude();                        /* some pre-ANSI */

int magnitude();                    /* some pre-ANSI */



When 'extern' was added to the pre-ANSI compilers, you could prefix the above with 'extern', too.



Of course, you have to decide what your teacher is looking for. One thing is certain, your teacher isn't expecting anyone in class with experience going back into the 1970's using early AT&T C compilers under Unix.



Now, your question says "declaration" not "definition." So the teacher isn't looking for pre-ANSI function definitions, I'd suppose. If you are curious, a pre-ANSI function definition often for the above might look like:



int magnitude( a )

int a;

{

    /* ... some code goes here ... */

}



or,



magnitude( a )

int a;

{

    /* ... some code goes here ... */

}



But that's a definition, not a declaration. If your teacher wanted one of those, the teacher should have said 'definition.'



Skipping (3) and (4)....



(5) The calling routine passes values to the called routine. Any expression is competent for creating a 'value' to pass and the caller can compute it before it is passed as a value to the called routine. Of course, this assumes that the called routine is looking for pass-by-value. Since an expression isn't an l-value, it's not possible (or shouldn't be) to pass the result of an expression to a function expecting a pointer. So not all expressions can be passed to all types of parameters. The called routine expects to have a "name" that represents the value passed. An expression wouldn't make much sense there.



(6) A pointer variable is a typed name (used at compile time only) and a run-time instance of memory necessary to hold a physical representation of a memory address of the appropriate type. They are used for arrays, matrices, function pointers, as well as for allowing functions to modify values in the calling routine.



(7)

int * size;



(8)

float * water_temperature;
Jared
2013-04-14 13:46:28 UTC
1) no, you don't need to copy the EOF character since this is just a special return value that tells you that you have reached the end of the file...EOF does NOT exist in the actual file so you shouldn't copy it.



2-4) I have no idea.



5) Not totally sure (this seems to be a compiler question not really specific to C). It doesn't make sense for the formal argument to be an expression (which I'm assuming is in the function definition/declaration). The function takes input, so why would you need an expression as a formal argument?? If that's what you wanted, then just take the input to the expression and then use the expression inside the function definition. To me this is like an inline function:



Perhaps you don't like writing x*x to square quantities, but writing a function like this:



double sqr(double x){ return x*x; }



Seems kind of cumbersome (why require a full fledged function call for such a trivial function). Instead you could use an inline function (or even better use a #define function...)



inline double sqr(const double x) { return x*x;}



Now, when you call something like this in your code:



double x, y, z;



z = sqr(x + y);



Instead of a cumbersome function call, the compiler will insert the function "inline" so that it really looks like this:



z = (x + y) * (x + y);



6) Well these will be my words, not yours. A pointer variable is the value of an address to a variable. Pointers are used in many different ways, one way is as an array of values. Another way is to modify variables in function calls (i.e. send the pointer and then modify the value pointed to by the pointer).



7) int *size;

8) float *water_temperature;
mcmakin
2016-11-03 07:52:17 UTC
no longer anymore, at one time C++ became in simple terms an extension of C. considering the fact that then, C has replaced, and so has C++. whether that being mentioned it is commonplace for the comparable application to collect the two C and C++. the technique is the comparable, yet there are purely some adjustments int he libraries which you relatively choose the compiler to link to.
Nirpendra Patel
2013-04-14 13:40:28 UTC
I will recommend you to goto :



http://minefreeclasses.blogspot.in/



Actually you will find there all about c

with coding

downloading compilers and installing

step by step solutions


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...