CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2003
    Posts
    417

    Please tell me if I've written this correctly

    Hi friends,

    I am beginning to practice C from K&R. Although I am only familiar with the language, this time I want to invest time learning it
    thoroughly. I've written this practice excercise program to replace more than a single blank in an input with only a single blank in the
    output. Here's the code.

    Code:
    #include <stdio.h>
    
    /*A program that displays keyboard input to the monitor, but trimming
    more than a single space to a single space I've not replaced tabs with single 
    spaces yet*/
    
    #define SPACE 1
    
    void main()
    {
    
     int c, state;
     c=0;
     state=SPACE;
     while ((c=getchar()) != EOF)
     {
      if (!(state && (c==' '))) putchar(c);
    
      if (c==' ')
      {
       state=SPACE;
      }
      else
      {
       state= !SPACE;
      } 
     }
    }
    Although I've tested it four to five times, I don't know if I can be sure as to whether it is right, since I've made guesses about the
    BITWISE operators such as NOT to be the same as the logical NOT ! and the BITWISE AND to be the same as the LOGICAL AND (&&). Also, on purpose, I have not yet detected tabbed spaces.

    Could you gurus please tell me if I've written this program correctly?

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Please tell me if I've written this correctly

    Originally posted by Sathyaish
    Hi friends,

    I am beginning to practice C from K&R.
    What books do you have to use as a reference?
    Here's the code.
    Code:
    //...
    void main()
    That's int main(), not void main()
    Although I've tested it four to five times, I don't know if I can be sure as to whether it is right, since I've made guesses about the
    BITWISE operators such as NOT to be the same as the logical NOT ! and the BITWISE AND to be the same as the LOGICAL AND (&&)
    Again, a good C book will point out the differences. The bitwise AND/OR is not the same as the logical AND/OR.

    Basically, what you want to do is to learn the language properly, and not rely on guesses. Also, usage of a debugger is helpful in finding out what your program is doing.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Feb 2003
    Posts
    417
    Besides K&R, I have two three books on C, one Balaguru swamy, and another by two Indian authors, one by Yashwant Karnitkar and one by Johnathan Morrison (that's for C++ actually). I also have course books from NIIT, and have read a few tutorials. Besides, I have worked extensively on Win32 SDK. And I started out learning C++ as my first language so I am kinda familiar with it.

    That's int main(), not void main()
    I am aware of that, but I didn't want to return a value from main so I, on purpose, cast the return type as void.

  4. #4
    Join Date
    Feb 2003
    Posts
    417
    And I also realize I could have written

    Code:
    	/********************************************************************
    		if (c==' ')
    		{
    			state=SPACE;
    		}
    		else
    		{
    			state= !SPACE;
    		}
    		*/
    		//I could better write the above if construct as
    		state = (c==' ');
    		//**********************************************************************
    I'd be grateful for help. The debugger, I did use but since the functions getchar and putchar are buffered, as I've been explained, I found it difficult to use the debugger. I am told many C/C++ programmers find the Visual C++ debugger a nightmare to dream.

  5. #5
    Join Date
    Dec 2003
    Posts
    99
    Originally posted by Sathyaish
    Besides K&R, I have two three books on C, one Balaguru swamy, and another by two Indian authors, one by Yashwant Karnitkar and one by Johnathan Morrison (that's for C++ actually). I also have course books from NIIT, and have read a few tutorials. Besides, I have worked extensively on Win32 SDK. And I started out learning C++ as my first language so I am kinda familiar with it.



    I am aware of that, but I didn't want to return a value from main so I, on purpose, cast the return type as void.

    C++ int main() is ok. As we are dicussing C, it is strictly int main(void).

    regards

    Mike

  6. #6
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815
    What I don't understand: You say you already know C++, and you're now learning C. But C is a subset of C++, so all you have to learn are the limitations of C as compared to C++. Basically, if you know C++, you know C as well. Of course you will have to learn about the functions of the C standard library. However, the things you are asking here about bitwise or logical operators are exactly the same in C++ as they are in C.

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449
    Originally posted by Sathyaish
    I am aware of that, but I didn't want to return a value from main so I, on purpose, cast the return type as void.
    No. The return type for main() has always been int, even for ANSI C programs.

    The main() function is special in that you do not need to specify a return value. If you don't return a value, by rule, the return is implied to be a 0. You don't change the prototype for main() just because you're not returning a value.

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured