0

Case A:

  ‪#‎include‬<stdio.h> 

  int divide( int a, b) 
  { return 7; }

  int main() { 
    int a=divide(8,3); 
    printf("%d",a);
    return 0; 
  } 

Case B:

#include<stdio.h> 
int divide( a, b) 
{ 
  return 7; 
}

int main() 
{ 
  int a=divide(8,3);
  printf("%d",a); 
  return 0; 
} 

Why is Case A an error and Case B error free?

In Case B according to C99 standard, it assumes the variables to be of type int but then why not in case A, why is the type of b not considered to be of type int?

Pawan
  • 1,537
  • 1
  • 15
  • 19
radhika
  • 41
  • 2
  • why downvote this question? Its a valid question to me. – Pawan Aug 11 '15 at 17:26
  • Just explicitly give each variable its type.... – Isaiah Aug 11 '15 at 17:48
  • I'm voting to close this question as off-topic because, other than the OP, nobody cares. – Martin James Aug 11 '15 at 18:32
  • I up vote you, curiosity is always a good thing! – Jérémy Pouyet Aug 11 '15 at 19:37
  • @MartinJames With dues respect I want to say that I differ from your opinion. What might be stupid by your definition might be "INNOVATIVE" by someone else's view; and that's how disruptive innovation happens. If you don't want to answer, ignore the question; at the least, let's not demotivate anyone by calling the work stupid. – Pawan Aug 12 '15 at 06:42

1 Answers1

1

It looks like you are mixing old style argument list with new style argument list. Old style is name only with 'int' assumed, new style is type and name. For each additional name or type name a comma and the type and argument follows.

Additionally, for old style there should be declarations within the function for the argument types since they are not specified on the argument list.

This is in contrast to variables within the function which use commas to separate multiple variables of the same type, the functions' argument list uses commas to separate the list of arguments, each of which should be a type and name for new style.

eg:

int func(char arg1, long arg2, long arg3) {
  char a,b,c=2,d;
  long xPos,yPos;
  ...
}

vs:

int func(arg1, arg2, arg3) {
  int arg1,arg2,arg3;
  char a,b,c=2,d;
  long xPos,yPos;
  ...
}

see also C function syntax, parameter types declared after parameter list

Community
  • 1
  • 1
david binette
  • 51
  • 1
  • 7