linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* variable length function
@ 2005-06-14 17:43 Ankit Jain
  2005-06-14 19:14 ` Steve Graegert
  0 siblings, 1 reply; 11+ messages in thread
From: Ankit Jain @ 2005-06-14 17:43 UTC (permalink / raw)
  To: linux prg

hi

if somebody can tell me what is exactly variable
length functions and how the compilers are able to
identify them.............?

also, if somebody can tell me how can i perform the
operation of printf from scanf?

also, if what is the default return type and value of
main function in C in gcc, ansi C and turbo C?

thanks

ankit

Send instant messages to your online friends http://uk.messenger.yahoo.com 

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: variable length function
  2005-06-14 17:43 variable length function Ankit Jain
@ 2005-06-14 19:14 ` Steve Graegert
  2005-06-15  2:23   ` Glynn Clements
  0 siblings, 1 reply; 11+ messages in thread
From: Steve Graegert @ 2005-06-14 19:14 UTC (permalink / raw)
  To: Ankit Jain; +Cc: linux-c-programming

On 6/14/05, Ankit Jain <ankitjain1580@yahoo.com> wrote:
> hi
> 
> if somebody can tell me what is exactly variable
> length functions and how the compilers are able to
> identify them.............?
> 
> also, if somebody can tell me how can i perform the
> operation of printf from scanf?
> 
> also, if what is the default return type and value of
> main function in C in gcc, ansi C and turbo C?


Question 1:
I suppose you mean functions with variable argument lists, which are
usually indicated by three dots (...) in their synopsis like the
following:

	void func(const char *fmt, ...)

A typical example is the family of printf() functions.  Variable
length means that the number of arguments the function can take is not
fixed.  As with printf(), you can pass a myriad of arguments to a
function with a variable argument list (as long as the function is
able to process them).

A typical application of this kind of functions is to dump error
messages.  At first you create wrapper that takes the variable
argument list, processes it and call a function that does all the
work:

	void log(const char *fmt, ...) {
		va_list args;
		
		va_start(args, fmt);      /* initialize */
		__process_msg(fmt, args); /* process this message */
		va_end(args);             /* clean up */
	}
	
	void __process_msg(const char *fmt, va_list args) {
		int error = errno;
		
		vfprintf(stderr, fmt, args); /* here: output to stderr */
		if (error != 0) {
			fprintf(stderr, ": %s", strerror(error));
			putc('\n', stderr);
		}
	}

As you can see, a couple of functions/macros and data types help to
process the variable argument list.  Consult man va_start or something
similar to read more on that topic.  Processing a variable argument
list is easy, but the implementation is highly complicated.


Question 2:
There is nothing magic with variable argument lists and compilers. 
Some sophisticated library functions do all the processing.  The
compiler is not involved in this process.


Question 3:
Please refer to message
<20050605122105.1865.qmail@web52905.mail.yahoo.com> where your
question has already been discussed (or answered at least).


Question 4:
ISO C99 suggests the return type of main() to be of type int.  There
is a never ending debate among some professionals whether void is
equally legal.  Some compilers behave like ANSI C that allows
declaration of type void but return int implicitly.  In other words:
stick to int.


Kind Regards

    \Steve

--

Steve Graegert <graegerts@gmail.com> || <http://www.technologies.de/~sg/>
Independent Software Consultant {C/C++ && Java && .NET}
Mobile: +49 (176)  21 24 88 69
Office: +49 (9131) 71 26 40 9

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: variable length function
  2005-06-14 19:14 ` Steve Graegert
@ 2005-06-15  2:23   ` Glynn Clements
  2005-06-15  6:33     ` Steve Graegert
  0 siblings, 1 reply; 11+ messages in thread
From: Glynn Clements @ 2005-06-15  2:23 UTC (permalink / raw)
  To: Steve Graegert; +Cc: Ankit Jain, linux-c-programming


Steve Graegert wrote:

> > also, if what is the default return type and value of
> > main function in C in gcc, ansi C and turbo C?

