Thread: K & R Syntax for c!!

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    7

    Question K & R Syntax for c!!

    Hi,

    I am using the 16-bit bcc (not to be confused with gcc) compiler in ubuntu to compile my programs. This compiler checks for K&R syntax instead of the regular ANSI C syntaxes.
    Is it possible to configure the bcc tool to use ANSI C syntax?

    If it is not possible, then what is the K&R syntax to define the variable argument lists ? The ANSI C syntax is as follows :
    Code:
    int printf (char *str, ... ){
    }

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    If you are talking old style function definitions, I think it looked something like this:

    Code:
    int printf (str, ... )
        char *str;
    {
    }

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    7

    Unhappy

    Code:
    line 67: int printf (str, ... )
                 char *str;
                 {
                  }
    This doesnot work . .
    compiler errors:

    asm.c:67.18: error: need ')'
    asm.c:67.18: error: need '{'
    asm.c:67.18: error: bad expression
    asm.c:67.20: error: undefined structure element
    asm.c:67.22: error: compiler bug? - taking address of non-lvalue
    asm.c:67.22: error: need ';'
    asm.c:67.22: error: bad expression
    asm.c:68.8: error: need ';'
    asm.c:68.8: error: bad expression
    asm.c:71.3: error: bad expression
    asm.c:71.8: error: need ';'
    asm.c:71.11: error: need ';'
    asm.c:eof: error: need '}'

  4. #4
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    266
    And whats wrong with gcc? I'm not sure if it matters. But that should work. I don't have access to a compiler so I can't say.

  5. #5
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    You know, I have not run bcc, but why do you need to define your own version of printf? It seems to me that if you #include <stdio.h>, it is right there for you to use. Also, why burn up bandwidth on the net with your cross-posting? Give it some time to get an answer.

  6. #6
    Registered User
    Join Date
    Sep 2010
    Posts
    7
    Quote Originally Posted by Syscal View Post
    And whats wrong with gcc? I'm not sure if it matters. But that should work. I don't have access to a compiler so I can't say.
    This code needs to run in real mode.. and in this mode, cpu can only access 16-bt code. Hence using Bruce's C compiler and not gcc.


    Details:
    Bcc is a simple C compiler that produces 8086 assembler, in addition compiler compile time options allow 80386 or 6809 versions. The compiler understands traditional K&R C with just the restriction that bit fields are mapped to one of the other integer types.

  7. #7
    Registered User
    Join Date
    Sep 2010
    Posts
    7
    Quote Originally Posted by kermit View Post
    You know, I have not run bcc, but why do you need to define your own version of printf? It seems to me that if you #include <stdio.h>, it is right there for you to use. Also, why burn up bandwidth on the net with your cross-posting? Give it some time to get an answer.
    Need for printf:
    To ease my job of printing in real mode during Os booting. During real mode, dont have access to system libraries.

    Cross-posting:
    Jst increasing my chances of getting an answer by catering to different audience ..

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Increasing your chances of ........ing off the very people who might care to help you in the first place.
    How To Ask Questions The Smart Way

    Open the stdio.h header file for your K&R compiler, and see how it declares printf.
    IIRC, the ... notation is an ANSI invention.
    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.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    A little searching brings up a lead that asserts that kermit is correct, except that since the ... syntax is not supported, you should have just left it out when writing the function definition as suggested.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    It's been a while since I used K&R C, but IIRC it supported variable argument lists like this;

    Code:
    #include <varargs.h>
    
    int function(va_alist)
       va_dcl
    {
        va_list ap;
    
        /*  Assume we are interpreting the first argument as a char * and second as an int */
      
         char *firstarg;
         int secondarg;
    
         va_start(ap);
         
         firstarg = va_arg(ap, char *);
      
         secondarg = va_arg(ap, int);
    
         /*  do something with those two arguments  */
    
        va_end(ap);
    
         return 42;   /* Meaning of life ... */
    }
    To call such a function, it was not even necessary to have a prototype, as a K&R C compiler would implicitly declare any called (but undeclared) function as returning int, and accepting an arbitrary number of arguments.

    However, if one wants a prototype of such a function, the form is
    Code:
        int function();
    where "int" is optional.

    If you think the above is ugly .... yeah! Very early versions of K&R C did not allow implementation of functions with variable argument lists either - it was introduced later as a hack to allow functions like printf() to be implemented in K&R C. Before that, functions like printf() could not be implemented in K&R C, and had to be implemented in another language (eg assembler).

    As to whether bcc supports this .... no idea.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what am I missing? (Program won't compile)
    By steals10304 in forum C Programming
    Replies: 3
    Last Post: 08-25-2009, 03:01 PM
  2. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM