C/C++ : Question-1

Hi guys,

Let us discuss about some of the Interview questions based on C and C++.
Here is my first question.

1) Can we pass arguments to main() in C. What is the need to pass arguments to the main()? And what happens when we pass arguments to main()? Any example?

Here goes my answer :

A- Yes, we can pass arguments to main() in C. The arguments passed to main() are called command line arguments.

But, I dont know the need. Anyone can answer this question.
And lets discuss about that.

Replies

  • Gurjeet Singh
    Gurjeet Singh
    Command-line arguments are given after the name of a program in command-line operating systems like DOS or Linux, and are passed in to the program from the operating system.the two parameters are argc and argv.one argument is number of command line arguments, and the other argument is a full list of all of the command line arguments respectively.
    eg:
    int main ( int argc, char *argv[] )

    so when the user executes this and say put :hello world
    then the argc=2 and the value is stored in the array
    any more discussion is welcome.
  • Manish Goyal
    Manish Goyal
    when you use dos environment then in order to rename a file you will use command

    rename filename1 filename2

    By using command line argument you can provide file names like this

    as it looks pretty nice also instead asking for

    Enter file name 1
    Enter file name 2

    correct me if i am wrong
  • Morningdot Hablu
    Morningdot Hablu
    hi friends,
    i think we can also pass arguments in main function by using function overloading.
  • gaurav.bhorkar
    gaurav.bhorkar
    int main (int argc, char *argv[])
    When we pass the command line arguments, number of arguments is denoted by the variable argc.
    char *argv[] is an array of pointers to character strings.

    So when we say delete filename.txt
    argc gets the value 1
    arg[1] contains the base address of string filename.txt
  • sherya mathur
    sherya mathur
    here is my questions based on c++ which are frequently asked in interviews
    1:can a method be overloaded based on different return type but same argument type?
    2: what are the 2 ways of exporting a function from a DLL?
    3:What is downcasting?
  • ms_cs
    ms_cs
    1:can a method be overloaded based on different return type but same argument type?
    Yes.

    3:What is downcasting?
    #-Link-Snipped-#

    PS:
    Are you asking in visual C++?
  • sushant005
    sushant005
    sherya mathur
    here is my questions based on c++ which are frequently asked in interviews
    1:can a method be overloaded based on different return type but same argument type?
    2: what are the 2 ways of exporting a function from a DLL?
    3:What is downcasting?
    sherya mathur
    here is my questions based on c++ which are frequently asked in interviews
    1:can a method be overloaded based on different return type but same argument type?
    2: what are the 2 ways of exporting a function from a DLL?
    3:What is downcasting?
    1>yes, a method can be overloaded basd on different return type but having same argument type.
    Since,overloaded function only depends on arguments passed during function call and do not depends on the function return type is used to return the appropriate value after execution of function.

    3> Downcasting is just opposite to the upcasting in object oriented lang. but Downcasting is opposite of the object-oriented programming rule.
    for ex- suppose there are three classes "India","pakistan","usa" and all are derived from the base class "Country".
    Here if we consider the concept of upcasting we can say that "india" is a type of"country".
    but if we consider Downcast then a "Country" it could be "India",or"pakistan", or "usa".
  • Manish Goyal
    Manish Goyal
    can a method be overloaded based on different return type but same argument type?
    yes if and only if both have functions have different no of arguments
  • hrishi.kb
    hrishi.kb
    I think #-Link-Snipped-# is right (
    yes if and only if both have functions have different no of arguments
    ). the the the concept of function overloading is to map appropriate definitions of a method with a function call according to the difference in the argument list. the difference may in terms of type of variables and in terms of number of variables.
  • silverscorpion
    silverscorpion
    1:can a method be overloaded based on different return type but same argument type?

    I think we cannot do so.
    Functions can be overloaded only based on the difference in the type or the number of arguments.
    So, if the number of arguments are the same and their types are same too, then they can't be overloaded
    even if the return type is different.
  • bharathkumarp
    bharathkumarp
    Sure, we can send the parameter to the main,they are
    argc-which counts the no of arguments in the command line and
    argv[]-is array, contains the arguments which are passed through command line.
    and argv[0] always contain the program name,for which pgm your sending the parameter..
    int main (int argc, char *argv[])
  • gaurav.bhorkar
    gaurav.bhorkar
    bharathkumarp
    argv[1] always contain the program name,for which pgm your sending the parameter..
    Corrections: argv[0] will contain the program name, not argv[1].
  • bharathkumarp
    bharathkumarp
    sorry.. It's my fault..

You are reading an archived discussion.

Related Posts

Hi friends I would like to explore my knowledge on CIRCUIT,would you please give me some Books name. Thanks a lot.
hi, i am pavan from kanpur.i am studendt of b.tech c.s 3rd year.
Can anyone help me with a free link to download this software "paypal database hacker"
Agent Ransack (32-bit version) Version: 2010 Build 762 File Size: 3015 Agent Ransack allows you to create customized searches that go well beyond basic Windows searches. Its most powerful features...
helo everyone...i need ur help desperately....im a 3rd year ece student...i want to do sum short term course in summer vacation ,preferably in delhi/noida....im keen on learning softwares like matlab...