legal code?


 
Thread Tools Search this Thread
Top Forums Programming legal code?
# 1  
Old 05-17-2006
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);
}
}
# 2  
Old 05-17-2006
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?
# 3  
Old 05-17-2006
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 12:10 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

6 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

Legal and illegal bash variable names?

list of legal and illegal bash variable names and wht each is either illegal and ligal? 4. Seneca college, Toronto , Canada, peter wheeler, tech 154: (5 Replies)
Discussion started by: renegade755
5 Replies

2. Programming

Array[count+1] legal?

I get weird decimal digits when I run the program below. int coe_amount; cout << "How many coefficients exist in your term? "; cin >> coe_amount; float coefficient; for (int count = 0; count < coe_amount; count ++) { ... (4 Replies)
Discussion started by: DyslexicChciken
4 Replies

3. Shell Programming and Scripting

[CSH]legal to declare a variable like this

I am trying to declare a variable like this #!/bin/csh -f set c_arg = $a $b $c However, since i need it to declare before declaring $a ,$b or $c. As of now i am getting an error which says $a not defined. Is it possible to define a variable c_arg w/o interpreting the values $a $b $c (2 Replies)
Discussion started by: animesharma
2 Replies

4. AIX

Legal Disclaimer setup in CDE

Hi pals I manage nearly 200+ aix workstations. I need to setup a legal disclaimer in all the workstations. When the user do a interactive login in CDE the legal disclaimer should be displayed and once he accepts the same he should be able to login to system. Can anybody suggest me as to... (0 Replies)
Discussion started by: sriram.s
0 Replies

5. Programming

Is this a legal close-on-exec-move?

In another part of the program, a file is opened using fopen(). Anyhow, I was wondering if using dup2() in the following snippet was a legal close-on-exec move. static int tel(char *user, char *tty, const char *what) { pid_t cpid, wpid; int stats; cpid = fork(); if... (9 Replies)
Discussion started by: frequency8
9 Replies

6. Shell Programming and Scripting

Not Legal Characters

I have a file that I want to grep and identify all of the illegal characters. I have a list of legal ascii characters \11\12\40-\176,\0-\255 so i try a grep -v to exclude these but my syntax is not correct?? $ cat TRANS_20050613_00.DAT.ERROR | grep -v '\11\12\40-\176\0-\255' grep:... (2 Replies)
Discussion started by: lesstjm
2 Replies
Login or Register to Ask a Question