-
struct emp
-
{
-
char name[20];
-
int age;
-
};
-
fun(int aa)
-
{
-
int bb;
-
bb=aa*aa;
-
return(bb);
-
}
-
main()
-
{
-
int cc=fun(15);
-
printf("%d",cc);
-
}
I have a prob in that if i donot define the prototype of an function why this program runs correctly plz explain...........
22 1662
You need to mention the return type of the function.
Whether your prototype looks like above?
BTW, Do you get expected output?
Regards
Dheeraj Joshi
@dheerajjoshim
ya i get the expected output . u can check urself also....
How do you define function prototype?
In your code i can not see any prototyping.
Regards
Dheeraj Joshi
Banfa 9,065
Expert Mod 8TB
A function definition effectively declares the function prototype as well.
Since you only call the function after it is declared there is no need to a prototype the function as well.
@Banfa
but i did not mention the return type of an function why its work properly
It works because where there is no type, C defaults to int. So your function is similar to one that explicitly declares an int return type.
This behaviour is actually removed from the current C standard but compilers still support it for the sake of old code. It should not be relied on in new code.
Actually you could even have a variable like this... -
-
mytyplessvar = 0; //This will actually be treated as an int.
-
-
Regards,
Alex.
@myusernotyours
but about the prototype its not declare in that program but program works correctly
OK here goes.
This is an integer:
Now you can say:
No problem so far. However, let's assume you have a second implementation file and you want to use the value integer in that other file. Since the integer is outside the file, you code:
Now you can say:
and the compiler will mark value as external and this lets the linker locate the actual integer and make this code use the integer in the other file.
Now just do the same with a function: - int MyFunction(int a, int b)
-
{
-
int answer;
-
/* do some calculation here */
-
answer = a + b;
-
return answer;
-
}
-
-
int main()
-
{
-
int result = MyFunction(3,4);
-
}
This works fine because the compiler has seen the function. If this function happens to be in another implementation file then, like the int, you have to tell the compiler that the function is outside this file: - extern int MyFunction(int a, int b);
-
-
int main()
-
{
-
int result = MyFunction(3,4);
-
}
and the compiler lets the linker find the function to make the call. The extern is a default for functions and you need not supply it. When you leave it off you have: - int MyFunction(int a, int b);
-
-
int main()
-
{
-
int result = MyFunction(3,4);
-
}
and that is what is called a function prototype.
@weaknessforcats
if the return type is struct then we have to define the prototype whether i use this function the same file or in diffrent file is it true.......
weaknessforcat
That is false.
If a function returns a struct, then the struct must be declared before you use it or you get a compile error. This has nothing to do with where the code for the function is.
This is OK on a header file: - struct Data
-
{
-
int a;
-
int b;
-
};
-
-
Data MyFunction( int a, int b); /* a prototype */
You just follow the rule that you can refer to nothing that hasn't been previously declared or you will get a compile time error.
In the above case, the function is coded in another file. In that other file you must again declare the struct. Usually, by #including the same header file again. Each implementation file is separately compiled so each file must have a complete set of declarations.
Life will be simpler if you get in the habit of always providing function prototypes for all of your functions. Prototype for global functions should be in a header file. Prototypes for local (static) functions should be near the top of the source file.
True, there are situations where the prototype is not strictly needed, but I would rather spend scarce brain cells on more important issues than trying to figure out if I can get away with omitting a particular function prototype.
@donbock
so the 7 lines u mention in the code its define in header file.
ur means if i define the prototype of an function in header files
& this header file i use in other file
so in that file we have to define the prototype of an function again & after that, we give the defnition of that function......
so what is the use of header file if each & every thing we have to define again.. which we already define in a header file.
suppose i write the following code -
-
struct emp
-
{
-
char name[20];
-
int age;
-
};
-
emp fun(emp aa)
-
{
-
return(aa);
-
-
}
-
main()
-
{
-
emp bb,dd;
-
bb={"anang",23};
-
dd= fun(bb);
-
printf("%s %d",dd.name,dd.age);
-
-
}
-
ur mean this prongram works without defining the prototype of an function ...
but my question remain as its in case of int type function why we donot need to define the prototype of an function...
Are you familiar with the notion of constructing a program from several source files?
A function that you want to use in more than one source file needs to be a global function. You will want to have a prototype for that function in each source file that calls it; and that prototype must appear first, before the function is called. You could type the prototype into each source file, but that is tedious. Instead, it is more common to put all of the prototypes for your global functions into a header file and then include that header in each source file.
A function that is only used in one source file need not be global. My personal policy is to make all such functions local (that is, static). It is also my practice to have prototypes for all the static functions near the top of my source file. - program.h:
-
struct emp {
-
...
-
};
-
-
int func1(void);
-
struct emp func2(int a);
-
-
-
file1.c:
-
#include "program.h"
-
static int func3(int a);
-
-
int func1(void) {
-
int val;
-
...
-
val = func3(4);
-
...
-
}
-
-
static int func3(int a) {
-
struct emp w;
-
...
-
vv = func2(a);
-
...
-
}
-
-
-
file2.c:
-
#include "program.h"
-
-
...
-
struct emp func2(int a) {
-
int value;
-
...
-
value = func1();
-
...
-
}
@weaknessforcats
if i write the following code -
struct emp MyFunction(int a, int b)
-
{
-
code here
-
return(struct emp aa}
-
}
-
-
int main()
-
{ struct emp bb;
-
bb = MyFunction(3,4);
-
}
-
this is will work ? we donot define the function prototype
according to u compiller already see the function....
here we talk about single source file...
@donbock
tnx i m getting ur pointttttt .... can u plz see my orginal Question which i send
in that program prototype is not defined but programs work . plz clear my that thing also.....
by the way i m not familler construction a prog from several source file . but i get ur point.
struct emp MyFunction(int a, int b)
{
code here
return(struct emp aa}
}
int main()
{ struct emp bb;
bb = MyFunction(3,4);
}
This will work if struct emp is declared before MyFunction.
You only need the function prototype if MyFunction is being called in another .c file.
BTW: Multiple files in C/C++ programs is a way of life. You need to know how to do this.
@weaknessforcats
ur mean if i write the following code" -
struct emp MyFunction(int ,int);
-
before ur" MyFunction"
then its work
The question remain same this is the prototype of an function.we have to declare the prototype to run this program correctly.....
but in my original question program run correctly without defining the prototype
why?///////////
Banfa 9,065
Expert Mod 8TB
That has already been answered in posts 5 and 7 but to summarise - Since the definition appears before main, where you call the function and definitions automatically provide the function prototype the function has been prototyped before it was called.
- This is C therefore it will use a default type of int if no type is specified.
Not also that since this is C a prototype is not actually required although it is best practice and a lot of modern compilers issue a warning if you call a function without a prototype.
@Banfa
sorry i have to send one more reply about this topic.....
u say that when we give the defnition of an function outside the main
& call that function in main it automaticaly take the prototype of an function.
but in case of return type of struct type varibale . i give the defnition of a function outside the main & call it in main then i produce an error
thats the prob i face...it shuold work but donot...
than you
Let's see your code that not working. Please post it.
@weaknessforcats -
struct emp MyFunction(int a, int b)
-
{
-
code here
-
return(struct emp aa}
-
}
-
-
int main()
-
{ struct emp bb;
-
bb = MyFunction(3,4);
-
}
-
donot add functiom prototype just use the defnition of an function & run
it will not run
- struct emp MyFunction(int a, int b)
-
{
-
code here
-
return(struct emp aa}
-
}
What's wrong here? - You need to declare struct emp before this point.
- Of course, 'code here' won't work. Presumably you replace this with legitimate C code.
- The return statement is wrong. Presumably somewhere in your program you declare variable aa as a struct emp. If so, then all you need to do is return aa.
- The return statement ends with a close-brace rather than a close-parenthesis.
What do you mean by "it will not run"? Do you get compiler errors? If so, please reproduce them so we can help you. Do you get runtime errors? If so, please reproduce them so we can help you. Does the program give the wrong result? If so, please describe the output you get and the output you expect. Don't make us guess.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Doug Farrell |
last post by:
Hi all,
I'm trying to do the following from within a code module:
import re
# text to match
text = "Good morning x something /x, how are you today x something
else /x"
|
by: James Gregory |
last post by:
I was having some issues with a program involving iterators and handles,
and decided to try and make a short test program to try to work out where
my problem was. However, my test program went...
|
by: Charles Ulrich |
last post by:
Greetings,
I hope my greenness isn't showing too bad by asking this, but I ran across
this trivial program today that left me flabbergasted:
#define MESSAGE "This account is currently not...
|
by: Steve |
last post by:
I have a form with a dataset and a datagrid.
I created a dataview on this dataset.
When the user modifies the datagrid, I look up this record in the dataview
to make sure it is unique.
Here is...
|
by: joelagnel |
last post by:
hi friends,
i've been having this confusion for about a year, i want to know the
exact difference between text and binary files.
using the fwrite function in c, i wrote 2 bytes of integers in...
|
by: Jeff |
last post by:
After reading all I could find about auto_ptr, I decided to write a
little program to test my comprehension:
#include <iostream>
#include <memory>
class A {
public:
void say_hi() {...
|
by: =?Utf-8?B?SmltSGVhdmV5?= |
last post by:
Working with my first XML file and the first time I have used the
XmlTextReader class. Reading a book on this subject, I have constructed code
which I thought would navigate thru the file and...
|
by: vonbreslau |
last post by:
Hello C-landers, can you find any improvement or critique to this
program?
#include <stdio.h>
#define ZER 0
#define LIM 7
/* Sort numbers according to its increasing size */
void e(void);...
|
by: abasili |
last post by:
Hi everyone, I'm trying to compile a C++ function and then call it from
a C program.
Since Google is my friend I've ended up to this link which seems very clear:
...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
| |