Urgent C Questions

H

Hans Schneider

1. in the prg bellow what vars are stored on stack, heap, data segment?

int i;

void main()
{
int j;
int *k = (void *)malloc(1);
}

(I think j and k are on stack, but where is i?)


2. how to find sizeof variable w/o using sizeof command?

(no clue)


3. what is the o/p of this prg?

void main()
{
int x = 5;
x = x++;
printf("x=%i", x);
}

(I think it shoulld be printing 5 but it prints 6!)

ps: I am using QuickC 2.0

pps: Is very urgent!
 
W

Walter Roberson

Hans Schneider said:
1. in the prg bellow what vars are stored on stack, heap, data segment?

int i;

void main()
{
int j;
int *k = (void *)malloc(1);
}

(I think j and k are on stack, but where is i?)

Very much the same question was asked less than a week ago. The
answer is that as far as C is concerned, 'stack', 'heap',
and 'data segment' are implementation details and conforming
implementations exist which do not use any of those.

2. how to find sizeof variable w/o using sizeof command?
(no clue)

This is in the C FAQ.

3. what is the o/p of this prg?

void main()
{
int x = 5;
x = x++;
printf("x=%i", x);
}

(I think it shoulld be printing 5 but it prints 6!)

The output can be anything at all, as the program violates a
constraint, has an incorrect declaration for main, and fails to #include
a necessary interface file.
 
J

Jack Klein

On Tue, 15 Jan 2008 03:41:56 +0100 (CET), Hans Schneider

For it to be a C question, urgent or not, it would have to be about
valid C code.
1. in the prg bellow what vars are stored on stack, heap, data segment?

int i;

void main()

The line above generates undefined behavior, not valid C code.
{
int j;
int *k = (void *)malloc(1);

Since there is no prototype for malloc() in scope, undefined behavior,
not valid C code.
}

(I think j and k are on stack, but where is i?)


2. how to find sizeof variable w/o using sizeof command?

(no clue)

There is no sizeof command, not a valid C question.
3. what is the o/p of this prg?

void main()

The line above generates undefined behavior, not valid C code.
{
int x = 5;
x = x++;

The line above generates undefined behavior, not valid C code.
printf("x=%i", x);

Since there is no prototype for printf() in scope, this causes
undefined behavior, not valid C code.
}

(I think it shoulld be printing 5 but it prints 6!)

I think it should print, "This code was written by an idiot!".
ps: I am using QuickC 2.0

pps: Is very urgent!

Sorry, non of these is a question about the C language. If you are in
an actual C programming course, and your instructor asked these
questions for a homework assignment, do the following:

1. Immediately drop the class.

2. Complain to the school administration about the unqualified
instructor and erroneous material.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 
K

Keith Thompson

Hans Schneider said:
1. in the prg bellow what vars are stored on stack, heap, data segment?

int i;

void main()
{
int j;
int *k = (void *)malloc(1);
}

(I think j and k are on stack, but where is i?)


2. how to find sizeof variable w/o using sizeof command?

(no clue)


3. what is the o/p of this prg?

void main()
{
int x = 5;
x = x++;
printf("x=%i", x);
}

(I think it shoulld be printing 5 but it prints 6!)

ps: I am using QuickC 2.0

pps: Is very urgent!

You urgently need to read <http://www.c-faq.com>.

If you had deliberately set out to ask questions that have been asked
here repeated by clueless newbies (no offense), you couldn't have done
a much better job. I'm not accusing you, but the coincidence is a bit
troubling.

Incidentally, question #2 isn't hard to answer. Use the sizeof
operator. (C has no sizeof *command*.)
 
M

Mark Bluemel

Hans said:
1. in the prg bellow what vars are stored on stack, heap, data segment?

it depends :)

Read the documentation for your C implementation and platform.

The terms stack, heap and data segment don't appear in the C standard
and don't need to apply to your platform.
int i;

void main()

You need a new C reference - this is an invalid declaration for main.
{
int j;
int *k = (void *)malloc(1);
}

