473,387 Members | 1,903 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Confusion in c program?

Expand|Select|Wrap|Line Numbers
  1. struct emp
  2. {
  3. char name[20];
  4. int age;
  5. };
  6. fun(int aa)
  7. {
  8. int bb;
  9. bb=aa*aa;
  10. return(bb);
  11. }
  12. main()
  13. {
  14. int cc=fun(15);
  15. printf("%d",cc);
  16. }
I have a prob in that if i donot define the prototype of an function why this program runs correctly plz explain...........
May 13 '10 #1
22 1662
Dheeraj Joshi
1,123 Expert 1GB
You need to mention the return type of the function.

Expand|Select|Wrap|Line Numbers
  1. int fun(int);
  2.  
Whether your prototype looks like above?
BTW, Do you get expected output?

Regards
Dheeraj Joshi
May 13 '10 #2
@dheerajjoshim
ya i get the expected output . u can check urself also....
May 13 '10 #3
Dheeraj Joshi
1,123 Expert 1GB
How do you define function prototype?
In your code i can not see any prototyping.

Regards
Dheeraj Joshi
May 13 '10 #4
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.
May 13 '10 #5
@Banfa
but i did not mention the return type of an function why its work properly
May 13 '10 #6
myusernotyours
188 100+
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...
Expand|Select|Wrap|Line Numbers
  1.  
  2. mytyplessvar = 0; //This will actually be treated as an int.
  3.  
  4.  
Regards,

Alex.
May 13 '10 #7
@myusernotyours
but about the prototype its not declare in that program but program works correctly
May 13 '10 #8
weaknessforcats
9,208 Expert Mod 8TB
OK here goes.

This is an integer:

Expand|Select|Wrap|Line Numbers
  1. int value;
Now you can say:

Expand|Select|Wrap|Line Numbers
  1. value = 10;
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:

Expand|Select|Wrap|Line Numbers
  1. extern int value;
Now you can say:

Expand|Select|Wrap|Line Numbers
  1. value = 20;
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:

Expand|Select|Wrap|Line Numbers
  1. int MyFunction(int a, int b)
  2. {
  3.       int answer;
  4.       /* do some calculation here */
  5.       answer = a + b;
  6.       return answer;
  7. }
  8.  
  9. int main()
  10. {
  11.     int result = MyFunction(3,4);
  12. }
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:

Expand|Select|Wrap|Line Numbers
  1. extern int MyFunction(int a, int b);
  2.  
  3. int main()
  4. {
  5.     int result = MyFunction(3,4);
  6. }
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:

Expand|Select|Wrap|Line Numbers
  1. int MyFunction(int a, int b);
  2.  
  3. int main()
  4. {
  5.     int result = MyFunction(3,4);
  6. }
and that is what is called a function prototype.
May 13 '10 #9
@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
May 13 '10 #10
weaknessforcats
9,208 Expert Mod 8TB
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:

Expand|Select|Wrap|Line Numbers
  1. struct Data
  2. {
  3.      int a;
  4.      int b;
  5. };
  6.  
  7. 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.
May 14 '10 #11
donbock
2,426 Expert 2GB
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.
May 14 '10 #12
@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
Expand|Select|Wrap|Line Numbers
  1.  
  2. struct emp 
  3. char name[20]; 
  4. int age; 
  5. }; 
  6. emp fun(emp aa) 
  7. return(aa);
  8.  
  9. main() 
  10. emp bb,dd;
  11. bb={"anang",23};
  12. dd= fun(bb);
  13. printf("%s %d",dd.name,dd.age); 
  14.  
  15. }
  16.  
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...
May 14 '10 #13
donbock
2,426 Expert 2GB
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.

Expand|Select|Wrap|Line Numbers
  1. program.h:
  2.     struct emp {
  3.         ...
  4.         };
  5.  
  6.     int func1(void);
  7.     struct emp func2(int a);
  8.  
  9.  
  10. file1.c:
  11.     #include "program.h"
  12.     static int func3(int a);
  13.  
  14.     int func1(void) {
  15.         int val;
  16.         ...
  17.         val = func3(4);
  18.         ...
  19.         }
  20.  
  21.     static int func3(int a) {
  22.         struct emp w;
  23.         ...
  24.         vv = func2(a);
  25.         ...
  26.         }
  27.  
  28.  
  29. file2.c:
  30.     #include "program.h"
  31.  
  32.     ...
  33.     struct emp func2(int a) {
  34.         int value;
  35.         ...
  36.         value = func1();
  37.         ...
  38.         }
May 14 '10 #14
@weaknessforcats
if i write the following code
Expand|Select|Wrap|Line Numbers
  1. struct emp MyFunction(int a, int b) 
  2. {   
  3.     code here
  4.   return(struct emp aa} 
  5. }
  6.  
  7. int main() 
  8. { struct emp bb;
  9.     bb = MyFunction(3,4); 
  10.  
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...
May 15 '10 #15
@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.
May 15 '10 #16
weaknessforcats
9,208 Expert Mod 8TB
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.
May 15 '10 #17
@weaknessforcats
ur mean if i write the following code"
Expand|Select|Wrap|Line Numbers
  1. struct emp MyFunction(int ,int);
  2.  
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?///////////
May 16 '10 #18
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.
May 16 '10 #19
@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
May 16 '10 #20
weaknessforcats
9,208 Expert Mod 8TB
Let's see your code that not working. Please post it.
May 16 '10 #21
@weaknessforcats
Expand|Select|Wrap|Line Numbers
  1. struct emp MyFunction(int a, int b)  
  2. {    
  3.     code here 
  4.   return(struct emp aa}  
  5.  
  6. int main()  
  7. { struct emp bb; 
  8.     bb = MyFunction(3,4);  
  9. }  
  10.  
donot add functiom prototype just use the defnition of an function & run
it will not run
May 16 '10 #22
donbock
2,426 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. struct emp MyFunction(int a, int b)   
  2. {     
  3.     code here  
  4.   return(struct emp aa}   
  5. }
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.
May 17 '10 #23

Sign in to post your reply or Sign up for a free account.

Similar topics

1
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"
4
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...
24
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...
13
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...
10
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...
23
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() {...
2
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...
10
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);...
38
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: ...
0
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
marktang
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,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.