5

I came across this C program in a blog post:

main()
{
    int n;
    n = 151;
    f(n);
}

f(x)
int x;
{
    printf("%d.\n", x);
}

The post doesn't explain it. Can anybody explain what this weird function definition means?

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
opu 웃
  • 464
  • 1
  • 7
  • 22
  • 2
    Please don't change the code/question once you ask. This might be misleading to people who have a look at the answer and find it unrelated. – Vishal R Mar 21 '14 at 07:03
  • @ruakh, You have changed my entire code. There was an another code before the code you've have included in my question. So I've to edit this one more. – opu 웃 Mar 21 '14 at 07:08
  • What part of this code don't you understand? Please be specific what you need explained. – Raymond Chen Mar 21 '14 at 07:14
  • @vishram0709, Scotia: Mea culpa. – ruakh Mar 21 '14 at 14:48

5 Answers5

10

This is the K&R style of C code, it's still valid C syntax, but I suggest you not using it in new-written code.

f(x)
int x;

is equivalent to ANSI C:

void f(int x)

K&R C is the C language described in the first edition of the book C programming language by Brian Kernighan and Dennis Ritchie, and named after the two authors. The second edition of the book updated to use ANSI C.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • 2
    It's still _valid,_ even in C11 so, while you are correct in _suggesting_ it shouldn't be used, saying "don't use it" could be considered a little too strong. Especially since glibc uses it :-) – paxdiablo Jun 27 '14 at 05:13
6
f(x)
int x;
{
    printf("%d.\n", x);
}

is an older way of defining function. Now it can be read as

void f(int x)
{
    printf("%d.\n", x);
}
Sakthi Kumar
  • 3,047
  • 15
  • 28
1

This code is simply bad.

  1. Obsolete declaration of function f()
  2. There should be forward declaration of f() before main()
  3. Return type is missing for f()
Robert Mutke
  • 2,757
  • 1
  • 16
  • 14
  • 1
    Also, the compiler may make `f()` default to int as return type, which in turn will lead to the compiler complaining for non returned value. – Asblarf Mar 21 '14 at 07:03
0

please change your c learning source!

that is a very old style of C (K&R C).

f(x)

int x;
{}

is equivalent to

void f(int x)
{}

you should really not wasting time learning that.

look for sources that teach ANSI C C89/C90 and also note the new features of C99 (that isn't widely adopted by many compilers, so know the differences)

vlad_tepesch
  • 6,681
  • 1
  • 38
  • 80
  • @RobertMutke have you ever worked on some serios industrial products? the most compiler for the embedded systems that are certified does not actually support c99. – vlad_tepesch Mar 21 '14 at 07:05
  • Let me disagree with you. gcc, keil, iar fully support c99 – Robert Mutke Mar 21 '14 at 07:07
  • @RobertMutke gcc does not fully support c99! and you seldem will use gcc in safety relevant products. also i haven't sad that c99 should be ignored. i just sad 'know the basics' and know the differences. – vlad_tepesch Mar 21 '14 at 07:09
  • @RobertMutke also look at this thread: http://stackoverflow.com/questions/139479/how-universally-is-c99-supported and this link http://gcc.gnu.org/c99status.html – vlad_tepesch Mar 21 '14 at 07:12
-2

This is a very old style of coding. This doesn't work out in ANSI. Better use something like

void f(int x)
{
 ... ... ...;/*Whatever is required*/
}
Moriarty
  • 1
  • 3