Friday, October 25, 2013

How to Learn to Program in C

Edited by Eshafer, Nicole Willson, Vikram Kumar Rohra, Horses4Ever and 27 others
C is a powerful programing language that was first developed in the 1970's. Depending on your previous programming experience, it may take time and patience to learn C. However, once learned, learning other programming languages will come naturally. While learning the entire language is beyond the scope of this article, it will get you started in the right direction.

EditSteps

  1. 1
    First find a compiler that is compatible with your operating system, and decide whether you want to run an Integrated Development Environment (IDE) or if you want to edit C files manually through an editor like Notepad and compile from the command line.
    • If you're a Windows user, try using Visual C++ Express Edition which is available for download for free.
    • If you're a Mac user, you can use Xcode, available from the Leopard install disk or the App Store for free.
    • If you're a GNU/Linux user, try KDevelop or Eclipse. Eclipse is also available for Windows.
    • Alternatively, you can use the command line tools (GCC runs under any Unix and it is possible to run it under Windows and Mac).
    • Tiny C Compiler (TCC) is great if you don't want to get a ton of bells and whistles that come with other compilers
  2. 2
    Learn how to compile and run a basic program, this will be your first program, typically it will just print "Hello World" to the screen and exit. Don't worry about all the minor details of the syntax, just become comfortable with compiling and running.
  3. 3
    Learn about variable types, such as the difference between char, int, float, double, etc.
  4. 4
    Learn about the concept of variables, arrays and functions. Variables are where information is stored, functions are pieces of code that can be executed and arrays are groups of data of the same type.
  5. 5
    Learn pointers. Pointers are very important in C since you can directly access memory contents through pointers, unlike Java. The drawback to this is that if your program isn't thoroughly tested, it can crash.
  6. 6
    Learn conditional statements, such as the "if" and "switch" statements. The "if" statement will be one of your most frequently used statements, you can execute code based on whether a condition is true or not (e.g. whether the color the user provided was red).
  7. 7
    Learn loops. Learn the difference of the "for" loop and the "while" loop - make sure to avoid infinite loops! Learn the continue and break statements.
  8. 8
    Learn data structures. Although data structures are not directly related to programming, but for an advanced user, knowledge of basic concepts in Computer Science is essential.
  9. 9
    Start with small programs. When you are making your own code, try to identify the most essential part of the problem - is it the data input or the calling of the functions, the structure of the loop (these are some very elementary examples) and start from there. Then build upon that in small increments.
  10. 10
    Learn key steps about debugging. Although it looks little complicated in the beginning, but if you have a large source code, then using print statements gets little overwhelming.
  11. 11
    Enroll yourself in some of the mailing lists online. You can post your own of questions and support other new users.
  12. 12
    Remember, the key to learning anything new is perseverance & hard work.

EditSample Code

Function to calculate the greatest common divisor (using the euclidean algorithm):
int gcd(int u, int v)
{
  int r;
  while (v != 1)
  {
    r = u % v;
    u = v;
    v = r;
  }
  return u;
}
The function takes two integers named 'u' and 'v' and returns an integer. The function has the name 'gcd'.


Function to calculate xy:
int power(int x, int y)
{
  int result = -1
;
  int i;
  for(i = 1; i < y; i++)
    result *= x;
  return result;
}
The function takes two integers named 'x' and 'y' and returns an integer. The function has the name 'power'.

EditTips

  • Your source code needs to have a *.c extension, so that your compiler can understand that it's a C source file.
  • Find a good C programming book. A recommendable C resource book is "The C Programming Language" by Brian W. Kernighan, Dennis Ritchie (ISBN 0131103628 - You can a free electronic copy of the original draft here). Find a book that has tutorials and projects to facilitate your exposure to C.
  • Always add comments to your programs. Not only does this help others who might take a look at its source code, but also it helps you remember what you're writing and why. You may know what you're doing the moment that you're writing your code, but after two or three months, you won't remember much.
  • Don't try to avoid mistakes; embrace them. Making mistakes is the most important part in learning how to programm.
  • Remember, C is a programming language. Learning a programming language may not necessarily lead to learning to program, which is more about problem solving than about compiling and running a program in a specific language.
  • When encountering a syntax error when compiling, if you're stumped, search Google (or another search engine) with the error you received. Chances are someone has already experienced the same problem and posted a solution.                    source - http://www.wikihow.com/Learn-to-Program-in-C

No comments:

Post a Comment