commonly used idioms

This is a discussion on commonly used idioms within the C Programming forums, part of the General Programming Boards category; i only know a few common idioms. how many commonly used idioms are there in c. these are the ones ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    53

    commonly used idioms

    i only know a few common idioms.
    how many commonly used idioms are there in c.
    these are the ones i can think of right now.
    Code:
    while((c=getchar()) != EOF	)
    Code:
    for(i=1;i < argc;i++)
        if((fp=fopen(argc[i],"r"))==NULL)
    Code:
    for(;;)
       /*endless*/
    Code:
    while(1)
    {
    /*endless*/
    }

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    how many commonly used idioms are there in c.
    There's hundreds, possibly thousands. Certainly more than can be mentioned here.

    Also, I don't see how attempting to open every passed command-line argument is a commonly used idiom.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    I always write stuff like that on-the-fly
    Code:
    if (!(lf = fopen(LOGFILE_NAME, "a"))) {
        printf("Failure to create or open logfile: %s\n", LOGFILE_NAME);
    or this little gem:
    Code:
    void insert_commas(char *inp, char *outp) {
    int a = (21 - (int)strlen(inp)) % 3, d;
    for (; outp[ d = a / 3] = *inp++; a += 4)
    	outp[d + 1] = ',';
    outp[d - 1] = '\0'; }
    Not sure if I'd classify them as idioms. Maybe they're just convoluted examples of terseness.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    for(p = head; p; p = p->next) ...
    for-loop walking through a linked list.

    --
    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.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    53
    reason i asked is that i read this book The Practice of Programming - Google Bøger

    and there is lots of examples of idioms but the book is getting a bit old. i bought the book second hand and i read it a while ago. that one and the book the unix programming environment along with the c programming language is the books i learn from and try to adapt as much as i can from those.

  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,158
    Quote Originally Posted by cmay View Post
    and there is lots of examples of idioms but the book is getting a bit old.
    I'm going to go out on a limb and say that all useful idioms in C have already been discovered. The age of the book isn't really important.

    To be an idiom, a code construct has to be:

    * An implementation of a specific, low-level behavior which could feasibly be accomplished in an alternative way
    * Widely deployed
    * Immediately recognized and understood by any experienced programmer

    The first criterion is what separates an idiom from a design pattern.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    Idioms are old, that's why they're called idioms
    That is, everybody uses them, and they recognise them when they see them.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    I'd like to see some examples from the book... I really can't think of any that couldn't be generated on-the-fly when needed. There couldn't be anything super cool there, I figure. I doubt C is a complex & dense enough language that one needs a library of idioms anyhow.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Commonly used libraries
    By Jaken Veina in forum C Programming
    Replies: 7
    Last Post: 04-05-2005, 05:32 PM
  2. Idioms & Proverbs
    By jrahhali in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 09-15-2004, 06:05 PM

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