C/C++ : Question-1

Sahithi Pallavi

Sahithi Pallavi

@sahithi-oJZaYj Oct 25, 2024
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

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • Gurjeet Singh

    Gurjeet Singh

    @gurjeet-LUX7B1 May 4, 2010

    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

    @manish-r2Hoep May 4, 2010

    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

    @morningdot-6Xuj4M May 5, 2010

    hi friends,
    i think we can also pass arguments in main function by using function overloading.
  • gaurav.bhorkar

    gaurav.bhorkar

    @gauravbhorkar-Pf9kZD May 5, 2010

    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

    @sherya-mathur-cI63wY May 5, 2010

    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

    @ms-cs-Ab8svl May 6, 2010

    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

    @sushant005-tyt4WK May 6, 2010

    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

    @manish-r2Hoep May 6, 2010

    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

    @hrishikb-O8wlRD May 6, 2010

    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

    @silverscorpion-iJKtdQ May 7, 2010

    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

    @bharathkumarp-kwU5lr May 16, 2010

    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

    @gauravbhorkar-Pf9kZD May 16, 2010

    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

    @bharathkumarp-kwU5lr May 16, 2010

    sorry.. It's my fault..