implicit typecast

P

Peter Drese

Hello,

I have to write an exam and I am not sure about implicit typecasting. Can
somebody please tell me of what type the following c-statements are? And why
they are of this type? It is also possible that the statements are wrong.

first exercise:

int x;
unsigned int ux;
float f;
char *p1;
void *p3;
int (*pf)(void);
void (*pf2)(double);
int f1(void);
void f3(double x2);


p1 = p3;
x += f;
p3 + ux;
pf = f1();
pf2 == f3;
pf2 = f3;
pf = f1;
p3 ? x : f;
*p3;


second exercise:

int x;
float f;
char *p1;
int f1(int x1);
int f2(int x1, int x2);
int f3(double x1);
int (*pf)(int);
int (*pf2)(double);


p1 ? f1 : pf;
pf = f1;
pf = f1(x);
pf2 = pf;
pf == f1;
pf == pf2;
f = f3(f);
f1 == f2;
++f;
p1 ? i : f;



Thanks in advance!

Greets Pete
 
J

Joona I Palaste

Peter Drese said:
I have to write an exam and I am not sure about implicit typecasting. Can
somebody please tell me of what type the following c-statements are? And why
they are of this type? It is also possible that the statements are wrong.

You wouldn't want us to do your homework for you, would you? This smells
suspiciously like it.

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Outside of a dog, a book is a man's best friend. Inside a dog, it's too dark
to read anyway."
- Groucho Marx
 
P

Peter Drese

You wouldn't want us to do your homework for you, would you? This smells
suspiciously like it.

I'm sorry if you think so. Unfortunatly it is not my homework. In about
three weeks I have to write an exam at university about this stuff. I tried
to solve the exercise a couple of times but I'm not sure about my solutions.
If you want, I can post them but I don't want to influence anybody!

Greets Pete
 
C

Clark Cox

"Peter Drese" <[email protected]> said:
Hello,

I have to write an exam and I am not sure about implicit typecasting. Can
somebody please tell me of what type the following c-statements are? And why
they are of this type? It is also possible that the statements are wrong.

This smells like homework. Anyone who would be in a position teaching
C, and writing exams on the subject should know the answers to all of
these.
 
E

E. Robert Tisdale

Peter said:
I have to write an exam and I am not sure about implicit typecasting.
Can somebody please tell me
of what type the following c-statements are?
And why they are of this type?
It is also possible that the statements are wrong.

first exercise:

[snip]

The rule is:

Attempt to answer these homework questions yourself!
Then submit the questions along with your answers
and subscribers to comp.lang.c will be happy to help you.
 
B

Ben Pfaff

Clark Cox said:
This smells like homework. Anyone who would be in a position teaching
C, and writing exams on the subject should know the answers to all of
these.

Sometimes "write an exam" means "take an exam". I suspect that's
what Peter means.
 
K

Kevin Goodsell

Peter said:
Hello,

I have to write an exam and I am not sure about implicit typecasting.

There is no such thing. A "cast" or "type cast" is defined as an
explicit type conversion. In C, this is accomplished via the cast
operator (which looks like the name of a type inside parentheses).
"Implicit type cast" is an oxymoron, since a cast can never be implicit.
Can
somebody please tell me of what type the following c-statements are?

Statements do not have types. A statement is sort of a syntactic unit
that "stands alone", so to speak. Statements are evaluated for
side-effects, and don't "return" a result. Expressions, on the other
hand, have a resulting value (unless the type is 'void') of a particular
type, and may be evaluated both for side-effects and for this result.
And why
they are of this type? It is also possible that the statements are wrong.

C's type rules can be somewhat complex. You'd do better to ask specific
questions.

<Remainder snipped>

Regardless of whether this is homework or not, the fact that it seems
very much like homework is enough for most of us to avoid simply
supplying the answers, particularly because you gave no indication of
any attempt to determine those answers yourself. If we see some effort
on your part to solve the problem, we can tell you whether you are right
or not, gauge your level of knowledge about the subject, and see where
you might have misconceptions. All this helps us to help you, and shows
that you are interested in learning, not just in receiving answers. Most
of us consider helping people learn to be a good use of our time, but
don't consider supplying free answers to be.

-Kevin
 
K

Keith Thompson

Peter Drese said:
I have to write an exam and I am not sure about implicit typecasting.

I think the phrase "write an exam" is ambiguous. Are you an
instructor creating a test for your students, or a student trying to
write answers for a test given to you by an instructor?
 
D

Darrell Grainger

Hello,

I have to write an exam and I am not sure about implicit typecasting. Can
somebody please tell me of what type the following c-statements are? And why
they are of this type? It is also possible that the statements are wrong.

first exercise:

int x;
unsigned int ux;
float f;
char *p1;
void *p3;
int (*pf)(void);
void (*pf2)(double);
int f1(void);
void f3(double x2);


p1 = p3;
x += f;
p3 + ux;
pf = f1();
pf2 == f3;
pf2 = f3;
pf = f1;
p3 ? x : f;
*p3;

I would recommend using www.google.ca to search for "n869". You will find
a copy of the draft of the ANSI C standard. Section 6.3 covers
"Conversions". If you can figure out what type the variable are then you
can figure out the result. For example, what is p3 and ux? I see that p3
is a pointer amd ux is an integer. Does ux get converted to a pointer? Or
does p3 get converted to an integer? If ux gets converted to a pointer
then maybe look in 6.3.2.3 Pointer of the standard.

I'm not going to give you the answer. If you don't do the work yourself
you will never remember but hopefully a good text book or the n869 file
will help you figure things out.
 
P

Peter Nilsson

Kevin Goodsell said:
Statements do not have types. <snip>

Expression statements are evaluated as void expressions. So, in a
sense, the answer is 'void' in all the expression statements in
question. ;)
 
J

J. J. Farrell

Peter Drese said:
I'm sorry if you think so. Unfortunatly it is not my homework. In about
three weeks I have to write an exam at university about this stuff. I tried
to solve the exercise a couple of times but I'm not sure about my solutions.
If you want, I can post them but I don't want to influence anybody!

Post what you've got, with an explanation of which answers you are
doubtful about and why, and you'll be much more likely to get some
help. I don't understand what you mean by 'influencing people'. Your
answers are either right or wrong. If they're wrong you'll be told so.
Your answers won't influence whether or not your answers are right.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top