C: For Beginners

For some reason, in order to invent a programming language (such as C), you have to have a crazy beard; a goatee won't cut it, and neither will a giant handlebar mustache. Although, giant handlebar mustaches do allow you to teach college students to program (or be a cop, or a news anchor, or a member of the Village People).

Contributed by
Rating: 4 stars4 stars4 stars4 stars4 stars / 13
January 28, 2008
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

And perhaps beard size/craziness determines the power of your language as well. I don't know. What I do know, is that Dennis Ritchie, the inventor of C and co-inventor of Unix (alongside his buddy Ken Thompson, who strangely enough invented the B language, from which many of C's features came), has one of the weirdest beards imaginable. By the way, it isn't lost on me that the ratio of programmers to crazy beards in India is quite low.

The development of C took place at AT&T's Bell Labs (the same people who brought you the laser, Unix, the transistor, and this device that lets you make julienne fries) between the years 1969 and 1973. Since then, it has given birth to and influenced a number of other programming languages, including C++, D, Objective-C, Java, C#, Perl, PHP, JavaScript, and more.

Now that we have learned a little about the history of C and the importance of having a Santa Claus-like beard, or at least a beard big enough to do a beard-over with, we can start learning how to code in C. Before we do so however, I have to make several assumptions:

  • You have either a text editor, such as Notepad, and a compiler that you know how to operate or

  • You have an IDE (Integrated Development Environment).

  • If you are using an IDE, that you know your way around it (you know how to compile programs etc.).

  • You have a check for $10,000 with my name on it you will mail as soon as you finish reading this tutorial.

  • And at least a little beard stubble. Otherwise you don't stand a chance.

For the purposes of this tutorial we will be using Notepad and will be focusing strictly on coding. If you need help with compilers, there are many sites on the web that can help you.

Let's get started...

Insert Obligatory First Program Here

Writers that create books and articles for programmers believe that programmers are trained monkeys, obsessed with one goal: to say "Hello" to the world. They always give you these dumbassed introduction programs to print out "Hello World" to the screen. We're not going to do that though.

(Note: Interestingly enough, the tradition of using "Hello World" as a programming introduction was started by Kernighan in his 1972 book Tutorial Introduction to the Language B and was inherited from an internal memo by the same author from Programming In C: A Tutorial).


#include <stdio.h>

int main (void)

{

printf("Why does every programmer look like this...n");

printf(" ~~n");

printf("c00cn");

printf(" (0)n");

printf(" Vn");

return 0;

}

Before I explain the above code, let me show you the result:

  Why does every programmer look like this...

   ~~

 c00c

  (0)

   V

You have no idea how long that took me to make. He looks a little bit like Dilbert, right? Anyway, here is how the program works:

The first part of our code tells the compiler to include a file named stdio.h. This file is made up of descriptions of input/output functions and is used when writing output and accepting input from a user.

The next part is the int main(void). This is required in every C program. The int part tells the compiler that an integer will be returned to whatever made the function itself run when it is finished. The main is a function, and the void in the parentheses states that there will be no parameters in main. As a beginner, it's okay if you don't understand any of that. We will cover it more in depth in future articles. For now, just know that every C program you create must have int main(void) in it.

The printf is a function (it is defined in the stdio.h file, and that is why we had to include it) that prints text to the monitor. The words in between the parentheses and the double quotes will be printed to the monitor. And we use the semi-colon to end the line.

Also, you will note the n. This is known as an escape and it tells the program to add a new line at the end of the sentence (or wherever you place it). If we write the following code without the escape...


#include <stdio.h>

int main (void)

{

printf("Why does every programmer look like this...");

printf("ooga booga");

return 0;

}

...then our result would be:

  Why does every programmer look like this...ooga booga

However, if we had included the n escape, like so:


#include <stdio.h>

int main (void)

{

printf("Why does every programmer look like this...n");

printf("ooga booga");

return 0;

}