> Question 4:
> ISO C99 suggests the return type of main() to be of type int.  There
> is a never ending debate among some professionals whether void is
> equally legal.  Some compilers behave like ANSI C that allows
> declaration of type void but return int implicitly.  In other words:
> stick to int.

The value returned from main is used as the process' exit code. If you
declare main() as returning void, and return from it, the process is
likely to have a random exit code (e.g. whatever happens to be in the
EAX register upon return from main()).

-- 
Glynn Clements <glynn@gclements.plus.com>

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: variable length function
  2005-06-15  2:23   ` Glynn Clements
@ 2005-06-15  6:33     ` Steve Graegert
  2005-06-15  8:36       ` Ankit Jain
  0 siblings, 1 reply; 11+ messages in thread
From: Steve Graegert @ 2005-06-15  6:33 UTC (permalink / raw)
  To: Glynn Clements; +Cc: Ankit Jain, linux-c-programming

On 6/15/05, Glynn Clements <glynn@gclements.plus.com> wrote:
> 
> Steve Graegert wrote:
> 
> > > also, if what is the default return type and value of
> > > main function in C in gcc, ansi C and turbo C?
> 
> > Question 4:
> > ISO C99 suggests the return type of main() to be of type int.  There
> > is a never ending debate among some professionals whether void is
> > equally legal.  Some compilers behave like ANSI C that allows
> > declaration of type void but return int implicitly.  In other words:
> > stick to int.
> 
> The value returned from main is used as the process' exit code. If you
> declare main() as returning void, and return from it, the process is
> likely to have a random exit code (e.g. whatever happens to be in the
> EAX register upon return from main()).

Yes, another important issue.  The most reliable way to return a valid
and correct value I am aware of is returning a value explicitly. 
You're right. just declaring int does not imply the return code to be
the expected one, though being valid.  It is __not__ guaranteed that
the return type is not some random integer value unless it is returned
explicitly.

Another point that comes to mind are compilers with distinct calling
conventions for int and void functions and job control for example. 
This can cause headaches when main() does not push a return value on
the stack, as being the case with void, and the caller tries to pop an
int which obviously is not found.  It __may__ lead to subtle crashes
later on.


Kind Regards

    \Steve

--

Steve Graegert <graegerts@gmail.com> || <http://www.technologies.de/~sg/>
Independent Software Consultant {C/C++ && Java && .NET}
Mobile: +49 (176)  21 24 88 69
Office: +49 (9131) 71 26 40 9

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: variable length function
  2005-06-15  6:33     ` Steve Graegert
@ 2005-06-15  8:36       ` Ankit Jain
  2005-06-15  9:08         ` Steve Graegert
  2005-06-15 11:33         ` Glynn Clements
  0 siblings, 2 replies; 11+ messages in thread
From: Ankit Jain @ 2005-06-15  8:36 UTC (permalink / raw)
  To: Steve Graegert, Glynn Clements; +Cc: linux-c-programming

Hi

thanks for help

Well then i am just trying to clear my concept again.

main()
{

         printf("This is main");

}

now this simple function main according to this
discussion will return a int value.......Am i
right....but is it true with any compiler?

another thing........... if it returns a int value and
we have not returned any value explicitly.........then
what is the value returned ....as far as i know its a
non-zero value. but then what will that mean.....?

regards,

ankit
--- Steve Graegert <graegerts@gmail.com> wrote:

> On 6/15/05, Glynn Clements
> <glynn@gclements.plus.com> wrote:
> > 
> > Steve Graegert wrote:
> > 
> > > > also, if what is the default return type and
> value of
> > > > main function in C in gcc, ansi C and turbo C?
> > 
> > > Question 4:
> > > ISO C99 suggests the return type of main() to be
> of type int.  There
> > > is a never ending debate among some
> professionals whether void is
> > > equally legal.  Some compilers behave like ANSI
> C that allows
> > > declaration of type void but return int
> implicitly.  In other words:
> > > stick to int.
> > 
> > The value returned from main is used as the
> process' exit code. If you
> > declare main() as returning void, and return from
> it, the process is
> > likely to have a random exit code (e.g. whatever
> happens to be in the
> > EAX register upon return from main()).
> 
> Yes, another important issue.  The most reliable way
> to return a valid
> and correct value I am aware of is returning a value
> explicitly. 
> You're right. just declaring int does not imply the
> return code to be
> the expected one, though being valid.  It is __not__
> guaranteed that
> the return type is not some random integer value
> unless it is returned
> explicitly.
> 
> Another point that comes to mind are compilers with
> distinct calling
> conventions for int and void functions and job
> control for example. 
> This can cause headaches when main() does not push a
> return value on
> the stack, as being the case with void, and the
> caller tries to pop an
> int which obviously is not found.  It __may__ lead
> to subtle crashes
> later on.
> 
> 
> Kind Regards
> 
>     \Steve
> 
> --
> 
> Steve Graegert <graegerts@gmail.com> ||
> <http://www.technologies.de/~sg/>
> Independent Software Consultant {C/C++ && Java &&
> .NET}
> Mobile: +49 (176)  21 24 88 69
> Office: +49 (9131) 71 26 40 9
> 


Send instant messages to your online friends http://uk.messenger.yahoo.com 

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: variable length function
  2005-06-15  8:36       ` Ankit Jain
