Sinisterly
Language C for all beginners - Printable Version

+- Sinisterly (https://sinister.ly)
+-- Forum: Coding (https://sinister.ly/Forum-Coding)
+--- Forum: C, C++, & Obj-C (https://sinister.ly/Forum-C-C-Obj-C)
+--- Thread: Language C for all beginners (/Thread-Language-C-for-all-beginners)



Language C for all beginners - CrazyFrog - 07-12-2013

Hello Code Community Members
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


Here you can see a lot more header includes but I am not going to cover all, google is your best friend for it


Lets learn how to include a header file
  • Here is syntax to include header file in C language

    Code:
    #include <header_file_name>

  • I'm going to show you two examples, let's take <conio.h> and <stdio.h> as example :fuckyea:
    1. Code:
      #inlcude <conio.h>
      • It's use for getting input and showing input message on screen but it has some other built in functions like :
        1. Code:
          clrscr()
          • stand for clear screen use to clear the contents of screen.
        2. Code:
          getch()
          • 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
        3. Code:
          ch = getch;
          • 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


      • Code:
        #iclude <sdtio.h>
        • (standard input/output) it also contains different function that use in that task of input and output, Here is some common functions:
          1. Code:
            getchar()
            • This works similar as getch() works in conio.h header.

          2. Code:
            putchar()
            • stand for Put Character use to display single character using stdio.h header.

          3. Code:
            gets()
            • Stand for Get String use to get input from user.

            As I said find others on google because google is your best friend

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.


Code:
#include <stdio.h> //header file for input output main(){ //main function starts //within these brackets {} << we can write our execution code printf("Hello world"); //printf function use to get output message show on screen getchar(); //this will pause your console window till you press any character //without getchar() function your program will run so fast try running your program without getchar() //semi colon(;) are recommended in c language after any command }// main function ends


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.
      • Code:
        #include <Header_file_name> main(){ <data type> <variable name>;
        • Here is a example for your better understand.
          Code:
          int a; //using this you have declared an integer data type variable named a

          gives value to variable a
          Code:
          a = <value>; a= 10; //now (a) variable has a decimal value (10) //print out the value of (a) printf("%d",a);
          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:

          1. %d : for decimal values
          2. %s : string values
          3. %c : character values
          4. %x : hex values


        • So if you want to print decimal value using printf function, syntax will be:

          Code:
          printf("%d",a);


Let's go to talk about Operators (e.g. + - * = in C and C++)
you can know what is operators from Wikipedia, If you want to know everything about it

Here 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
    Code:
    a =10 || b = 9 || c = 8 || d = 7[code] [code]a = 10 && b = 9 && c=8[code] [/list] You will need these all operators with if statement and then it will clear up your mind how to use these operators. [size=large][b]Let's learn about if/else statement[/b][/size] They are conditional statements use use to check if condition is true, if yes then execute the code Here are example : [list] [*][code] #include <Header_file_name> main(){ int a = 10; // (a) variable with value 10 int b = 9; //(b) variable with value 9

  • Here is syntax for using if statement
    Code:
    if(condition){code here to execute if condition is true} //within these two curly brackets write your code to execute.

  • The conditional operators are use with if statement
    Code:
    ==,!=,<,>,<=,>=

    Code:
    if(a=>b){ //here we are checking is the value of (a) is equal to or greater then (b)

  • 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.

    Code:
    } } // End of main

  • Let's make a simple program using if statement.

    Code:
    #include <stdio.h> main(){ int a,b; //declaring two integer values a and b a = 10; b = 9; if(a >= b){ printf("%s","yes the condition is true"); } getchar(); }

    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:

    Code:
    else{code here to execute};
    • Here is a example how it works

      Code:
      if (condition ){ execute this code } else { execute this code }

Now little bit wired right? let's do it practically
make another simple program using if/else statement.



Code:
#include <stdio.h> main(){ int a,b; a = 8; b = 9; if(a >= b){ printf("%s","running if statement"); } else { printf("%s","Running else statement"); } getchar(); }


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:
    • Code:
      if (marks ==40){you are pass} if(marks = 50){you have c grade}


  • Like this huh? :no:
    If you are doing like this then you are going wrong . here are multiple if/else.

  • Take a look here:
    • Code:
      if(condition){ code here } else if(2nd condition){ code here } else if (3rd condition){ code here } else { if not matched above then execute this }

  • I think now little bit confused for sure :yuno:, Don't worry let's get a real life example with simple program :
    • Code:
      #include <stdio.h> main(){ int a,b; // declared two integer values printf("Enter value of a : "); //simply out put //message using printf scanf("%d",&a); //here is new function "scanf" //what is this ? // it's used for getting input //from user in stdio.h header file //syntax for using this is scanf("string format",Null variable) //got it hah ? printf("Enter value of b : "); scanf("%d",&b); if (a==10 && b==20){ printf("%s","A = 10 and b = 20"); } else if(a==30 && b==40){ printf("%s"," a = 30 and b = 40"); } else if(a==50 && b==60){ printf("%s","Above then 50"); } else { printf("%s","Not matched..!!!!"); } getchar(); }

  • 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:
      • Code:
        switch(variable){ }
  • Example for switch statement :
    • Code:
      switch(variable here){ case 1: //same as if a == 1 printf("%s","If case one print this"); break; //this will separate two statements case 2: //same as if a==2 code here break; default: printf("%S","This default will work same as else"); }

  • Let's get real life example :
    • Code:
      #include <stdio.h> main(){ int a; printf("Enter value of a : "); //simply out put //message using printf scanf("%d",&a); //get user input switch (a){ //switch start code within these curly brackets case 20: //same as if(a==20) printf("%s","A = 20"); //then print this break; //it will separate two statements case 30: //if(a==30) printf("%s","A = 30"); //print this break; case 40: //if(a==40) printf("%s","a is equal to 40"); break; default: //same as else{print this} printf("%s","Not matched") ; }//switch end here getchar(); }

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:
      • Code:
        while(condition){ statement1 statement 2 . . . increment/decrement operator }

    • 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:
      • Code:
        #include <iostream> #include <stdio.h> main(){ int a= 0; //the value of (a) is 0 while(a <=10){ /*while loop starts, the condition will remain true till the value of (a) is less then or equal to 10*/ printf("%d",a); //show the value of (a) a++; //this is increment operator this mean a = a+1; printf("\n"); //\n using this cursor will go to the next line \n = start new line }//loop ended getchar(); }
    • 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:
          • Code:
            do{ statement here } while(condition);

        • Let's take a real life example :
          • Code:
            #include <stdio.h> main(){ int a = 10; //integer a = 10 do{ printf("\n%d",a); // print the value of (a) which is 10 a = a-1; //now decrease the value of a by 1 , so value becomes 9 }while(a>=0); //check condition now still the value of a is greater then 0, run again getchar(); }

      • For loop

        It is a wide loop used by programmers it executes the statement for specific number of times.
        • Syntax of for loop is:
          • Code:
            for(initialize value, condition, increment/decrement){ statement here }

        • Real life example:
          • Code:
            #include <stdio.h> main(){ for(int a=1;a<=10;a++) /* initialize variable a with value 0, set condition of loop it will run till the value of a is less then or equal to 10,3rd increase the value of a by 1*/ { printf("\n%d",a); } getchar(); }
      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



RE: Language C for all beginners - Hexology - 07-12-2013

Give credits to the original owner of this tutorial. I can see the other forum tags in here, including emoticons that we dont have.


RE: Language C for all beginners - Blue - 07-12-2013

Props to the owner, Was very in-depth. Not looking for c/p though.


RE: Language C for all beginners - CrazyFrog - 07-12-2013

i posted this in my forum and posted here in my forum i have this meme smiles, uhm well i don't post my tutorials anymore here. i just tried to do something for the community, this is my own tutorial and Original Link click here

Remember to think twice before say something to someone


RE: Language C for all beginners - Eternity - 07-12-2013

Finally we got your forum link !


RE: Language C for all beginners - Blue - 07-12-2013

(07-12-2013, 01:30 PM)CrazyFrog Wrote: i posted this in my forum and posted here in my forum i have this meme smiles, uhm well i don't post my tutorials anymore here. i just tried to do something for the community, this is my own tutorial and Original Link click here

Remember to think twice before say something to someone

(07-12-2013, 01:35 PM)Eternity Wrote: Finally we got your forum link !

Eternity, It really wasn't that hard to find it. Simple GOOGLE of eHackerz simple. He has said it was his site before. And since you are the original owner, Then I thanked you. So I don't get your panties in a wad beautiful.


RE: Language C for all beginners - Hexology - 07-12-2013

Well I didn't know that it was your post, but if it is then great tutorial and good job!


RE: Language C for all beginners - CrazyFrog - 07-12-2013

Thank you, enjoy Smile


RE: Language C for all beginners - Wet - 07-14-2013

Great tutorial! Very HQ too. And don't worry, if you wrote this tutorial then you're free to post it anytime, anywhere.
I want to see more awesome tutorials from you.


RE: Language C for all beginners - Platinum - 07-29-2013

(07-12-2013, 01:37 PM)Blue Wrote:
(07-12-2013, 01:30 PM)CrazyFrog Wrote: i posted this in my forum and posted here in my forum i have this meme smiles, uhm well i don't post my tutorials anymore here. i just tried to do something for the community, this is my own tutorial and Original Link click here

Remember to think twice before say something to someone

(07-12-2013, 01:35 PM)Eternity Wrote: Finally we got your forum link !

Eternity, It really wasn't that hard to find it. Simple GOOGLE of eHackerz simple. He has said it was his site before. And since you are the original owner, Then I thanked you. So I don't get your panties in a wad beautiful.

But truly, that website looks pretty cool ;3