The result would be:

  Why does every programmer look like this...

  ooga booga

There are other escape methods, such as the t, which creates a tab, but we will go over those later.

Lastly, the line return 0 literally returns the value 0 to the operating system. This tells the operating system that the program ran with no errors. If the value was anything but a zero, it would have indicated an error or a warning.

So there you have it, your first C program.

Working with Numbers

The printf function described above is used to print text or strings to the screen. If we typed in the code:


#include <stdio.h>

int main (void)

{

printf("2+2= ");

return 0;

}

It would simply print:

  2+2=

and not solve the math. To get the program to perform our math for us, we would write the following:


#include <stdio.h>

int main (void)

{

printf("2+2= %d", 2+2);

return 0;

}

The result:

  2+2= 4

The way this code works is it creates a placeholder (in this instance %d) that will hold a value, which we defined after the comma (,): 2+2 (or 4). There are many placeholders (or format specifiers) in C, and we will discuss them more later on. But for now, here is a basic set of them in an awe-inspiring table:


Placeholder

What it Holds

%i and %d

Integer

%f

Float

%If

Double

%s

String

%x

Hexadecimal

%c

Character

We will discuss the various data types in a different article. I just wanted to show you these here to give you an idea of how placeholders operate. An example of another way to use a placeholder is to define where your text will appear. Suppose I wanted to print some text to the left and then some text to the right. I could do so this way:



#include <stdio.h>

int main (void)

{

printf("%40s","Hey! Where are you?!?n");

printf("%-40s","I'm over here!");

printf("%400s","Oh! Now I see you!");

return 0;

}

The %40s tells the program to move the text 40 spaces to the right. The %-40s moves the text to the left. And the %400s moves the text 400 spaces to the right; however, there aren't 400 spaces on a line, so it moves it down a few more lines until four hundred spaces have been entered, then it prints the text. Here is the result:

                                         Hey! Where are you?!?

  I'm over here!




                                                       Oh! Now I see you!


Putting on the Puts()

Another way to print strings to the screen is using the puts() function. You can use puts() when you do not plan on using placeholders:


#include <stdio.h>

int main (void)

{

puts("Hello, pretty lady!");

return 0;

}

This prints out:

  Hello, pretty lady!

Note that puts() automatically places a new line in your code, whether you like it or not. What a jerk. There is also a function called fputs, which works in a similar manner, but it is a little beyond the scope of this article, and to be honest, I would typically just stick with the printf() function.

Printing Is Fun Until Someone Loses Your Eyes

Printing is all fun and good and is probably the most used function in all of C, but wouldn't you like to do more than blabber at the user all day? I mean, I like to do that, but I am a writer. You, yer just a programmer. You let your programs interact with humans so you don't have to go out in the sunlight and get all burned up by that big yellow ball of heat someone told you was called "the sun."

Haha! I tricked you! You thought I was going to teach you how to do more than print, but I was merely Jedi mind tricking you to come back and read my next article, where I will discuss getting input from the user and working with variables to store information.

So come back. What else have you got to do?

Till then...

blog comments powered by Disqus
BRAINDUMP ARTICLES

- Microsoft Windows 8 Committed to Cloud Compu...
- Independent Developers Favor Windows Phone 7
- Dell Introduces VMware-based Cloud
- Microsoft and Skype Agree to Acquisition Deal
- Transfer Contacts in Microsoft Outlook
- Zune`s Next Steps
- Safari Books Online Review
- Does Microsoft Get Touch Screens Now?
- Microsoft`s Record Quarterly Earnings Not En...
- Basic Operations and Registers in Assembly
- Assembly Coding within Visual C/C++ IDE
- New Microsoft Office Coming with a Twist
- Microsoft`s FUSE Labs Unveils Spindex Social...
- HP Slate with Windows 7: Dead or Alive?
- Windows Phone 7 Mobile OS to Rival Android a...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 6 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials