Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

Beginning C Tutorial


  • Please log in to reply

#1
xBPM07x

xBPM07x

    Member

  • Member
  • PipPip
  • 53 posts
Beginning C Tutorial

+ Table of Contents +

- Introduction -
- Getting Ready -
- Start Coding -
- If Statements -
- Loops -
- Functions -
- Arrays -
- Quiz -
- Credits -


{Introduction}

- This tutorial is for beginners, so for all the 'pros' who already know C, no need to read this unless you want to give me suggestions or corrections. Beginners will respect this tutorial and it's simplicity. -

This tutorial is all about the programming language C, it will introduce to you the power of C and teach you the basics you need to know to start coding. C is a great language, but many beginning coders are thrown off and confused by it, because it is such a low-level language. C syntax is pretty much like any other, so if you already know a language that's great, because this tutorial will be easier to follow for you. First off, before we start, you have to be devoted to learning the language. C and other low-level languages can get hard at times, but if you stick with it, you'll get it and love it. So, lets start, shall we?

{Getting Ready}

First off, as you probably know, we need a compiler. A compiler will, obviously, compile your code. A compiler compiles your code to machine language, to an executable, so you may run it and see what you have done. It is the most important tool in programming, and without it, you can't compile your code, so no application for you. A really nice one, and the one I use/am going to use in this tutorial, is Dev-C++. You can download it here (download the one with "Mingw/GCC"):

http://www.bloodshed...dev/devcpp.html

Once you downloaded it and have it installed, you can start coding!

{Start Coding}

You adventure starts here, in your first piece of code. I'm sure you've heard of "Hello, World!"? No? Well, it is the most common starting-out application you can make, and that is what we will be creating. Hold your breath, here it comes!

#include <stdio.h>

int main() {
  printf("Hello, World!");
  getchar();
  return 0;
}
Ok, before we compile, let us examine this code. The first line:

#include <stdio.h>
This line has #include, which is a preprocessor directive. It pretty much tells the compiler to take the code from stdio.h (which is a header file) and put it into your code, so you can use certain functions in that header. You can have many headers in your program, and you can even make your own! We include this in our program for getchar and printf. You may also have noticed the semi-colon, well it is used to end commands in C, don't forget it, as your compiler throws nasty errors when forgotten.

int main() {
This is our main function, that every program in C must have. The program reads all the code in here first, so thats where we are going to be putting our code, if we want it to work. You could also make your main function call out another function, but we will learn about that later on. Also, the curly bracket { means the start (and end) of a function.

getchar();
This line makes it so the program requires the user to input something with the keyboard, put in this program just to make it so it won't close immediately when opened.

return 0;
We return 0 to the Operating System, meaning our program is done and a success. You should include this in all your programs. And lastly, you'll see {, the curly bracket that ends our main function. Now, to compile this, in Dev-C++ click File, New, Source File. Copy and paste the code, and then click Execute, Compile and Run. You will now see the text "Hello, World!" printed on your screen!

Now we will learn how to comment your code. Commenting code is very useful at times, when looking back 2 years at some old code, wondering what it does... A comment in C looks like this:

//Include the header
#include <stdio.h>

//My main function
int main() {
  //Print "Hello, World!" to the screen
  printf("Hello, World!");
  //Get keyboard input
  getchar();
  //End the program
  return 0;
}
So, now that you know how to make comments, lets start talking about variables. Variables rock, and you will end up using and loving them in mostly all your programs. Here is how to declare a variable in C:

int somevariablename;
Here are some more:

int a,b,c;
char lettervariable;
float floatvariable;
You starting to see how it works? Here is an example I made:

#include <stdio.h>

int main() {
  //You may have noticed \n, it means newline
  printf("Hello, World!\n\n");
  int x = 0;
  printf("The value of x is: %d\n", x);
  x++;
  printf("Now the value of x is: %d\n", x);
  x++;
  printf("Now the value of x is: %d\n", x);
  x++;
  printf("Now the value of x is: %d", x);
  getchar();
  return 0;
}
Now this is cool and all, but lets get on to something cooler, accepting keyboard input. To accept keyboard input in C, you would do something like this:

#include <stdio.h>

int main() {
  int x = 0;
  printf("Please enter a number: ");
  scanf("%d", &x);
  printf("You entered %d.\n\n", x);
  getchar();
  return 0;
}
You could also multiply (*), divide (/), subtract (-), and add (+) numbers with C. For example...

#include <stdio.h>

int main() {
  int x,y,g;
  x = 5;
  y = 2;
  g = 10;
  printf("Problem one is 5-2, answer is: %d\n", x - y);
  printf("Problem two is 5+2, answer is: %d\n", x + y);
  printf("Problem three is 10/5, answer is: %d\n", g / x);
  printf("Problem four is 5*2*10, answer is: %d", x * y * g);
  getchar();
  return 0;
}
Well, thats it for this section, next you'll learn about if statements!

{If Statements}

An if statement is really useful, and you will certainly be using them in the future. They test certain conditions, and execute instructions accordingly. There is also elseif statements, to test more than one condition. Also else statements, so if all else fails, it resorts to that. To create an if statement, all you need to do is this:

#include <stdio.h>

int main() {
	int favnum;
	int myfavnum = 17;
	
	printf("Enter your favorite number: ");
	scanf("%d", &favnum);
	
	//Here is the if statement
	if (favnum != myfavnum) {
			   printf("You are incorrect, sorry!");
			   getchar();
			   }
			   
	else if (favnum == myfavnum) {
		 printf("You rock!");
		 getchar();
		 }
		 
	else {
		 printf("This text will never show up...");
		 }
		 
	return 0;
}
Now, there is a lot of code here. Let me try to break it down for you. First we declare 2 integers, favnum and myfavnum. We then ask the user for their favorite number, and use if statements to reply accordingly. If their favorite number is ours, we say they rock. If it is not, we say they suck. And the else statement is just a last resort kinda thing, if nothing else, the program chooses it. In our application, it can not be chosen, as we only have 2 answers it can be, true or false. The if/elseif/else statements will be used in your programs, so it is important to remember the syntax for all this. In this example, I used the operator ==, meaning equal to, and !=, meaning not equal to. Here is a list of operators for future reference:

>	 greater than
<	 less than
>=	greater than or equal
<=	less than or equal
==	equal to
!=	not equal to
{Loops}

While programming, if you need to repeat a piece of code, without typing it over and over 100 times, you can use loops. They allow you to repeat a piece of code any number of times you wish, and are very awesome. You could easily create a program that displays the numbers 1-500, in only 12 lines!

#include <stdio.h>

int main() {
	int counter;
	
	for (counter = 0; counter < 501; counter++) {
		printf("%d\n", counter);
		}
		
	getchar();
	return 0;
}
See how useful loops can be? In that example, I used a for loop. There are 3 types of loops:

For loop
While loop
DoWhile loop
Here is an example of a while loop:

#include <stdio.h>

int main() {
	int counter = 0;
	
	while (counter < 46) {
		  printf("%d\n", counter);
		  counter++; //If removed, an infinite loop is created
		  }
		
	getchar();
	return 0;
}
You may have noticed the comment I made? If not, it said that if you remove the piece of code that increases the counter by one, an infinite loop is created. Meaning, the condition will never be reached. This could be a good thing, but most of the time it is bad. Do..while loops are sometimes useful, but I do not see myself using them often. Just for you to see, here is an example of a do..while loop:

#include <stdio.h>

int main() {
	int counter = 0;

	do {
		printf("%d\n", counter);
		}
		
	while (counter != 0);
	   
	getchar();
}
Next up, functions!

{Functions}

Functions are useful for bigger programs, and there is no doubt that you will be using them in the future. Everything in this tutorial you will be using in the future! But anyways, functions are a piece of code that can be accessed by simply calling it. We have already used functions, if you didn't know. 'Main' is a function, and so is getchar(). You can create your own too, that is the greatest part. Functions can also take/require parameters, such as the rand() function. Lets try making our own:

#include <stdio.h>
#include <math.h>

int math(int x, int y) {
	return x * y;
}

int main() {
	int x;
	int y;
	
	printf("Enter two numbers to be multiplied (separated by spaces): ");
	
	scanf("%d", &x);
	scanf("%d", &y);
	
	printf("The answer is: %d\n", math(x, y));
	
	getchar();
	return 0;
}
Functions can make your life much easier, recalling code whenever you want.

{Arrays}

An array is pretty much one variable with many values. It is useful in many cases, and you'll learn to love them. Without arrays, you'll need to create numerous variables for something small. Here is an example of an array and how it could benefit you:

#include <stdio.h>

int main() {
	int x;
	int y;
	int array[8][8];
  
	for (x = 0; x < 8; x++) {
		for (y = 0; y < 8; y++)
		array[x][y] = x * y;
		}
		
  printf("Array indices: \n\n");
  for (x = 0; x < 8;x++) {
	  for (y = 0; y < 8; y++) {
		  printf("[%d][%d]=%d\n", x, y, array[x][y]);
		  }

	printf("\n");
	}
	
  getchar();
  return 0;
}
Arrays may look complex, but they are really simple, and will help you in the long run.

{Quiz}

Ok, are you ready for your quiz? This quiz will see if you really read and understood this tutorial. This quiz is for you, so there is no point in cheating. Your challenge is to create a program that:

1. Tells the user a bit about yourself
2. Then asks the user for their name
3. Then their age, on a new line
4. Then you display it back in a sentence
Simple? Maybe so, but don't say that until you do it from scratch and without peeking!

{Credits}

I could not have written this tutorial without a few sites, and a few people. Those sites and people will now be listed.

Web pages:

http://www.cprogramm...m/tutorial.html
http://www.iu.hio.no.../CTutorial.html
http://www.academictutorials.com/c/

People:

Pingu
Azorbix
Deadlyp99
RunningBon

Edited by xBPM07x, 27 January 2009 - 11:12 PM.

  • 0

Advertisements


#2
mocipher

mocipher

    New Member

  • Member
  • Pip
  • 1 posts
Excellent Job mate !!! I've Bookmarked this Thread For Reference In The Future ..

Big Thanks Guys !!
  • 0

#3
Joshua666

Joshua666

    Member

  • Member
  • PipPip
  • 21 posts
Bookmarked this, I wanted to learn C, now I will! :D

Joshua
  • 0

#4
Datasmurfen

Datasmurfen

    Member

  • Member
  • PipPipPip
  • 106 posts
Nice!
Thx so much!
  • 0






Similar Topics

1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP