Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,151,310 members, 7,811,919 topics. Date: Sunday, 28 April 2024 at 11:39 PM

Lets Learn C - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Lets Learn C (9198 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)

Lets Learn C by stack1(m): 4:47pm On Jul 23, 2016
C and C++ are my personal favorite programming language and at least i consider myself intermediate in both, and so lets see if we could start C lessons here on NL. I need interested folks before i start posting lessons.. anyone

2 Likes

Re: Lets Learn C by stack1(m): 4:57pm On Jul 23, 2016
I am going to be taking examples from several books in this tutorial series including

C Programming A Modern Approach 2nd Edition by KN King
K&R 2nd edition (Brian W. Kernighan & Dennis M. Ritchie)
Beginning C fifth edition -Ivor Horton
C Primer Plus 6th edition - Stephen Prata

so we have hundreds of examples to go through together..

1 Like

Re: Lets Learn C by stack1(m): 5:00pm On Jul 23, 2016
I do not like writing about programming history and philosophy that much, because they simply are not so informative to beginners, you usually learn the history, philosophies and rationale behind programming languages as you mature in them.

so i'm just going to provide a description from wikipedia

C is a general-purpose, imperative computer programming language, supporting structured programming, lexical variable scope and recursion, while a static type system prevents many unintended operations. By design, C provides constructs that map efficiently to typical machine instructions, and therefore it has found lasting use in applications that had formerly been coded in assembly language, including operating systems, as well as various application software for computers ranging from supercomputers to embedded systems.

C was originally developed by Dennis Ritchie between 1969 and 1973 at Bell Labs,[5] and used to re-implement the Unix operating system.[6] It has since become one of the most widely used programming languages of all time,[7][8] with C compilers from various vendors available for the majority of existing computer architectures and operating systems. C has been standardized by the American National Standards Institute (ANSI) since 1989 (see ANSI C) and subsequently by the International Organization for Standardization (ISO).
Re: Lets Learn C by Nobody: 5:08pm On Jul 23, 2016
Please tell us difference between C, C++ and not C#?
Re: Lets Learn C by stack1(m): 5:12pm On Jul 23, 2016
I recommend downloading codeblocks, its one of the best IDE's (integrated development enviroments for C/C++ and many other languages)

Get the latest binary of codeblocks for windows here https://sourceforge.net/projects/codeblocks/files/Binaries/16.01/Windows/codeblocks-16.01-setup.exe/download

We are going to be using GCC/Mingw compilers, specifically GCC version 6 which fully supports C11 and C++11/14 and partially C++17 standards

if you are on 32bit windows get this file here
https://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win32/Personal%20Builds/dongsheng-daily/6.x/gcc-6-win32_6.1.1-20160715.7z/download

and for 64bit systems get this
https://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win64/Personal%20Builds/dongsheng-daily/6.x/gcc-6-win64_6.1.1-20160715.7z/download

after downloading unpack the compiler archive to your C directory
Re: Lets Learn C by stack1(m): 5:27pm On Jul 23, 2016
dhtml18:
Please tell us difference between C, C++ and not C#?
Bro for now, please lets just focus on the tutorials, differences would come later
Re: Lets Learn C by stack1(m): 5:38pm On Jul 23, 2016
I am going to assume at this point you have downloaded codeblocks and the GCC/mingw compliler, and aslo you have extracted the compiler archive to your C: directory

2 Likes

Re: Lets Learn C by stack1(m): 5:43pm On Jul 23, 2016
install and launch codeblocks and go to settings menu and select compiler

select GCC compiler from the list and go to Toolchain executables tab,
modify the compilers installation directory to point to the directory where you unpacked the mingw compiler you downloaded, and modify other tools path as needed, see the attached picture to get an idea of what i mean

Re: Lets Learn C by Nobody: 5:45pm On Jul 23, 2016
stack1:

Bro for now, please lets just focus on the tutorials, differences would come later
That was out-rightly rude, but it is alright, i hereby step out of this thread permanently.
Re: Lets Learn C by stack1(m): 5:59pm On Jul 23, 2016
dhtml18:

That was out-rightly rude, but it is alright, i hereby step out of this thread permanently.

oops, didnt mean to be rude the differences between C and C++ are not subtle, so its usually necessary to have some working knowledge of both to really grasp their diffrences. My apologies pls, i'm kinda multitasking on various stuff right now so i could explain better
Re: Lets Learn C by stack1(m): 6:16pm On Jul 23, 2016
if you have completed all the previous steps then its time start coding

create a new file in codeblocks and make sure it is saved with .c extension

then type in the following (please do not copy and paste always try to type in every code)


#include <stdio.h>
int main()
{
printf("hello, world\n" );
getchar();
}

1 Like

Re: Lets Learn C by stack1(m): 6:30pm On Jul 23, 2016
C is a very compact language and doesn't include many features or operators and most useful things you'll be doing would be provided by using pre-written code that comes packaged with a standard C compiler this kind of pre-written code are called libraries.

In the code above the line


#include <stdio.h>


is called an include directive, stdio (for standard io) is a standard C library that provides utilities (lets call them functions) for various input output operations.

Libraries in C are included in a project using the libraries header file, header files end in .h
so stdio.h is the header file for the Standard Input Library.
Re: Lets Learn C by stack1(m): 6:48pm On Jul 23, 2016
C programs are usually just a collection of functions. A functions (called routines or procedures in some language) can be seen as a series of statements and is given a name, functions are usually meant to perform just one *function.

The standard structure of a function is something like..


return-type function-name( function-arguments) {

function-statements

}


more on functions later...

Every C program must have (ok not strictly must) have a function called main, which acts as the entry and exit point of your program
In our example above the main function just prints the Line Hello World using the printf function. The printf function is from the standard Input/Output Library which we have included with the statement

#include <stdio.h>
Re: Lets Learn C by stack1(m): 6:48pm On Jul 23, 2016
questions/comments before we proceed??
Re: Lets Learn C by Nobody: 6:52pm On Jul 23, 2016
while(none==true)
continue;

2 Likes

Re: Lets Learn C by stack1(m): 7:08pm On Jul 23, 2016
As said before, the printf function is used to print or output text to the standard output (usually the screen but could be other stuffs like network ports).

In printf sequence of characters are enclosed in double quotes e.g. "Hello world" and such quoted characters are known as string constants
The sequence \n is known as a new line character and what it does is to print a new line so the text following it starts on a new line

To illustrate the use of \n edit the printf statements in the previous example to


printf(" brevity is the soul of wit. \n --Shakespeare\n" );






you'll notice --Shakespeare starts on a new line
Re: Lets Learn C by stack1(m): 7:15pm On Jul 23, 2016
Ok i havent talked about how you'll turn your program text (a.k.a sourcecode into a working program you can run).

C is a compiled language (though interpreters exist) and traditionally after writing your C code in a source file and saving it, you compile it by running the compiler on your command line or shell. We would actually still get to that but for now such complexities are best avoided.

since we are using the codeblocks IDE, to compile your code simply type

Ctrl+F9 -- to compile only
Ctrl+F10 -- to run
F9 to -- to compile and run in one step

while compiling you might get errors that have being detected in your code, usually the first few (<5) errors should pinpoint where exactly in your code the mistake was made so you could edit and correct them
Re: Lets Learn C by stack1(m): 7:15pm On Jul 23, 2016
..more in a few hours
Re: Lets Learn C by Nobody: 10:55pm On Jul 23, 2016
when you refuse to follow the advise of elders,well, I shall try help.
Guys guys, please drop your comments on this thread, at least you can see the OP is trying to carry people along.
Re: Lets Learn C by Nobody: 11:06pm On Jul 23, 2016
Smh cry cry

Na so ppl go follow learn c,cc,c--

Still smh embarassed embarassed

Guy just spread ur wares, if Na eBook you wan sell toh, if Kuma Na affiliate link you carry come, no wahala.
Re: Lets Learn C by Nobody: 11:44pm On Jul 23, 2016
bros, you harsh small o
Re: Lets Learn C by stack1(m): 12:22am On Jul 24, 2016
Ok so lets move on

VARIABLES

In a C program (and in probably all programming languages), your program usually does some form of calculation or manipulation or transformation on data, this data could be predefined (stored or hard-coded in the program) of it might be inputted by the user or gotten from other sources while the program is running, such data that is used to provide information to a program are called variables.

Creating a variable in C is ease, variables take the form

variable-type variable-name;


for variable type we spoecify the type that tells us more about what we intend to store in the variable.

lets look at the basic C types
we have the a integer (or number types), this include

int (which stands for integer), the integer type allows us store numbers in our programs, you could declare an integer variable as such

int age;
int date_of_birth;


Originally the range of numbers the int type could hold varied depending on the CPU architecture.
Nowadays actually you can expect an integer to hold any value between

-2147483647 - 1 to -2147483647 (i.e it can hold upto 32 bits of data), this also shows us that the standard int type is signed and can hold negative numbers.

If you do not want your variable to hold negative values declare it using the unsigned modifier, an example is


unsigned int age;

or just
unsigned date_of_birth;



unsigned integers can hold a value between 0 and 4294967295,

They are other modifiers one cane use with int
1. use short to clip the value that can be stored in the variable to -32768 - 32767,
and use unsigned short to store values from 0 - 65535

2. we have long int declared (long int) in most compilers nowadays though the allowed values is the same as int

3. long long int, capable of holding values between -9223372036854775807 - 1 to -9223372036854775807
and unsigned long long int which holds 0 to 18,446,744,073,709,551,615

so here are the different ways to declare an integer


int number
long int number
unsigned int number (or just unsigned number)

short int number (or just short number)
unsigned short int ( or just unsigned short)

long long int ( or just long long)
unsigned long long int ( or unsigned long long)
Re: Lets Learn C by stack1(m): 12:40am On Jul 24, 2016
..more about variables