@ 2005-06-15  9:08         ` Steve Graegert
  2005-06-15 11:33         ` Glynn Clements
  1 sibling, 0 replies; 11+ messages in thread
From: Steve Graegert @ 2005-06-15  9:08 UTC (permalink / raw)
  To: Ankit Jain; +Cc: Glynn Clements, linux-c-programming

On 6/15/05, Ankit Jain <ankitjain1580@yahoo.com> wrote:
> Hi
> 
> thanks for help

You're welcome.

> Well then i am just trying to clear my concept again.
> 
> main()
> {
>         printf("This is main");
> }
> 
> now this simple function main according to this
> discussion will return a int value.......Am i
> right....but is it true with any compiler?

This is true for most cases, although warnings may be issued.  In
practice, specifying a return type is a __very__ good idea.
 
> another thing........... if it returns a int value and
> we have not returned any value explicitly.........then
> what is the value returned ....as far as i know its a
> non-zero value. but then what will that mean.....?

It __may__ return a non-zero value.  If not, one can not predict the
actual value, it's random.  Under normal circumstances a non-zero
return value indicates a program error and can be used to specify its
types.

> --- Steve Graegert <graegerts@gmail.com> wrote:
> 
> > On 6/15/05, Glynn Clements
> > <glynn@gclements.plus.com> wrote:
> > >
> > > Steve Graegert wrote:
> > >
> > > > > also, if what is the default return type and
> > value of
> > > > > main function in C in gcc, ansi C and turbo C?
> > >
> > > > Question 4:
> > > > ISO C99 suggests the return type of main() to be
> > of type int.  There
> > > > is a never ending debate among some
> > professionals whether void is
> > > > equally legal.  Some compilers behave like ANSI
> > C that allows
> > > > declaration of type void but return int
> > implicitly.  In other words:
> > > > stick to int.
> > >
> > > The value returned from main is used as the
> > process' exit code. If you
> > > declare main() as returning void, and return from
> > it, the process is
> > > likely to have a random exit code (e.g. whatever
> > happens to be in the
> > > EAX register upon return from main()).
> >
> > Yes, another important issue.  The most reliable way
> > to return a valid
> > and correct value I am aware of is returning a value
> > explicitly.
> > You're right. just declaring int does not imply the
> > return code to be
> > the expected one, though being valid.  It is __not__
> > guaranteed that
> > the return type is not some random integer value
> > unless it is returned
> > explicitly.
> >
> > Another point that comes to mind are compilers with
> > distinct calling
> > conventions for int and void functions and job
> > control for example.
> > This can cause headaches when main() does not push a
> > return value on
> > the stack, as being the case with void, and the
> > caller tries to pop an
> > int which obviously is not found.  It __may__ lead
> > to subtle crashes
> > later on.
> >
> >
> > Kind Regards
> >
> >     \Steve
> >
> > --
> >
> > Steve Graegert <graegerts@gmail.com> ||
> > <http://www.technologies.de/~sg/>
> > Independent Software Consultant {C/C++ && Java &&
> > .NET}
> > Mobile: +49 (176)  21 24 88 69
> > Office: +49 (9131) 71 26 40 9
> >
> 
> 
> Send instant messages to your online friends http://uk.messenger.yahoo.com
>

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: variable length function
  2005-06-15  8:36       ` Ankit Jain
  2005-06-15  9:08         ` Steve Graegert
