Thread: printf in Else statement at the end is printing, even if condition in if is false.

  1. #1
    Registered User
    Join Date
    Aug 2017
    Posts
    4

    printf in Else statement at the end is not printing,even if condition in if is false.

    insert
    Code:
    // Program for searching 1-d array.
    #include<stdio.h>
    void main()
    {
     int i,a[60],val,find;
     clrscr();
     for(i=0;i<5;i=i+1)
     {
      printf("\nenter value of index[%d]:",i);
      scanf("%d",&a[i]);
     }
     for(i=0;i<5;i=i+1)
     {
      printf("\nvalue of array a[%d]:%d",i,a[i]);
     }
     printf("\nEnter value to search:");
     scanf("%d",&val);
     for(i=0;i<5;i=i+1)
     {
      if(val==a[i])
      {
       find=i;
       break;
      }
     }
     if(find>-1)  // here used -1 bcz value can be found at index 0 so 0 will be stored in var find.
     {
      printf("found value in index:%d",find);
     }
     else
     {
      printf("value not found");
     }
     getch();
    }
    Last edited by Pranay Rawat; 08-15-2017 at 10:06 AM.

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Firstly, make sure you read my response to your previous post.

    Think about it. How could find ever equal -1? Where do you set it to -1? (Nowhere!) Where should you set it to -1?

    BTW, there's a bizarre newbie disease that causes them to put newlines at the beginning of an output string instead of at the end. You apparently have that disease. You should attempt to cure it.

    main should return an int and I don't see the point of clrscr() and getch(). All that garbage just makes your code non-portable for absolutely no reason. Get rid of clrscr() and use getchar() instead of getch() (which means you'll have to hit enter instead of any key).

    And you should use i++ (or ++i) instead of i = i + 1.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    First of all, learn how to indent code. One space isn't enough. Most people go for 2 or 4 spaces.
    Code:
    #include<stdio.h>
    int main()
    {
        int i, a[60], val, find = -1;
        //clrscr();
        for (i = 0; i < 5; i = i + 1) {
            printf("\nenter value of index[%d]:", i);
            scanf("%d", &a[i]);
        }
        for (i = 0; i < 5; i = i + 1) {
            printf("\nvalue of array a[%d]:%d", i, a[i]);
        }
        printf("\nEnter value to search:");
        scanf("%d", &val);
        for (i = 0; i < 5; i = i + 1) {
            if (val == a[i]) {
                find = i;
                break;
            }
        }
        if (find > -1)              // here used -1 bcz value can be found at index 0 so 0 will be stored in var find.
        {
            printf("found value in index:%d", find);
        } else {
            printf("value not found");
        }
        //getch();
        return 0;
    }
    You've already been told about the void main thing, so please stop doing it.

    Your find variable is uninitialised, so you need to fix that
    Code:
    $ ./a.out 
    
    enter value of index[0]:2
    
    enter value of index[1]:3
    
    enter value of index[2]:4
    
    enter value of index[3]:6
    
    enter value of index[4]:8
    
    value of array a[0]:2
    value of array a[1]:3
    value of array a[2]:4
    value of array a[3]:6
    value of array a[4]:8
    Enter value to search:1
    found value in index:32647$
    > printf("\nEnter value to search:");
    > printf("value not found");
    The usual convention is to put \n at the end of every printf call to ensure that the line is seen.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with last printf statement not printing?
    By bellagomes21 in forum C Programming
    Replies: 2
    Last Post: 02-16-2016, 01:23 AM
  2. Replies: 5
    Last Post: 05-07-2011, 06:49 PM
  3. Intended false statement is seen as true
    By 987654 in forum C++ Programming
    Replies: 7
    Last Post: 09-17-2010, 12:00 PM
  4. Printing %d and newline in printf statement
    By lookforraja07 in forum C Programming
    Replies: 2
    Last Post: 12-19-2007, 10:04 AM
  5. False statement evaluation?
    By kwikness in forum C Programming
    Replies: 5
    Last Post: 11-11-2007, 11:13 PM

Tags for this Thread