CrazyEngineers
  • C/C++ : Question-1

    Updated: Oct 25, 2024
    Views: 1.0K
    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.
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • Gurjeet Singh

    MemberMay 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.
    Are you sure? This action cannot be undone.
    Cancel
  • Manish Goyal

    MemberMay 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
    Are you sure? This action cannot be undone.
    Cancel
  • Morningdot Hablu

    MemberMay 5, 2010

    hi friends,
    i think we can also pass arguments in main function by using function overloading.
    Are you sure? This action cannot be undone.
    Cancel
  • gaurav.bhorkar

    MemberMay 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
    Are you sure? This action cannot be undone.
    Cancel
  • sherya mathur

    MemberMay 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?
    Are you sure? This action cannot be undone.
    Cancel
  • ms_cs

    MemberMay 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++?
    Are you sure? This action cannot be undone.
    Cancel
  • sushant005

    MemberMay 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".
    Are you sure? This action cannot be undone.
    Cancel
  • Manish Goyal

    MemberMay 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
    Are you sure? This action cannot be undone.
    Cancel
  • hrishi.kb

    MemberMay 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.
    Are you sure? This action cannot be undone.
    Cancel
  • silverscorpion

    MemberMay 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.
    Are you sure? This action cannot be undone.
    Cancel
  • bharathkumarp

    MemberMay 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[])
    Are you sure? This action cannot be undone.
    Cancel
  • gaurav.bhorkar

    MemberMay 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].
    Are you sure? This action cannot be undone.
    Cancel
  • bharathkumarp

    MemberMay 16, 2010

    sorry.. It's my fault..
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register