I am guessing differences in the following two codes. The first one is in python and works just fine.Here it is:  &gt;&gt;&gt; def foo():         if 1:             print "this is working"             n=6          print "the value of n is {0}".format(n)   &gt;&gt;&gt; foo() this is working the value of n is 6   The second one is in c, i guess the way i want to implement both programs is same.Here it is:  void main(){     if(1){         printf("this is working");         int n=9;     }     printf("the value of n is %d",n); }   n goes undeclared in the c code while it works good in python. I know that in the c code n has scope within the if block.But why there is no as such scope issue in python. Do the variables inside a block { } are stored in different memory stacks in c while in python they are stored in function's memory stack ?.Correct me if i am wrong somewhere. Regards.
2152
What exactly are the Python scoping rules?  If I have some code:  code1 class Foo:    code2    def spam.....       code3       for code4..:        code5        x()   Where is x found?  Some possible choices include the list below:   In the enclosing source file In the class namespace In the function definition In the for loop index variable Inside the for loop   Also there is the context during execution, when the function spam is passed somewhere else. And maybe  pass a bit differently?  There must be a simple reference or algorithm somewhere.  It's a confusing world for intermediate Python programmers.
89137
What would be better practice when giving a function the original variable to work with:  unsigned long x = 4;  void func1(unsigned long&amp; val) {      val = 5;             } func1(x);   or:  void func2(unsigned long* val) {      *val = 5; } func2(&amp;x);   IOW: Is there any reason to pick one over another?
6852
What is the difference between below variables assignments?   var=23 var =23 var= 23 var = 23   Is there any difference in space around the assignment operator?
15584
In C, what is the difference between using ++i and i++, and which should be used in the incrementation block of a for loop?
32907
Are there any downsides to passing structs by value in C, rather than passing a pointer?  If the struct is large, there is obviously the performancd aspect of copying lots of data, but for a smaller struct, it should basically be the same as passing several values to a function.  It is maybe even more interesting when used as return values. C only has single return values from functions, but you often need several. So a simple solution is to put them in a struct and return that.  Are there any reasons for or against this?  Since it might not be obvious to everyone what I'm talking about here, I'll give  a simple example.  If you're programming in C, you'll sooner or later start writing functions that look like this:  void examine_data(const char *ptr, size_t len) {     ... }  char *p = ...; size_t l = ...; examine_data(p, l);   This isn't a problem. The only issue is that you have to agree with your coworker in which the order the parameters should be so you use the same convention in all functions.  But what happens when you want to return the same kind of information? You typically get something like this:  char *get_data(size_t *len); {     ...     *len = ...datalen...;     return ...data...; } size_t len; char *p = get_data(&amp;len);   This works fine, but is much more problematic. A return value is a return value, except that in this implementation it isn't. There is no way to tell from the above that the function get_data isn't allowed to look at what len points to. And there is nothing that makes the compiler check that a value is actually returned through that pointer. So next month, when someone else modifies the code without understanding it properly (because he didn't read the documentation?) it gets broken without anyone noticing, or it starts crashing randomly.  So, the solution I propose is the simple struct  struct blob { char *ptr; size_t len; }   The examples can be rewritten like this:  void examine_data(const struct blob data) {     ... use data.tr and data.len ... }  struct blob = { .ptr = ..., .len = ... }; examine_data(blob);  struct blob get_data(void); {     ...     return (struct blob){ .ptr = ...data..., .len = ...len... }; } struct blob data = get_data();   For some reason, I think that most people would instinctively make examine_data take a pointer to a struct blob, but I don't see why. It still gets a pointer and an integer, it's just much  clearer that they go together. And in the get_data case it is impossible to mess up in the way I described before, since there is no input value for the length, and there must be a returned length.
2571
What is the correct (most efficient) way to define the main() function in C and C++ — int main() or void main() — and why? And how about the arguments? If int main() then return 1 or return 0?  There are numerous duplicates of this question, including:       — For C++, with a very good answer indeed.     Related:
56146
I long thought that in C, all variables had to be declared at the beginning of the function. I know that in C99, the rules are the same as in C++, but what are the variable declaration placement rules for C89/ANSI C?  The following code compiles successfully with gcc -std=c89 and gcc -ansi:  #include &lt;stdio.h&gt; int main() {     int i;     for (i = 0; i &lt; 10; i++) {         char c = (i % 95) + 32;         printf("%i: %c\n", i, c);         char *s;         s = "some string";         puts(s);     }     return 0; }   Shouldn't the declarations of c and s cause an error in C89/ANSI mode?
97897
Is there a way to conveniently define a C-like structure in Python? I'm tired of writing stuff like:  class MyStruct():     def __init__(self, field1, field2, field3):         self.field1 = field1         self.field2 = field2         self.field3 = field3
4613
How can I create or use a global variable in a function?  If I create a global variable in one function, how can I use that global variable in another function? Do I need to store the global variable in a local variable of the function which needs its access?
3114
I'd like to do it in Python. What I'd like to do in this example in C: #include &lt;stdio.h&gt;  int main() {     int i;     for (i=0; i&lt;10; i++) printf(&quot;.&quot;);     return 0; }  Output: ..........  In Python: &gt;&gt;&gt; for i in range(10): print('.') . . . . . . . . . . &gt;&gt;&gt; print('.', '.', '.', '.', '.', '.', '.', '.', '.', '.') . . . . . . . . . .  In Python print will add a \n or space, how can I avoid that? Now, it's just an example, don't tell me I can first build a string then print it. I'd like to know how to &quot;append&quot; strings to stdout.
40474
I have a Python function that takes a numeric argument that must be an integer in order for it behave correctly. What is the preferred way of verifying this in Python?  My first reaction is to do something like this:  def isInteger(n):     return int(n) == n   But I can't help thinking that this is 1) expensive 2) ugly and 3) subject to the tender mercies of machine epsilon.   Does Python provide any native means of type checking variables? Or is this considered to be a violation of the language's dynamically typed design?  EDIT: since a number of people have asked - the application in question works with IPv4 prefixes, sourcing data from flat text files. If any input is parsed into a float, that record should be viewed as malformed and ignored.
2525
My  has failed me.  In Python, are the following two tests for equality equivalent?  n = 5 # Test one. if n == 5:     print 'Yay!'  # Test two. if n is 5:     print 'Yay!'   Does this hold true for objects where you would be comparing instances (a list say)?  Okay, so this kind of answers my question:  L = [] L.append(1) if L == [1]:     print 'Yay!' # Holds true, but...  if L is [1]:     print 'Yay!' # Doesn't.   So == tests value where is tests to see if they are the same object?
23722
I've been wondering this for some time. As the title say, which is faster, the actual function or simply raising to the half power?  UPDATE  This is not a matter of premature optimization. This is simply a question of how the underlying code actually works. What is the theory of how Python code works?  I sent Guido van Rossum an email cause I really wanted to know the differences in these methods.  My email:     There are at least 3 ways to do a square root in Python: math.sqrt, the   '**' operator and pow(x,.5).  I'm just curious as to the differences in   the implementation of each of these.  When it comes to efficiency which   is better?   His response:     pow and ** are equivalent; math.sqrt doesn't work for complex numbers,   and links to the C  sqrt() function. As to which one is   faster, I have no idea...
27093
I occasionally see the list slice syntax used in Python code like this:  newList = oldList[:]   Surely this is just the same as:  newList = oldList   Or am I missing something?
93044
What is better: void foo() or void foo(void)? With void it looks ugly and inconsistent, but I've been told that it is good. Is this true?  Edit: I know some old compilers do weird things, but if I'm using just GCC, is void foo() Ok? Will foo(bar); then be accepted?
111324
I declare a variable for a 64 bit counter as :  long long call_count;   What is the format specifier that I should use in print statements?  I tried, %l, %ld, %ll. None seems to be correct.  I use Diab C compiler for compiling my application code to run on pSOS operating system.
71027
The following code fails to compile stating  "A local variable named 'st' cannot be declared in this scope because it would give a different meaning to 'st', which is already used in a 'child' scope to denote something else":           var l = new List&lt;string&gt;();         l.Find(st =&gt; st.EndsWith("12"));         string st = "why this fails?";   I understand why this won't work:          string preParent = "";         {             string preParent = "Should fail cause we change the meaning";         }   When we do the following we get "CS0103: The name 'postParent' does not exist in the current context":          {             string postParent=string.Empty;         }         postParent = "Should this work?";   What I don't get is why is the compiler smart enough to see that postParent is not within scope, but won't let me define a new variable that has the same name as a variable used within a child scope (which is obviously out of scope at this point).   Is the compiler simple enforcing scope by refusing to let me use the variable? If so this makes sense.  ===========  Edited:   I guess what I also find interesting is how you can have the same variable within two child scopes in a single method, so this is valid:          {             string thisWorks= string.Empty;         }         {             string thisWorks= "Should this work?";         }   I'm just a little curious that you can have two variables with the same name as long as they are at the same level (if you look at scope as a tree). This makes sense because you can have local variables in two methods of the same class with the same name.   I'm just surprised that the compiler is able to differentiate and allow this, while it wouldn't allow the postParent variable. And is this a technical limitation or was this a design decision? That's what I'm really trying to get at;-)