@ 2005-06-15 11:33         ` Glynn Clements
  2005-06-15 15:59           ` Ankit Jain
  2005-06-16  2:31           ` Rajkumar Andrews
  1 sibling, 2 replies; 11+ messages in thread
From: Glynn Clements @ 2005-06-15 11:33 UTC (permalink / raw)
  To: Ankit Jain; +Cc: linux-c-programming


Ankit Jain wrote:

> Well then i am just trying to clear my concept again.
> 
> main()
> {
> 
>          printf("This is main");
> 
> }
> 
> now this simple function main according to this
> discussion will return a int value.......Am i
> right....

Correct.

> but is it true with any compiler?

It's true for any compiler which supports pre-ANSI C (aka "K&R C"). 
ANSI C requires the return type to be specified.

> another thing........... if it returns a int value and
> we have not returned any value explicitly.........then
> what is the value returned ....as far as i know its a
> non-zero value. but then what will that mean.....?

It's an unspecified value. It could be any "int" value, including
zero. As there is only one zero but many more non-zero values, it
probably won't be zero, but it isn't guaranteed.

Hopefully the compiler will issue a warning in this situation.

-- 
Glynn Clements <glynn@gclements.plus.com>

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: variable length function
  2005-06-15 11:33         ` Glynn Clements
@ 2005-06-15 15:59           ` Ankit Jain
  2005-06-15 16:26             ` Steve Graegert
  2005-06-16  2:31           ` Rajkumar Andrews
  1 sibling, 1 reply; 11+ messages in thread
From: Ankit Jain @ 2005-06-15 15:59 UTC (permalink / raw)
  To: Glynn Clements; +Cc: linux-c-programming

hi

i had checked up in VC++....this may not be with
respect to this mailing list but question is there

if i write the same module in it then it takes void as
the default return type...........?

ankit
--- Glynn Clements <glynn@gclements.plus.com> wrote:

> 
> Ankit Jain wrote:
> 
> > Well then i am just trying to clear my concept
> again.
> > 
> > main()
> > {
> > 
> >          printf("This is main");
> > 
> > }
> > 
> > now this simple function main according to this
> > discussion will return a int value.......Am i
> > right....
> 
> Correct.
> 
> > but is it true with any compiler?
> 
> It's true for any compiler which supports pre-ANSI C
> (aka "K&R C"). 
> ANSI C requires the return type to be specified.
> 
> > another thing........... if it returns a int value
> and
> > we have not returned any value
> explicitly.........then
> > what is the value returned ....as far as i know
> its a
> > non-zero value. but then what will that mean.....?
> 
> It's an unspecified value. It could be any "int"
> value, including
> zero. As there is only one zero but many more
> non-zero values, it
> probably won't be zero, but it isn't guaranteed.
> 
> Hopefully the compiler will issue a warning in this
> situation.
> 
> -- 
> Glynn Clements <glynn@gclements.plus.com>
> 


Send instant messages to your online friends http://uk.messenger.yahoo.com 

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: variable length function
  2005-06-15 15:59           ` Ankit Jain
@ 2005-06-15 16:26             ` Steve Graegert
  2005-06-15 17:22               ` Ron Michael Khu
  0 siblings, 1 reply; 11+ messages in thread
