Thread: Having problems with the basics.

  1. #1
    Registered User deepak_007's Avatar
    Join Date
    May 2012
    Location
    Delhi,INDIA
    Posts
    4

    Question Having problems with the basics.

    Hello everyone,
    I am new in programming and started with C. but i am having some problems with the basics since i am studing on my own with no teacher thus online support is the only key.
    Following are the doubts i am having:-

    1) For the code below there was not output,the screen flashed and i was back on coding screen
    Code:
    #include<stdio.h>
    #include<conio.h>
    
    main()
    {
            return(printf("AMAZING!!"));
    }
    but without running it again i immediately changed it to
    Code:
    #include<stdio.h>
    #include<conio.h>
    
    main()
    {
           printf("AMAZING!!");
    }
    i got the result as
    AMAZING!!
    AMAZING!!

    Please help me understand this

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well you should write it normally for starters. Make sure that main returns an integer 0 and write the return type before main(). I don't have an explanation for why the output prints twice, but I am willing to blame not following the rules as a cause.

  3. #3
    Registered User deepak_007's Avatar
    Join Date
    May 2012
    Location
    Delhi,INDIA
    Posts
    4
    I don't have an explanation for why the output prints twice, but I am willing to blame not following the rules as a cause.
    I dont wanna sound rude here but i was hoping for a reason for this output
    Last edited by deepak_007; 05-16-2012 at 03:37 AM. Reason: inappropriate words

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Maybe your IDE merely showed you the output from the previous run while it was showing you the output from the second run.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by deepak_007 View Post
    I dont wanna sound rude here but i was hoping for a reason for this output
    Like whiteflags said, you should follow the rules and best practices first. A couple to get you started:
    1) Don't use conio.h it's non-standard
    2) main should be declared with an int return
    3) Don't return the rcode from printf, absolutely no need to do that.
    4) Return 0 from main (or 1 if there was an error)
    Other than that, try opening up a console window and running your program from there. I'm sure it will only print once, maybe the output from your first run was buffered and thus it was shown on the screen when you ran the program a second time.
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  6. #6
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    5) Throw away Turbo C(/++) and get a new compiler. Using a 20+ years old compiler/IDE is going to teach you a lot of bad practices and invalid techniques.

    I agree with Laserlight, that is definitely it.. I helped a friend (&&...Turbo C victim) out yesterday..and was confused by this exact same thing.

  7. #7
    Registered User deepak_007's Avatar
    Join Date
    May 2012
    Location
    Delhi,INDIA
    Posts
    4
    Thank you, i will take care of that but here is my next doubt
    2) I wrote this code to take radius of a circle and print the area using function area of return type float.
    Code:
    #include<stdio.h>
    #include<conio.h>
    float area(float);
    
    #define PI 3.14
    
    int main()
    {
    float rad, ar;
    printf("Enter the radius of circle : ");
    scanf("%d"&rad);
    
    ar = area(rad);
    printf("Area of Circle with Radius %f is %f", rad, area);
    getch();
    }
    
    float area(float r)
    {
    float a;
    a = PI * r * r;
    return(a);
    }
    sorry but my code was not showing the result on console without using conio and getch........

    secondly as you can see i have written the name of the function(area) without the arguments inplace of the variable ar in line 14...at first i did it by mistake but when i realised it i was confused for y was it not showing an error.I hope you can help me in this.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This is wrong:
    Code:
    scanf("%d"&rad);
    It should be:
    Code:
    scanf("%f", &rad);
    Notice the change from %d to %f for scanf to read in a float, and then the addition of a comma to separate the arguments to scanf.

    Quote Originally Posted by deepak_007
    sorry but my code was not showing the result on console without using conio and getch........
    If you run your program from a separate command prompt window, you won't need to do that.

    Quote Originally Posted by deepak_007
    secondly as you can see i have written the name of the function(area) without the arguments inplace of the variable ar in line 14...at first i did it by mistake but when i realised it i was confused for y was it not showing an error.I hope you can help me in this.
    You may need to increase the warning level for your compilation and/or take manasij7479's suggestion of switching to a more modern compiler.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User deepak_007's Avatar
    Join Date
    May 2012
    Location
    Delhi,INDIA
    Posts
    4
    Sorry for the delay, my internet connection was showing problems.
    Thank you all for your valueble answers but i would also request you to suggest a nice IDE for C language.
    Thanking you again.

  10. #10
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by deepak_007 View Post
    suggest a nice IDE for C language.
    If you are a Linux User:
    KDevelop
    Eclipse
    Code::Blocks
    Anjuta
    (or any of the 'smart' text editors and the terminal)

    If you use Windows:
    Code::Blocks
    Pelles C
    Visual Studio
    Eclipse

  11. #11
    Registered User
    Join Date
    Aug 2007
    Location
    MD, USA
    Posts
    71
    You can use the good old standard stdio.h getchar() or getc(stdin)

    instead of the windows specific conio.h getch()

    to hold the "Run" terminal open.
    Only thing is you have to hit "ENTER" instead of "THE ANY KEY" (big deal).

    I personally prefer gcc compiler on command line compiling, where I am in control of where everything is.
    Lookup mingw .

    I also use "vim" as my text editor in windows as well as linux although textpad is very nice in windows.
    Last edited by HowardL; 05-16-2012 at 10:24 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help for the basics
    By Samblaize in forum C++ Programming
    Replies: 4
    Last Post: 06-01-2008, 07:14 PM
  2. the very basics
    By michigan353 in forum C Programming
    Replies: 4
    Last Post: 10-13-2005, 04:26 AM
  3. Win Api Basics...
    By Devil Panther in forum Windows Programming
    Replies: 19
    Last Post: 09-09-2004, 11:28 AM
  4. Basics
    By Benzakhar in forum Linux Programming
    Replies: 6
    Last Post: 01-11-2004, 04:37 AM
  5. help with basics
    By pwbtech in forum C++ Programming
    Replies: 7
    Last Post: 02-08-2003, 04:44 PM

Tags for this Thread