C/cpp class Knowledge What happens when a function is called before it declaration in C? What happens when you use a function that is not declared in C

Source: Internet
Author: User

http://www.geeksforgeeks.org/g-fact-95/

1 in C, if a function is called before it is declared, then the compiler assumes that the return value of the function is of type int,

So the following code will not compile:

#include <stdio.h>int main (void) {    //    printf ("  %d\n", Fun ());     return 0  Char  fun () {   return'G';}

Error: In fact, the fun function is defined two times, conflict

TEST1.C:9:6for"fun"char fun ()      ^  TEST1.C:5:implicit'fun' is here     printf ("%d\n", Fun ());                     ^

Changing the return value to an int row compiles and runs:

#include <stdio.h>int main (void) {    printf ("%d\n" , Fun ());     return 0  int  Fun () {   returnten;}

2 in the C language, if the function is called before the declaration, what if the function's parameters are detected?

compiler assumes nothing about parameters. Therefore, the compiler'll not being able to perform compile-time checking of argument types and arity when the function is Applied to some arguments. This can cause problems.

The compiler does not make any assumptions about the parameters, so type checking cannot be done. The following code will be problematic, the output is garbage

int Main (void) {    printf ("%d", sum (5 ));     return 0 ;} int sum (intintint  a) {    return (a+b+c);}

Output Result:

[Email protected]:~/myprog/geeks4geeks/cpp$./A. out 1954607895[email protected]:~/myprog/geeks4geeks/cpp$./A. out 1943297623[email protected]:~/myprog/geeks4geeks/cpp$./A. out-16827881[email protected]:~/myprog/geeks4geeks/cpp$./A. out 67047927[email protected]:~/myprog/geeks4geeks/cpp$./A. out-354871129[email protected]:~/myprog/geeks4geeks/cpp$./A. out-562983177[email protected]:~/myprog/geeks4geeks/cpp$./A. out 33844135[email protected]:~/myprog/geeks4geeks/cpp$

So it's always recommended to declare a function before their use so that we don ' t see any surprises when the program is run (see this for more details).

C/cpp class Knowledge what happens when a function is called before it declaration in c? What happens when you use a function that is not declared in C

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.