Question about if-statement.

This is a discussion on Question about if-statement. within the C Programming forums, part of the General Programming Boards category; I was wondering why the following program doesn't function properly: Code: #include <stdio.h> int main() { int a; printf("Enter a ...

  1. #1
    Novice.
    Join Date
    Oct 2005
    Posts
    88

    Question about if-statement.

    I was wondering why the following program doesn't function properly:

    Code:
    #include <stdio.h>
    
    int main()
    {
     int a;
     printf("Enter a number: ");
     scanf("%d", a);
     if (a = 1)
        {
         printf("One.\n");
        }
     else
         {
          printf("Not one.\n");
         }
      getchar();
      getchar();
    }
    It will output: One. No matter what number you input.

    The crappy C-tutorial on this site is no good either, since the example of how to use it is written in C++.

    Thank You.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,941
    Look hard:
    Code:
    if (a = 1)
    See it now?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,673
    ==, not =

    The scanf call is wrong as well, it's missing an &

    With extra warnings, all those problems are at least hinted at.
    Code:
    $ gcc -W -Wall -ansi -pedantic -O2 foo.c
    foo.c: In function `main':
    foo.c:7: warning: format argument is not a pointer (arg 2)
    // That's the missing &
    foo.c:8: warning: suggest parentheses around assignment used as truth value
    // That hints that = should be ==
    foo.c:18: warning: control reaches end of non-void function
    // you forgot the return 0;
    foo.c:5: warning: 'a' might be used uninitialized in this function
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Novice.
    Join Date
    Oct 2005
    Posts
    88
    Ah well I guess I just need some sleep then. Thanks!

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Using scanf fucntion to read an integer is a standard procedure which everyone uses, but you can see the impact of using scanf by calling getchar function twice at the end.

    There are many better way of reading an integer rather than using scanf function, perhaps using fgets and sscanf function. Have a look

    ssharish
    Last edited by ssharish2005; 12-21-2007 at 12:27 PM.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,791
    Quote Originally Posted by omnificient View Post
    Code:
    #include <stdio.h>
    
    int main()
    {
    	int a;
    	printf("Enter a number: ");
    	scanf("%d", a);
    	if (a = 1)
    	{
    		printf("One.\n");
    	}
    	else
    	{
    		printf("Not one.\n");
    	}
    	getchar();
    	getchar();
    }
    Indentation lesson. There you go.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Elysia, one more lesson on indentation lol, why don't you place that indentation note on your signature. That makes your life more easy isn't it?

    ssharish

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,791
    Not enough space >_<
    Need to write an essay on indenting and link to it. Maybe once there's a wiki up.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User t3chn0n3rd's Avatar
    Join Date
    Dec 2007
    Location
    kansas city
    Posts
    25
    Did that program compile

  10. #10
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Quote Originally Posted by t3chn0n3rd View Post
    Did that program compile
    That program will compile if you do the necessary changes which is been pointed out.

    ssharish

  11. #11
    Dragon Rider jas_atwal's Avatar
    Join Date
    Nov 2007
    Location
    India
    Posts
    54
    and scanf will accept a pointer. so replace scanf("%d", a) with scanf("%d",&a);

  12. #12
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Quote Originally Posted by jas_atwal View Post
    and scanf will accept a pointer. so replace scanf("%d", a) with scanf("%d",&a);
    YES thats right, Salem had pointed that one out earlier.

    ssharish

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about break in switch statement
    By alone2002dj in forum C Programming
    Replies: 1
    Last Post: 12-09-2007, 07:42 PM
  2. Quick IF statement question (beginner)
    By jim.rattlehead in forum C Programming
    Replies: 23
    Last Post: 11-29-2007, 05:51 AM
  3. Design layer question
    By mdoland in forum C# Programming
    Replies: 0
    Last Post: 10-19-2007, 04:22 AM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 02:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21