Thread: Pretty printting

  1. #1
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751

    Pretty printting

    Just curious how much more work i'll need to do to pretty print the source code so each number is perfectly aligned.


    Code:
    /* multiplication table.c */
    
    #include <stdio.h>
    
    int main(void)
    
    {
    
        int row, clm;
    
       
        /* winxp system call erase if othe OS */
        system("TITLE Multiplication Table");
    
        for (row = 1; row <= 12; row++)
        {
           for (clm = 1; clm  <= 12; clm++)
              printf(" %d", row * clm);
           printf("\n");
    
            }
    
    
         getchar();
         return(0);
    
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Code:
    /* multiplication table.c */
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
      int row, clm;
    
      /* winxp system call erase if othe OS */
      system("TITLE Multiplication Table");
      for (row = 1; row <= 12; row++)
      {
        for (clm = 1; clm  <= 12; clm++)
          printf("%4d", row * clm);
        printf("\n");
      }
      getchar();
    
      return(0);
    }
    My best code is written with the delete key.

  3. #3
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    wow much better. thank you.

    Couple questions. Why was it necessary to inlcude the stdlib.h in order to format the ouput differently. I checked for the fucntions and there wasn't any you utilized.
    The stdlib header defines several general operation functions and macros.

    Macros:

    NULL
    EXIT_FAILURE
    EXIT_SUCCESS
    RAND_MAX
    MB_CUR_MAX

    Variables:

    typedef size_t
    typedef wchar_t
    struct div_t
    struct ldiv_t

    Functions:

    abort();
    abs();
    atexit();
    atof();
    atoi();
    atol();
    bsearch();
    calloc();
    div();
    exit();
    free();
    getenv();
    labs();
    ldiv();
    malloc();
    mblen();
    mbstowcs();
    mbtowc();
    qsort();
    rand();
    realloc();
    srand();
    strtod();
    strtol();
    strtoul();
    system();
    wcstombs();
    wctomb();
    Also I tried to format the output with a field width as well and I did not get the same ouput. My field width was 10 -15, i experimented randomly.
    I ask this because I read your tutorial at Flashdada about solving problems, today actually and I knew what it was I needed to do to solve it, but I think I got the logic wrong.
    So my question is how did you know the best width specifier would be 4. What is the logic behind it?

    Also on your tutorial it is very good, however, I'm sure it is a typo, but there is no header file and sevreal include statements, you might want to check it out for others like myself who download it. Thanks again for all your help and indirect advice.



    Okay never mind. I see the system call. Doesn't the compiler automatically include those header files necessary for system calls? kinda like
    Code:
    #include <stdio.h>
    
    ......
    .......
    set = sizeof(var)/* used and works without calling Ctype or string.h */
    .......
    .......
    Last edited by caroundw5h; 12-31-2003 at 10:58 PM.

  4. #4
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by caroundw5h
    wow much better. thank you.

    Couple questions. Why was it necessary to inlcude the stdlib.h in order to format the ouput differently. I checked for the fucntions and there wasn't any you utilized.
    The system() call needs stdlib.h

    So my question is how did you know the best width specifier would be 4. What is the logic behind it?
    12 * 12 = 144

    3 digits + 1 for spacing I'll bet

    Okay never mind. I see the system call. Doesn't the compiler automatically include those header files necessary for system calls? kinda like
    Code:
    #include <stdio.h>
    
    ......
    .......
    set = sizeof(var)/* used and works without calling Ctype or string.h */
    .......
    .......
    sizeof() is technically an operator (like + - *), not a function call. It is not in any header. And no, the compiler doesn't automatically include headers for you.

    Where did you get the idea it was part of ctype or string.h?
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  5. #5
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    My bad. Thanks for clearing that up. But how come i was able to make the system calls without the stdlib.h?
    Last edited by caroundw5h; 01-01-2004 at 12:12 AM.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > But how come i was able to make the system calls without the stdlib.h?
    The compiler just invents prototypes for functions it does not know about, based on the first use of that function. This is refererred to as implicit declaration.

    Some compilers can be made to warn when this happens, and I think C99 compilers will definitely warn you about it.

    For many of the standard library functions, the implicit declaration is good enough to generate correct code which does what you want. Long term, lack of prototypes will eventually bite.
    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.

  7. #7
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    so even though this works
    Code:
    int main()
    
    
    {
    
    .......
    ......
    
    myFUNCTION(PARAMETERS);
    return (0);
    }
    
    
    
    
    myFUNCTION(PARAMETERS)
    
    {
    
    DO THIS AND THAT;
    
    }

    I should really still declare a protoype first? Just want to make sure I understand it fully. Cause it seems like there are a lot of things you can do in C but really shouldn't.

    "Avoid putting barriers in your way, but give you the responsibilty of not abusing that freedom". Kinda like A god. I'll give you life and put sin in your way and some serious temptation, but don't touch it, its bad. Why put it there in the first place like: scanf(), implicit declarations etc..?

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I should really still declare a protoype first?
    Yes.

    Although you can get away with defining the function before you call it, this only works well in a single source file.

    You can write multi-file programs without prototypes if you want, but mistakes are easy to make and hard to diagnose. This is one of the reasons why prototypes were put into the ANSI-C standard (K&R didn't have such things).

    > Cause it seems like there are a lot of things you can do in C but really shouldn't.
    Yes there are.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pretty simple for you good programers...
    By eazhar in forum C++ Programming
    Replies: 5
    Last Post: 06-28-2002, 05:51 PM
  2. wanna read some code
    By snakattak in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 06-19-2002, 04:21 PM
  3. I need help with a pretty simple program please
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 05-29-2002, 10:53 AM
  4. Pretty Print Program
    By 2TheGrindStone in forum C++ Programming
    Replies: 5
    Last Post: 11-16-2001, 12:16 PM
  5. Pretty Optimistic
    By mithrandir in forum A Brief History of Cprogramming.com
    Replies: 42
    Last Post: 11-06-2001, 10:27 PM