Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,282 members, 7,807,946 topics. Date: Wednesday, 24 April 2024 at 11:27 PM

An Introduction To Programming Using C - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / An Introduction To Programming Using C (9213 Views)

An Introduction To PHP - Beginners Base / Plz Can Anyone Solve This Using C++? / I Need Help On Oop Project Using C# Console-base Application (2) (3) (4)

(1) (2) (Reply) (Go Down)

An Introduction To Programming Using C by qleyo(f): 12:25pm On Mar 01, 2006
TUTORIAL 1 - Alot of people said to start a new thread and teach C, personally I'm in love with the language and once you understand it, you will find most languages are almost a direct copy with different strong points.

So here goes,

For the first part of this I am going to make this tutorial like a discussion, I want you to understand the concepts of programming before we dive into the real deal. First things first get a compiler. What is a compiler you ask? Its simple, say you wanted to talk to a person in french, you get a translator. Ok so lets say here our compiler is our translator, we talk to the computer using a language (for example C, or Java ) the compiler takes this speech (our code) and translates it into machine code (computers only understand 1s and 0s aka binary numbers, imagine if you had to talk to a machine in 1s and 0s, you might just run mad).

I use these two compilers http://www.salfordsoftware.co.uk/software/downloads/compilers.html click on the link FTN95 Personal Edition a screen will pop and click download. (This compiler is a really good IDE, it compiles C, C++, Java, Foltran etc) or Dev C++ go to http://prdownloads.sourceforge.net/dev-cpp/devcpp-4.9.9.2_setup.exe and click download (any of the download links). I tend to use the first compiler more often so I would download that if I were you. Now you have a compiler, we'll get to using it shortly.

Screen shot of Salford Plato below,

Re: An Introduction To Programming Using C by qleyo(f): 1:09pm On Mar 01, 2006
TUTORIAL 2 - Data types and declarations.

I will not go into too much details, but for every character,letter or number there is an equivalent binary combination for example 1 in 8 bit binary is 0000 0001 and 1111 1111 is . Now notice how I said 8 bit binary. A bit is 0 or 1, 8 bits make up a byte. So back to our datatypes, by telling the compiler what datatype we want it allocates a number of bytes to store the variable (a variable is anything that holds a value). We define/declare a variable by telling the compiler its datatype and the variable name.

Some declartions

char C (a char is a 1 byte datatype)
int SumNumber (an int is short for integer, and takes up 4 bytes)
float a_float (a float is a real number and takes up 4 bytes)

"Assignment" values:
After declaring a variable, we can assign values to it. Here are some examples of some declarations (these are called statements).

int a = 5;
we can also say
int a;
a = 5;

Did you notice the semi-colon at the end of the line, in C we "terminate" every "statement" with a semi-colon. Its like a full stop in english.
Some more statements,

float f = 0.5;

float b;
b = 2.3457;

char c = 'a';
char e;
e = '2';

Notice something else the ''? Now in C, 2 is different from '2', the former is a value, the latter a character. And characters are always assigned to chars (chars can also hold numbers by the way). A is different from 'A', B is different from 'B' etc. A collection of characters is called a string.

"My name is Qleyo" is a string
"                          " is a string, because ' ' (space) is a character.

Phew Ok so we've cover quite alot, you understand datatypes, variables, declarations, characters, strings, what next, lets write some code!

Go to start, look for Salford Software > Salford Software FTN95 > Plato3 IDE. Open Plato3 IDE, I'd advise you create a shortcut on your desktop as we will continue to use this, and from now on we'll call it Plato. So, open plato. Go to File and Click New, alteranatively click CTRL + N. A window should open with a list of file types. Click on C++ file.

Go to File, Click Save or alternatively CTRL + S, enter main1.c in File name and click save.

Now copy and paste the following code into main1.c


#include <stdio.h>

int main ()
{
     //This is a comment, the compiler does not see anything here and should appear green in your window
     //for every new comment line, we add to forward slashes, you can write your own comments too,

     //we start by declaring some variables
     
     char c = 'a';
     //when naming variables, a variable name must containt letters, numbers or a _ , and must start with a letter or _ that is char 2a is invalid
     //char a2 is valid, char a_2 is also valid
     int a = 2;
     int a_really_long_variable_name;
     float another_really_long_variable_name;

     puts ("Hello World!\n"wink;
}


In your menu bar go to Build and click Start Run alternatively hit CTRL + F5
Give yourself a pat on the back, you've just written your first program, you should see a command window open and print "Hello World!"
Re: An Introduction To Programming Using C by qleyo(f): 1:10pm On Mar 01, 2006
TUTORIAL 3 - A closer look at our program

#include <stdio.h>
These are called preprocessor directives, meaning before the compiler even starts compiling your code, it processes these directives.
We will look at other preprocessor directives later on, and they always begin with a #.
The #include directive tells the compiler to "include" the file specified in between the < > so if we had a piece of code in a file called mycode.h and wanted to insert it into our existing
programming, we "include" the file like so #include "myfile.h", so why have I used "" instead of < >.
<mycode.h> tells the compiler to look where system include files are held.
"mycode.h" looks for a file in the current directory, that is the same directory as our code.

Stdio.h is a header file (header files end with .h) they contain functions, what is a function.
So lets say you wanted to fry yams. You buy the yam, peel it, wash it, add some salt, heat up the oil, and fry it. So this is the method for
preparing yam.

If we were somehow defining the method for frying yams, we would like it if we could write this method once (lets call it fryyam() ), and when ever it is required
perhaps after making some stew, we "call" the method[b] fryyam()[/b]. These methods are called functions. There are quite a few of them
the stdio.h header file contains lots and lots of predefined functions for "ST[/b]ANDAR[b]D I[/b]NPUT AND [b]O[/b]UTPUT.
We'll be using some more functions in stdio.h later on.

int main ()
 
this is the main method, when you compile a program and run it, the first thing your program does is "jump" to the main, from the main
we can call other functions, o and we can also call functions inside functions. so perhaps if we wanted to make the washing of the yam a function
we could call [b]washyam() [/b]inside [b]fryyam()


{

The curly brackets, we use it to "nest a block of code", lets write some pseudo code for fryyam()

/*function to peel yam, this is a block comment any thing inside this will not be compiled*/
//this is also a comment anything after the two slashes will not be compiled
/*
we can also write block comments like this
*/


void fryyam()
{
  peal yam;
  //call to predefined function washyam()
  washyam();
  add some salt;
  heat oil;
  put yam in oil;
}

So the two curly brackets { and } tell denoted the block of code that define fryyam.

 //This is a comment, the compiler does not see anything here and should appear green in your window

     
     char c = 'a';

we define a variable c, of type char and assign a character 'a' to it

     int a = 2;
     int a_really_long_variable_name;
     float another_really_long_variable_name;


Some more declarations and assignments above

     puts ("Hello World!\n"wink;

aha! something new, puts() is a function in stdio.h, it simply prints the string inside the " " to the screen.
\n tells the compiler to print a new line, \n\n would print two lines (as though you hit return twice).

}

Denotes the end of main.

So now we know what functions are, we have been introduced to puts() a function in stdio.h.

Exercise: Write a program that puts a string to the screen. The string should look like this

Hello!
My Name is yourname
What is your name.



Notice how everything is on a "new line", hint hint.

Re: An Introduction To Programming Using C by sbucareer(f): 7:33pm On Mar 01, 2006

Good work Qleyo, I will be watching this thread as it grows. Qleyo when you have time probably at the end of the tutorial can you touch on RPC (Remote Procedural Call) with COBRA and IDL (interface Definition Language) please. Thanks in advance.

Re: An Introduction To Programming Using C by qleyo(f): 7:36pm On Mar 01, 2006
Its Qleyo with a Q, pronounced Cleo :-D. Thnx.
Re: An Introduction To Programming Using C by c0dec(m): 6:42am On Mar 02, 2006
uhm . . . you've got to say something about the gcc compiler since most c programmers are linux/unix based. =)

keep it up though. a female C programmer? i dey trip. grin
Re: An Introduction To Programming Using C by Viper(m): 9:02am On Mar 02, 2006


keep it up though. a female C programmer? i dey trip. grin

no be only u oo Codec, i just dey watch dey learn and dey love tongue

cleo more palmoil to ur elbow cheesy
Re: An Introduction To Programming Using C by skima(m): 5:29pm On Mar 02, 2006
U r really trying cleo . grin

wat if u teach C++ instead of C. as its modern.

save file as?   .c or .cpp before compilation as java has .java as extension.
Re: An Introduction To Programming Using C by qleyo(f): 7:58pm On Mar 02, 2006
what if u teach C++ instead of C. as its modern.

Learn to walk before running,
Save file as .c (although if you save it as .cpp it will work just fine)
Re: An Introduction To Programming Using C by skima(m): 8:56pm On Mar 02, 2006
though am from C background, i started learning to program using javascript->php (now java), etc. I think since the new concept of programing is OOP which C++ has, i just think it will be wise to go that way straight.

Well i must commend ur effort by trying to enrich others with ur knowledge, Girls like u are scarce
Re: An Introduction To Programming Using C by qleyo(f): 9:33pm On Mar 02, 2006
TUTORIAL 4 - Data from the outside world

So we now know how to "print" messages to the screen using puts(). Another function, which does the exact same thing as puts (but with alot more flexibility which we will come to later) is called printf().

puts("My name is Qleyo\n"wink;
printf("My name is Qleyo\n"wink;


Both print the same thing to the screen.

So now wouldn't it be nice if we could read data back? Well we can, otherwise I wouldn't have brought it up.
So introducing another function getch() in the clib.h library (a library is a header file like stdio.h)

getch() reads a character entry from your keyboard and returns this character.

So lets put getch() to the test, create a new file and save it as getch.c
hit CTRL + F5

#include <stdio.h>
#include <clib.h>

int main ()
{
puts ("Welcome to the getch() test program!\n"wink;
puts ("Please enter a character\n"wink;

char data;
data = getch();

printf("you entered %c",data);
return 0;
}


Code explained
Nothing here is new, we print to the screen, declare a variable called data, assign the value of data to the value returned from getch(). Aha functions returning values? yes of course, functions can return values, lets go back to our frying yam scenario, after frying the yams, we can either reports "yams fried" or report nothing at all. When a program returns nothing, we call it a void function, when a function returns an integer, we call it an int, when it returns a char its a char etc etc.

So say we wanted a sumation function that added 2+2 and returned the value, we would define it as

int sum ()
{
int result = 2 + 2;
return result;
}


Moving on, we come to the print statement

printf("you entered %c",data);

Normally we would print a string, so now we want to print variables. What happens if I say

printf("you entered data"wink;

The program would print "you entered data", so how do we tell the program we want it to print the variable data, not the string. We replace the variable name with a "format tag", for chars we replace them with %c, for integers %d, and floats %f.

So here we are telling the compiler print "you entered data variable"

If we had 2 chars, say data1 and data2 and we wanted to print "data1 = %c and data2 = %c"
the[b] arguments[/b] that come after would be in the format

printf("you entered data1 = %c and data2 = %c",data1,data2);

if i swapped them around and entered

printf("you entered data1 = %c and data2 = %c",data2,data1);

The first %c would be data2 and the second %c in the string would be data1.

Say we had integers and floats, once again we put them in the order. So lets say we had a float realnumber and an int number
we print them like so

printf("realnumber is %f and number is %d",realnumber,number);

be careful to ensure that the format tags correspond to the variable type arguments, otherwise the compiler will return an error.
Re: An Introduction To Programming Using C by gbengaijot(m): 9:37pm On Mar 02, 2006
Qleyo, i have downloadd the devc++ on my PC. Can u give instructions on how to compile the code u gave in the first tutorial?.


?
Re: An Introduction To Programming Using C by qleyo(f): 9:44pm On Mar 02, 2006
Qleyo, i have downloadd the devc++ on my PC

Could you please download salford plato instead? undecided Dev c is a tad bit tricky, and I do not want to have to give instructions for two compilers, in addition I am using plato, so I'd rather if we all stuck with plato. Later on, we will look at Dev-C++ as it runs on a GCC platform (long store). Let me know if you have any problems smiley
Re: An Introduction To Programming Using C by qleyo(f): 9:15am On Mar 16, 2006
Conditional Statements

So we have covered printing to the screen, and receiving input data from the keyboard. The next fundamental part of programming are conditions.

if, else if and else.

So lets say we wanted to ask the user some options. Perhaps a crude menu or some options.
We modify our program from tutorial 4

#include <stdio.h>
#include <clib.h>

int main ()
{
     puts ("Welcome to modified getch() program!\n"wink;
     puts ("Please enter a character\n"wink;
     puts ("Enter 1 to print Hello World \n"wink;
     puts ("Enter 2 to print Good bye\n"wink;

     char data;
     data = getch();

     printf("you entered %c\n",data);

     //now we run some checks

     if (data == '1')
     {
           puts("Hello World\n"wink;
     }


     else if (data == '2')
     {
           puts("Good bye\n"wink;
     }

     else
     {
           puts("Invalid option entered please try again\n"wink;
     }

     return 0;
}


Code Explained

So now our program remains the same up until the if statement
The if statement will execute the expressions in the curly brackets ONLY if the condition in the brackets next to the if is TRUE
Also note when checking data, we use "==" instead of "="; the former checks i.e. is a == b ? or is b == a the latter sets i.e. a = b (a will now be
same as b).

Also notice the single quotation marks. This is because a character is entered by the user.

If the if statement fails, the program will check the else if condition, and once again if this is TRUE, the expressions in the curly brackets for else if will be executed.
We always ONE if statement, however multiple else if statements can follow an if statement.

Lastly, the else statement is the default case. This means if neither the if statement nor any of the else if statements execute the program WILL execute the block of
code in the else statement. In our case we tell the user neither option 1 nor 2 was selected, and therefore something invalid was entered.

Arrays

An array is a block of memory.

We define arrays like variables but also specify the size of the array.

Example

#include <stdio.h>

int main ()
{
     int x[5];
     //we now have 5 integer elements, and can access each element by specifying the address. In C we start from 0 - 4

     //assign some values
     x[0] = 1;
     x[1] = 5;

     //we can also perform operations on arrays as variables, simply in a block
     x[2] = x[0] + x[1];

     printf("x[2] = %d\n",x[2]); //this should print x[2] = 6

     return 0;
}



We can have arrays of chars, floats anything.
Note: an array of chars becomes a string.

Have a play with for loops and arrays. Let me know if you get stuck on anything smiley
Re: An Introduction To Programming Using C by qleyo(f): 12:03pm On Mar 21, 2006
Qleyo, i have downloadd the devc++ on my PC. Can u give instructions on how to compile the code u gave in the first tutorial?.

gbengaijot, how did you get along with the compilers?
Re: An Introduction To Programming Using C by gbengaijot(m): 2:37pm On Mar 21, 2006
.
Re: An Introduction To Programming Using C by Viper(m): 1:04pm On Jul 18, 2006
where is qleyo cry
Re: An Introduction To Programming Using C by qleyo(f): 12:02pm On Jul 19, 2006
here, was not getting any responses/feedback and i'd hate to spend time on something that would be of no use to anyone,

I've also been fighting time the last couple of days. Hows everyone doing?
Re: An Introduction To Programming Using C by Viper(m): 12:50pm On Jul 19, 2006
awwww, she is back. I knew you would hear my cries grin
We weren't doing fine!! you abandoned us angry


jus kidding, we are cool. So are you continuing with the tutorial? smiley
Re: An Introduction To Programming Using C by rufflychux(m): 2:24pm On Aug 04, 2006
Hi, I am new to this forum and I must say I really admire your efforts in giving out hints about writing programs in C. I also tried to evaluate some of the assignments you gave and got the exact answers you got. I am new to programming so I am studying Scheme and C side by side. Here is the code to your post.


#include <stdio.h>

main ()
{
printf ("hello !\n"wink;
printf ("my name is rufflychux\n"wink;
printf ("what is your name?\n"wink;
}
Re: An Introduction To Programming Using C by qleyo(f): 8:26pm On Aug 08, 2006
Oh yay, thats good to know, How did you get along with conditional statements/loops and arrays
Re: An Introduction To Programming Using C by adewaleafolabi(m): 12:22am On Sep 15, 2006
thanks qleyo but i was able to go through the tutorial and finish it but i guess theres more have you moved the forum to sumwhere else
Re: An Introduction To Programming Using C by qleyo(f): 7:45am On Sep 15, 2006
No I haven't moved it anywhere. I'm waiting for anyone to get to the last subject - if statements and arrays. How did you get along, and what did you do with if statements, for loops and arrays.
Re: An Introduction To Programming Using C by adewaleafolabi(m): 10:50pm On Sep 15, 2006
the if statement looks almost exactly like Java script so it gave me no problem.
but i don't know how to do loops is it the same with javascript
thank you
Re: An Introduction To Programming Using C by qleyo(f): 12:36am On Sep 17, 2006
Javascript (JS), Java, PHP, syntax are almost exactly the same as C (they are all based on C so it follows). I thought I covered loops :/ yes they are the same in JS - for and while loops
Re: An Introduction To Programming Using C by adewaleafolabi(m): 12:28pm On Sep 17, 2006
thanks qleyo whats next after the loops.
Re: An Introduction To Programming Using C by adewaleafolabi(m): 10:11pm On Sep 20, 2006
Qleyo plz come back and continue this tutorial cry
Re: An Introduction To Programming Using C by Tonyblu(m): 9:04pm On Sep 22, 2006
I wuz trying this and got stuck:

#include <stdio.h>
#include <clib.h>

int main ()
{
puts ("Welcome to the getch() test program!\n"wink;
puts ("Please enter a character\n"wink;

char data1, data2,data3,data4, realnumer, number;
data1 = getch();
data2 = getch();
data3 = getch();
data4 = getch();
realnumber = getch();
number = getch();

printf("realnumber is %f and number is %d",realnumber,number);

printf("you entered\n%c\n%c\n%c\n%c\n",data1,data2,data3,data4);


return 0;
}

Can someone help me out lipsrsealed

Re: An Introduction To Programming Using C by Tonyblu(m): 9:09pm On Sep 22, 2006
@qleyo

#include <stdio.h>
#include <clib.h>

int main()
{
puts ("welcome to modified getch() program!\n"wink;
puts ("Please enter a character\n"wink;
puts ("Enter 1 to print HELLO WORLD!\n"wink;
puts ("Enter 2 to print GOOD BYE!\n\n"wink;

char data;
data = getch ();

printf("you entered %c\n\n",data);

//now we run some checks

if (data =='1')
{
puts ("HELLO WORLD!\n"wink;
}


else if (data=='2')
{
puts ("GOOD BYE!\n"wink;
}

else if (data=='3')
{
puts ("NICE TRYING THERE!\n"wink;
}

else
{
puts ("Invalid option entered please try again\n"wink;
}

return 0;
}


Well, when's Tutorial 5 comin out. it's all so juicy . . .
Nice wkend y'all . . .
Re: An Introduction To Programming Using C by qleyo(f): 9:26pm On Sep 22, 2006
Nice work Tony,

If you get stuck in future, with an error or bug try to explain the problem as comprehensively and concise as you can. This is enough motivation to continue now I seem to have a lot of students hehe cheesy

I shall post the next one over the weekend. Keep up the good work
Re: An Introduction To Programming Using C by c0dec(m): 11:26am On Sep 24, 2006
Tonyblu:

@qleyo

#include <stdio.h>
#include <clib.h>

int main()
{
puts ("welcome to modified getch() program!\n"wink;
puts ("Please enter a character\n"wink;
puts ("Enter 1 to print HELLO WORLD!\n"wink;
puts ("Enter 2 to print GOOD BYE!\n\n"wink;

char data;
data = getch ();

printf("you entered %c\n\n",data);

//now we run some checks

if (data =='1')
{
puts ("HELLO WORLD!\n"wink;
}


else if (data=='2')
{
puts ("GOOD BYE!\n"wink;
}

else if (data=='3')
{
puts ("NICE TRYING THERE!\n"wink;
}

else
{
puts ("Invalid option entered please try again\n"wink;
}

return 0;
}


Well, when's Tutorial 5 comin out. it's all so juicy . . .
Nice wkend y'all . . .


Though useful, note that "getch()" isn't ANSI standard. Even as a newbie, you should keep this in mind and try to write portable code as often as you can. Consider using "getc()" for reading in characters. But if the "getch()" functionality is what you really want, then it's ok. Read this too --> http://c-faq.com/osdep/cbreak.html

I also noticed you declared "char data" after calling the "puts()" function. This means your code will only compile with a C++ compiler and not a C compiler. In C, all function variables are declared first before functions, expressions, conditions etc are evaluated.

It'll be a good idea also for your program to wrap around to the beginning if a user enters the wrong option. otherwise the statement puts "("Invalid option entered please try again\n"wink" is pretty pointless.

keep it up though.
Re: An Introduction To Programming Using C by otonye(m): 12:59pm On Sep 24, 2006
First,i will like to commend the entire nairaland (both management and members for making these forum a reality.
I thought of how and when would i knew how to write programmes.With references to your tutorial i saw programming as simple to understand.Please you've done well. Thanks otonye!

(1) (2) (Reply)

How Do You Get A CEH / Pentesting Job In Nigeria? / Android Quiz Application With Json Parser, Php And Mysql Database / Take Your Web Development Skill/Career To The Next Level By Creating A Web App

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 76
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.