Find the answer to your Linux question:
Results 1 to 2 of 2
Try creating a C function like Code: int x = 789; void myfunc() { return x; } Now try calling this function in your code: first try: myfunc(); then try: ...
  1. #1
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714

    C function oddity

    Try creating a C function like

    Code:
    int x = 789;
    
    void myfunc()
    {
          return x;  
    }
    Now try calling this function in your code:

    first try:

    myfunc();

    then try:

    myfunc(3456);

    and then:

    myfunc(3456, "this is some text");

    You will find the function will work correctly, that is it will ignore the values pasted to the function...

    To correct this define the function as:

    Code:
    int x = 789;
    
    void myfunc(void)
    {
          return x;  
    }
    Just one of the odd things I discovered in C...Gerard4143

  2. #2
    Linux Guru smolloy's Avatar
    Join Date
    Apr 2005
    Location
    CA, but from N.Ireland
    Posts
    2,413
    You mean the second code snippet will return a double passed to it, despite "x" being defined as a global outside of the function ?
    Registered Linux user #388328 || Registered LFS user #15880
    AMD 64 X2 4600+ :: 2X1GB DDR2 800 :: GeForce 9400 GT 512MB :: ASUS M2N32 Deluxe :: 4X250GB SATAII
    Need instant help? Try us on IRC -- #linuxforums on freenode

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...