Introduction to C Programming [Part 2: your first program] 01-13-2015, 07:20 PM
#1
Some members have made me think maybe writing this tutorial was a bad choice, but I will write it anyways, for the few that actually give a rats ass about learning.
So, you now have the tools, and are ready to begin your first program. Lets get started. Open up a blank file named "hello.c"
Alright. A few rules I need to state before I jump into code.
1. EVERY C program MUST have an entry point. We call that "main".
2. EVERY C program MUST be compiled from object or source files (these are the .c files)
So, step 1. Main.
Main is a function. For those of you who went above the high school level in math will already know what a function is. v(p)=dp/dt type of thing.
Basically, a function takes zero or more parameters, does a task, and returns zero or one output. It sounds simple to me, but it may not have been explained correctly. So, let's do a quick example:
f(x, y)=x*y
Simple. Here is the equivalent integer function in C:
There we go. There are a few things in that I need to explain before we can go any farther. Firstly, data types.
Data types are what we will use to store our data, manipulate it, or really do anything with it. There are a few basic types:
int - integer in the range +/-2^32
float - floating point (decimal) number (32-bit)
double - double precision float (64-bit)
char - integer in the range +/-2^8 (-128 to +127)
short - integer in the range +/-2^16
void - nothing (used for functions)
Memorize those, they are VERY IMPORTANT.
Next, here are some rules.
EVERY STATEMENT MUST END IN A SEMICOLON
MAIN MUST RETURN A VALUE (int)
EVERY FUNCTION MUST BE CONTAINED WITHIN CURLY BRACES ({ })
So, let's break down that function:
(also, anything following a // is ignored by the compiler, known as a comment)
Easy enough. I may not have gone in depth enough on functions. they are defined with the following format:
The return-statement must be present unless the return-type is void.
sample return statements:
and now, we can introduce you to your first function (ones already written).
You can basically ignore that for now, it will make sense in part 3 when we start making more functions in our programs.
But in order to use printf, we must tell our compiler that we want to. This is done with include statements. I will go over this a little later, but essentially, that last code block is in a file called stdio.h somewhere on your system, and including it just pastes that entire file in our code when we compile it.
What is the difference?
Well, <file> is used for files we didn't write (ones on our system somewhere) and "file" is for files in the same directory
and compile it! (^X that, say yes to saving)
this will create a file named a.out
You should see hello world on the command line!
Stay tuned for part 3 where we start writing some cool functions.
So, you now have the tools, and are ready to begin your first program. Lets get started. Open up a blank file named "hello.c"
Code:
nano hello.cAlright. A few rules I need to state before I jump into code.
1. EVERY C program MUST have an entry point. We call that "main".
2. EVERY C program MUST be compiled from object or source files (these are the .c files)
So, step 1. Main.
Main is a function. For those of you who went above the high school level in math will already know what a function is. v(p)=dp/dt type of thing.
Basically, a function takes zero or more parameters, does a task, and returns zero or one output. It sounds simple to me, but it may not have been explained correctly. So, let's do a quick example:
f(x, y)=x*y
Simple. Here is the equivalent integer function in C:
Code:
int f(int x, int y)
{
return x * y;
}There we go. There are a few things in that I need to explain before we can go any farther. Firstly, data types.
Data types are what we will use to store our data, manipulate it, or really do anything with it. There are a few basic types:
int - integer in the range +/-2^32
float - floating point (decimal) number (32-bit)
double - double precision float (64-bit)
char - integer in the range +/-2^8 (-128 to +127)
short - integer in the range +/-2^16
void - nothing (used for functions)
Memorize those, they are VERY IMPORTANT.
Next, here are some rules.
EVERY STATEMENT MUST END IN A SEMICOLON
MAIN MUST RETURN A VALUE (int)
EVERY FUNCTION MUST BE CONTAINED WITHIN CURLY BRACES ({ })
So, let's break down that function:
(also, anything following a // is ignored by the compiler, known as a comment)
Code:
int f(int x, int y) //define a function named f that takes x and y and returns another integer
{ //remember, code must be inside braces
return x * y; //return the value x * y. end it in a semicolon, it is a statement
} //tell the compiler our function is doneEasy enough. I may not have gone in depth enough on functions. they are defined with the following format:
Code:
return-type name(parameters)
{
statements;
return-statement;
}The return-statement must be present unless the return-type is void.
sample return statements:
Code:
return 1;
return a;
return 3.14;
return;and now, we can introduce you to your first function (ones already written).
Code:
int printf(const char *fmt, ...);You can basically ignore that for now, it will make sense in part 3 when we start making more functions in our programs.
But in order to use printf, we must tell our compiler that we want to. This is done with include statements. I will go over this a little later, but essentially, that last code block is in a file called stdio.h somewhere on your system, and including it just pastes that entire file in our code when we compile it.
Code:
#include <file>
and
#include "file"What is the difference?
Well, <file> is used for files we didn't write (ones on our system somewhere) and "file" is for files in the same directory
Code:
#include <stdio.h> //so we have printf
int main() //memorize this line
{
printf("Hello, world!\n");
return 0; //this should be in main usually. Will go into more details later. Memorize for now
}and compile it! (^X that, say yes to saving)
Code:
gcc hello.cthis will create a file named a.out
Code:
./a.outYou should see hello world on the command line!
Stay tuned for part 3 where we start writing some cool functions.

























![[+]](https://sinister.ly/images/modern/collapse_collapsed.png)