(I think j and k are on stack, but where is i?)


2. how to find sizeof variable w/o using sizeof command?

See http://www.c-faq.com
3. what is the o/p of this prg?

void main()

Already commented on this.
{
int x = 5;
x = x++;

See the FAQ.
printf("x=%i", x);
}

(I think it shoulld be printing 5

Why do you think that?
but it prints 6!)

Why do you think this is wrong?
ps: I am using QuickC 2.0

If you think the questions are specific to a particular C
implementation, this is the wrong newsgroup in which to ask them.
pps: Is very urgent!

Then a newsgroup is probably a poor place to ask anyway.
 
C

Chris Hills

The post from Jack is a classic example of why c.l.c is getting such a
bad name for itself.

However that said, this is nto a place to get last minute homework done.


Jack Klein said:
On Tue, 15 Jan 2008 03:41:56 +0100 (CET), Hans Schneider

For it to be a C question, urgent or not, it would have to be about
valid C code.


The line above generates undefined behavior, not valid C code.

It is valid C... just not strictly conforming .

How does it's undefined behaviour impinge on the question asked?
Since there is no prototype for malloc() in scope, undefined behavior,
not valid C code.

It is valid C... for reasons of brevity the headers were not included.

However your pedantry is irrelevant to answering the question at hand.
There is no sizeof command, not a valid C question.

Yes it is. Unless you are an internationally obtuse pedant. Try
communicating with humans once in a while
The line above generates undefined behavior, not valid C code.

It is valid C... just not strictly conforming .

How does it's undefined behaviour impinge on the question asked?

The line above generates undefined behavior,
Why?

not valid C code.


Since there is no prototype for printf() in scope, this causes
undefined behavior, not valid C code.

The headers were omitted in this code fragment for brevity.
Assuming the headers were there what is the problem?
I think it should print, "This code was written by an idiot!".

No the OP is ignorant. You are just insulting and not much of a person.
Try communicating with people occasionally.
Sorry, non of these is a question about the C language.

They are but you don't have the breath of intelligence or common sense
to understand. You must be a liability if you have to work with humans,
If you are in
an actual C programming course, and your instructor asked these
questions for a homework assignment, do the following:

Ignore Jack.
1. Immediately drop the class.

Keep going but do your own homework and don't do it at the last
minute.
2. Complain to the school administration about the unqualified
instructor and erroneous material.

But do remember as he can actually communicate with his human class he
is probably going to be better than Jack.
 
J

jameskuyper

Chris said:
The post from Jack is a classic example of why c.l.c is getting such a
bad name for itself.

I agree with you that Jack was being excessively pedantic, rude, and
not as helpful as he could have been. In his defense, it does appear
that the OP is asking us to do his homework.

....
....
How does it's undefined behaviour impinge on the question asked?

The question asked was (after translation into English) "what is the
output of this program". If the behavior of the program is undefined,
the answer to that question must be "it could be anything".

6.5p2: "Between the previous and next sequence point an object shall
have its stored value
modified at most once by the evaluation of an expression."

Violation of a "shall" occurring outside of a "Constraints" section
renders the behavior of the program ins undefined.
 
R

Richard Bos

Chris Hills said:
It is valid C... for reasons of brevity the headers were not included.

You do not know that. From the quality of the rest of the code I suspect
that they were not included for reasons of an incompetent teacher not
knowing that they're needed.

Richard
 
J

Joachim Schmitz

Richard said:
You do not know that. From the quality of the rest of the code I
suspect that they were not included for reasons of an incompetent
teacher not knowing that they're needed.
the (pointless) cast to a void * seconds that...

Bye, Jojo
 
J

John Bode

1. in the prg bellow what vars are stored on stack, heap, data segment?

