variable length function
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
Quoted 11 lines · Ankit Jain
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 <[email protected]> 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 <[email protected]> || <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
Quoted 9 lines · Steve Graegert
Quoted 2 lines
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 ofmain()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()).
Signature · 1 line
Glynn Clements <[email protected]>
Quoted 13 lines · Glynn Clements
Quoted 9 lines · Steve Graegert
Quoted 2 lines
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 ofmain()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.
Quoted 4 lines
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 frommain()).
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 <[email protected]> || <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
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
Quoted 57 lines · --- Steve Graegert
Quoted 4 lines · Glynn Clements
Quoted 1 line · Steve Graegert
Quoted 1 line
also, if what is the default return type and
value of
Quoted 4 lines
Quoted 4 lines
Quoted 1 line
main function in C in gcc, ansi C and turbo C?
Question 4:
ISO C99 suggests the return type ofmain()to beof type int. There
Quoted 1 line
Quoted 1 line
is a never ending debate among some
professionals whether void is
Quoted 1 line
Quoted 1 line
equally legal. Some compilers behave like ANSI
C that allows
Quoted 1 line
Quoted 1 line
declaration of type void but return int
implicitly. In other words:
Quoted 3 lines
Quoted 1 line
stick to int.
The value returned from main is used as the
process' exit code. If you
Quoted 1 line
declare
main()as returning void, and return fromit, the process is
Quoted 1 line
likely to have a random exit code (e.g. whatever
happens to be in the
Quoted 1 line
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 whenmain()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
Quoted 9 lines
--
Steve Graegert <[email protected]> ||
<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
Quoted 3 lines · Ankit Jain
Hi
thanks for help
You're welcome.
Quoted 10 lines
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.
Quoted 4 lines
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.
Quoted 72 lines
Quoted 57 lines · --- Steve Graegert
Quoted 4 lines · Glynn Clements
Quoted 1 line · Steve Graegert
Quoted 1 line
also, if what is the default return type and
value of
Quoted 4 lines
Quoted 4 lines
Quoted 1 line
main function in C in gcc, ansi C and turbo C?
Question 4:
ISO C99 suggests the return type ofmain()to beof type int. There
Quoted 1 line
Quoted 1 line
is a never ending debate among some
professionals whether void is
Quoted 1 line
Quoted 1 line
equally legal. Some compilers behave like ANSI
C that allows
Quoted 1 line
Quoted 1 line
declaration of type void but return int
implicitly. In other words:
Quoted 3 lines
Quoted 1 line
stick to int.
The value returned from main is used as the
process' exit code. If you
Quoted 1 line
declare
main()as returning void, and return fromit, the process is
Quoted 1 line
likely to have a random exit code (e.g. whatever
happens to be in the
Quoted 1 line
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 whenmain()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
Quoted 9 lines
--
Steve Graegert <[email protected]> ||
<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 9Send instant messages to your online friends http://uk.messenger.yahoo.com
Quoted 5 lines · Ankit Jain
Well then i am just trying to clear my concept again.
main()
{
Quoted 7 lines
printf("This is main");}
now this simple function main according to this discussion will return a int value.......Am i right....
Correct.
Quoted 1 line
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.
Quoted 4 lines
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.
Signature · 1 line
Glynn Clements <[email protected]>
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
Quoted 33 lines · --- Glynn Clements
Quoted 1 line · Ankit Jain
Well then i am just trying to clear my concept
again.
Quoted 11 lines
main()
{printf("This is main");}
now this simple function main according to this
discussion will return a int value.......Am i
right....Correct.
Quoted 1 line
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.Quoted 1 line
another thing........... if it returns a int value
and
Quoted 1 line
we have not returned any value
explicitly.........then
Quoted 1 line
what is the value returned ....as far as i know
its a
Quoted 1 line
non-zero value. but then what will that mean.....?
Quoted 12 lines
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.
Signature · 1 line
Glynn Clements <[email protected]>
Send instant messages to your online friends http://uk.messenger.yahoo.com
Quoted 1 line · Ankit Jain
hi
[snip]
Quoted 2 lines
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.
Quoted 48 lines
Quoted 45 lines · --- Glynn Clements
Quoted 1 line · Ankit Jain
Well then i am just trying to clear my concept
again.
Quoted 11 lines
main()
{printf("This is main");}
now this simple function main according to this
discussion will return a int value.......Am i
right....Correct.
Quoted 1 line
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.Quoted 1 line
another thing........... if it returns a int value
and
Quoted 1 line
we have not returned any value
explicitly.........then
Quoted 1 line
what is the value returned ....as far as i know
its a
Quoted 1 line
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 <[email protected]>
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
Quoted 104 lines · Steve Graegert
Quoted 3 lines · Ankit Jain
hi
[snip]
Quoted 4 lines
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.
Quoted 66 lines
Quoted 62 lines · --- Glynn Clements
Quoted 3 lines · Ankit Jain
Well then i am just trying to clear my concept
again.
Quoted 12 lines
main()
{printf("This is main");}
now this simple function main according to this
discussion will return a int value.......Am i
right....Correct.
Quoted 3 lines
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.Quoted 3 lines
another thing........... if it returns a int value
and
Quoted 3 lines
we have not returned any value
explicitly.........then
Quoted 3 lines
what is the value returned ....as far as i know
its a
Quoted 3 lines
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.Quoted 8 lines
Quoted 8 lines
Hopefully the compiler will issue a warning in this situation.
--
Glynn Clements <[email protected]>-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
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
Quoted 40 lines · Glynn Clements
Quoted 5 lines · Ankit Jain
Well then i am just trying to clear my concept again.
main()
{Quoted 7 lines
printf("This is main");}
now this simple function main according to this discussion will return a int value.......Am i right....
Correct.
Quoted 1 line
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.
Quoted 4 lines
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.
Signature · 5 lines
Glynn Clements <[email protected]> - To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html