Home > other >  In C when i execute programe it shows a random value
In C when i execute programe it shows a random value

Time:11-20

#include <stdio.h>


[Result Screenshot Attached ]

int main(){

int a;

printf("Enter a number %d\n", a);
scanf("%d", &a);

(a < 5) ? printf("A is less than 5") : printf("A is Greater than 5");

return 0;

}

Result is given below also:-

PS D:\Leaning C language> cd "d:\Leaning C language" ; if ($?) { gcc ternary.c -o ternary } ; if ($?) { .\ternary } Enter a number 12914124

CodePudding user response:

Before taking input you have printed 'a' so it is throwing the garbage value. Try this code :

#include <stdio.h>
int main(){
int a;
printf("Enter a number :");
scanf("%d", &a);

(a < 5) ? printf("A is less than 5") : printf("A is Greater than 5");

return 0;
}

CodePudding user response:

Your Code -

#include <stdio.h>
      
        
int main() {
        
    int a; // <-- Here, You are not assigned any value, so compiler assign garbage integer value to the a. In your case it is 12914124
        
    printf("Enter a number %d\n", a); // <-- Here you are printing that value, so compiler give output as "Enter a number 12914124"
                                      // That's why you are getting the random number.
     
    scanf("%d", &a);
        
    (a < 5) ? printf("A is less than 5") : printf("A is Greater than 5");
        
    return 0;
}

Corrected Code -

#include <stdio.h>
      
        
int main() {
        
    int a;
        
    printf("Enter a number "); // <-- Remove '%d\n' and 'a' in here
                                            
     
    scanf("%d", &a);
        
    (a < 5) ? printf("A is less than 5") : printf("A is Greater than 5");
        
    return 0;
}
  •  Tags:  
  • c
  • Related