473,396 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Argument for main()

61
Hi,

I am trying to get an understanding of main( ) functions with arguements.

I read in a book where the code had some thing like this

Expand|Select|Wrap|Line Numbers
  1. int main(int argc, char *argv[])
  2. {
  3.    /* Lines of codes in the body of main function */
  4. }
  5.  
Question is that main always have only this arguement? Can we make up something like this?

Expand|Select|Wrap|Line Numbers
  1. int main(int x)
  2. {
  3.    printf("%d\n", x);
  4. }
  5.  
Regards,

Alien
Oct 16 '07 #1
4 1551
JosAH
11,448 Expert 8TB
According to the C and C++ Standards main should have either no arguments; as in:

Expand|Select|Wrap|Line Numbers
  1. int main() { ... }
  2.  
or exactly two arguments as in:

Expand|Select|Wrap|Line Numbers
  1. int main(int argc, char** argv) { ... }
  2.  
Of course the names of the arguments can be different, but the first argument
is an int and the second argument is a pointer to an array of char pointers, each
pointing to one command line argument when the process was started.

All others, and that includes the return type of the main function induce undefined
behaviour which can be anything at all including the canonical deamons flying
out of your nose ;-)

kind regards,

Jos
Oct 16 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
main() can only have these arguments:
Expand|Select|Wrap|Line Numbers
  1. int main(int argc, char *argv[])
  2. {
  3.  
  4. }
  5.  
The reason is that this is the starting point of the program and the operating system has to know how to call your main(). The first argument is always the number of strings in the second argument and argv[0] is always the program name.

By doing it this way, your main() can have unlimited arguments. They just appear as an array of strings. To use an int argument, it will appear as a string in the argv array. All you do is convert the string to an int and off you go.
Oct 16 '07 #3
oler1s
671 Expert 512MB
You have to realize that main is a very special function that gets called by the compiler crt. So you have to define the return type of main and arguments according to the C and C++ standards, or it's a bit difficult for the compiler to work with your code.

main either takes no arguments, because you don't care about the command line arguments, or it takes an int and a char** argument to hold the parsed command line arguments. That's defined, and that's what the compiler expects. Do anything else, and either it won't compile outright, or your compiler will try and put something together, and you'll have completely undefined behavior as was said before.
Oct 16 '07 #4
Alien
61
Thank you guys for the responses. It helped me a lot to understand.

Kind Regards.
Oct 16 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: aling | last post by:
What's the rule of default argument of function in C++? I found that the default argument of function could not necessary be a constant value. Is it true? Previously I thought that the default...
4
by: Vijai Kalyan | last post by:
I was decomposing a task into different policies. Essentially, there is a general option obtained from a server and user options obtained from configuration variables. The two options are...
12
by: Uncle | last post by:
I am an untrained hobbyist. Everything about programming I have learned from the internet. Thank you all for your gracious support. This is what I have: #define CONST_CHAR 0 void some_func(...
5
by: Booted Cat | last post by:
I've seen lots of discussions on the proposed inclusion of "function call with named arguments" to C/C++ on these newsgroups. My proposal is slightly different in that: * No ANSI approval is...
8
by: Chandrashekar Tippur | last post by:
All, We want to associate a argument with a corresponding entry in header file. How do we do this? The argument is defined in a header file. We want to extract the value of the #define...
6
by: thesushant | last post by:
hi, whats the use of third argument to main( ), i.e. environmental parameters.... if i am not wrong ? 1st 1 is argc 2nd is argv and what bout the 3rd 1??????????? sushant
64
by: Morgan Cheng | last post by:
Hi All, I was taught that argument valuse is not supposed to be changed in function body. Say, below code is not good. void foo1(int x) { x ++; printf("x+1 = %d\n", x); } It should be...
17
by: Charles Sullivan | last post by:
The library function 'qsort' is declared thus: void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *)); If in my code I write: int cmp_fcn(...); int...
3
by: linarin | last post by:
#include <iostream> using namespace std; typedef bool (*CallableFunction)(int argc,char* argv); void DefineMyFunction(const char* name,CallableFunction func){ //here do the define action. }...
5
by: puzzlecracker | last post by:
C++ standard says the following: I am reading through c++ standard and have a difficulty understanding the difference between these two terms. Thanks, puzzlecracker
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.