Thread: Learning C

  1. #1
    Odd Mental Process Alexander's Avatar
    Join Date
    Aug 2008
    Posts
    22

    Learning C

    Ok, I'm kind of confused. I swore I understood what I was doing a second ago. xD
    I'm trying to learn C and failing apparently. My tutorial gave me this assignment:

    Assignment: Write a program that repeatedly accepts user input and then prints the numbers between 0 and the entered number. If the user enters a number less than or equal to zero, the program will exit.
    So, I thought that it would be easy enough. So I write some probably atrocious code.

    Code:
    #include <stdio.h>
    
    int main(void){
    
    	int x, z = 0; //variables
    	scanf("&#37;d",x); //user input
    
    	if(x>0){ //check user input
    	
    		int y;
    		for(y=1;y<=x;y++){ //loop to add all integers between 0 and x together
    			z += y;
    		}
    
    		printf("%d\n",z); //print result
    		main(); //restart
    	}
    
    	return 0; //end
    }
    After compiling, I run and it gives me this:
    20776 [main] a 2268 _cygtls::handle_exceptions: Error while dumping state (probably corrupted stack)
    Segmentation fault (core dumped)

    Now I really have no idea what I'm doing. Can anyone give me pointers? -.-;
    Last edited by Alexander; 08-27-2008 at 09:30 PM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You need a while loop to enclose the scanf() line. As is, you have no "until" in there.

    And don't forget to synch up your scanf() with a getchar() to get past the newline char when you hit the enter key.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Go to this web site and read about scanf() and how to use it.

    http://www.cppreference.com/stdio/index.html
    Mainframe assembler programmer by trade. C coder when I can.

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Also, and more importantly, you need to change your parameters around on scanf() too.

    Example
    Code:
    scanf("&#37;d",&x);
    Usually people are all over these kinds of errors... It must be a day where people have those one things that I almost never have... what are they called... Oh yeah, a social life.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    scanf("%d",&x);

    not scanf("%d,x) but don't ask me why. I would just call it a reference.

    Also, you should move main() down two lines (past } and before return) if you want the loop to work.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Silly me, I didn't even explain to you what that & means or why you are putting it there. My deepest apologies.

    The & thingy denotes when you are wanting to take the memory address of a variable. In this case you need the memory address of x. Why do you need that? Because if you give scanf() the address of x, it can write to that specific place in memory. Otherwise if you just pass in x, it will interpret the value which x holds (which since you didn't initialize x, its value is going to be undefined) as a memory address.

    So if x initialized to lets say... -351. It will try to write to the whatever is the 0xFFFFFEA1 (or 0xFFFFFFFFFFFFFEA1 on a 64-bit platform) memory address.

    Of course that is a crazy memory address that your program doesn't have the right to be writing to, thus there may be a segment fault or stack corruption depending on what exactly was attempted to be written to. Is that helpful?

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Oh whoa... I didn't even see the main() thing until MK27 mentioned it. Why in the hell are you recursing main()? I am not liking your freakish voodoo program you are trying to write here...

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Code:
    main(); //restart
    In addition to the problems mentioned above with scanf(), calling the main() function in your program is illegal. Take it out and save yourself a lot of grief!

  9. #9
    Odd Mental Process Alexander's Avatar
    Join Date
    Aug 2008
    Posts
    22
    Gah! I'm retarded. I forgot the address part. Sorry. ^^; Thanks for that. And what do you mean by having to enclose the scanf() in a while loop? o.o I didn't know I had to do that. And I want the loop to only work if the user has entered a number larger then 0.

  10. #10
    Odd Mental Process Alexander's Avatar
    Join Date
    Aug 2008
    Posts
    22
    Quote Originally Posted by cpjust View Post
    Code:
    main(); //restart
    Calling the main() function in your program is illegal. Take it out and save yourself a lot of grief!
    Yeah, but then it stops. How should I make the program restart again preferably?

  11. #11
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Move all the code that's in your main() function into another function, then you can call that other function as much as you like.

  12. #12
    Odd Mental Process Alexander's Avatar
    Join Date
    Aug 2008
    Posts
    22
    Quote Originally Posted by Adak View Post
    You need a while loop to enclose the scanf() line. As is, you have no "until" in there.

    And don't forget to synch up your scanf() with a getchar() to get past the newline char when you hit the enter key.
    Oh! Now I get what you mean!

    Code:
    #include <stdio.h>
    
    int main(void){
    
    	int x, z = 0; //variables
    
    	while(x>0){ //check user input
                    scanf("&#37;d",&x); //user input
    		int y;
    		for(y=1;y<=x;y++){ //loop to add all integers between 0 and x together
    			z += y;
    		}
    
    		printf("%d\n",z); //print result
    	}
    
    	return 0; //end
    }
    But I don't get what you mean by the getchar(), unfortunately, I have no idea how to use that yet.
    Last edited by Alexander; 08-27-2008 at 09:28 PM.

  13. #13
    Odd Mental Process Alexander's Avatar
    Join Date
    Aug 2008
    Posts
    22
    Quote Originally Posted by cpjust View Post
    Move all the code that's in your main() function into another function, then you can call that other function as much as you like.
    Hmm, yeah. I didn't want to do that, I know how to do functions, but my tutorial hadn't gotten to them yet, so I thought I'd do it with out that and do it with what they have taught me so far.

  14. #14
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Alexander View Post
    Hmm, yeah. I didn't want to do that, I know how to do functions, but my tutorial hadn't gotten to them yet, so I thought I'd do it with out that and do it with what they have taught me so far.
    Then use a loop, or if you're feeling particularly naughty, you could always try a goto statement.

  15. #15
    Odd Mental Process Alexander's Avatar
    Join Date
    Aug 2008
    Posts
    22
    Quote Originally Posted by cpjust View Post
    Then use a loop, or if you're feeling particularly naughty, you could always try a goto statement.
    Oooh, I like the goto statement. Is there a drawback to it though?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Machine Learning with Lego Mindstorms
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 01-30-2009, 02:34 PM
  2. Best Approach for Learning
    By UCnLA in forum C Programming
    Replies: 5
    Last Post: 03-21-2008, 02:35 AM
  3. Need Help On a Simple Bank Program
    By oobootsy1 in forum C# Programming
    Replies: 9
    Last Post: 08-08-2005, 10:51 AM
  4. Learning Rate Of C++
    By Krak in forum C++ Programming
    Replies: 27
    Last Post: 01-29-2003, 01:53 PM