3

I've been teaching myself in C programming with the book recommended by a friend who is great in C. The book title is "Programming in C" by Stephen Kochan.

I have a background in Java, and I feel a little bit crazy with the way the codes were written in Stephen's book. For example, the following code, in which I commented my confusion. Maybe I'm missing something important here, so I'm looking to hear some inputs about the correct way of coding in C.

#include <stdio.h>

void test(int *int_pointer)
{
  *int_pointer = 100;
}

int main(void)
{
  void test(int *int_pointer);  // why call the test() function here without any real argument? what's the point?
  int i = 50, *p = &i;

  printf("Before the call to test i = %i\n", i);

  test(p);
  printf("After the call to test i = %i\n", i);

  int t;
  for (t = 0; t < 5; ++t)   // I'm more used to "t++" in a loop like this. As I know ++t is different than t++ in some cases. Writting ++t in a loop just drives me crazy
  {
    if (4 == t)  // isn't it normal to write "t == 4" ?? this is driving me crazy again!
        printf("skip the number %i\n", t);
    else
        printf("the value of t is now %i\n", t);
  }

  return 0;
}
TonyW
  • 18,375
  • 42
  • 110
  • 183

4 Answers4

1

why call the test() function here without any real argument? what's the point?

Here test is declared as function (with void return type) which expects an argument of the type a pointer to int.

I'm more used to "t++" in a loop like this. As I know ++t is different than t++ in some cases. Writting ++t in a loop just drives me crazy

Note that, when incrementing or decrementing a variable in a statement by itself (t++; or ++t), the pre-increment and post-increment have same effect.
The difference can be seen when these expression appears in a large or complex expressions ( int x = t++ and int x = ++t have different results for the same value of t).

isn't it normal to write "t == 4" ?? this is driving me crazy again!

4 == t is much safer than t == 4, although both have same meaning. In case of t == 4, if user type accidentally t = 4 then compiler would not going to throw any error and you may get erroneous result. While in case of 4 == t, if user accidentally type 4 = t then compiler would through you a warning like:

 lvalue is required as left operand of assignment operator.
haccks
  • 104,019
  • 25
  • 176
  • 264
1

Please read about pointers then you will understand that a pointer to an int has been passed as an argument here...

void test(int *int_pointer); 

You can see the difference between ++t and t++ nicely explained in this link . It doesn't make a difference in this code. Result will be the same.

if(4 == t) is same as if(t == 4) . Just different styles in writing. 4 == t is mostly used to avoid typing = instead of ==. Compiler will complain if you write 4 = t but wont complain if you write t = 4

Community
  • 1
  • 1
Deepika Sethi
  • 213
  • 1
  • 2
  • 10
1

// why call the test() function here without any real argument? what's the point? It is not a call, it is function declaration. Completely unnecessary at this location, since the function is defined few lines before. In real world such declarations are not used often.

// I'm more used to "t++" in a loop like this. As I know ++t is different than t++ in some cases. Writting ++t in a loop just drives me crazy In this case they are equivalent, but if you think of going to C++ it is better to switch completely to ++t form, since there in some cases (e.g. with iterators) it makes difference.

// isn't it normal to write "t == 4" ?? this is driving me crazy again! Some people tend to use 4 == t to avoid a problem when t = 4 is used instead of t == 4 (both are valid in C as if condition). Since all normal compilers signal a warning for t = 4 anyway, 4 == t is rather unnecessary.

Wojtek Surowka
  • 20,535
  • 4
  • 44
  • 51
1

void test(int *int_pointer); is a function prototype. It's not required in this particular instance since the function is defined above main() but you would need it (though not necessarily in the function body) if test was defined later in the file. (Some folk rely on implicit declaration but let's not get into that here.)

++t will never be slower than t++ since, conceptually, the latter has to store and return the previous value. (Most compilers will optimise the copy out, although I prefer not to rely on that: I always use ++t but plenty of experienced programmers don't.)

4 == t is often used in place of t == 4 in case you accidentally omit one of the =. It's easily done but once you've spent a day or two hunting down a bug caused by a single = in place of == you won't ever do it again! 4 = t will generate a compile error but t = 4 is actually an expression of value 4 which will compare true and assigns the value of 4 to t: a particularly dangerous side-effect. Personally though I find 4 == t obfuscating.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483