Thread: main func

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    32

    Lightbulb main func

    Code:
    int main (int argc, char **argv)
    {
    	   int sum;
    	   if (argc < 3 )
    				 return -1;
    		else
    				 sum = argv[1] + argv[2];
    		printf("Sum is %c\n",sum);
    		return 0;
       }
    
    If the executable name is "sum", then what would be the output of below command line execution?
    
    $ ./sum 3 4
    
    Please explain the above code. And when we needed to pass argument to main func? And main function recieves parameters from where?

  2. #2
    Registered User
    Join Date
    Nov 2010
    Posts
    1
    The main function recieves parameters from command line. If you are using Turbo C, then in Run there is an option Parameters from there you just pass parameters, in example you said 3,4.
    Also, as much what I know char **argv is a array of pointers so its char *argv[]. You may confirm it from more experienced people here on forum.
    With char *argv[], argv[0] is the name of your executable and parameters you pass are argv[1] and argv[2].
    One more thing,in your case only two arguments are passed 3,4 so the condition if(argc<3) holds true. Hence it will return -1. If the initial if condition evaluates to false then the next statements will be executed, i.e 3 and 4 will be added, and its value will be stored in sum.
    Last edited by RajeshHB; 11-16-2010 at 03:12 AM.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    32
    command line means. Pls give one ex.

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
    				 sum = argv[1] + argv[2];
    What are you trying to do here?
    The type of argv[i] is pointer to char.
    This code SHOULD NOT even compile....

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Bayint is right. Use strtol() to convert from the string to a number.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    The first step would be to get a compiler which generates several error message in response to that code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Defining functions below int main() - Best practices?
    By Programmer_P in forum C++ Programming
    Replies: 17
    Last Post: 12-09-2009, 02:08 PM
  2. loading vectors and data before main
    By lawrenced in forum C++ Programming
    Replies: 8
    Last Post: 07-30-2009, 01:42 PM
  3. what to do with the main window
    By stallion in forum Windows Programming
    Replies: 2
    Last Post: 01-28-2003, 08:58 PM
  4. typedef a func
    By trekker in forum C Programming
    Replies: 4
    Last Post: 07-02-2002, 05:15 AM
  5. void main(), int main(), argc, argv[]????
    By Jonny M in forum C Programming
    Replies: 3
    Last Post: 03-06-2002, 09:12 AM