From: Steve Graegert @ 2005-06-15 16:26 UTC (permalink / raw)
  To: Ankit Jain; +Cc: Glynn Clements, linux-c-programming

On 6/15/05, Ankit Jain <ankitjain1580@yahoo.com> wrote:
> hi

[snip]

> if i write the same module in it then it takes void as
> the default return type...........?

No it does not.  When specifying no return type int is returned
implicitly.  Under Windows, even if main is void-valued, the exit
code, whatever value it may have, is converted to int.

> --- Glynn Clements <glynn@gclements.plus.com> wrote:
> 
> >
> > Ankit Jain wrote:
> >
> > > Well then i am just trying to clear my concept
> > again.
> > >
> > > main()
> > > {
> > >
> > >          printf("This is main");
> > >
> > > }
> > >
> > > now this simple function main according to this
> > > discussion will return a int value.......Am i
> > > right....
> >
> > Correct.
> >
> > > but is it true with any compiler?
> >
> > It's true for any compiler which supports pre-ANSI C
> > (aka "K&R C").
> > ANSI C requires the return type to be specified.
> >
> > > another thing........... if it returns a int value
> > and
> > > we have not returned any value
> > explicitly.........then
> > > what is the value returned ....as far as i know
> > its a
> > > non-zero value. but then what will that mean.....?
> >
> > It's an unspecified value. It could be any "int"
> > value, including
> > zero. As there is only one zero but many more
> > non-zero values, it
> > probably won't be zero, but it isn't guaranteed.
> >
> > Hopefully the compiler will issue a warning in this
> > situation.
> >
> > --
> > Glynn Clements <glynn@gclements.plus.com>
> >
>

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: variable length function
  2005-06-15 16:26             ` Steve Graegert
@ 2005-06-15 17:22               ` Ron Michael Khu
  0 siblings, 0 replies; 11+ messages in thread
From: Ron Michael Khu @ 2005-06-15 17:22 UTC (permalink / raw)
  To: Steve Graegert; +Cc: Ankit Jain, Glynn Clements, linux-c-programming

Mr Ankit Jain,

u can verify all these "main return value" ambiguity
by making a simple script file that would print the return value
of the last app executed. In bash u could simply use "echo $?" on the 
command line.

So in dos mode, Perhaps u could make use of  "echo %errorlevel%" on the 
dos prompt

with these simple shell commands, u will then be able to see for urself 
if a
void-valued main would implicity return an int or not...

-ron


Steve Graegert wrote:

>On 6/15/05, Ankit Jain <ankitjain1580@yahoo.com> wrote:
>  
>
>>hi
>>    
>>
>
>[snip]
>
>  
>
>>if i write the same module in it then it takes void as
>>the default return type...........?
>>    
>>
>
>No it does not.  When specifying no return type int is returned
>implicitly.  Under Windows, even if main is void-valued, the exit
>code, whatever value it may have, is converted to int.
>
>  
>
>>--- Glynn Clements <glynn@gclements.plus.com> wrote:
>>
>>    
>>
>>>Ankit Jain wrote:
>>>
>>>      
>>>
>>>>Well then i am just trying to clear my concept
>>>>        
>>>>
>>>again.
>>>      
>>>
>>>>main()
>>>>{
>>>>
>>>>         printf("This is main");
>>>>
>>>>}
>>>>
>>>>now this simple function main according to this
>>>>discussion will return a int value.......Am i
>>>>right....
>>>>        
>>>>
>>>Correct.
>>>
>>>      
>>>
>>>>but is it true with any compiler?
>>>>        
>>>>
>>>It's true for any compiler which supports pre-ANSI C
>>>(aka "K&R C").
>>>ANSI C requires the return type to be specified.
>>>
>>>      
>>>
>>>>another thing........... if it returns a int value
>>>>        
>>>>
>>>and
>>>      
>>>
>>>>we have not returned any value
>>>>        
>>>>
>>>explicitly.........then
>>>      
>>>
>>>>what is the value returned ....as far as i know
>>>>        
>>>>
>>>its a
>>>      
>>>
>>>>non-zero value. but then what will that mean.....?
>>>>        
>>>>
>>>It's an unspecified value. It could be any "int"
>>>value, including
>>>zero. As there is only one zero but many more
>>>non-zero values, it
>>>probably won't be zero, but it isn't guaranteed.
>>>
>>>Hopefully the compiler will issue a warning in this
>>>situation.
>>>
>>>--
>>>Glynn Clements <glynn@gclements.plus.com>
>>>
>>>      
>>>
>-
>To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
>the body of a message to majordomo@vger.kernel.org
>More majordomo info at  http://vger.kernel.org/majordomo-info.html
>
>
>  
>



^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: variable length function
  2005-06-15 11:33         ` Glynn Clements
  2005-06-15 15:59           ` Ankit Jain
@ 2005-06-16  2:31           ` Rajkumar Andrews
  1 sibling, 0 replies; 11+ messages in thread
From: Rajkumar Andrews @ 2005-06-16  2:31 UTC (permalink / raw)
  To: linux-c-programming

umm... also, a little further understanding maybe useful.  A compiler 
(C in this case), will do some 'intelligent' processing and guessing of 
your program.  The compiler may issue warnings or may do something by 
default .. the default settings could either come from the factory (eg. 
Intel's C compiler or IBM's C compiler) or be community driver (eg. GNU 
C).

In the case of GNU C, read the manuals carefully.  Versions change the 
default settings... so nobody can say anything with authority.  The 
manuals and documentation associated with the compiler is 'the 
authority'.  GNU C also allows many defaults to be set or changed by a 
person (systems administrator).

We must also remember that the hardware discussions would also play a 
role here -- now-a-days from 32 bit to 64 bit.  Perhaps there could be 
discussions on 8 bit or 4 bit, but I'm assuming that would be rare.

And, my two-cents-worth-of-advice:  do not take anything for granted 
when you write C programs.  Don't assume anything -- be clear.  Test it 
out yourself and KNOW what happens.  In the example asked by Ankit, I 
would generally do it as follows (although what I've added is almost 
taken for granted and is not necessarily required!):

#include <stdio.h>

int main(void)
{
print("This is main");
return 0;
}

Regards,
RKA

On 15-Jun-05, at 7:33 AM, Glynn Clements wrote:

>
> Ankit Jain wrote:
>
>> Well then i am just trying to clear my concept again.
>>
>> main()
>> {
>>
>>          printf("This is main");
>>
>> }
>>
>> now this simple function main according to this
>> discussion will return a int value.......Am i
>> right....
>
> Correct.
>
>> but is it true with any compiler?
>
> It's true for any compiler which supports pre-ANSI C (aka "K&R C").
> ANSI C requires the return type to be specified.
>
>> another thing........... if it returns a int value and
>> we have not returned any value explicitly.........then
>> what is the value returned ....as far as i know its a
>> non-zero value. but then what will that mean.....?
>
> It's an unspecified value. It could be any "int" value, including
> zero. As there is only one zero but many more non-zero values, it
> probably won't be zero, but it isn't guaranteed.
>
> Hopefully the compiler will issue a warning in this situation.
>
> -- 
> Glynn Clements <glynn@gclements.plus.com>
> -
> To unsubscribe from this list: send the line "unsubscribe 
> linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html


^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2005-06-16  2:31 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-06-14 17:43 variable length function Ankit Jain
2005-06-14 19:14 ` Steve Graegert
2005-06-15  2:23   ` Glynn Clements
2005-06-15  6:33     ` Steve Graegert
2005-06-15  8:36       ` Ankit Jain
2005-06-15  9:08         ` Steve Graegert
2005-06-15 11:33         ` Glynn Clements
2005-06-15 15:59           ` Ankit Jain
2005-06-15 16:26             ` Steve Graegert
2005-06-15 17:22               ` Ron Michael Khu
2005-06-16  2:31           ` Rajkumar Andrews

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).