Help an ambitious 16 year old learn C

This is a discussion on Help an ambitious 16 year old learn C within the C Programming forums, part of the General Programming Boards category; This is not a question anyway related a single homework assignment, but rather a request for someone to help someone ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    9

    Help an ambitious 16 year old learn C

    This is not a question anyway related a single homework assignment, but rather a request for someone to help someone learn C on the side, without them taking a class. This post starts out with some general information on my skill level, and works its way to down to a list of things you could do to help me learn C. It's a fairly long read so if you aren't interested in helping this is a forewarning that it might be a waste of time.

    Hello, I am a Junior in High School, and recently I decided to take a class called "AP Computer Science I" which required me to skip "Intro to Computer Science" (a class which does not cover much) and "Windows Programming" (a dual-enrollment class which mainly works with Visual Basic? I think... I didn't take it lol). At first, I had a real hard time understanding programming terminology, and I got off to a rough start. Over the first 2 weeks or so, we learned some scheme. The programs that we made were extremely low level and after this brief intro into programming (for me at least), we switched to Java. After more struggling, I began to get the gist of programming at a low level.

    Currently, we are working on a program called StringUtil, in which we have to create functions that manipulate strings. Such as: Reverse, Palindrome Checker, Pig Latin Converter, and Short Hand Converter. Each of these must be done recursively, and since we have not learned loops or arrays, neither can be used, along with StringBuffer and additional useful methods that can be found in the Java API. I have already written: Reverse and the Palindrome Checker. I had them checked and they work as intended and were done recursively (the right way).

    As far as Programming in C, I have only read the "C Made Easy" Beginner Tutorials on this site from the first lesson "Intro to C" through "C-Style Strings." I read this tutorials today, and I do not fully understand them, but I have learned somewhat the basics. While coming from Java Background (though a very incomplete one), the lesson on pointers was the most confusing and difficult lesson.

    I do understand some basics of pointers vaguely, such as:

    - How to declare a pointer
    - How to assign the address of a variable to a pointer
    - How to initialize pointers using malloc
    - How to free pointers and set them to become null pointers

    But, I lack the understanding of when to use them and ultimately, why to use them? This is a very important subject to learn and fully understand, and anyway in which you could help me understand them would be greatly appreciated.

    Even though we have not learned loops, I decided to read ahead in our Java Lessons, and I have a basic understanding of loops in Java (and some in C via the Tutorials on this site). After reading this I decided to go ahead and do one of the assignments which we may do or may not. This assignment included finding magic squares and writing a method to find the Least Common Multiple or LCM of two numbers. I found this not too hard in Java. I wrote a program which included the two methods which worked.

    Now, I am trying to write the magic square portion of that program in C as an attempt to grasp what I have read because without any practice I will not learn anything about C. This is one of the major problems I face. I have no assignments which are at my level and no person to help me through this process.

    I have a fair amount of desire to learn C, but I am somewhat lazy, and I would like to ask if anyone (or a number of people) would like to help me get the hang of C by:

    1) Taking the first step by helping me complete and understand this magic square program which I have attempted writing in C

    2) Possibly take a step back to review the very basics of C by providing me a couple of simple programs to write and for you (or a number of people to check)

    3) Continue to move forward by giving me additional programs to write increasing in difficulty provided with help to complete them and obtain the optimal solution

    I know this a lot to ask, but any help would be much appreciated. I am considering programming as a career.


    Now, back to the magic square program which i am trying to duplicate in C. Here is a little bit of information that was provided to help me understand what a magic square was:

    Magic square problem:

    1. Some perfect squares have unique mathematical properties. For example, 36 is:

    - A perfect square, 6^2
    - And the sum of the integers 1 to 8 (1+2+3+4+5+6+7+8 = 36)

    So let us call a "magic square" any number that is both a perfect square AND equal to the sum of consecutive integers beginning with 1.

    2. The next magic square is 1225:
    352 = 1225
    1225 = sum of 1 to 49

    3. Write a method that prints the first n magic squares.

    Here is my version of this completed in Java:

    Code:
    public class FunLoops
    {
        public void magicSquare(int n)
        {
            int ms = 0; //Variable for storing the amount of magic squares found
            long num = 1, num2 = 1;
            long ps = 1, cia = 1; //Ps stands for Perfect Square, and Cia stands for Consecutive Integer Addition
            
            while( ms < n)
            {
                 while( ps < cia)
                 {
                     num++;
                     ps = num*num;
                 }
                 
                    while( ps > cia)
                    {
                        num2++;
                        cia += num2;
                    }   
                        if( ps == cia)
                        {
                            System.out.println(ps);
                            ms++;
                            num++;
                            num2++;
                            ps = num*num;
                            cia += num2;
                        }              
            } 
        }
    Here is my attempt in C:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        int n;
        int ms = 0;
        int num1 = 1;
        int num2 = 1;
        int ps = 1;
        int cia = 1;
    
        printf( "Please enter the number of magic squares\n" );
        printf( "to be found and displayed:\n\n" );
        printf( "(Remember these values will be found in\n" );
        printf( " order, from the lowest possible magic\n" );
        printf( " square to the highest possible magic square)\n\n" );
        printf( " Input: " );
        scanf( "&d\n", &n );
    
        while ( ms < n )
        {
            while ( ps < cia )
            {
                num1++;
                ps = num1*num1;
            }
    
            while ( ps > cia )
            {
                num2++;
                cia += num2;
            }
    
            if ( ps == cia )
            {
                printf( "\n&#37;d\n", ps );
                ms++;
                num1++;
                num2++;
                ps = num1*num1;
                cia += num2;
            }
        }
    }
    It does compile and it gives me the first 2 magic squares (1 and 36) but for some reason stops right there.

    I know that this was a horrible attempt, and I basically just copied the code, but I did learn a couple of things by this attempt. Remember, that I don't know when to use pointers and anything I do know was learned in a 4 hour reading of half of the Tutorials on this site.
    Last edited by Xlayer; 10-06-2007 at 10:41 PM.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You're certainly a good writer. That's a better post than many university students write . . .

    Besides a few minor changes, like the variable num versus num1, and what is printed, and the fact that the Java version is implemented in a function, your programs are virtually the same. So good job converting the Java to C, if that is in fact what you did . . .

    There is one thing I noticed:
    Code:
    scanf( "&d\n", &n );
    You should leave off the \n. scanf() automatically skips whitespace, so you don't need it; and your code requires that the user types a \n -- which will probably happen, but there's a chance it might not (if the input is redirected from a file, for instance).

    Another thing I noticed about your C code is that you don't have
    Code:
    return 0;
    at the end of main(). However, as you have it is perfectly valid in C99, so this is a minor point.

    Also, here's a nice feature of C, the absence of which I find annoying when I program in Java. Adjacent string literals are automatically concatenated. What this means is that this code
    Code:
        printf( "Please enter the number of magic squares\n" );
        printf( "to be found and displayed:\n\n" );
        printf( "(Remember these values will be found in\n" );
        printf( " order, from the lowest possible magic\n" );
        printf( " square to the highest possible magic square)\n\n" );
        printf( " Input: " );
    does the same thing as
    Code:
        printf( "Please enter the number of magic squares\n"
        "to be found and displayed:\n\n"
        "(Remember these values will be found in\n"
        " order, from the lowest possible magic\n"
        " square to the highest possible magic square)\n\n"
        " Input: " );
    The preprocessor automatically concatenates any string literals into one string; Java, of course, with no preprocessor, does not have this feature.

    Now, as to your logic. I'll be commenting on your C program because this is CBoard and because it has better indentation.

    I guess that your outer while loop is supposed to generate n magic squares. Fair enough.

    Code:
     while ( ps < cia )
            {
                num1++;
                ps = num1*num1;
            }
    
            while ( ps > cia )
            {
                num2++;
                cia += num2;
            }
    The first thing to note is that both of the while loops will only work once, because num1 and num2 are initialized at the beginning of the program. You need to initialize num1 to zero right before the first while loop, and num2 right before the second.

    Next notice that both of the loops rely on cia to be the number that they are "aiming" at. This is fine if you have a number which you want to check for magic-squareness, but if you want, say, the first three magic squares, then there are probably more efficient ways of doing this.

    For example, you could start with 1*1 = 1; then use 2*2 and calculate 1 + 2, and so on, keeping a running total of the sum.

    As written, you'll need to set the number cia somehow.

    [How did I get that nice coloured code? I used codeform, a syntax highlighter that I wrote, which you can use online here: http://dwks.theprogrammingsite.com/myprogs/cfonline.htm]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,673
    Currently, we are working on a program called StringUtil, in which we have to create functions that manipulate strings. Such as: Reverse, Palindrome Checker, Pig Latin Converter, and Short Hand Converter. Each of these must be done recursively, and since we have not learned loops or arrays, neither can be used [ ... ]
    Your teacher expects you to reinvent a crude wheel without the simplest of tools? Ask your teacher if he prefers a solution written his way or one that will work.

    I suggest you wait and hear what other people have to say but it seems rather odd to me that jumping from language to language is going to teach you how to program any better than, say, learning functional programming with scheme one semester, and then OOP with Java or something the rest of the year.

    *shock and awe*

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    For learning C, I would recommend the tutorials on this website, as well as checking out http://www.howstuffworks.com/c.htm. The explanations on this website are pretty accurate, however, they are slightly less friendly imo than the other site I gave you. The issue with the other site, is that while the diagrams are awesome, they give you some really bad examples and bad code that isn't truly correct C necessarily. Beware.

    Pointers are simply variables. The difference between them and regular variables is that they are meant to contain addresses of other variables as opposed to "straight values". To us, we don't like to remember variables by number, so we name them with friendly names (well, hopefully...) so that it's easier for us to remember what they are for. Sometimes various functions need to know where a variable resides in memory as opposed to what value it contains (ie. printf() and scanf() for printing and reading integers). This is where pointers come in. They are basically just addresses of variables, which in essence, are just numbers.

    Since you say you're coming from a Java background, think about how Java objects work.

    Code:
    String s = new String("Tesing 1 2 3");
    s is really a pointer of sorts. It's not the actual string variable. Java hides the real underlying mechanisms of pointers and uses what many call "references" (which there is something called a reference in C and also in an additional form of references in C++ as well... Don't worry about it). The idea is still the same, though. A Java reference contains information in a variable so that the system can find the object in question. In the same manner for C, a pointer is used so that you can deal with the actual variable in question.

    The need for pointers may not be apparent, yet. In general, for now, if you don't need to use a pointer, don't. As you progress and get a better grasp of C, the need for pointers should become clearer.

    Edit: BTW, "&d" should be "&#37;d".

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    9
    Quote Originally Posted by dwks View Post
    There is one thing I noticed:
    Code:
    scanf( "&d\n", &n );
    You should leave off the \n. scanf() automatically skips whitespace, so you don't need it; and your code requires that the user types a \n -- which will probably happen, but there's a chance it might not (if the input is redirected from a file, for instance).

    Another thing I noticed about your C code is that you don't have
    Code:
    return 0;
    at the end of main(). However, as you have it is perfectly valid in C99, so this is a minor point.
    Thank you, I changed both and before that explanation of the scanf thing I didn't even really understand what I was doing when I put the \n. Now, I understand that it "requires that the user types a \n."

    Quote Originally Posted by dwks View Post
    Also, here's a nice feature of C, the absence of which I find annoying when I program in Java. Adjacent string literals are automatically concatenated.
    Also, a very useful thing to know.


    The first thing to note is that both of the while loops will only work once, because num1 and num2 are initialized at the beginning of the program. You need to initialize num1 to zero right before the first while loop, and num2 right before the second.
    For some reason when I do this it actually generates incorrect magic squares...
    http://i59.photobucket.com/albums/g2...3Oct062251.gif

    Here is my code with all but the initialization place changes:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        int n;
        int ms = 0;
        int num1 = 1;
        int num2 = 1;
        int ps = 1;
        int cia = 1;
    
        printf( "Please enter the number of magic squares\n"
        "to be found and displayed:\n\n"
        "(Remember these values will be found in\n"
        " order, from the lowest possible magic\n"
        " square to the highest possible magic square)\n\n"
        " Input: " );
        scanf( "&#37;d", &n );
    
        while ( ms < n )
        {
            while ( ps < cia )
            {
                num1++;
                ps = num1*num1;
            }
    
            while ( ps > cia )
            {
                num2++;
                cia += num2;
            }
    
            if ( ps == cia )
            {
                printf( "\n%d\n", ps );
                ms++;
                num1++;
                num2++;
                ps = num1*num1;
                cia += num2;
            }
        }
        return 0;
    }
    This code gives the correct answers as shown in this image:
    http://i59.photobucket.com/albums/g2...1Oct062249.gif

    But, it will not display 10 magic squares because they eventually become greater than the allowance of an Unsigned long int (0 to 4294967295)??? Right?

    This is shown in this image:
    http://i59.photobucket.com/albums/g2...2Oct062250.gif

    Thank you MacGyver for the "&d" to "%d" correction... This caused my program to output the first 2 magic squares but nothing else. Extra thanks, for the pointer explanation.

    Citizen, I completely agree with you. Starting briefly in one language and then jumping to another almost instantly didn't help me much. I don't even remember anything about scheme or why we just briefly covered it (for 2 weeks). Your schedule makes much more sense. Also, not being able to use certain things is really annoying, but it is forcing us to use recursion. It annoys me though because all the functions can be done so much better and more efficient with %10 of the amount of code needed for a recursive solution.

    DWK, I would use your codeform program if I could download it. It keeps saying that the file can not be found when I go to download it from one of your links. Also, thanks for everything and maybe you could just show me your optimal solution for this program and we could move on (possibly backwards) by you giving me a very simple program to write. Then you could check it and then we could Rinse, Wash, Repeat to learn other concepts and check for understanding.

    Thanks to everyone for all of the feedback. I first got discouraged when I saw that this thread had 22 views and no replies. I thought everyone was reading the forewarning and deciding not to help. Everyone is very generous on these forums.
    Last edited by Xlayer; 10-07-2007 at 12:19 AM.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    I didn't look at your algorithm very closely. You're right, initializing those variables would not be a good idea . . . so it looks like your code is working now?

    DWK, I would use your codeform program if I could download it. It keeps saying that the file can not be found when I go to download it from one of your links.
    Strange. What link were you trying to download from? The download works for me. Here's the direct link: http://dwks.theprogrammingsite.com/m...n/codeform.zip
    But if you want to try it out it's much easier to do so online. Just type some code and click "codeform" at the end, and paste the generated code into a BBCode forum like this one.

    But, it will not display 10 magic squares because they eventually become greater than the allowance of an Unsigned long int (0 to 4294967295)??? Right?
    Well, yes -- I don't know if it's number 10, but eventually your number will wrap. [edit] If you want larger numbers, try a long long -- your compiler may support it. (You'll have to use &#37;lld for scanf() and printf().) Larger than that, and you'll need an external library. Or a floating point number, but that will have inaccuracies for large numbers. [/edit]

    Also, not being able to use certain things is really annoying, but it is forcing us to use recursion. It annoys me though because all the functions can be done so much better and more efficient with %10 of the amount of code needed for a recursive solution.
    But it's the concept that counts. When you come across a situation where you really do need to use recursion, you'll then know how to do so.

    Thanks to everyone for all of the feedback. I first got discouraged when I saw that this thread had 22 views and no replies. I thought everyone was reading the forewarning and deciding not to help. Everyone is very generous on these forums.
    Check out "Who's Online": http://cboard.cprogramming.com/online.php
    Most of the time (though not right now, it seems) web crawlers and spiders outnumber real users by a large number. They'll look at your page and not reply . . . many times the number of views for a thread will be five or ten times the number of replies. Don't worry about it.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,673
    > Citizen, I completely agree with you
    I'm glad that you feel this way. Allow me to express my concern that you might burn out horribly, and instead of learning how to program, you will learn three or so languages poorly. And end up in a Microsoft basement patching Vista with all the wonderful crap you learned!

    I would recommend that you have a discussion with your teacher and see if he can help you address your concerns. Above all I think you should express our mutual concern that you might burn out from an unreasonably tight, counter-productive academic schedule. Give him the opportunity to explain why his lesson plans, speedy as they are, are the most effective choice for the students of his classroom.

    The important thing is if he is able (or even interested) in helping you succeed in his class. If your teacher can explain his plans to your satisfaction, then stay in class and work at it. If he doesn't, then I would drop it and be melded into another course. You might find a more basic course just as interesting, more productive, and have the necessary free time to overload your schedule learning C.

    While you're at it, you should probably ask him to explain why the loops lesson is being delayed so long. I mean, there are whole algorithms that don't really work without recursion (quick sort, for one), why aren't you learning those? A good way to get him to teach recursion is to suggest that he teach an algorithm that needs it, because you either don't understand recursion or feel that your learning to kludge.
    Last edited by whiteflags; 10-07-2007 at 01:00 AM.

  8. #8
    Registered User
    Join Date
    Oct 2007
    Posts
    9
    Dwks, can you give me a program to attempt to write in C for tomorrow? If you can, please make it fairly simple, and then you can check it. Then we could just keep moving up in increasing difficulty.

  9. #9
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Usually math based programs are a good place to start, or database sort of programs. Like a program that manages clients and their jobs or something, it should get you working with structs, pointers, file i/o, user i/o and maybe even linked lists.

    >because you either don't understand recursion or feel that your learning to Kludge.
    I was wondering where KDE came from ... lol jk

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Dwks, can you give me a program to attempt to write in C for tomorrow? If you can, please make it fairly simple, and then you can check it. Then we could just keep moving up in increasing difficulty.
    dwks, official program assigner, hereby grants you the opportunity to create:
    • A Fibonacci number program. Recursive, and non-recursive. (You may have already done this.)
    • A simple calculator. Start with "enter num 1", "enter operator", "enter num 2", etc.
    • A user login system, as zacs7 has suggested. Prompt for usernames and passwords, etc. Store the data in a file. Use encryption. Grant users access to your calculator.

    Whatever you like. Use your imagination. If you can't get it working, post it here and I'm sure someone will help you.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    What? No deathray?

  12. #12
    Registered User
    Join Date
    Oct 2007
    Posts
    9
    Thanks for all the help and for the program ideas. I am going to start with the Fibonacci Numbers program. I am now reading over what Fibonacci Numbers are after which, I will attempt to write the actual program, and then I will most likely be back asking questions of why this isn't working. I will post my attempt soon.

    Edit: Lol, I just barely figured out a working algorithm... I took me 30 minutes, but I was somewhat preoccupied with watching TV. Will post my attempt soon.

    Edit2: Wow, it didn't take long to program that after figuring out the algorithm. I would post my code, but I want to post it with Codeform which I am going to try to use right now.

    Edit3: I can't figure out how to use codeform, and I found a problem with my program. For some reason it starts to output negative values mixed with positive values after the 46th Fibonacci Number. After, I figure out codeform I will take a picture to show this output, and I will post my code so we can edit it. (I also added some comments to my code to explain what I am doing or what certain variables mean.)
    Last edited by Xlayer; 10-07-2007 at 04:09 PM.

  13. #13
    Registered User
    Join Date
    Oct 2007
    Posts
    9
    I finally figured out how to use Codeform. I had to use the online version though. Here is my code for the Fibonacci Number Program.

    I used this website to figure out what a Fibonacci Number was:
    http://www.jimloy.com/algebra/fibo.htm

    It works all the way up to Line 47 which gives me a negative value:
    Picture of Output

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        int n; /* Number of Fibonnacci Numbers to Find (Inputed by User) */
        int index = 1; /* Index of Fibonnaci Number (Starts with 1) */
        long int fib_num = 0; /* Fibonacci Number */
        long int num1 = 1; /* Represents 1 Fib# before Last Fib# */
        long int num2 = 1; /* Reprsents Last Fib# */
    
        /* Prompts user for n, and reads input */
    	printf( "Please enter the amount of Fibonacci\n"
    	"numbers to find: ");
    	scanf( "&#37;ld", &n );
    
        /* Executed until Fib#'s Found == Fib#'s Wanted by User */
    	while ( index <= n )
    	{
    	    if ( index <= 2 )
    	    {
    	        printf( "\nFibonacci Number #%d = %ld", index, num2 );
    	    }
    	    else
    	    {
                fib_num = num1 + num2;
                num1 = num2;
                num2 = fib_num;
                printf( "\nFibonacci Number #%d = %ld", index, num2);
    	    }
    	    index++;
    	}
    	return 0;
    
    }
    I completely ignored pointers, and I am sure there is probably a better algorithm. Let the corrections/learning begin.
    (I don't see a use for pointers even though I am sure there is one, but in this situation it seems that the variables are used until the program terminates so i can't see how you could free the memory much sooner than it already is.)
    Last edited by Xlayer; 10-07-2007 at 04:55 PM.

  14. #14
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by Xlayer View Post
    I completely ignored pointers, and I am sure there is probably a better algorithm. Let the corrections/learning begin.
    (I don't see a use for pointers even though I am sure there is one, but in this situation it seems that the variables are used until the program terminates so i can't see how you could free the memory much sooner than it already is.)
    So don't use pointers. Why would you need them here?

    As far as a better method, depending upon what you mean by "better", I would suggest, at least for fun, to try a recursive approach.

    Edit: Also I suggest this because I like recursion for things like this.

  15. #15
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    Quote Originally Posted by dwks View Post
    You should leave off the \n. scanf() automatically skips whitespace, so you don't need it; and your code requires that the user types a \n -- which will probably happen, but there's a chance it might not (if the input is redirected from a file, for instance).
    Actually, it is usually a good idea to have it there, given:
    A directive that is an ordinary character shall be executed as follows: the next byte shall be read from the input and compared with the byte that comprises the directive; if the comparison shows that they are not equivalent, the directive shall fail, and the differing and subsequent bytes shall remain unread. Similarly, if end-of-file, an encoding error, or a read error prevents a character from being read, the directive shall fail.
    Leaving the trailing "\n" ensures that it is taken off the input stream, so that the next call to a read won't be taking in stale bytes (which will save many headaches if you decide to use non-scanf functions afterwards, such as fgetc).
    Quote Originally Posted by dwks View Post
    Another thing I noticed about your C code is that you don't have
    Code:
    return 0;
    at the end of main(). However, as you have it is perfectly valid in C99, so this is a minor point
    Once again, that is actually a good thing; and I would encourage all new programmers to leave it out. Not only does it promote current standards, makes for less typing and reduces errors, if in the future ISO decides to change the standards so that main implicitly returns "1" instead, it means that much less dependancy checking that you have to perform.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Little help with leap years
    By Jayhawk_26 in forum C Programming
    Replies: 17
    Last Post: 10-05-2006, 02:03 PM
  2. Constructive criticism/suggestions
    By LineOFire in forum C Programming
    Replies: 11
    Last Post: 09-30-2006, 09:32 AM
  3. debug program
    By new_c in forum C Programming
    Replies: 3
    Last Post: 03-18-2002, 10:50 PM
  4. Simplified code
    By soonerfan in forum C Programming
    Replies: 2
    Last Post: 12-05-2001, 02:50 PM
  5. Help Me Out!! Pls
    By Joanna in forum C++ Programming
    Replies: 5
    Last Post: 10-27-2001, 05:08 AM

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