PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > questions
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
questions

Réponse
 
LinkBack Outils de la discussion
Vieux 21/11/2007, 10h43   #1
aarklon@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut questions


1) I have seen programs like this:-
#include<stdlib.h>
#include<stdio.h>

void f(j)
{ printf("\n j = %d",j); }

void fun(i){ void (*p)(int); p = f; p(100 + i); (p)(100 + i); }

int main(void)
{

void (*ptr)(int);
ptr = fun;

(*ptr)(222);
ptr(333);

puts("\nEND OF THE PROGRAM");
return(EXIT_SUCCESS);
}


does this mean that if we are defining a function without
explicitly specifying the type of the parameter,
will it default to int (signed or unsigned)???

2) how this program works..???

#include<stdlib.h>
#include<stdio.h>

char fun()
{
static i = 3;
int main = 10;

return
printf("%d\n",i-- > 0 ?i && fun():main);
}

int main(void)
{
fun();
puts("");
return(EXIT_SUCCESS);
}

o/p is given as :- 0 1 1

is ternary operator a sequence point...????
for the first time when the function is called
how will be i && fun() evaluated....???

assuming ternary operator is not sequence point

i && fun() will evaluate to 3 && fun()
is n't it then what value will fun() evaluate to...????

3) is there any concept of default return value for a function
returning int...???

suppose i am having a fn such as this

int myfun()
{


}

and i am calling it in main as
#include<stdio.h> main(){printf("%d",myfun())}

what value will it print by default...???


4) why the following program prints o/p correctly as far as my
understanding
goes this program returns address of local variable is n't
it...???
int* f()
{
int a = 12;
return &a;
}

int* fun()
{
int *b = f();

return b;
}

int main(void)
{

printf("\nfun = %d",*(fun()));

puts("");
return(EXIT_SUCCESS);
}

  Réponse avec citation
Vieux 21/11/2007, 11h26   #2
Mark Bluemel
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: questions

aarklon@gmail.com wrote:
> 1) I have seen programs like this:-
> #include<stdlib.h>
> #include<stdio.h>
>
> void f(j)
> { printf("\n j = %d",j); }
>
> void fun(i){ void (*p)(int); p = f; p(100 + i); (p)(100 + i); }

[snip]
> does this mean that if we are defining a function without
> explicitly specifying the type of the parameter,
> will it default to int (signed or unsigned)???


Yes - int, which is signed unless explicitly made unsigned.

This is for compatibility/consistency with pre-ansi code, I think.

>
> 2) how this program works..???
>
> #include<stdlib.h>
> #include<stdio.h>
>
> char fun()
> {
> static i = 3;
> int main = 10;
>
> return
> printf("%d\n",i-- > 0 ?i && fun():main);


This looks suspiciously like UB to me.

> }
>
> int main(void)
> {
> fun();
> puts("");
> return(EXIT_SUCCESS);
> }
>
> o/p is given as :- 0 1 1
>
> is ternary operator a sequence point...????


> for the first time when the function is called
> how will be i && fun() evaluated....???


>
> assuming ternary operator is not sequence point
>
> i && fun() will evaluate to 3 && fun()
> is n't it then what value will fun() evaluate to...????


My expectation is that fun() will always evaluate to a char with a value
of 2 (the number of characters output by printf).

>
> 3) is there any concept of default return value for a function
> returning int...???


No

> suppose i am having a fn such as this
>
> int myfun() { }


A decent compiler, with appropriate options, should warn you about this.

(gcc with "-ansi -Wall -pedantic" said
"xxx.c: In function `fred':
xxx.c:10: warning: control reaches end of non-void function")

>
> and i am calling it in main as
> #include<stdio.h> main(){printf("%d",myfun())}
>
> what value will it print by default...???


Any garbage that happens to be handy
>
>
> 4) why the following program prints o/p correctly as far as my
> understanding
> goes this program returns address of local variable is n't
> it...???
> int* f()
> {
> int a = 12;


initialize an auto int variable to 12.

> return &a;


return the address of the variable

> }
>
> int* fun()


This function simply passes the address on

> {
> int *b = f();
>
> return b;
> }
>
> int main(void)
> {
>
> printf("\nfun = %d",*(fun()));


This tries to access the data currently held in that address. Depending
on all sorts of factors, that could still be 12, or it may not. In your
case it was.
  Réponse avec citation
Vieux 21/11/2007, 13h59   #3
James Kuyper
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: questions

aarklon@gmail.com wrote:
> 1) I have seen programs like this:-
> #include<stdlib.h>
> #include<stdio.h>
>
> void f(j)
> { printf("\n j = %d",j); }
>
> void fun(i){ void (*p)(int); p = f; p(100 + i); (p)(100 + i); }
>
> int main(void)
> {
>
> void (*ptr)(int);
> ptr = fun;
>
> (*ptr)(222);
> ptr(333);
>
> puts("\nEND OF THE PROGRAM");
> return(EXIT_SUCCESS);
> }
>
>
> does this mean that if we are defining a function without
> explicitly specifying the type of the parameter,
> will it default to int (signed or unsigned)???


Having unspecified types default to 'int' (which is signed) was allowed
in older versions of the C language. Starting with C99, it's no longer
allowed.

> 2) how this program works..???
>
> #include<stdlib.h>
> #include<stdio.h>
>
> char fun()
> {
> static i = 3;
> int main = 10;
>
> return
> printf("%d\n",i-- > 0 ?i && fun():main);
> }
>
> int main(void)
> {
> fun();
> puts("");
> return(EXIT_SUCCESS);
> }
>
> o/p is given as :- 0 1 1
>
> is ternary operator a sequence point...????


Yes, there is a sequence point after evaluation of the left operand
(6.5.15p4).

> for the first time when the function is called
> how will be i && fun() evaluated....???


The first time i is called, the side effects of the i-- expression will
have been completed before the && expression is evaluated. Therefore,
the value of 'i' will be 2. Since 'i' is non-zero, the right operand of
the && expression must be evaluated, resulting in a recursive call to fun().

In the second call to fun(), i starts out with a value of 2, so the &&
expression is evaluated. By that time, i is reduced to 1. Since Since
1!=0, a third call to fun() occurs.

In the third call to fun(), i starts out with a value of 1, so the &&
expression is evaluated. By that time, i has been reduced to 0. Since
it's left operand compares equal to 0, && doesn't need to evaluate it's
right operand, and the ?: expression has a a value of 0. This gets
printed out. printf() returns the number of characters printed, or a
negative value indicating failure; either way, it's value is non-zero.

Control returns to the second call to fun(). Since the value returned by
fun is non-zero, the value of the && expression is 1. This value gets
printed out, and the non-zero value returned by printf() is returned by fun.

Control returns to the first call to fun(). Since the value returned by
the second call to fun is non-zero, the value of the && expression is 1.

> 3) is there any concept of default return value for a function
> returning int...???


No. It's a constraint violation to return from a function declared as
returning a value, unless you explicitly return a value.

> 4) why the following program prints o/p correctly as far as my
> understanding
> goes this program returns address of local variable is n't
> it...???
> int* f()
> {
> int a = 12;
> return &a;
> }
>
> int* fun()
> {
> int *b = f();
>
> return b;
> }
>
> int main(void)
> {
>
> printf("\nfun = %d",*(fun()));
>
> puts("");
> return(EXIT_SUCCESS);
> }


The behavior of that program is undefined. That means that the C
standard imposes no restrictions on it's behavior. In particular, the C
standard doesn't prohibit that function from producing what you consider
"correct" output. By the way, what output do you consider "correct"? As
far as the C standard is concerned, any behavior whatsoever is equally
correct.
  Réponse avec citation
Vieux 21/11/2007, 14h56   #4
Christopher Benson-Manica
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: questions

[comp.lang.c] James Kuyper <jameskuyper@verizon.net> wrote:

> No. It's a constraint violation to return from a function declared as
> returning a value, unless you explicitly return a value.


For the curious (including me), the constraint in question is
contained in 6.8.6.4p1 of n869. However, I would qualify that
statement as "It's a constraint violation to _explicitly_ return from
a function ...", given that the case of reaching the } terminating a
function is dealt with separately by 6.9.1p12.

--
C. Benson Manica | I appreciate all corrections, polite or otherwise.
cbmanica(at)gmail.com |
----------------------| I do not currently read any posts posted through
sdf.lonestar.org | Google groups, due to rampant unchecked spam.
  Réponse avec citation
Vieux 22/11/2007, 03h02   #5
Jack Klein
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: questions

On Wed, 21 Nov 2007 11:26:09 +0000, Mark Bluemel
<mark_bluemel@pobox.com> wrote in comp.lang.c:

> aarklon@gmail.com wrote:
> > 1) I have seen programs like this:-
> > #include<stdlib.h>
> > #include<stdio.h>
> >
> > void f(j)
> > { printf("\n j = %d",j); }
> >
> > void fun(i){ void (*p)(int); p = f; p(100 + i); (p)(100 + i); }

> [snip]
> > does this mean that if we are defining a function without
> > explicitly specifying the type of the parameter,
> > will it default to int (signed or unsigned)???

>
> Yes - int, which is signed unless explicitly made unsigned.


Only prior to 1999. Newer versions of the C standard, 1999 and later,
have made this code a constraint violation.

--
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
  Réponse avec citation
Vieux 22/11/2007, 09h08   #6
Barry Schwarz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: questions

On Wed, 21 Nov 2007 02:43:31 -0800 (PST), aarklon@gmail.com wrote:

>
> 1) I have seen programs like this:-
> #include<stdlib.h>
> #include<stdio.h>
>
> void f(j)
> { printf("\n j = %d",j); }
>
> void fun(i){ void (*p)(int); p = f; p(100 + i); (p)(100 + i); }
>
> int main(void)
> {
>
> void (*ptr)(int);
> ptr = fun;
>
> (*ptr)(222);
> ptr(333);
>
> puts("\nEND OF THE PROGRAM");
> return(EXIT_SUCCESS);
> }
>
>
> does this mean that if we are defining a function without
> explicitly specifying the type of the parameter,
> will it default to int (signed or unsigned)???


C99 deleted the "implicit int" feature. If you are compiling under
the old standard, the typeless arguments will be int (which is
signed).

>
> 2) how this program works..???
>
> #include<stdlib.h>
> #include<stdio.h>
>
> char fun()
> {
> static i = 3;
> int main = 10;
>
> return
> printf("%d\n",i-- > 0 ?i && fun():main);
> }
>
> int main(void)
> {
> fun();
> puts("");
> return(EXIT_SUCCESS);
> }
>
> o/p is given as :- 0 1 1
>
> is ternary operator a sequence point...????


There is a sequence point after the first operand is evaluated (at the
?) but not after whichever other argument is evaluated.

> for the first time when the function is called
> how will be i && fun() evaluated....???
>
> assuming ternary operator is not sequence point


Then you are not talking about C any more. If there is no sequence
point here, your program need not start in main. And then fun will
never get called.

>
> i && fun() will evaluate to 3 && fun()
> is n't it then what value will fun() evaluate to...????


Yes. And if fun returns 0, the expression will evaluate to 0. If fun
returns any other value, the expression will evaluate to 1.

>
>3) is there any concept of default return value for a function
> returning int...???


Only for main and only in C99.

>
> suppose i am having a fn such as this
>
> int myfun()
> {
>
>
> }
>
> and i am calling it in main as
> #include<stdio.h> main(){printf("%d",myfun())}
>
> what value will it print by default...???


Since you invoke undefined behavior, it could print your telephone
number or your grandmother's birthdate. On a well-designed system
(tm), your program would terminate in error when it tried to return
from myfunc without returning a value and the printf would never
execute.

>
>
> 4) why the following program prints o/p correctly as far as my
>understanding


Since you invoke undefined behavior, there is no such thing as correct
output. Or perhaps it should be phrased that any output is correct.
One of the worst kinds of UB is to appear to work as expected.

> goes this program returns address of local variable is n't
>it...???
> int* f()
> {
> int a = 12;
> return &a;


At the time the return statement executes, a still exists so &a is not
a problem (yet).

> }
>
> int* fun()
> {
> int *b = f();


Once f returns, a has ceased to exist. By definition, the address of
a becomes indeterminate. Attempting to assign this indeterminate
value to b invokes undefined behavior.

>
> return b;
> }
>
> int main(void)
> {
>
> printf("\nfun = %d",*(fun()));
>
> puts("");
> return(EXIT_SUCCESS);
> }



Remove del for email
  Réponse avec citation
Vieux 23/11/2007, 04h43   #7
Amandil
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: questions

On Nov 22, 4:08 am, Barry Schwarz <schwa...@doezl.net> wrote:
> On Wed, 21 Nov 2007 02:43:31 -0800 (PST), aark...@gmail.com wrote:
>
> >3) is there any concept of default return value for a function
> > returning int...???

>
> Only for main and only in C99.
>
> > suppose i am having a fn such as this
> > int myfun()
> > {
> > }
> >
> > and i am calling it in main as
> > #include<stdio.h> main(){printf("%d",myfun())}
> >
> > what value will it print by default...???

>
> Since you invoke undefined behavior, it could print your telephone
> number or your grandmother's birthdate. On a well-designed system
> (tm), your program would terminate in error when it tried to return
> from myfunc without returning a value and the printf would never
> execute.
>


I don't see why any system should have the program terminate in error.
On most systems (I believe) an int return value is returned in a
register, not on the stack. I think something (somewhere, perhaps K&R)
limited the size of a return value to the size of a register. Since
there is some value in the register, although that value is garbage
(as far as we are concerned) it exists, and therefore should not
generate an error.

> > 4) why the following program prints o/p correctly as far as my
> >understanding

>
> Since you invoke undefined behavior, there is no such thing as correct
> output. Or perhaps it should be phrased that any output is correct.
> One of the worst kinds of UB is to appear to work as expected.
>
> > goes this program returns address of local variable is n't
> >it...???
> > int* f()
> > {
> > int a = 12;
> > return &a;

>
> At the time the return statement executes, a still exists so &a is not
> a problem (yet).
>
> > }

>
> > int* fun()
> > {
> > int *b = f();

>
> Once f returns, a has ceased to exist. By definition, the address of
> a becomes indeterminate. Attempting to assign this indeterminate
> value to b invokes undefined behavior.
>
> > return b;
> > }

>
> > int main(void)
> > {
> > printf("\nfun = %d",*(fun()));
> > puts("");
> > return(EXIT_SUCCESS);
> >

>


As a practical explanation of why it 'seems' to work properly, a is
stored as a local variable of f(), usually on the stack. As the stack
has not yet been deallocated yet, *(fun()) doesn't cause a fault, and
as the stack has not yet been reused, the value (in stack space) is
still the same. If you would have saved the address and called another
function before the printf() call, the value likely would be
different.

-- Martie (Gandalf fell into the abyss; but he's all white now)
  Réponse avec citation
Vieux 23/11/2007, 07h01   #8
santosh
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: questions

In article
<4585f818-02a8-4a0d-ab2d-3a8715402381@g30g2000hsb.googlegroups.com>,
Amandil <mazwolfe@gmail.com> wrote on Friday 23 Nov 2007 10:13 am:

> On Nov 22, 4:08 am, Barry Schwarz <schwa...@doezl.net> wrote:
>> On Wed, 21 Nov 2007 02:43:31 -0800 (PST), aark...@gmail.com wrote:
>>
>> >3) is there any concept of default return value for a function
>> > returning int...???

>>
>> Only for main and only in C99.
>>
>> > suppose i am having a fn such as this
>> > int myfun()
>> > {
>> > }
>> >
>> > and i am calling it in main as
>> > #include<stdio.h> main(){printf("%d",myfun())}
>> >
>> > what value will it print by default...???

>>
>> Since you invoke undefined behavior, it could print your telephone
>> number or your grandmother's birthdate. On a well-designed system
>> (tm), your program would terminate in error when it tried to return
>> from myfunc without returning a value and the printf would never
>> execute.
>>

> I don't see why any system should have the program terminate in error.
> On most systems (I believe) an int return value is returned in a
> register, not on the stack. I think something (somewhere, perhaps K&R)
> limited the size of a return value to the size of a register.


No. Composite objects like structures can be returned, and they most
certainly won't likely fit in a machine register.

> Since
> there is some value in the register, although that value is garbage
> (as far as we are concerned) it exists, and therefore should not
> generate an error.


Not if the machine implements trap representations. The compiler could
initialise unused objects with a trap value whose access could trigger
an exception.

<snip>

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 07h46.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,23363 seconds with 16 queries