the statement
 int age; 


is called a declaration and composes of two parts
1. the type which is int, and the name of the variable (called the identifier)

A variable that is simply declared doesn't have any valued stored in it, however later on in the program we might decide to associate a value with the variable.
e.g
 age = 32; 


now this is called an assignment as we have assigned a value to the variable, and the variable can now be used in the program, assignmets can also be done at the point of declaration
e.g

int width = 40;
int height = 25;

then say we wanted to add both values we simply do

weight + height



trying to access variables that haven't been assigned a value (or that haven't been initialized is a common source of program errors)
Re: Lets Learn C by stack1(m): 12:49am On Jul 24, 2016
... still on variables


again we have the types for decimal input

1. float - holds a value between 1.2E-38 to 3.4E+38 and is precise to 6 decimal places
2. double - holds a value between 2.3E-308 to 1.7E+308 and is precise to 15 decimal places
3. long double - usually the same as floats or could hold between 3.4E-4932 to 1.1E+4932

Floats and doubles are much more complex to discuss in detail so we skip that for now

programs usually need character input so C has the char type

1. char - holds 1 byte (usually a letter) with the ASCII range -128 to 128
and if its unsigned its range is 0-255

ASCII stands for American Standard Code for Information Interchange. Computers can only understand numbers, so an ASCII code is the numerical representation of a character such as 'a' or '@' or an action of some sort. (cited from wikipedia)

Here's a link to a good ASCII table that shows you how charcters map to ASCII numbers
http://www.asciitable.com/


in C storing a letter such as

char letter = 'a';

is the same as storing its ascii equivalent
char letter = 97;

Re: Lets Learn C by stack1(m): 12:58am On Jul 24, 2016
lets jump into more examples...
Type this in a new C file, save then compile it


/*
* simple farenheit to celsius converter
*/

#include <stdio.h>

int main(void)
{
int farh, celsius, upper, lower, step;

lower = 0;
upper = 300;
step = 20;
farh = lower;


//print the table header
printf("Fahrenheit \t \t Celsius\n" );

while(farh <= upper)
{
celsius = 5* (farh-32)/9 ;


printf("%3d \t \t \t %6d\n", farh, celsius);

farh += step;

}

getchar();

return 0;
}


Re: Lets Learn C by stack1(m): 1:06am On Jul 24, 2016
Explanation
The last program prints a table of Celsius values from converted from Fahrenheit.

You might have noticed some text surrounded by /* */ this is called a comment, in programs comments are used to document
and explain parts of your program C has both multi-line comments which start with /* and end only when the pair */ is found
single line comments start with // and end on the same line,

the statement
 
#include <stdio.h>
includes the header for the standard Input/Output library

next the main function starts, the int in front of main tells us this functions is meant to return an int and the void means it doesn't take any arguments

the line
 int farh, celsius, upper, lower, step; 

declares 5 integer variables,

then we assign values to four of the variables with the statements

lower = 0;
upper = 300;
step = 20;
farh = lower;



the -while- statement is used to create a loop that executes repeatedly, until the value of expression becomes equal to zero
in our example

while(farh <= upper)


means while the value of the farh variable is less then equal to the value of the upper variable the statments within
the body of the while loop should keep executing

to calculate our celsius value we use use the statement


celcius = 5* (farh-32)/9


which uses the common formular °C = (°F - 32) × 5/9, but note we didnt do 5/9 this is because we declared our variables as integer,
and if we had done

celcius = 5/9 * (farh-32)


our answer would have been fractional and int's cant store fractions, so the answer wouldn't (and actually isnt accurate)

Exercise Try and rewite the program declaring the celcius and farh variables as float's
Re: Lets Learn C by stack1(m): 1:21am On Jul 24, 2016
do we proceed?

1 Like

Re: Lets Learn C by Nobody: 3:54am On Jul 24, 2016
Bros no kill me abeg grin grin grin

You dey here dey call witches on yourself

Na so e dey start o

tear race.........
Re: Lets Learn C by Nobody: 6:43am On Jul 24, 2016
o'di egwu for this thread and all the bad bad winch people!
Re: Lets Learn C by mojeed4(m): 12:45pm On Jul 24, 2016
dhtml18:

That was out-rightly rude, but it is alright, i hereby step out of this thread permanently.

Really? That's rude? I think that sounds more like a word to qualify you. tongue leave and lets enjoy his thread

1 Like

Re: Lets Learn C by romme2u: 3:21pm On Jul 24, 2016
dhtml18:

That was out-rightly rude, but it is alright, i hereby step out of this thread permanently.

i don't see anything rude in what he said, pls chill wink

op i am following as C was and is still my first love though a bit rusted cool
Re: Lets Learn C by Nobody: 4:06pm On Jul 24, 2016
romme2u:


i don't see anything rude in what he said, pls chill wink

op i am following as C was and is still my first love though a bit rusted cool
O yeah, tell him to provide the link from the ebook he is pasting from.

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