Can someone help explain these basic consepts to me please?

This is a discussion on Can someone help explain these basic consepts to me please? within the C Programming forums, part of the General Programming Boards category; Hi, Im trying to learn C and ive been reading through the tutorials on this site but im getting stuck ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    93

    Lightbulb Can someone help explain these basic consepts to me please?

    Hi,

    Im trying to learn C and ive been reading through the tutorials on this site but im getting stuck with a few things so I wonder if anyone would try to help me understand them.

    First thing, int main(). From what i understand this line is used to define if there are any command line arguments for the program. Is that its only job? Why is it always int, what about if there are letters in the arguments, would it be char main()? If there are no arguments, would it really matter what is put at the start (int, float etc)?

    Secondly, when using scanf, im having a hard time understanding why a & sign is used before the variable. It seems to work without it, is it bad practice?

    Finally with the functions, why is the function defined as a prototype above with pretty much the same information inlcuded in the actual function, for example:

    int mult ( int x, int y );

    Is this really nessesary or is it justgood coding practice?

    Thank you for any help you can give.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,931
    Quote Originally Posted by spadez
    First thing, int main(). From what i understand this line is used to define if there are any command line arguments for the program. Is that its only job? Why is it always int, what about if there are letters in the arguments, would it be char main()? If there are no arguments, would it really matter what is put at the start (int, float etc)?
    int is the return type, and the main function must be defined with that return type (with an exception that you need not be concerned with at the moment). You might want to read the tutorial on command line arguments.

    Quote Originally Posted by spadez
    Secondly, when using scanf, im having a hard time understanding why a & sign is used before the variable. It seems to work without it, is it bad practice?
    You need to understand a little on pointers to understand why this is so. Whether to use the address of operator depends on context. I suggest that you read the tutorial on pointers.

    Quote Originally Posted by spadez
    Finally with the functions, why is the function defined as a prototype above with pretty much the same information inlcuded in the actual function, for example:

    int mult ( int x, int y );

    Is this really nessesary or is it justgood coding practice?
    It is not necessary since you can declare the function as having an unknown number of parameters, but that is generally bad practice.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by spadez View Post
    Hi,

    Im trying to learn C and ive been reading through the tutorials on this site but im getting stuck with a few things so I wonder if anyone would try to help me understand them.

    First thing, int main(). From what i understand this line is used to define if there are any command line arguments for the program. Is that its only job? Why is it always int, what about if there are letters in the arguments, would it be char main()? If there are no arguments, would it really matter what is put at the start (int, float etc)?
    No. The int is the return type of the program (which is required to always be int). The parameters that the program accepts from the command line go inside the parentheses. So in this case int main() doesn't take any command line parameters.
    int main( int argc, char* argv[] ) does take command line parameters; argc tells you how many parameters, and argv is an array of strings for each parameter.

    Quote Originally Posted by spadez View Post
    Secondly, when using scanf, im having a hard time understanding why a & sign is used before the variable. It seems to work without it, is it bad practice?
    Putting & before a variable name returns the address of that variable, which is what scanf() needs in order to change that variable. If you just pass the variable by itself, you're passing a copy of that object and scanf() would be modifying the copy instead of the original.

    Quote Originally Posted by spadez View Post
    Finally with the functions, why is the function defined as a prototype above with pretty much the same information inlcuded in the actual function, for example:

    int mult ( int x, int y );

    Is this really nessesary or is it justgood coding practice?

    Thank you for any help you can give.
    Code is compiled from top to bottom, so if a function tries to call mult() before it's declared, the compiler will complain that it doesn't know what mult() is.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  4. #4
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    1) I think this has sufficiently been covered above. Some implementations allow you to do void main(), and you'll see this in a lot of code, but it's a bad practice, and it's actually against the standard. Your OS expects a return value when it runs the process.

    2) Putting the name of a char array "decomposes" into a pointer to the first object, so my_array and &my_array[0] are effectively the same thing - that's probably why it works for you. Pointers / references like this are used to pass strings, so that's why functions require a pointer, and why it works when you specify the name of the array

    3) Can't add much...

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    93
    Hi, thank you for the help.

    I know now that main has to be an int, but i dont really understand why. Is it because the program always returns a number? Is this going back to return 0 means the program is completed sucessfully and none zero is an error. What exactly is the porpose of the int main, just to signify the start of the body of the code?.

    For part two, am i right in saying that the & sign points to the location of the variable instead of the actual variable itself?

    Finally for part three the code is as follows:

    Code:
    #include <stdio.h>
    
    int mult ( int x, int y );
    
    int main()
    {
      int x;
      int y;
      
      printf( "Please input two numbers to be multiplied: " );
      scanf( "%d", &x );
      scanf( "%d", &y );
      printf( "The product of your two numbers is %d\n", mult( x, y ) );
      getchar(); 
    }
    
    int mult (int x, int y)
    {
      return x * y;
    }
    However since it is being defined at the start (because the program executes from the top), would it not be possible to code it like this:

    Code:
    #include <stdio.h>
    
    int mult ( int x, int y );
    
    int main()
    {
      int x;
      int y;
      
      printf( "Please input two numbers to be multiplied: " );
      scanf( "%d", &x );
      scanf( "%d", &y );
      printf( "The product of your two numbers is %d\n", mult( x, y ) );
      getchar(); 
    }
    
    int mult 
    {
      return x * y;
    }
    I have removed a portion of the code because it is already being defined at the start of the code. Is this not possible? It seems like its defining it twice.

    Again thank you for your help, im sorry my questions are probably dumb but ive been reading the tutorials on this site and some things are still not clear.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    It's not defining it twice, it's declaring it once and then defining it once.
    You could get rid of the declaration by moving the definition above main() like this:
    Code:
    #include <stdio.h>
    
    int mult (int x, int y)
    {
      return x * y;
    }
    
    int main()
    {
      int x;
      int y;
      
      printf( "Please input two numbers to be multiplied: " );
      scanf( "%d", &x );
      scanf( "%d", &y );
      printf( "The product of your two numbers is %d\n", mult( x, y ) );
      getchar(); 
    }
    Usually you would put the declaration into a header (.h) file and the definition into a source (.c) file.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    93
    Ok, that makes more sence. Is anyone able to help me with the first two questions?

  8. #8
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by spadez View Post
    Hi, thank you for the help.

    I know now that main has to be an int, but i dont really understand why. Is it because the program always returns a number? Is this going back to return 0 means the program is completed sucessfully and none zero is an error. What exactly is the porpose of the int main, just to signify the start of the body of the code?.

    .
    every function is called by another function.but main is a special case, the OS calls it and in turn expects it to return some value and that value is 0 if the program has successfully terminated and a non-zero value signal abnormal termination.since 0 is taken as int that's why int main(void) is used.
    Quote Originally Posted by spadez View Post
    For part two, am i right in saying that the & sign points to the location of the variable instead of the actual variable itself?

    .
    yes you're right.'&' points to the variable's address in the memory where it is stored.for eg
    int i=10;
    then '&i' is not 10 but its address where 'i' is stored.and scanf requires this address to store i's value.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,785
    Quote Originally Posted by BEN10 View Post
    every function is called by another function.but main is a special case, the OS calls it and in turn expects it to return some value and that value is 0 if the program has successfully terminated and a non-zero value signal abnormal termination.since 0 is taken as int that's why int main(void) is used.
    That is simply not true. There are no defined values for success or error and the OS usually doesn't even care what it returns. It's simply a user-defined value that you return.
    Now, other programs can pick up this value that you return and interpret it in some way. That's what its purpose usually is. What the numbers means is up to the implementor, the coder - ie YOU.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by BEN10 View Post
    every function is called by another function.but main is a special case, the OS calls it and in turn expects it to return some value and that value is 0 if the program has successfully terminated and a non-zero value signal abnormal termination.since 0 is taken as int that's why int main(void) is used.
    That is a good simplifeid explanation. Technically speaking, main isn't known to the OS - the executable file contains a value called "start address", which is where the application is supposed to start. In most cases, that will not be main() itself, but some C runtime startup code, that happens inside the startup code, after doing various steps to set the environment so that C code can be run (setting up a stack pointer, sorting out the arguments to main: argc, argv; and such things).

    The return address from main is then given to the OS in some way - usually, there is a call to the OS to say "exit now, and my 'result' is X". This allows OTHER programs to figure out if it went OK or not - you can then use the reuslt to say "well, that worked, so lets do the next step" or "that didn't work, so lets tell the user and quit". This is what the compiler does when compiling your code in an IDE - the IDE will know how to compile and link, but it will only do the linking step if you got through the compile step. [Linking is what compines your code with library functions such as printf and the startup code that sets the environment up for the application].

    If this is confusing you, please just ignore it.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    @Elysia
    it means we can use any value as return value.for eg there's no problem in return 1,return 2 etc but it has to be int or it can be float,char also.
    @matsp
    can you give me some link where it is explained in detail.

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,785
    It must be int because the standard says so.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by Elysia View Post
    It must be int because the standard says so.
    i just executed a simple "hello world" code with different return values(return 2,return 2.0,return '2') and in all the cases the program ran successfully.does the standard also says that returned value has to be 0.or return 1 is also according to the standards.

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,785
    As mats says, the OS doesn't really care what you return. It's like a message for any program that wants to know the state of your program (ie how it ran), if it ran well or bad.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    plz be definite.is return 1 according to the standards or not? i just wanna know it although i'll use return 0.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 12:54 AM
  2. Basic 3D gfx tutorial?
    By ichijoji in forum Game Programming
    Replies: 6
    Last Post: 05-02-2003, 08:21 PM
  3. Basic windows programming
    By Remedy in forum C Programming
    Replies: 3
    Last Post: 04-22-2003, 11:04 PM
  4. Visual Basic
    By The15th in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 10-08-2001, 01:50 AM
  5. Can someone explain "extern" to me?
    By valar_king in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2001, 12:22 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21