Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,833 members, 7,810,207 topics. Date: Friday, 26 April 2024 at 11:48 PM

Lets Learn C - Programming (2) - Nairaland

Nairaland Forum / Science/Technology / Programming / Lets Learn C (9190 Views)

Is It Possible To Learn C++ In A Year? / Learn C++ Programming Language In Few Days / Lets Learn Programming From Bignners To Expert Through Tutorials (2) (3) (4)

(1) (2) (3) (4) (5) (Reply) (Go Down)

Re: Lets Learn C by romme2u: 9:12pm On Jul 24, 2016
donjayzi:

O yeah, tell him to provide the link from the ebook he is pasting from.

Even if he is pasting from an online tutorial cheesy, bringing such great a knowledge to us here is commendable.

he is able to introduce C to most people, refresh our memories and most importantly spread the gospel of C.

Moreover he specified the materials and resources needed for the tutorial.

i believe he is on track

1 Like

Re: Lets Learn C by Nobody: 10:22pm On Jul 24, 2016
he will tire and never complete this tutorial. i have seen many TROLLS like this in my lifetime on NL.
Re: Lets Learn C by stack1(m): 12:24am On Jul 25, 2016
interesting comments, anyways lets proceed

most programs require ways of accepting input to work on or process, in the case of C they are library functions to allow reading and manipulation of characters from STDIN (standard input - usually your screen), files on disk, or say from network ports etc, we would be looking at several programs that accept input from the keyboard, does some manipulations on them and outputs the result
Re: Lets Learn C by stack1(m): 12:27am On Jul 25, 2016
Lets start with two similar programs (please for anyone following try to input & run this programs, to be sure you fully understand how they work)

program listing 03

#include <stdio.h>
int main(void){

char c;
c = getchar();

while(c != EOF){

putchar(c);
c = getchar();
}

getchar();

return 0;
}




program listing 04

#include <stdio.h>
int main(void){

char c;

while((c = getchar()) != EOF){

putchar(c);
}
}
Re: Lets Learn C by stack1(m): 12:50am On Jul 25, 2016
If you ran both programs you'll notice they both do the same thing, you type in some input and it prints it back.. now lets look at each program in more detail

Program listing 03 starts with the #include preprocessor directive (which we have earlier covered) and so the line
 #include <stdio.h> simply includes the header file for the standard input/output library that comes with all C compilers, the library contains a plethora of functions related to input and output and so by including its header file we have access to this input/output functions

Next we start the main function again the line
[code] int main( void )


tells us several things, first the int means that this function is meant to return an integer, and void between the parenthesis tells us the function takes no arguments

next we declare a variable called c and using the char

gectchar is a function from the input/output library that reads one character at a time
so the line
 c = getchar(); 
reads the first character entered by the user and stores it into the variable c

next we have

while(c != EOF){

putchar(c);
c = getchar();
}


the while loop as explained before continue to executes the statements in its block till its condition doesnt evaluate anymore
so in this case
  while(c != EOF) 

means while the value (or character) stored in the variable c isnt EOF , the statements in the while loop's body should keep executing

So what exactly is EOF, in C and C++ this means End Of File, and its simply an indicator that we have reached the end of an input source
now if you run the program and enter a line of text the program simply prints back the text and again waits for you to enter a new line, and the process repeats again and again..

EOF is traditionally useful when reading from actual files on disk or maybe when receiving data from a network port, in case of command line program like ours to indicate EOF to the program so it can exit you press Ctrl+Z and enter

The body of the while loop is simple it prints the last character entered using the putchar function and and prompts for another character

The second program does the same this as the first the only difference is that it is more concise

read through and lets discuss this programs before we move on..
Re: Lets Learn C by shirgles(m): 1:20am On Jul 25, 2016
Can u teach me in person
Re: Lets Learn C by stack1(m): 1:36am On Jul 25, 2016
shirgles:
Can u teach me in person

hmm, i guess..
Re: Lets Learn C by bestiyke(m): 4:26am On Jul 25, 2016
Following
Re: Lets Learn C by bestiyke(m): 4:54am On Jul 25, 2016
donjayzi:
he will tire and never complete this tutorial. i have seen many TROLLS like this in my lifetime on NL.
Yes, that is true. Reason, the pros and the know them alls here will always try to frustrate the person so that he/she will not complet the tutoria. Which is very bad. But by the way @stack1 is going, it seems he has chosen to ignore every distraction and determined to break record and also set a new record by completing his tutorial. I wish he will. More power, will and zeal to you. Let try and encourage him and not to discourage him. Make meaningful contributions and not distructive ones. Thank you all. Fire on bros!!!

2 Likes

Re: Lets Learn C by bestiyke(m): 5:26am On Jul 25, 2016
stack1:
Lets start with two similar programs (please for anyone following try to input & run this programs, to be sure you fully understand how they work)

program listing 03

#include <stdio.h>
int main(void){

char c;
c = getchar();

while(c != EOF){

putchar(c);
c = getchar();
}

getchar();

return 0;
}




program listing 04

#include <stdio.h>
int main(void){

char c;

while((c = getchar()) != EOF){

putchar(c);
}
}
You have been using while loop. Though you have try to explain what it is and what it does, but I want you to know that it will still be blunt for a complete newbee who doesn't have any knowladge of loop statement and make it difficult for the person to flow with the tutorial at this stage. I feel you would talk more on operators as you have explain data types and introduce control and loop statements later. Well I don't know how you have organize your lessons. But I believe you future lessons will make clear any confussion now. And people are free to ask questions where they are confuse. Carry on. Atleast If you complete this tutorial, I boost of learning C language from Nairaland Programing session and from stack1 the tutor. Move with good work.

1 Like

Re: Lets Learn C by Nobody: 7:32am On Jul 25, 2016
When i say the tutorial will die, i am speaking based on facts and not because of BEEF. Anyway, this is a challenge for the OP to try yo prove me wrong - if he can (which i doubt).
All in all, it is a great tutorial, so mr OP fire on.
Re: Lets Learn C by jidez007: 9:03am On Jul 25, 2016
@op the course makes sense. Permit me to repost them on my forum, because my forum can filter unnecessary comments and show only the important posts, instead of having the user to keep sieving through all the comments.
Re: Lets Learn C by D34lw4p(m): 12:49pm On Jul 25, 2016
Tho am not here to follow up with this post I must really say you're doing a great work by trying to impact knowledge on other people. Keep it up boss, God Bless you abundantly.
Re: Lets Learn C by Nobody: 1:14pm On Jul 25, 2016
jidez007:
@op the course makes sense. Permit me to repost them on my forum, because my forum can filter unnecessary comments and show only the important posts, instead of having the user to keep sieving through all the comments.
You mean your forum can filter out my comments? I dont think so, i will bypass the filter somehow.
Re: Lets Learn C by jidez007: 2:26pm On Jul 25, 2016
donjayzi:

You mean your forum can filter out my comments? I dont think so, i will bypass the filter somehow.
Not exactly, the author will be the one to mark the relevant posts. Then an option will available to users to show only that marked posts by the author.

1 Like

Re: Lets Learn C by Nobody: 2:35pm On Jul 25, 2016
Okay, that is very good jide. @op, please continue, I am enjoying your tutorial. By the way, CodeBlocks is my favorite IDE for C/C++.

1 Like

Re: Lets Learn C by stack1(m): 3:02pm On Jul 25, 2016
jidez007:
@op the course makes sense. Permit me to repost them on my forum, because my forum can filter unnecessary comments and show only the important posts, instead of having the user to keep sieving through all the comments.

permitted bro

1 Like

Re: Lets Learn C by stack1(m): 3:05pm On Jul 25, 2016
hey guyz, thanks for the feedback, would be posting several more examples later today, i'm usually a bit busy during the day, but would definitely posst by evening/night

1 Like

Re: Lets Learn C by stack1(m): 3:20pm On Jul 25, 2016
bestiyke:
You have been using while loop. Though you have try to explain what it is and what it does, but I want you to know that it will still be blunt for a complete newbee who doesn't have any knowladge of loop statement and make it difficult for the person to flow with the tutorial at this stage. I feel you would talk more on operators as you have explain data types and introduce control and loop statements later. Well I don't know how you have organize your lessons. But I believe you future lessons will make clear any confussion now. And people are free to ask questions where they are confuse. Carry on. Atleast If you complete this tutorial, I boost of learning C language from Nairaland Programing session and from stack1 the tutor. Move with good work.

you are right and i'm doing that on purpose the while loop is a very powerful construct and very compact and smart programs can be written with proper use of while, its use (to newbies) would become more clearer as we proceed

1 Like

Re: Lets Learn C by bestiyke(m): 4:18pm On Jul 25, 2016
stack1:


you are right and i'm doing that on purpose the while loop is a very powerful construct and very compact and smart programs can be written with proper use of while, its use (to newbies) would become more clearer as we proceed
Ok dear, thank you. Continue your good job for I am following.
Re: Lets Learn C by stack1(m): 5:22pm On Jul 25, 2016
bestiyke:
Ok dear, thank you. Continue your good job for I am following.

aiit, but em, sorry o, u using *dear in that sentence, i be male o
Re: Lets Learn C by stack1(m): 5:23pm On Jul 25, 2016
bestiyke:
Ok dear, thank you. Continue your good job for I am following.

aiit, but em, sorry o, u using *dear in that sentence, i be male o tongue
Re: Lets Learn C by Nobody: 5:51pm On Jul 25, 2016
stack1:


aiit, but em, sorry o, u using *dear in that sentence, i be male o tongue
I am back again from the ban - thanks Seun for setting this honourable troll free.

Hmn, e be like say some things don dey happen for this thread in my absence o, but make i no talk sha.
Re: Lets Learn C by stack1(m): 1:27am On Jul 26, 2016
/** a character counting program **/

#include <stdio.h>
int main()
{

int n = 0, c;


//make sure the return value of getchar isnt in the range of ASCII whitespace values \n\f\v\r
while ( ((c = getchar()) < 8 || c > 13) && c != EOF)
{

n++;

}

printf ("%d", n);
getchar();
}
Re: Lets Learn C by stack1(m): 1:44am On Jul 26, 2016
The last example seems a bit more involved

first we create two variable n which would be our counter, and c to store character input

now the main stuff happens in the while loop


while ( ((c = getchar()) < 8 || c > 13) && c != EOF)


lets break this down

first
 c = getchar() 
reads a character from the key board input and stores it in c

remember characters in C are stored internally using their numerical (ASCII) values

so
 ((c = getchar()) < 8 || c > 13)  

does this: take the ASCII value stored in variable c and check if its less than 8 or greater than 13, the operator || means OR in C
So why less that 8 or greater than 13, characters between this ASCII range (look it up in the ASCII table link) are all white space characters,
that is they do not get a visible printed representation like normal letters,

so the code
 while ( ((c = getchar()) < 8 || c > 13) && c != EOF)  
, checks to make sure that the character entered is not within the range of white-space characters ( because the main aim of the program is to count normal (printable) characters

so if the character isn't white space the next check is


&& c != EOF


the symbol && means AND , so if the character isn't the END_OF_FILE marker (aka we haven't reached the end of the input)

So together the while loop checks that the entered character isn't a white space and it isn't EOF, if this two conditions are met it means we have a normal character
so in the body of the while loop we increment the value of the counter variable using the statement

 n++ 


which is called a post-increment

then after the loop ends the final character count is printed
Re: Lets Learn C by stack1(m): 1:45am On Jul 26, 2016
/** program to count how many lines are there in the input **/

#include <stdio.h>
int main()
{
int c, nl;
while ((c = getchar()) != EOF){

if(c == '\n') ++nl; //increase count on each new line

}
printf ("%d", nl);
getchar();
}
Re: Lets Learn C by stack1(m): 1:48am On Jul 26, 2016
ok, before i go into the details of the line counting program, can anyone provide us summary of how it works?
Re: Lets Learn C by romme2u: 2:11am On Jul 26, 2016
stack1:
ok, before i go into the details of the line counting program, can anyone provide us summary of how it works?

u are back, that is great cool

like bestiyke said, u are skipping the basics and posting obfuscated codes before explanation. that ain't cool sad

don't let what dthml18 said come true . i believe treating operators, primitive data types and loops with common library functions will make ur programs to be self-explanatory.


Don't post obfuscated codes, it scares beginners and makes it harder for anybody including urself to read the codes at a later time even with verbose comment.

let ur code flow line by line in simpler statements and expression with good indentation such that ur code is readable even without comments.

1 Like

Re: Lets Learn C by stack1(m): 2:15am On Jul 26, 2016
romme2u:


u are back, that is great cool

like bestiyke said, u are skipping the basics and posting obfuscated codes before explanation. that ain't cool sad

don't let what dthml18 said come true . i believe treating operators, primitive data types and loops with common library functions will make ur programs to be self-explanatory.


Don't post obfuscated codes, it scares beginners and makes it harder for anybody including urself to read the codes at a later time even with verbose comment.

let ur code flow line by line in simpler statements and expression with good indentation such that ur code is readable even without comments.

hmm, ok

well in this case, the next post's would go over C's operators, control structures and loops, i had already discussed integer and char data type, that should do for now till we go into advanced stuff like structures and arrays/pointers
Re: Lets Learn C by stack1(m): 2:28am On Jul 26, 2016
My apologies if i have seemed to loose anyone in between so first lets look at loops and a few operators in detail

The for loop

To loop simply means to execute repeatedly. C's for loop are one of its control structures.

The general form of a for loop is

The syntax of a for loop is:

for (initializationStatement; testExpression; updateStatement)
{
// codes
}



the initializationStatement is executed only once
the testExpression is a condition that needs to be met each time before the body of the loop is executed, when condition of this testExpression isnt met the loop is terminated

the updateStatement is updated until the test expression is false
Re: Lets Learn C by stack1(m): 2:28am On Jul 26, 2016
lets see a very basic example of using the for loops, this example counts and prints from 1-10
Re: Lets Learn C by stack1(m): 2:43am On Jul 26, 2016


//count from 1-10

#include <stdio.h>
int main()
{
int count;

for(count = 0; count < 10; count++){

printf("%d \n", count);

}
getchar();
}




when executed the program prints the numbers 1-10 on a new line, so exactly how does it do this...

in the main function we declared a integer varable named count and wehave a single for loop written as

for(count = 0; count < 10; count++){


From our general definition of the for loop , count = 0 here acts as the initializationStatement, as it initializes the count variable to 0, note that this happens only once no matter how many times the loop is executed

The statement

count < 10

is the testExpression, so each time before the body of the loop is executed we test to see if the variable count is less than ten, and every time this test passes the body of the loop is executed
i.e the current value of the count variable is printed followed by a new line,

The statement count++ is a post-increment statement that increases the value of the count variable by one each time the loop is run

so when the loop first runs count is initialized to 0, the test statement count < 10 passes and the loop body is run
then the [count] variable is incremented to 1 by the updateStatement count++

on the next run of the loop, the testStatement count < 10 would now be 1 < 10 since count is now 1, then since its true the body of the loop is executed and count is once again incremented this goes on till the test count < 10 fails and the loop is terminated

Note if the updateStatement had been ++count instead of count++ , the count variable would be incremented before the body of the loop is executed, we call this pre-increment

(1) (2) (3) (4) (5) (Reply)

An Introduction To Programming Using C / How Do You Get A CEH / Pentesting Job In Nigeria? / Android Quiz Application With Json Parser, Php And Mysql Database

(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. 80
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.