LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-08-2009, 07:55 PM   #1
thelink123
Member
 
Registered: Aug 2008
Location: India, Kerala
Distribution: openSUSE 11
Posts: 46

Rep: Reputation: 15
Output of the C program?


Hi,

Please see the first program below :

Code:
/*1*/int main()
/*2*/{
/*3*/   extern int fun(float);
/*4*/   int a;
/*5*/   a = fun(3.14);
/*6*/   printf("%d",a);
/*7*/}

/*8*/int fun(aa)
/*9*/float aa;
/*10*/{
/*11*/   return ((int)aa);
/*12*/}
This code when compiled gives the following errors:

1. conflicting types for 'fun' at line no: 9

2. previous declaration of 'fun' was here at line no: 3

Why is this happening??

If i modify the above program as shown in the second program below :

Code:
/*1*/int main()
/*2*/{
/*3*/   extern int fun(int); /* float replaced by int */
/*4*/   int a;
/*5*/   a = fun(3.14);
/*6*/   printf("%d",a);
/*7*/}

/*8*/int fun(aa)
/*9*/int aa; /* float replaced by int */
/*10*/{
/*11*/   return ((int)aa);
/*12*/}
This code when compiled gives no errors.

Please tell me why the difference occurs between the first and second program??

thanks
thelink.
 
Old 06-08-2009, 08:06 PM   #2
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
It also works if your function declaration is int fun(float aa) {

And it works if you move your prototype statement out of your main program and have it outside of any program module.

That is interesting. You shouldn't be prototyping from within your main program.
 
Old 06-08-2009, 08:09 PM   #3
jamescondron
Member
 
Registered: Jul 2007
Location: Scunthorpe, UK
Distribution: Ubuntu 8.10; Gentoo; Debian Lenny
Posts: 961

Rep: Reputation: 70
Incidentally, what are you aiming to do with this code? What do you expect it to do? What would you like it to do? I mean, your first issue is reasonably simple, though I'd ask your Instructor/ Lecturer, and your second is a follow on from the first.


EDIT: Next time you post code, don't put those horrible line numbers in please, I know you may want the line numbers in your code, but they hamper readability

Last edited by jamescondron; 06-08-2009 at 08:11 PM.
 
Old 06-08-2009, 10:20 PM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Moved: This thread is more suitable in <Programming> and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 06-09-2009, 02:19 AM   #5
noctilucent
Member
 
Registered: Jun 2009
Distribution: slackware
Posts: 123

Rep: Reputation: 34
Code:
int main()
{
        extern int fun();
        /* ... */
It is left as an exercise to the OP to research old-style C function prototypes / definitions and understand why:

1. The above works.
2. Having "aa" of type int yields no errors.

Last edited by noctilucent; 06-09-2009 at 02:26 AM.
 
Old 06-09-2009, 05:49 AM   #6
thangappan
Member
 
Registered: May 2009
Posts: 52

Rep: Reputation: 16
Re:C programming

It is interesting.But syntactically it is funny.

Traditionally we define a function as

return_type function_name(parameters with its data type)
{
return value;
}

This is first time I have seen the code like this.

In your program if you follow the unique steps there will not be any error.

You are specifying the prototyping inside the function.In the case of integer it will not consider( This is my assumption).

Try to follow the coding rules.

Anyway I appreciate your curiosity.
 
Old 06-09-2009, 08:44 PM   #7
thelink123
Member
 
Registered: Aug 2008
Location: India, Kerala
Distribution: openSUSE 11
Posts: 46

Original Poster
Rep: Reputation: 15
Smile RE: Output of the C program?

Ya the question i asked is indeed very interesting. But as programmers we should have an answer for this, if the code i have given is in C language. The forum members have no right to question my curiosity. Its better not to have an opinion if one cannot find the solution(Referring to the boorish words from jamescondron)

Anyways thanks for all the replies. But in future kindly do not question the sincerity of a programmer. Find answers which are helpful.

Thanks to moderator for moving the Post to the correct place.

The bottom line is that LQ still cannot answer my question. Waiting for the answers.

All the best
thelink
 
Old 06-09-2009, 09:48 PM   #8
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by thelink123 View Post
Hi,

Please see the first program below :

Code:
/*1*/int main()
/*2*/{
/*3*/   extern int fun(float);
/*4*/   int a;
/*5*/   a = fun(3.14);
/*6*/   printf("%d",a);
/*7*/}

/*8*/int fun(aa)
/*9*/float aa;
/*10*/{
/*11*/   return ((int)aa);
/*12*/}
This code when compiled gives the following errors:

1. conflicting types for 'fun' at line no: 9

2. previous declaration of 'fun' was here at line no: 3

Why is this happening??

If i modify the above program as shown in the second program below :

Code:
/*1*/int main()
/*2*/{
/*3*/   extern int fun(int); /* float replaced by int */
/*4*/   int a;
/*5*/   a = fun(3.14);
/*6*/   printf("%d",a);
/*7*/}

/*8*/int fun(aa)
/*9*/int aa; /* float replaced by int */
/*10*/{
/*11*/   return ((int)aa);
/*12*/}
This code when compiled gives no errors.

Please tell me why the difference occurs between the first and second program??

thanks
thelink.
Because default type in "C" is int, so

/*3*/ extern int fun(float);

contradicts

/*8*/int fun(aa)

in function argument type, i.e. typeless aa mean it is int, and on line #3 you declared argument to be of float.
 
Old 06-09-2009, 11:08 PM   #9
thelink123
Member
 
Registered: Aug 2008
Location: India, Kerala
Distribution: openSUSE 11
Posts: 46

Original Poster
Rep: Reputation: 15
Thumbs up

Thats it...This solves the problem i think...thanks Sergei Steshenko for the answer

Thanks LQ

Regards
thelink
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Program : Not getting the desired output thelink123 Linux - Newbie 4 11-17-2008 01:59 AM
How to pass the output of one program to another program without backticks? BrianK Linux - General 4 11-11-2008 06:27 PM
Strange output from C program Gins Programming 8 07-11-2006 10:29 PM
program console output using C xrado Programming 5 04-04-2005 02:48 PM
C function to execute a program and return the output of the program ryan.n Programming 4 08-14-2004 10:11 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:12 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration