The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Stephen Shankland,Corporate & legal - ZDNet iBot UNIX and Linux RSS News 0 08-30-2007 04:00 PM
Is this a legal close-on-exec-move? frequency8 High Level Programming 9 07-08-2007 10:30 AM
Not Legal Characters lesstjm Shell Programming and Scripting 2 08-25-2005 04:23 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-17-2006
Registered User
 

Join Date: Apr 2006
Posts: 22
Lightbulb legal code?

hi friends,
the following code works fine,but the question is
"is this a valid c".
i really have no idea.......

void func()
{
int x = 50;
{
int y;
y = x + 400;
printf("x = %d\n",x);
printf("y = %d\n",y);
}
}
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 05-17-2006
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,474
Quote:
"is this a valid c".
what do you mean by that ?

Its a valid 'C' function,
can you please be more specific in stating your queries?
Reply With Quote
  #3 (permalink)  
Old 05-17-2006
Registered User
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 923
Please use code tags for code, they show the spacing properly.
Code:
void func()
{
    int x = 50;
   {
         int y;
         y = x + 400;
         printf("x = %d\n",x);
         printf("y = %d\n",y);
   }
}
Quote:
Originally Posted by mxms755
hi friends,
the following code works fine,but the question is
"is this a valid c".
i really have no idea.......
Assuming you've included stdio.h earlier in the file, then yes, this is perfectly legal code. It's not even trick code in any way, I can't see anything wrong with it.

What might be confusing you is the strange by-themselves {'s, yes? Those are defining a code block, the same kind you use whenever you do if(something) { do_something(); }, but not attached to any statement. Naked ones like these are used to limit the scope of variables. The variables defined inside it, are only valid inside the code block. If you know the difference between global and local variables, then a codeblock is just one level deeper:
Code:
/* global variable */
int a;

void func()
{
  /* local variable */
  int b;
  /* variables a,b are visible here */

  {
    int c;
    /* variables a,b,c visible here */

    {
      int d;
      /* variables a,b,c,d visible here */
    }

    {
      int e;
       /* variables a,b,c,e visible here */
    }
  }

  {
    int f;

    /* variables a,b,f visible here */
    {
      int g;
      /* variables a,b,f,g visible here */
    }

    {
      int g;
      /* variables a,b,f,g visible here.
       * Note that the value of the PREVIOUS g is not preserved!  Treat this
       * like a totally seperate variable. */
    }
  }
}

Last edited by Corona688; 05-17-2006 at 08:10 AM.
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 11:23 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0