The concepts of "heap", "stack", and "data segment" are implementation
details independent of the C language (i.e., the language does not
mandate that some things be stored on a stack or a heap). All the
language cares about is the extent (the lifetime of the variable) and
the visibility (where the variable can be used). For example, since
it's declared at file scope, i has *static* extent (storage is
allocated at the beginning of program execution and held until the
program exits), whereas j and k have *local* extent (storage is
allocated when each variable is defined, and deallocated when main
exits). The memory allocated with malloc() (which is not the variable
k, but what k points to) has *dynamic* extent, meaning it's held until
explicitly released.

Again, whether this storage is on a stack or a heap is not defined by
the language, but up to the individual implementation.
    int i;

    void main()

main() should return int, not void. The following are standard
definitions for main():

int main(void)
int main(int argc, char **argv) /* or char *argv[] */

Typing main() as void will invoke undefined behavior.
    {
        int j;
        int *k = (void *)malloc(1);
    }

(I think j and k are on stack, but where is i?)

2. how to find sizeof variable w/o using sizeof command?

(no clue)

There's a sizeof operator; it's not a command. I can think of one
trick, but I don't know how reliable it would be, so I'm not going to
relay it here.
3. what is the o/p of this prg?

    void main()
    {
        int x = 5;
        x = x++;
        printf("x=%i", x);
    }

(I think it shoulld be printing 5 but it prints 6!)

The statement "x = x++;" invokes undefined behavior; technically
speaking, *any* answer is acceptable. It could print 5, it could
print 6, it could print 42, it could launch Rogue.

Replace that line with one of the following:

x++;
x += 1;
x = x + 1;
 
C

Chris Hills

I agree with you that Jack was being excessively pedantic, rude, and
not as helpful as he could have been.
Absolutely.

In his defense,
There is no defence for that sort of behaviour from an adult.
it does appear
that the OP is asking us to do his homework.

I agree and that should have been rebuked. Not descending lower than the
level of the OP in manners.
...

The question asked was (after translation into English) "what is the
output of this program". If the behavior of the program is undefined,
the answer to that question must be "it could be anything".

But starting with

void main ()

which is not strictly conforming for a hosted environment is probably
going to be accepted by most compilers.

I would expect an adult to read it as a main with no parameters or
return. Not produce the sort of childish reaction it got.

All we learn from Jacks response is he is probably very bright but very
poor at communicating with humans,.
 
C

Chris Hills

Richard Bos said:
You do not know that. From the quality of the rest of the code I suspect
that they were not included for reasons of an incompetent teacher not
knowing that they're needed.

Possibly but I think it is far more likely they were not there to save
typing. In most programming books for example K&R 2nd ed main is

main ()

and K&R 2nd ed is a book many here advocate as a good book to learn C
from.....

So K&R is not C either... EVERY example in the book is "undefined" and
a C like language but not C...

How pedantic do you want to get?

Constructive criticism is one thing but proving you are a pedant with a
photographic memory and no social skills of little help to anyone.
 
B

Ben Pfaff

Chris Hills said:
So K&R is not C either... EVERY example in the book is "undefined"
and a C like language but not C...

The examples in K&R2 are written in C++, according to the
preface.
 
F

Flash Gordon

John Bode wrote, On 15/01/08 17:19:
There's a sizeof operator; it's not a command. I can think of one
trick, but I don't know how reliable it would be, so I'm not going to
relay it here.

<snip>

It is easy. All you have to do is take the following two steps...
1) Wait until after the OPs deadline before giving him the answer
2) Remember that any variable can be treated as an array of 1 element
3) Remember the rules of pointer arithmetic.

Note that it is *not* possible to write a replacement for the sizeof
operator, but that was not the question asked.
 
C

Chris Hills

Ben Pfaff said:
The examples in K&R2 are written in C++, according to the
preface.

That's not what it said. Initial testing in a C++ compiler and final
testing in an ANSI C compiler.

At the time C++ was a superset of C. Both have moved since then.
 

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

No members online now.

Forum statistics

Threads
473,871
Messages
2,569,919
Members
46,171
Latest member
A.N.Omalum

Latest Threads

Top