Paramaters, variables, strings, arguments

Discussion in 'Mac Programming' started by cybrscot, Jan 5, 2011.

  1. cybrscot macrumors 6502

    cybrscot

    Joined:
    Dec 7, 2010
    Location:
    Somewhere in Southeast Asia
    #1
    Just a quick newbie question.....

    When we talk about this programming stuff, are parameters synonymous with variables, and are strings synonymous with arguments?

    lets say,

    int x ;
    int y ; Are these both considered parameters and variables?

    and,

    printf strings and scanf strings, Are these both considered arguments as well as strings?
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    Your book is doing you some serious disservice if it hasn't covered basic nomenclature.

    Code:
    void myFunction(int iAmAParameterOrArgument) {
      int iAmAVariable;
    }
    Arguments/Parameters also happen to be variables (you can change them.. unless they're const... nevermind that for now), but the terms are not interchangable.

    I don't really know what you mean here. Generally in plain C when "string" is used, it means an array of characters terminated with a null. There is no actual string type in C. There are a lot of library functions that act on these null-terminated arrays of characters, and there is even a literal "I am a string literal" that will give you a pointer to a null terminated array of characters. In any event, ask this question a different way if the answer isn't covered.

    -Lee
     
  3. cybrscot thread starter macrumors 6502

    cybrscot

    Joined:
    Dec 7, 2010
    Location:
    Somewhere in Southeast Asia
    #3
    It has gone over variables which are of types int and/or float, and the book calls printf's "string literals"

    The parameters and arguments it has not covered yet. However, when I get errors in my compiling, often it makes reference to "invalid argument". So since I did not know what an argument was, I looked it up. But it seemed confusing because I interpreted the argument as being the "statement", or "string literal" in double quotes that followed the printf of scanf functions. Which is why I wanted clarification on whether what I considered "strings" to be the same thing as arguments. Or maybe their not strings at all, but rather arguments.

    Same with parameters, I looked them up also, as the book hasn't covered them yet. But I understood what I read as basically being the same thing as a variable.
     
  4. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    =( Hm.

    This is a bit difficult to clear up in a non-interactive setting such as this.
    printf is a function. It takes parameters (which you can also call arguments). The first one is a format string. It is generally a string literal (in "s). It does not have to be a literal, you can build up a format string elsewhere and pass that in. In this case, the string literal that appears immediately after the ( in your printf is the first parameter. It is a "string" i was just trying to be precise in the way C handles strings, because there are other languages with "proper" strings that are a lot better controlled that null-terminated "C strings". So:

    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv) {
      int x = 7;
      int y = 9;
      printf("This is the format string, argument one to printf. x: %d, y: %d\n",x,y);
    }
    
    In this segment main accepts two parameters, argc and argv. We are calling printf and passing it three parameters. The first is the string literal used as the format string, the second being the int variable x, the third being the int variable y.

    There are no variables that are int and float, but i don't think that's what you meant. There are other types, too, but if that's all they've introduced, that's fine. An argument/parameter can be any type, including the string literals, ints, floats, and others.

    "and the book calls printf's "string literals" " this doesn't make sense to me. printf is a function. The first argument/parameter can be a string literal. printf is not a string literal, it is the name of a function.

    This may not explain things clearly enough. Keep asking in different ways and we'll get there. At this stage there's a lot of things going on that have not been explained and you're trying to wrap your head around all of them. Things will hopefully fall into place soon.

    -Lee
     
  5. cybrscot thread starter macrumors 6502

    cybrscot

    Joined:
    Dec 7, 2010
    Location:
    Somewhere in Southeast Asia
    #5
    I follow you, I know printf is a function, sorry I was not clear, but I meant that what followed printf was a string literal printf ("string literal here:") ; so I was just referring to it in my first post as a "string". Of course, as I'm currently ignorant, I don't yet know that there are other "real" strings in other languages, or that C doesn't employ "proper" strings. So I can only ask the questions with the amount of information I have learned so far, (which is CH. 4 now :))

    And I wouldn't necessarily be trying to wrap my head around too much stuff so early, but the nomenclature I've learned so far doesn't match up with the errors I'm getting in my compiler when I make mistakes. When I see, "invalid argument", I'm like, "what the heck it that?". Since I want to fix my error, I'm forced to try and figure out what an argument is so I can look at the right part of my code to fix. It would be great if all the errors in the compiler started with "line 1", then "line 2", so it would be easy to find where the compiler has a problem with my code. The more I can understand what the compiler is saying, I figure the more quickly I can find and resolve my errors.

    Thanks for all your help everyday Lee! I feel like I ask too many questions, but unless there are other forums out there, I must take advantage of the forum as much as possible when I need help. It's all I've got other than the book. While I feel I'm doing okay teaching myself, it's certainly helpful to be in a real classroom setting with a real instructor, but I don't have access to that here. :D
     
  6. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #6
  7. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #7
    What seems to be vexing you a bit here is that some items have multiple names, and some pieces of your code are multiple things. The first parameter (or argument) to printf can be a string literal. It is both things. Things that aren't strings can be arguments/parameters as well.

    What compiler are you using? When i have an error with gcc i get:
    Where test.c is the source file, and 3 is the line where the error appears.

    There are certainly other forums out there, but this is the only one I frequent (so i guess i'm biased). I think people have progressed from mewling infant to crawling to walking here successfully, so I'd say the community is pretty good with helping people, even when they are in their coding infancy.

    -Lee
     
  8. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #8
    I had to drop to Terminal to check that myself.

    Note: Within Xcode (or most IDEs) the line number actually becomes a clickable hyperlink.

    B
     
  9. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #9
    Although most people use the terms parameter and argument interchangeably, they are actually different.

    The term parameter refers to the names defined by the function declaration or definition.

    The term argument refers to the values (or variables) passed to the function when the function is called.

    Consider the following code:

    Code:
    void myfunc(int a, float b, const char *c) {
       /* code omitted */
    }
    
    This defines a function called myfunc with 3 parameters. A int parameter called "a", a float parameter called "b", and a string parameter called "c". (BTW The first 2 happen to also be variables. The 3rd is not. It is a constant because of the const keyword).

    Now consider the following code:

    Code:
    int anInt = 1;
    myfunc(anInt, 2.5, "hello");
    
    This calls the myfunc function with 3 arguments: the variable anInt, the literal float 2.5, and the literal string "hello".

    As part of calling the function, the arguments are bound to the function's parameters. In C, this is done by effectively assigning the arguments to the parameters. In other languages, the binding is much tighter than this, where the parameters become another name for the arguments themselves.
     
  10. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #10
    It certainly is.

    I think this was the number one place I learnt to program really.

    P.S That Monty Python sketch is great.
     
  11. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #11
    jiminaus, thanks for that argument/parameter post!

    You kind of glossed over one thing that I think may still be eluding cybrscot.

    The "literal" bit, you mention "the literal float 2.5".

    Any time the value of a variable is explicitly spelled out in code it becomes a "literal"

    i.e.
    Code:
    int x,y,z;
    
    x = 2 + 3;
    y = 5;
    z = (x + y)/2;
    in words would be:
    the value of the variable int x is the sum of the literal int 2 and the literal int 3
    the value of the variable int y is the literal int 5
    the value of the variable int z is the sum of the values of the variable ints x and y divided by the literal int 2
    all three (x,y,z) have the value 5.

    The problem I have with cybrscot's interpretation of the book is that the first parameter for printf is a string (actually const char *), it doesn't have to be a literal string although many times it is. There are circumstances where you might build the format string dynamically in code depending on your needs and not use a literal string.

    Am I missing something?

    No it isn't! (Sorry again). :eek:

    B
     
  12. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #12
    we missed that the literal 2.5 is type double, 2.5f would be a float =).

    -Lee
     
  13. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #13
    Lee you are absolutely correct. I was thinking the compiler knows that in the context a float is required and would optimise the double literal into a float value. But that doesn't negate the fact that it's still a double literal.

    I've recently come back to good-old C and then on and upwards to Objective-C after being engaged in an Ada project for quite a while. I'm still struggling to drop Ada thinking.
     
  14. z0rt macrumors newbie

    Joined:
    Jan 3, 2011
    #14
  15. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #15
    "I told you once"
    Is there some ANSI C spec for that? Because when I compiled test code, a literal for a float generated "lfs f0, 0(r2)", but when I changed it to a double, it generated "lfd f0, 0(r2)" on a PPC. Not sure what an Intel would compile to, but I expect it would be essentially the same.
     
  16. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #16
    Being an ISO/ANSI spec it's not freely available AFAIK. A draft of the C99 spec (dated Sept 7, 2007) is available at http://www.open-std.org/jtc1/sc22/WG14/www/docs/n1256.pdf which should be good enough in regards to floating-point literals. Section 6.4.4.2 describes floating-point literals.

    BTW Are you looking at the assembler output with or without optimisations? The compiler may not optimise a double literal used in a float context into a float literal unless optimisations are turn on.
     

Share This Page