Thread: What difference can you find between both Codes and why 1 of them is not working??

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    225

    What difference can you find between both Codes and why 1 of them is not working??

    Hello,
    this is my 1st post just wanted to know why is 1 of the below program not working and one of them is working even though both mean exactly same??

    1)
    Code:
    #include "conio.h"
    #include "stdio.h"
    void main()
    {
    int i;
    if(i==1)
    {
      float n=3;
    }
    else
    {
      float n=5;
    }
    
    }

    2)
    Code:
    #include "conio.h"
    #include "stdio.h"
    void main()
    {
    int i;
    if(i==1)
      float n=3;
    
    else
      float n=5;
    
    }
    Also tell me why is the declaration allowed in between. I mean in C variables can never be declared in between then how is the 1st code working??can anybody tell me the solution?i'll be very grateful to him..

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    In the first example, you are declaring a variable "n" that is local to the block inside the if and else branches of the if/else statements.

    In the second case, you are declaring a new function-wide variable in the if-statement, then another with the same name in the else-branch. (and breaking the rules for standard C prior to C99, which states that varaibles must be declared at the beginning of the block).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    Ok thanks matsp!! for your help and quick response

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    1. conio.h isn't a standard include file
    2. stdio.h should be included as <stdio.h>, not "stdio.h"
    3. main returns int, see the FAQ.
    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.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    225

    @ Salem,

    Hello Salem,
    #include <stdio.h> can also be written as #include "stdio.h"

    and i know conio.h is not a standard C header file but when you write clrscr() to clear screen you have to include that file
    Last edited by chottachatri; 01-22-2008 at 09:42 AM.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by chottachatri View Post
    Hello Salem,
    #include <stdio.h> can also be written as #include "stdio.h"

    Don't trust me??then try it for yourself!
    and my question was something different, i think so or was it about header files?
    and i know conio.h is not a standard C header file but when you write clrscr() to clear screen you have to include that file
    Yes, you can include most files with either "file.h" or <file.h>.

    The point Salem is trying to make is that you should only include "local" headers with "file.h", and you should use <file.h> for system include files, such as stdio.h. Essentially, it tells the reader where it's likely that this file will be [not the compiler, it just searches until it finds the right path].

    clrscr() is also non-standard, and generally not necessary.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    Ok matsp,
    but clear this doubt also what do you mean by local header?and system header file?


    Also can you suggest me few questions that i can code for C? or may be some good book that you have came across?which has excellent programming problems?

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    A "local" header is produced as part of your current project - something that you would be likely to change at some time(s) during the code development.

    A "system" header is something you didn't produce within the current project.

    The same header file can be both at the same time:
    Say for example you make your own 3D library functions, and then use those to produce a game, then the 3D library header files will probably count as system header files - because you are not changing those header files as part of the game development [1].

    [1] You may of course fine bugs in your 3D library when you start using it for your game, and possibly fix that by modifying some header file that is part of the 3D library - but that should be done as part of the 3D library project bug fixing, and not as part of the game development.

    Edit: As to books, problems and such, I don't know of a good book right now - I learned C about 15 years back, and I'm not sure the books I studied then are still in print, and I'm quite sure there are newer books - there is a "Books" subject in the forum tho, where people write good and bad things about different books.

    For exercises, I don't know what sort of level to give those at, and you are probably better of with:
    1. Scanning the forum here - there are new posts almost every day asking questions on "How can I write a program that does <something>" - try some of those exercises and see what you can do - just don't post it as a result.
    2. Searching with Google for university classes online - there are quite a few with published exercises.

    --
    Mats
    Last edited by matsp; 01-22-2008 at 10:04 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed