Thread: RFC: A Simple Course in C

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    1

    RFC: A Simple Course in C

    Hello All,

    I am writing a book on C programming and would welcome your review/comments and suggestions on the same. You can download the latest draft on the following link.

    A Simple Course in C

    Warm Regards,
    Constantine

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by johnxconstantin View Post
    Hello All,

    I am writing a book on C programming and would welcome your review/comments and suggestions on the same. You can download the latest draft on the following link.

    A Simple Course in C

    Warm Regards,
    Constantine
    Do I get a cookie for spotting this?

    Code:
    void main() { printf(“hello world\n”); return 0; }

  3. #3
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    OK, some things I've spotted:
    Code:
    main()
    {
      char a;
      int i=10;
      printf(“\nThe value of i is %d”,i);
      a=getchar();
      if(a==’a’)
      {
        int i=1;
        printf(“\nThe value of i is %d”,i);
      }
      printf(“\nThe value of i is %d”,i);
    }
    It should be "int main(void)", your way is the older K&R style. And "a" should be of type int to check for EOF (explain this further)

    Code:
    #include <stdio.h>
    void multi_table(int i);
    main()
    {
      int i;
      multi_table(i);
      printf("\nValue of i is %d",i);
    }
    void multi_table(int i)
    {
      printf("\n 1 x %d = %d",i,i*1);
      printf("\n 2 x %d = %d",i,i*2);
      printf("\n 3 x %d = %d",i,i*3);
      printf("\n 4 x %d = %d",i,i*4);
      printf("\n 5 x %d = %d",i,i*5);
      printf("\n 6 x %d = %d",i,i*6);
      printf("\n 7 x %d = %d",i,i*7);
      printf("\n 8 x %d = %d",i,i*8);
      printf("\n 9 x %d = %d",i,i*9);
      printf("\n10 x %d = %d",i,i*10);
      i=0;
    }
    "i" is garbage in main. You didn't initialize it. The "i = 0" statement changes only a _copy_ of i.

    "The compiler would place variables of this type into the read‐only memory of the program." That's not guaranteed, const values can be changed by pointers:

    Code:
    const int i = 0;
    int *pointer = &i;
    *pointer = 10;
    "Generally when a program is implied, the compiler optimizes certain expressions by assuming that a variable’s value is unchanging if it does not occur on the left side of an assignment".

    Maybe that's correct and I'm a nerd, but i++ is not an assignment. And it may be changed by pointers.

    "auto is used to create a variable, usually a temporary variable, within a block of code. The variable is only available to the block of code it has been defined within and cannot be referenced outside of the block of code in which it is being used."

    Note that auto is the default.

    In general you are using some bad coding practices. And compile your programs first with all warnings enabled.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating very simple text editor using c
    By if13121 in forum C Programming
    Replies: 9
    Last Post: 10-19-2010, 05:26 PM
  2. Simple message encryption
    By Vicious in forum C++ Programming
    Replies: 10
    Last Post: 11-07-2004, 11:48 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 05:48 AM
  5. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 08:52 AM