-1
int main()
{
   int k;
   int i = 7;
   int j = 7;

   if( i==j)
   {
       int k = 1;
   }
   else
   {
      int k = 0;
   }
   printf("%d" , k);
}

When I try to run this program, whatever input I try to put why do I always get the k value 0. where I went wrong?

7
  • 4
    How many different ks do you have and which one do you print?
    – G.M.
    Commented Aug 16 at 8:52
  • For code formatting, don't place back ticks on every single line. To do code formatting, either indent the whole code at least 4 spaces or use 3 backticks in a row.
    – Lundin
    Commented Aug 16 at 8:52
  • 5
    As for the answer to the question, study the terms of scope and "variable shadowing". Each k inside brackets takes precedence over any k variable already declared in outer scopes. Here's where a good C book will be handy.
    – Lundin
    Commented Aug 16 at 8:54
  • 4
    Try to enable compiler warnings! I get warnings about unused variable k, in the if and else scope and a warning about the use of an uninitialized variable in the printf function. You should not redeclare variables: int k = 1 but just use k = 1 Commented Aug 16 at 9:01
  • OK , So i removed int inside if and else and program works perfectly , Can anyone tell how redeclaring int inside if is making issue ?
    – arnold
    Commented Aug 16 at 9:09

2 Answers 2

3

You are defining new Variables in the local scope of the if and else statements. To set the value of the existing k variable instead, do this:


int main()
{
   int k;
   int i = 7;
   int j = 7;
    
   if(i == j) {
       k = 1;
   } else {
       k = 0;
   }
   printf("%d" , k);
}

Since the equal comparison will be 0 or 1 anyway you could even do:

int main()
{
    int k;
    int i = 7;
    int j = 7;
    
    k = (i == j);
    
    printf("%d", k);
}
1
  • Should mention the middle ground (int k = i == j ? 1 : 0;) in case the reader is interested in values other than 0 and 1.
    – ikegami
    Commented Aug 16 at 13:41
-1

For starters in C operators == and != are named equality operators. The result of the equality operator == is integer value 1 if two operands of an expression with the equality operator are equal each other and 0 otherwise. So you could just write

int i = 7;
int j = 7; 
int k = i == j;

or

int k;
int i = 7;
int j = 7; 
k = i == j;

But if processing is more complex and compound statements include more statements apart from a simple assignment you indeed need to use if-else statements.

Pay attention to that variables have scopes. Variables declared in blocks have block scopes. Variables in inner scopes hide variables with the same name declared in enclosing scopes.

So relative to your program the variable k declared in if and else parts of the if statement

if( i==j)
{
    int k = 1;
}
else
{
   int k = 0;
}

hides the variable k declared before the if statement

int main()
{
   int k;
   //... 

And the variables k declared in the if and else parts of the if statement are not visible and alive after the if statement.

Thus in this call of printf

printf("%d" , k);

there is an attempt to output the variable k declared before the if statement. As the variable has automatic storage duration then it is not initialized implicitly. As a result the call of printf invokes undefined behavior.

To refer to the variable k declared in the beginning of main in the if statement you need just to write within the if statement

if( i==j)
{
   k = 1;
}
else
{
   k = 0;
}

That is instead of declaring new variables in inner scopes with the same name k you need just to refer the original variable k declared before the if statement using the assignment (expression) statement.

Nevertheless you may declare a variable with the same name in an inner scope as a variable declared in an outer scope provided that you will use storage class specifier extern that refers to a file scope variable.

Here is a demonstration program

#include <stdio.h>

int k;

int main(void) 
{
   int i = 7;
   int j = 7;

   if( i==j)
   {
       extern int k;
       k = 1;
   }
   else
   {
      extern int k;
      k = 0;
   }

   printf("k == %d\n" , k);
}

The declaration of the variable k before main is called tentative definition. Declarations in the if statement

   if( i==j)
   {
       extern int k;
       k = 1;
   }
   else
   {
      extern int k;
      k = 0;
   }

refer to the variable k declared in the file scope. So assignment statements within the if statement change that variable.

4
  • In your example extern int k; is redundant, as k is already in scope
    – M.M
    Commented Aug 18 at 23:29
  • @M.M It seems you have not understood what I wrote. I showed when a variable may be declared a second time in an inner block because in the original program there are used declarations. Commented Aug 19 at 4:54
  • Although it's possible, it's not something anyone should ever do, and I think it is more harm than help to show it as a code sample in answering a beginner question
    – M.M
    Commented Aug 19 at 9:48
  • @M.M The problem of the provided code in the question is using declarations in block scope. I showed when a block scope declaration can be used to refer a variable that was already declared. This is very useful for beginners because it enlarges their knoweledge about scopes and declarations and about using the storage class specifier extern in a block scope declaration. Commented Aug 19 at 19:18

Not the answer you're looking for? Browse other questions tagged or ask your own question.