top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Is it possible to declare two or more variables with the same name within one function in C?

0 votes
606 views
Is it possible to declare two or more variables with the same name within one function in C?
posted Jul 18, 2016 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote

In a single scope it is impossible to declare same name with more variables, compiler will through compilation error. but you can dot he following way to declare same name variable..

int main()
{
int var;
/// Use variables
{ /* different scope with in the function */
int var = 10;
/// Use the variable
after this line var is not accessible anymore to this function
}
int var ; // This will give compilation error because it is already defined in this scope.

answer Jul 19, 2016 by Jaganathan
+1 vote

No simply it is not possible to declare two variable with same name within one function
wondering why?
just as two person with same name may cause confusion, same goes with "C" compiler

answer Jul 21, 2016 by Shahsikant Dwivedi
Similar Questions
+1 vote

something like

for (i=0,j=7;i<j;++i,--j)
0 votes

Which is the best practice for variable declaration within the function ? Or is it vary from one language to other one ?

+5 votes

You can find three principal uses for the static. This is a good way how to start your answer to this question. Let’s look at the three principal uses:

  1. Firstly, when you declare inside of a function: This retains the value between function calls

  2. Secondly, when it is declared for the function name: By default function is extern..therefore it will be visible out of different files if the function declaration is set as static..it will be invisible for outer files

  3. And lastly, static for global parameters: By default you can use global variables from outside files When it’s static global..this variable will be limited to within the file.

is this right ?

...