07-12-2013, 08:14 AM
Hello Code Community Members
This is just a quick tutorial for all beginner who want to learn c language
This is just a quick tutorial for all beginner who want to learn c language
first of all you have to know about c standard libraries or header files, without including that, you can't even coding a bit in c or c++.
Let's take a look at some important header include.
- <iostream> : This one is stand for (input/output stream) and use to get input from user and give output message to users screen(it's use for c++)
- <stdio.h> : stdio Stand for (standard input/output) also use to get input and shows output message on screen. but it has some other more functions(see latter)
- <cmath.h> : If you are going to use mathematical function in your program then it's recommended to include cmath
- <conio.h> : Stand for (console input/output), also use to get input and shows output on screen ,but it has some built it functions(see latter)
- <string.h> : Use for string functions [very useful]
- <windows.h> : Use for windows applications
[To see links please register here]
but I am not going to cover all, google is your best friend for itLets learn how to include a header file
- Here is syntax to include header file in C language
Hidden Content
- I'm going to show you two examples, let's take <conio.h> and <stdio.h> as example :fuckyea:
Hidden Content- It's use for getting input and showing input message on screen but it has some other built in functions like :
Hidden Content- stand for clear screen use to clear the contents of screen.
- stand for clear screen use to clear the contents of screen.
Hidden Content- stand for Get Character. The function use to get single character from user, your console will be paused till you give any single character. The character user entered can also stored in variable. e.g
- stand for Get Character. The function use to get single character from user, your console will be paused till you give any single character. The character user entered can also stored in variable. e.g
Hidden Content- The console window will be paused till you give any character suppose you give (*) . the (*) character will saved in (ch) variable.
- The console window will be paused till you give any character suppose you give (*) . the (*) character will saved in (ch) variable.
You can know more about it with google so I'm not going to explain everything here because google is your best friend
Hidden Content- (standard input/output) it also contains different function that use in that task of input and output, Here is some common functions:
Hidden Content- This works similar as getch() works in conio.h header.
- This works similar as getch() works in conio.h header.
Hidden Content- stand for Put Character use to display single character using stdio.h header.
- stand for Put Character use to display single character using stdio.h header.
Hidden Content- Stand for Get String use to get input from user.
As I said find others on google because google is your best friend
- Stand for Get String use to get input from user.
- (standard input/output) it also contains different function that use in that task of input and output, Here is some common functions:
- It's use for getting input and showing input message on screen but it has some other built in functions like :
now we know how to include a header file so lets make a simple hello world program, In addition I will put comments for every line. for your better understand.
Hidden Content
Variables are important part of any program in any language without this you can't make any good program.
- int :stand for integers use for decimal numbers from 0 to 9
- char :stand for characters use for any single character
- float :use for real values such as 1.2, 2.3 , 4.5 etc
- double :use for big real values.
- Here is syntax to declare variables in C language.
Hidden Content- Here is a example for your better understand.
Hidden Content
gives value to variable a
Hidden Content
Now we got something new, It thing is (%d), what is this? it's actually string format use for function printf()
- Here is some common string formats and symbols:
- %d : for decimal values
- %s : string values
- %c : character values
- %x : hex values
- %d : for decimal values
- So if you want to print decimal value using printf function, syntax will be:
Hidden Content
- Here is a example for your better understand.
- Here is syntax to declare variables in C language.
Let's go to talk about Operators (e.g. + - * = in C and C++)
you can know what is operators from
[To see links please register here]
, If you want to know everything about itHere is list of operators:
- (+) : for adding two values e.g(a + b)
- (-) : for subtracting two values
- (*) : Multiply two values
- (/) : use for division
- (%) : it's modulus operator use to get remainder of two values.
- (=) : for assigning value to variable
- (==) : for comparing two values for example (a==10) here we are not giving value to (a) but we are comparing and checking is a equal to 10.
- (!=) : not equal, also use for comparing (a!=10) we are checking is (a) not equal to 10
- (<) : less then operator, (a < 10) this command will check is the value of (a) is less than 10.
- (>) : Greater then operator. (a > 10) is the value of (a) greater then 10.
- (<=) : Less then or equal to. use to check if the value of left side is less then or equal to the value of right side for example (a <=10) here if the value of (a) is 0 to 10 then the condition is true.
- (>=) : Greater then or equal to. (a >= 10) if the value of (a) is 10 or above then the condition is true.
- ( || ) : or operator. it will work like this (a=10 or b = 10) but you will get error using this now instead of "or" we will use operator "||" (a=10 || b=10) this statement is true.
- (&&) : And operator . wok like this (a = 10 and b = 10) again it's false then use and operator here, (a=10 && b=10) this is true statement.
- You also can use as more values as you want like this
Hidden Content
- Here is syntax for using if statement
Hidden Content
- The conditional operators are use with if statement
Hidden Content
Hidden Content
- yes condition is true because the value of (a) is 10 is not equal to (b=9) but the the 10 is greater then 9, and that's why the condition is true
The code you will write here will execute.
Hidden Content
- Let's make a simple program using if statement.
Hidden Content
Try the above code and you will get console message "yes the condition is true"
I think now you got a question, I'm sure now you are thinking if the condition is false what will happen right? well don't worry guys, here is answer for it, we go for else statement!
To make a better program you will need to use else statement with if statement. because if the condition of "if" is false, then the the "else" statement will execute automatically.
- The syntax of using else statement is:
Hidden Content- Here is a example how it works
Hidden Content
- Here is a example how it works
Now little bit wired right? let's do it practically
make another simple program using if/else statement.
Hidden Content
Run the above program it will show you "Running else statement"
how ?
Because the the condition of "if statement" is false because 8 is neither equal to 9 nor greater then 9. that's why the control skip the if statement and run the else statement code.
Multiple if/else statement and switch statement
Take a look at multiple if/else and switch statement.
- Q : why i said multiple if/else and switch together ?
A : Because both statements works same. you will need these statements if you have multiple conditions. still confused ?
Here are examples :
- Suppose if a student get 40% marks in exams then he is simply pass. if he takes above then 50% then he has grade "C". If above 70% then grade "B".
- And principle asked you to make a program for student then what will you do ?
of course this program needs "if statement"
but there are multiple if . then you will do it like this:
Hidden Content
- Like this huh? :no:
If you are doing like this then you are going wrong . here are multiple if/else.
- Take a look here:
Hidden Content
- I think now little bit confused for sure :yuno:, Don't worry let's get a real life example with simple program :
Hidden Content
- Note : always use (&) operator in function scanf(); like this scanf("%d",&a); else the program will be crashed as it will not allocate the value on right address. (&) sign use to get the memory address of variables.
how the above program works ? still confused ?
yeah it's a bit complicated instead of this you can use "switch statement"
Let's go ahead take a look at switch statement:
- It's easy to understand and easy to use for multiple conditions
- syntax for using this is:
Hidden Content
- syntax for using this is:
- Example for switch statement :
Hidden Content
- Let's get real life example :
Hidden Content
Go ahead take a look on looping:
Looping mean run the commands again and again till the condition get false ..got it ?:lulsec:
of course not :yuno:
Ok here we start:
There are three main loops
- for loop
- while loop
- do while loop
- First we will talk on "while loop".
- Syntax of using while loop is:
Hidden Content
- how loop This loop works ?
suppose you give while (a<=10) and the value of a is less then ten the loop will check condition oh yes the condition is true then it will execute the code again but using increment or decrement operator is changed and after all the time will come the value of a will not less then or equal to 10
The condition will false and the loop stops..wtf? pretty confused ? :yuno:
I know :pfft:
Let's take a real life example:
Hidden Content
- how the above program works?
- see the value of a = 0 while(a <=10) this condition is true the control enter in code area and executes the code. by using
a++ = a = a+1
now the value of a = 1+1=2
The loop will check again the condition, condition was (a<=10), now the value of a=2 here 2 is also less then 10, the control will enter again in execution area and will execute the program
again here a++, now the value of a =2
and a++ means a=a+1 = a= 2+1=3
now the value of a is three which is still less then 10.
At last the time will come the value of a will become 11 and the condition (a<=10) will become false and loop will be stopped.- Do while loop
This loop will work at least once either the condition is true or false.- Syntax for do while loop:
Hidden Content
- Let's take a real life example :
Hidden Content
- Syntax for do while loop:
- For loop
It is a wide loop used by programmers it executes the statement for specific number of times.- Syntax of for loop is:
Hidden Content
- Real life example:
Hidden Content
- Syntax of for loop is:
Classes, user define function, file handling, arrays will be covered in my next tutorial
-CrazyFrog-
I dont care if you leech this tutorial but please give me credits saying CrazyFrog :biggrin:
[/*]
- Do while loop
- Syntax of using while loop is: