First look at C
Programming
|
First time with C, Otoom
DoxCoding.com
Components
------------------------
Code::Blocks
Windows
------------------------
Contents
------------------------
Who this is aimed at
A first look
Variables
Functions
------------------------
Who this is aimed at.
This, as it says, First time with C.
This does not need any prior knowledge of C, or as that happens any other programming language.
Do not be put off, if you have not programmed / coded in your life, as i will be trying my best on this one
to keep you happy, and knowing what you are doing every step.
Best of all. You must enjoy it, you must also practice each chapter as it goes. If you think you can pick it up
just by looking at it, it's a mistake. Many people have done that before, and found they do really have to practice.
So... threee rules, practice, practice and practice.
A first look.
Okay, well lets get started on this one. A first look will quickly take you through a quick few programs, starting from a simple one
of just printing out text onto the screen, using console applications. To printing variables that have been given in.
Before we get started on coding our first application, i think it would help going through the main parts of a program that you will
always need to include.
They go as so..
Includes. You must include the correct headers to be able to declare / use commands.
Example 1.1
#include <stdio.h>
The previous header will allow you to use the standard input / output for C.
int main, is another must have. It tells the program that it is going into the main part.
Example 1.2
int main()
Finally, the last must need in an application. It is the curly braces '{}', these are needed, to sort of group the content. All the code inside that will be part of main.
Example 1.3
#include <stdio.h>
int main()
{
}
The above, i have added them all together to show you the main frame of a C program.
Lets get to the fun part now shall we. The part you have waited for.
The first application i talk you through, will be a simple, print text to a console.
You may think, well isn't that all C can do anyway. No it isn't. Learning about different libraries you can do many other things with C.
If we take the main frame from above.
Example 1.4
#include <stdio.h>
int main()
{
printf("Hello from a First look at C");
getchar();
}
If you haven't noticed already, i added a printf. Like i said when telling you about headers, the printf is part of the standard input/output in C.
That is why we need to add stdio.h whenever using printf, i will also show you a few more later on.
If you type that into your compiler, which i suggested at the top. i use Code::Blocks. It is very user friendly.
To create this into an executable. First you need to open up Code::Blocks. Go to file. New then project. Select the console application one.
Save it, and name it what you like.
It should give you know errors. If it does, make sure you have typed everything in correctly.
getchar() is just to pause the console, if that was not there the console would open and close straight away.
Lets move on now.
Variables
In this chapter i will be showing you about variables, and how to take them in and print them out.
To get started, variables, i guess you could say are like boxes. Storage. They hold different types.
I will explain the main one. The ones you will use the most.
Example 1.1
char charater //i can hold letters like "c"
int number // i can hold hole numbers like 4
float decimal //i can hold numbers with decimals like 4.7
As i have shown above, are what are variables. They are not hard to learn. And very easy to interact with them.
Once again taking the main frame.
Example 1.2
#include <stdio.h>
int main()
{
printf("Hello from a First look at C");
printf("Please enter your age: ");
getchar();
}
There, again is just another printf. This time i have asked for their age.
So now. I will add more code. This time i will declare the variable, (int) as it takes in numbers remember. I will then take that variable
in and print it back out.
This will need some practice. I will expain it as well as i can. Also comment it.
Example 1.3
#include <stdio.h>
int main()
{
int dAge; //here is the variable declared
printf("Hello from a First look at C");
printf("Please enter your age: ");
scanf("%d", &dAge); //Here i take the variable in. you must always use an & when taking in variables.
printf("Your age is:%d", age); //When printing the variabe out. You must put the % sign.
fflush(stdin);
getchar();
}
The fflush(stdin); is something new to you. All this does it flushes what has been entered, because you do not know but an enter
is still in there. So it will still close if that was not there. You will get what i mean if you didn't put that there. It is only needed when
using scanf.
Even though i have commented it. I will explain it too. As i haven't explained everything.
When you are taking in a variable. You must use the correct % then letter sign.
%c Character
%d or % i Signed decimal integer
%e Scientific notation (mantise/exponent) using e character
%E Scientific notation (mantise/exponent) using E character
%f Decimal floating point
%g Use the shorter of %e or %f
%G Use the shorter of %E or %f
%o Signed octal
%s String of characters
%u Unsigned decimal integer
%x Unsigned hexadecimal integer
%X Unsigned hexadecimal integer (capital letters)
%p Pointer address
%n Nothing printed. The argument must be a pointer to a signed int, where the number of characters written so far is stored.
% A % followed by another % character will write % to stdout.
They are the different % symbols.
Of course, you will not need everyone, not just yet.
Take a close look at the variables i told you though.
So that is.
char => %c
int => %i, but i use %d. Yes it is for decimals, but if you think. 4 and 4.0 are the same. So, you may use either.
float => %f
That must be enclosed within quotes. Once done that, you must use a comma, and use a & sign.
After the & sign, all you have to put is the variable. What this will do is, store your input (in this case you age) into that
variable dAge.
Simple really.
Next will be the printing out part. It is just like a normal print, except using that % then letter again. Whichever one you use at the start
you must use when printing out. Again is must be enclosed with quotation marks. Again using a comma, then print the variable. This time
without using the & sign.
scanf is another one of those that falls within the aid of stdio.h. Yes, the header.
Functions
Functions, are moving on a little. So if you do not know the first parts off by heart, i do not suggest you moving onto these.
Functions can be used in different ways. They can be easy or hard.
First off, i will start with the easy way.
Once again, taking the main frame.
Example 1.1
#include <stdio.h>
int main()
{
printf("Hello from a First look at C");
}
If you have one what i said, and know that off by heart, i should not have to explain it. So i will move on.
Example 1.2
#include <stdio.h>
void messge()
{
}
int main()
{
}
I have now added another part. This will be the function.
I can do the same within that function as i can within main.
So lets do something?
Example 1.3
#include <stdio.h>
void messge()
{
printf("Im now in a function");
}
int main()
{
printf("Im in main");
message();
getchar();
}
As you can see here. I have used a function. All this will do is. Print the main part. It will then call the function. Reads what the function tells it to do, which is another printf, and will print the function.
Part two of Functions
Being Wrote.Please login to rate coding articles.
Click here to register a free account with us. |
|
Comments
|
Please login to post comments. |
|
I think to prevent confusion later on you need to explain a few things in that:
int main()
Is actually a function which is loaded by the operating system as an entry point. It has to follow all the rules that other functions and it is also necessary on certain compilers to return a value, which all of your examples do not demonstrate.
int main(int argc, char *argv[])
Is the way I prefer the function to be written, even if you arn't using the parameters - you will find it written like that in most C codes.
You should also say the braces lock together different blocks of code. Blocks of code can come after loop statements, comparison statemans and are necessary after function declarations.
Another imporatant part of the C language is the preprocessor directives
#include <stdio.h>
That line tells the compiler to go to the include directory, hence the '<' and '>' instead of double quotes, and search for the stdio.h file which contains the standard input and output functions.
I understand that this is a beginners tutorial but I think even a begginer needs to understand what he is doing 100% otherwise things get blurred later on. I've tried helping too many people out who can code simples things such as calculators but have no idea what their code is actually doing - which can be dangerous.
It was a good explanation though - and im sure many people found it useful
|
|
That's awesome! I've been learning C++ and haven't had a true intro to C ... There are some differences between C and C++, major differences.
|
|
Very nice intro! Finally know what those % dymbols are used for :P
|
|
|
 |
Ant (17) United Kingdom, West Midlands |
|
otoom has 8 fans
become a fan |
|
 |
|
 |
|