Thread: Beginner question about understanding general Syntax

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    28

    Beginner question about understanding general Syntax

    I've been asking questions on the boards here and have gotten some great help, and I have been learning to program C through a book that is pretty good (so far)

    Anyway, there is going to be a time where I want to do something and it's not going to be covered in the book (It can't cover everything) and I may have difficult understand (or finding) someone that has used it in their code.

    I understand (the best way) is I can simply use the compiler's help file or find a list of syntax online. Well, my problem is actually understand what it's telling me (for ANY syntax not just the example below). Usually there are no examples of how to use it or if there are they are a little cryptic.

    I guess what I'm asking is in something like this:

    Code:
    <type> *p_subclass = dynamic_cast<<type> *>( p_obj );
    My main problem: How do I know what I "literally" type in vs. what I'm supposed to provide myself (or Inserting something over the top of one of those words).

    meaning what I'm using the above in my code can I just copy and paste that and that's it... or for example do I delete "*p_subclass" and put something in there of my own? also i assume where <type> is i'm supposed to put a type, well do i put it like <type>, type, <char>, or char?

    Also, Sometimes I'll see a syntax for a function and it will have a variable in there... and I don't understand if I can change that variable myself, if its just an example, or If I'm supposed to use exactly what it's telling me.

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    In help files, they use their own syntax, and there's no universal syntax everyone follow. The best answer is to understand enough to interpret what's left. You need to know what the keywords are, the operators, etc... and in general just have experience so you can recognize different patterns. The example you provide is actually C++, if I'm not mistaken, so I'll move your thread over there and someone who knows C++ better can probably explain the symbols more reliably than I can.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you're seeing that in a C book, then you're hallucinating. In a C++ book, you'd be okay.

    Anyway: you need to know what all the words mean, and sometimes that depends on context. (For instance, sometimes in a template context you may want to type the word "type" literally, although T is far more common, but in a "standard" function you would always want to replace it.) If you know what <> means, what "dynamic_cast" means, and what "*" means (another highly-context-specific symbol), then you'll know what's a variable, what's a placeholder, and so on. Similarly if you have a basic grasp of what a function is, then you'll know when you can change a name in the prototype (hint: always) and so on.

    If you have a real, as in on-paper, book, then at the beginning they almost will always tell you the conventions: this font for literal things you type, this font for things that are placeholders (like your "type" above), etc.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The help is basically telling you to replace "<type>" with the "real" type you're working with. For example:
    int* p_subclass = dynamic_cast<int*>(p_obj);

    Now the question is: are you studying C or C++ and do you need to know what dynamic_cast is?
    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.

  5. #5
    Registered User
    Join Date
    Jul 2009
    Posts
    28
    I posted a bad example... I thought it was C but I guess it was C++

    I am studying C for now at least until I have a firm grasp on how to do everything I want to do.

    A better example is the toupper and tolower syntax... I looked those up because I was curious about them. It -looks- like it is easy to do but by looking at the syntax online it isn't clear to me how I use it in my code. Like was said earlier sometimes online they won't specific by the look of the font what you type in literally what's there vs. what your supposed to replace with a variable or something else of your own. also it can't tell if the syntax i saw for toupper and tolower is talking about a prototype, a function, or a statement

    this is what i would guess would work (i'm not at home so i can not test this) ... am i correct?

    Code:
    int main() {
    
    char firstname[23];
    
    printf( "Please enter your FIRST name: " );
    scanf( "%[^\n]%*c", firstname );
    
    printf("\nYour first name in all lower case is: %c", tolower( firstname ) );
    
    // the above calls the tolower() function and converts the contents of firstname
    // to all lowercase and then displays it in printf
    
    return 0;
    
    }

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So if you looked up tolower at all, you saw this:
    Quote Originally Posted by man tolower
    Code:
    int tolower(int c);
    Hopefully by now you should recognize a function prototype when you see one, and this is one, and so you should recognize it as such. The things inside the parentheses are what goes in -- in this case an int -- and it returns an int back. Continuing:
    Quote Originally Posted by man tolower
    The tolower() function converts an uppercase letter to the corresponding lowercase letter.

    PARAMETERS
    c
    An integer, the value of which is representable as an unsigned char, or the value of the macro EOF.

    RETURN VALUES
    If the parameter is a character for which isupper() is true and there is a corresponding character for which islower() is true, the tolower() function returns the corresponding character. Otherwise, the parameter is returned unchanged.
    This tells you specifically what the input should be -- a character -- and what the return will be -- that character convernted to lowercase (if possible/necessary).

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    For standard C, try the "man" pages (they're freely available on Google). They're always for *nix-y systems, but they have a huge amount of explanation about what each parameter is for, what certain options, do, etc..

  8. #8
    Registered User
    Join Date
    Jul 2009
    Posts
    28
    int tolower(int c);

    ok... now i see it's a function prototype because of the ; at the end. however, the (int c) part throws me off a little... if (c) is supposed to be a character why don't they say "char"? also int c or int char wouldn't make any sense, so the part in the ( )'s is still throwing me off a little... i understand tolower will return some value because otherwise it would be void.


    Quote Originally Posted by tabstop View Post
    So if you looked up tolower at all, you saw this:

    Hopefully by now you should recognize a function prototype when you see one, and this is one, and so you should recognize it as such. The things inside the parentheses are what goes in -- in this case an int -- and it returns an int back. Continuing:

    This tells you specifically what the input should be -- a character -- and what the return will be -- that character convernted to lowercase (if possible/necessary).

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by lucidrave View Post
    int tolower(int c);

    ok... now i see it's a function prototype because of the ; at the end. however, the (int c) part throws me off a little... if (c) is supposed to be a character why don't they say "char"? also int c or int char wouldn't make any sense, so the part in the ( )'s is still throwing me off a little... i understand tolower will return some value because otherwise it would be void.
    It takes int because it accepts EOF, which is not a character. Why doesn't int c make sense?

  10. #10
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It may have to do something with EOF not being within char's range. If you can get an int (EOF) from input as with getchar, I suppose they figured it would be sort of nice if a character processing function would be able to handle that.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  11. #11
    Registered User
    Join Date
    Jul 2009
    Posts
    28
    i'm not sure what the function prototype has to do with end of file. i think i'm probably using something i'm not advanced enough to be dealing with yet.

    to me it looks like they are declaring c as an interger within the function prototype.

    i just made up this code below, so is my understanding correct? or is my logic off (and this code?)

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int tolower(int c);          //function prototype tolower
    
    int main() {
    
    char firstname[20];
    
    strcpy( firstname, "BRYAN" );
    printf( "my name in all lowercase is %c", tolower( firstname ) );
    
    /* is something like this needed below???
    int tolower( int c ) {
    
    blah blah blah stuff needs to go here...
    blah ..............
    is this "built into" the header file somewhere or is it a function i have to make?
    
    } */
    
    return 0;
    
    }
    Last edited by lucidrave; 08-13-2009 at 02:55 PM. Reason: minor edits

  12. #12
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You do know that char is also an integer type (only with a small range) and conversions between int and char happen automatically?

    EOF is read when you press something like Ctrl + Z (or when you get to the end of a file, I guess).

    ------

    Anyway in your example you are not passing a char nor an int to tolower, but instead an array of characters. Tolower only works on a single character, so to convert whole strings you need a loop that applies tolower to all characters in the string individually.
    Last edited by anon; 08-13-2009 at 03:05 PM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by lucidrave View Post
    i'm not sure what the function prototype has to do with end of file. i think i'm probably using something i'm not advanced enough to be dealing with yet.

    to me it looks like they are declaring c as an interger within the function prototype.

    i just made up this code below, so is my understanding correct? or is my logic off (and this code?)

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int tolower(int c);          //function prototype tolower
    
    int main() {
    
    char firstname[20];
    
    strcpy( firstname, "BRYAN" );
    printf( "my name in all lowercase is %c", tolower( firstname ) );
    
    /* is something like this needed below???
    int tolower( int c ) {
    
    blah blah blah stuff needs to go here...
    blah ..............
    is this "built into" the header file somewhere or is it a function i have to make?
    
    } */
    
    return 0;
    
    }
    Basically, there's not a line there that's right (except for strcpy). You know that firstname isn't a character, and you know that tolower accepts a character, so you know that you can't pass firstname to tolower.

    So, your book has been doing a good job so far you said. You should move on a little farther and read the section on functions.

  14. #14
    Registered User
    Join Date
    Jul 2009
    Posts
    28
    i figured since firstname is a string and that is just an array of characters than it would work. ok, i get that now

    so do i need to do a loop until it reaches NULL /0 in the array and convert each character to lowercase...

    the book i'm reading has covered functions to a point, but not everything there is to know about them... how to call them, how to prototype them, how to use return(); to return a value and how to pass using pointers (I'm still trying to understand the pointers part)

    anyway, am I getting closer to being correct with something like this?

    Code:
    strcpy ( firstname, "BRYAN" );
    
    for (i = 0, i != NULL, i++) {
    printf( "%c ", tolower( firstname[i] ) );
    }

  15. #15
    Registered User
    Join Date
    Jul 2009
    Posts
    28
    i see that firstname is a string and tolower won't accept strings, but if i did tolower( firstname[1] ); i figure tolower is only looking at the "R" in a name like "BRYAN"

    if that doesn't work other, i'll just go back to where i was learning before in the book. i tend to try and learn things on the side like this even if i'm not ready for them

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Question: Problem with scanf and strings. Help!
    By lucidrave in forum C Programming
    Replies: 8
    Last Post: 08-11-2009, 10:22 PM
  2. Beginner Question about Pointers in C
    By lucidrave in forum C Programming
    Replies: 2
    Last Post: 08-01-2009, 03:15 PM
  3. Avoiding Global variables
    By csonx_p in forum Windows Programming
    Replies: 32
    Last Post: 05-19-2008, 12:17 AM
  4. Replies: 8
    Last Post: 09-09-2005, 12:34 AM
  5. linked list inside array of structs- Syntax question
    By rasmith1955 in forum C Programming
    Replies: 14
    Last Post: 02-28-2005, 05:16 PM