└── README.md /README.md: -------------------------------------------------------------------------------- 1 | # C Tutorial For Beginners 2 | 3 | * [Installing CDT In Eclipse](#installing-cdt-in-eclipse) 4 | * [Online Editors](#online-editors) 5 | * [All Code Examples](#all-code-examples) 6 | - [Hello World](#hello-world) 7 | - [First Variable](#first-variable) 8 | - [Two Variables](#two-variables) 9 | - [Sum of Two Variables](#sum-of-two-variables) 10 | - [Swap Program](#swap-program) 11 | - [Floating point variable](#floating-point-variable) 12 | - [Function](#function) 13 | - [IF ELSE](#if-else) 14 | - [Nested If Else](#nested-if-else) 15 | - [Switch Statement](#switch-statement) 16 | - [Need for an Array](#need-for-an-array-) 17 | - [Array Basics](#array-basics) 18 | - [For Loop Example](#for-loop-example) 19 | - [Do while Example](#do-while-example) 20 | - [Leap Year](#leap-year-c-program) 21 | - [Sum of first n numbers program](#sum-of-first-n-numbers-program) 22 | - [C Program Sum of First n even numbers](#c-program-sum-of-first-n-even-numbers) 23 | - [Divisors of a number](#divisors-of-a-number) 24 | - [Prime Number Program](#prime-number-program) 25 | - [Number of digits in a number](#number-of-digits-in-a-number) 26 | - [Sum of Digits Program](#sum-of-digits-program) 27 | - [Write to a file](#write-to-a-file) 28 | - [Read From File](#read-from-file) 29 | * [About in28Minutes](#about-in28minutes) 30 | - [Our Beliefs](#our-beliefs) 31 | - [Our Approach](#our-approach) 32 | - [Find Us](#useful-links) 33 | 34 | ##Installing CDT In Eclipse 35 | https://www3.ntu.edu.sg/home/ehchua/programming/howto/EclipseCpp_HowTo.html 36 | 37 | ##Online Editors 38 | - http://www.tutorialspoint.com/compile_c_online.php 39 | - https://www.google.co.in/search?q=c+online+compiler 40 | 41 | #All Code Examples 42 | #Hello World 43 | ``` 44 | #include 45 | 46 | int main() //pre-defined function 47 | { 48 | printf("Hello World 123"); 49 | return 0; 50 | } 51 | ``` 52 | 53 | #Main is Main 54 | ``` 55 | #include 56 | 57 | int printSomething() 58 | { 59 | printf("I will not be executed"); 60 | return 0; 61 | } 62 | 63 | int main() 64 | { 65 | printf("I'm a great guy"); 66 | return 0; 67 | } 68 | ``` 69 | #Print Something 70 | ``` 71 | #include 72 | 73 | void printSomething() 74 | { 75 | printf("I will not be executed"); 76 | } 77 | 78 | int main() 79 | { 80 | printf("I'm a great guy"); 81 | printSomething(); 82 | return 0; 83 | } 84 | 85 | ``` 86 | #First Variable 87 | ``` 88 | #include 89 | 90 | //Variable - value can change 91 | //Assignment Operator 92 | 93 | ///////// 94 | // 3 // 95 | //////// 96 | //score - integer 97 | int main() 98 | { 99 | int score;//1854054454 100 | //TYPE NAME; 101 | //d - integer 102 | printf("score : %d",score); 103 | return 0; 104 | } 105 | 106 | ``` 107 | #Two Variables 108 | ``` 109 | #include 110 | 111 | //Assignment Operator 112 | 113 | ////////////////// 114 | // 20 // 115 | ////////////////// 116 | //score - integer 117 | int main() 118 | { 119 | int score1;// 120 | int score2; 121 | 122 | score1 = 5; 123 | score2 = 15; 124 | 125 | printf("score1 : %d \n",score1); 126 | 127 | printf("score2 : %d",score2); 128 | return 0; 129 | } 130 | ``` 131 | 132 | #Sum of Two Variables 133 | ``` 134 | #include 135 | 136 | //Assignment Operator 137 | 138 | ////////////////// 139 | // 20 // 140 | ////////////////// 141 | //score - integer 142 | int main() 143 | { 144 | int score1;// 145 | int score2; 146 | int sum; 147 | 148 | score1 = 5; 149 | score2 = 15; 150 | 151 | sum = score1 + score2; 152 | 153 | printf("score1:%d score2:%d sum:%d", 154 | score1,score2,sum); 155 | return 0; 156 | } 157 | ``` 158 | 159 | #Swap Program 160 | ``` 161 | #include 162 | 163 | //Assignment Operator 164 | 165 | /////////// /////////// 166 | // 10 // // 10 // 167 | ////////// ////////// 168 | // i j 169 | 170 | //score - integer 171 | int main() 172 | { 173 | int i=5; 174 | int j=10; 175 | int k; 176 | 177 | k = i;//k=5,i=5,j=10 178 | i=j;//k=5,i=10,j=10 179 | j=k;//k=5,i=10,j=5 180 | 181 | printf("i:%d j:%d", 182 | i,j); 183 | 184 | return 0; 185 | } 186 | ``` 187 | #Floating point variable 188 | ``` 189 | #include 190 | 191 | int main() 192 | { 193 | int i=1; 194 | int j=10; 195 | 196 | float avg;//1.5,2.5 197 | 198 | avg = (i+j)/2.0; 199 | 200 | printf("avg:%f",avg); 201 | //d - integer 202 | //f - float 203 | 204 | } 205 | ``` 206 | #Character Example 207 | ``` 208 | #include 209 | 210 | int main() 211 | { 212 | int i=1; 213 | int j=10; 214 | 215 | char ch = 67;//ASCII 216 | 217 | printf("ch:%c",ch);//A 218 | //d - integer 219 | //f - float 220 | //c - character 221 | 222 | } 223 | ``` 224 | 225 | #Function 226 | ``` 227 | #include 228 | 229 | //REturnType NameofFuntion() 230 | //{ 231 | // BODY; 232 | //} 233 | void welcome() //declaration 234 | { 235 | printf("Hi From in28Minutes.com\n"); 236 | } 237 | 238 | int main() 239 | { 240 | welcome(); //calling or invocation 241 | welcome(); 242 | welcome(); 243 | } 244 | ``` 245 | #IF ELSE 246 | ``` 247 | #include 248 | 249 | void welcome() 250 | { 251 | printf("Welcome to in28minutes.com\n"); 252 | } 253 | 254 | void subscribe() //declaration 255 | { 256 | printf("Subscribe at in28Minutes.com\n"); 257 | } 258 | 259 | int main() 260 | { 261 | int like=0; 262 | welcome(); //calling or invocation 263 | 264 | if(like) // true if like has non zero 265 | { 266 | subscribe(); 267 | } 268 | else 269 | { 270 | printf("Please tell us what we can do to improve"); 271 | } 272 | } 273 | ``` 274 | #Nested If Else 275 | ``` 276 | #include 277 | 278 | int main() 279 | { 280 | int score=3; 281 | //1 - Single 2- Double 282 | //3 - Triple 4 - Boundary 6 - Sixer 283 | 284 | if(score==1) 285 | { 286 | printf("Single"); 287 | } 288 | else if(score==2) 289 | { 290 | printf("Double"); 291 | } 292 | else 293 | { 294 | printf("Something Else"); 295 | } 296 | } 297 | 298 | ``` 299 | #Switch Statement 300 | ``` 301 | #include 302 | 303 | int main() 304 | { 305 | int score=6; 306 | //1 - Single 2- Double 307 | //3 - Triple 4 - Boundary 6 - Sixer 308 | 309 | switch(score) 310 | { 311 | case 1 : 312 | printf("Single"); 313 | break; 314 | case 2 : 315 | printf("Double"); 316 | break; 317 | default: 318 | printf("Something Else"); 319 | break; 320 | case 4: 321 | printf("Boundary"); 322 | break; 323 | } 324 | } 325 | ``` 326 | #Need for an Array 327 | ``` 328 | #include 329 | 330 | int main() 331 | { 332 | int score1=6; 333 | int score2=15; 334 | int score3=145; 335 | int count = 0; 336 | 337 | if(score1>99) 338 | count = count + 1; 339 | 340 | if(score2>99) 341 | count = count + 1; 342 | 343 | if(score3>99) 344 | count = count + 1; 345 | 346 | printf("Number of Centuries %d",count); 347 | } 348 | ``` 349 | #Array Basics 350 | ``` 351 | #include 352 | int main() 353 | { 354 | int score1=106; 355 | int score2=15; 356 | int score3=145; 357 | int score4=23; 358 | 359 | int scores[10] = {106, 15, 145, 23}; 360 | // 0 1 2 3 361 | int scoresLength = 4; 362 | 363 | //How to read values? 364 | //printf("%d",scores[0]); 365 | //How to set values? 366 | scores[0] = 108; 367 | //How to find length of an array? 368 | //What is the default value? 369 | //printf("%d",scores[5]);//0 370 | //What if I try to bite more than I can Chew? 371 | printf("%d",scores[11]);//-1308602447 372 | 373 | } 374 | ``` 375 | #For Loop Example 376 | ``` 377 | #include 378 | int main() 379 | { 380 | 381 | int scores[] = {106, 15, 145, 23}; 382 | // 0 1 2 3 383 | int scoresLength = 4; 384 | 385 | for 386 | ( 387 | int i = 0;//initialization 388 | i < scoresLength;//condition 389 | i++//increment 390 | ) 391 | { 392 | printf(" %d ",scores[i]); 393 | } 394 | 395 | } 396 | 397 | ``` 398 | #While Loop Example 399 | ``` 400 | #include 401 | int main() 402 | { 403 | 404 | int scores[] = {106, 15, 145, 23, 235,235,235}; 405 | // 0 1 2 3 406 | int scoresLength = 7; 407 | 408 | int i = 0;//initialization 409 | 410 | while(i < scoresLength)//condition 411 | {//i:7 412 | printf(" %d ",scores[i]); 413 | i++;//increment 414 | } 415 | 416 | for(int i=0;i 426 | int main() 427 | { 428 | 429 | int scores[] = {106, 15, 145, 23, 235,235,235}; 430 | // 0 1 2 3 431 | int scoresLength = 7; 432 | 433 | int i = 0;//initialization 434 | 435 | do 436 | {//i:7 437 | printf(" %d ",scores[i]); 438 | i++;//increment 439 | } 440 | while(i < scoresLength);//condition 441 | } 442 | 443 | ``` 444 | #C Program : Print an Array 445 | ``` 446 | #include 447 | 448 | //returntype name(arguments) 449 | void printArray(int array[],int length) 450 | { 451 | for(int i=0;i 473 | 474 | //0 - false, anything non-zero - true (1,-1) 475 | int isEven(int number) 476 | { 477 | if(number%2==0)// 5%2==1 - comparision 478 | return 1; 479 | 480 | return 0; 481 | } 482 | 483 | //isEven 484 | //2 - true 485 | //3 - false 486 | int main() 487 | { 488 | printf("1:%d\n",isEven(1)); 489 | printf("2:%d\n",isEven(2)); 490 | return 0; 491 | } 492 | 493 | 494 | 495 | ``` 496 | #Leap Year C Program 497 | ``` 498 | #include 499 | 500 | //0-false 1-true 501 | int isLeapYear(int year) 502 | { 503 | if(year%400==0) 504 | return 1; 505 | 506 | if(year%100==0) 507 | return 0; 508 | 509 | if(year%4==0) 510 | return 1; 511 | 512 | return 0; 513 | } 514 | 515 | //%4 = Leap YEar 516 | //1900,2000,2100,2200,2300,2400 517 | 518 | int main() 519 | { 520 | printf("2000:%d\n",isLeapYear(2000)); 521 | printf("1900:%d\n",isLeapYear(1900)); 522 | printf("1904:%d\n",isLeapYear(1904)); 523 | printf("1901:%d\n",isLeapYear(1901)); 524 | 525 | return 0; 526 | } 527 | 528 | ``` 529 | #Sum of first n numbers program 530 | ``` 531 | #include 532 | 533 | //0-false 1-true 534 | int calculateSumUpto(int n) 535 | { 536 | int result = 0; 537 | 538 | for(int i=1; i<=n; i++) 539 | { 540 | result = result + i; 541 | } 542 | 543 | return result; 544 | // 1 to n 545 | // result = result + index 546 | } 547 | 548 | // 5 = 1 to 5, 1 + 2 + ... + 5 = 15 549 | 550 | int main() 551 | { 552 | printf("upto 5:%d\n",calculateSumUpto(5)); 553 | printf("upto 10:%d\n",calculateSumUpto(10)); 554 | 555 | return 0; 556 | } 557 | 558 | ``` 559 | #C Program Sum of First n even numbers 560 | ``` 561 | #include 562 | 563 | //0-false 1-true 564 | int calculateSumEvenNumbersUpto(int n) 565 | { 566 | int result = 0; 567 | 568 | for(int i=2; i<=n*2; i = i + 2) //1 to n 2,4,6,8,10 569 | { 570 | result = result + i; //1 to n 2 * 1 to 2 * n 571 | } 572 | 573 | return result; 574 | // 1 to n 575 | // result = result + index 576 | } 577 | 578 | // 5 = 1 to 5, 1 + 2 + ... + 5 = 15 579 | 580 | int main() 581 | { 582 | printf("upto 5:%d\n",calculateSumEvenNumbersUpto(5)); 583 | printf("upto 10:%d\n",calculateSumEvenNumbersUpto(10)); 584 | 585 | return 0; 586 | } 587 | 588 | ``` 589 | #Divisors of a number 590 | ``` 591 | #include 592 | 593 | void printDivisors(int n) 594 | { 595 | for(int i=2;i 613 | 614 | //0-Not prime 615 | //1 - prime 616 | // 12 (2, 3,4,...,11) 617 | int isPrime(int n) 618 | { 619 | for(int i=2;i 640 | 641 | int numberOfDigits(int n) //345 642 | { 643 | int temp = n;//0 644 | int count = 0;//3 645 | while(temp!=0) 646 | { 647 | count++; 648 | temp = temp/10; 649 | } 650 | return count; 651 | 652 | } 653 | 654 | //456 - 3 655 | //24567 - 5 656 | 657 | int main() 658 | { 659 | printf("456: %d\n",numberOfDigits(456)); 660 | printf("24567: %d\n",numberOfDigits(24567)); 661 | return 0; 662 | } 663 | 664 | ``` 665 | #Sum of Digits Program 666 | ``` 667 | #include 668 | 669 | //345 670 | //34 - 5 671 | //3 - 5 + 4 672 | //0 - 5 + 4 + 3 673 | 674 | int sumOfDigits(int n) //345 675 | { 676 | int temp = n;//3 677 | int sum = 0;//0 + 5 + 4 + 3 678 | while(temp!=0) 679 | { 680 | sum = sum + temp % 10; 681 | temp = temp/10; 682 | } 683 | return sum; 684 | 685 | } 686 | 687 | //456 - 15 688 | //24567 - 24 689 | 690 | int main() 691 | { 692 | printf("456: %d\n",sumOfDigits(456)); 693 | printf("24567: %d\n",sumOfDigits(24567)); 694 | return 0; 695 | } 696 | 697 | ``` 698 | #Write to a file 699 | ``` 700 | #include 701 | 702 | struct Student 703 | { 704 | char name[100]; 705 | int marks; 706 | int year; 707 | }; 708 | 709 | void writeStudentToFile(struct Student student) 710 | { 711 | //Get a pointer to the opened file 712 | // w 1 = 1 713 | // r 714 | // a - 5 + 1 = 6 715 | FILE *fp = fopen("Student.dat","w"); 716 | 717 | //Write to the file 718 | fprintf(fp,"%s %d %d\n",student.name,student.marks,student.year); 719 | 720 | //Close the file 721 | fclose(fp); 722 | } 723 | 724 | int main() 725 | { 726 | struct Student student = 727 | {"in28Minutes",100,4}; 728 | 729 | writeStudentToFile(student); 730 | } 731 | 732 | ``` 733 | #Read From File 734 | ``` 735 | #include 736 | 737 | struct Student 738 | { 739 | char name[100]; 740 | int marks; 741 | int year; 742 | }; 743 | 744 | struct Student readStudentFromFile() 745 | { 746 | //Get a pointer to the opened file 747 | FILE *fp = fopen("Student.dat","r"); 748 | struct Student student; 749 | 750 | //Read from the file 751 | fscanf(fp,"%s %d %d\n",student.name,&student.marks,&student.year); 752 | 753 | //Close the file 754 | fclose(fp); 755 | 756 | return student; 757 | } 758 | 759 | void printStudent(struct Student student) 760 | { 761 | printf("%s %d %d\n",student.name,student.marks,student.year); 762 | } 763 | 764 | int main() 765 | { 766 | struct Student student = readStudentFromFile(); 767 | printStudent(student); 768 | } 769 | 770 | ``` 771 | 772 | ##About in28Minutes 773 | - At in28Minutes, we ask ourselves one question everyday. How do we create more effective trainings? 774 | - We use Problem-Solution based Step-By-Step Hands-on Approach With Practical, Real World Application Examples. 775 | - Our success on Udemy and Youtube (2 Million Views & 12K Subscribers) speaks volumes about the success of our approach. 776 | - While our primary expertise is on Development, Design & Architecture Java & Related Frameworks (Spring, Struts, Hibernate) we are expanding into the front-end world (Bootstrap, JQuery, Angular JS). 777 | 778 | ###Our Beliefs 779 | - Best Course are interactive and fun. 780 | - Foundations for building high quality applications are best laid down while learning. 781 | 782 | ###Our Approach 783 | - Problem Solution based Step by Step Hands-on Learning 784 | - Practical, Real World Application Examples. 785 | - We use 80-20 Rule. We discuss 20% things used 80% of time in depth. We touch upon other things briefly equipping you with enough knowledge to find out more on your own. 786 | - We will be developing a demo application in the course, which could be reused in your projects, saving hours of your effort. 787 | - All the code is available on Github, for most steps. 788 | 789 | ###Useful Links 790 | - [Our Website](http://www.in28minutes.com) 791 | - [Youtube Courses](https://www.youtube.com/user/rithustutorials/playlists) 792 | - [Udemy Courses](https://www.udemy.com/user/in28minutes/) 793 | - [Facebook](http://facebook.com/in28minutes) 794 | - [Twitter](http://twitter.com/in28minutes) 795 | - [Google Plus](https://plus.google.com/u/3/110861829188024231119) 796 | 797 | ###Other Courses 798 | - [Spring Framework](https://www.udemy.com/spring-tutorial-for-beginners/) 799 | - [Maven](http://www.in28minutes.com/p/maven-tutorial-for-beginners.html) 800 | - [Eclipse](http://www.in28minutes.com/p/eclipse-java-video-tutorial.html) 801 | - Java 802 | * [Java](https://www.youtube.com/watch?v=Y4ftqcYVh5I&list=PLE0D4634AE2DFA591&index=1) 803 | * [Java Collections](http://www.in28minutes.com/p/java-collections-framework-video.html) 804 | * [Java OOPS Concepts](https://www.udemy.com/learn-object-oriented-programming-in-java/) 805 | - [Design Patterns](http://www.in28minutes.com/p/design-patterns-tutorial.html) 806 | - [JUnit](https://www.udemy.com/junit-tutorial-for-beginners-with-java-examples/) 807 | - [C](https://www.udemy.com/c-tutorial-for-beginners-with-puzzles/) 808 | - [C Puzzles](https://www.udemy.com/c-puzzles-for-beginners/) 809 | - [Javascript](https://www.youtube.com/watch?v=6TZdD-FR6CY) 810 | - [More Courses on Udemy](https://www.udemy.com/user/in28minutes/) 811 | * Java Servlets and JSP : Your first web application in 25 Steps 812 | * Learn Spring MVC in 25 Steps 813 | * Learn Struts in 25 Steps 814 | * Learn Hibernate in 25 Steps 815 | * 10 Steps to Professional Java Developer 816 | - [Java Interview Guide](http://www.in28minutes.com/p/buy-our-java-interview-guide.html) 817 | * Core Java 818 | * Advanced Java 819 | * Spring, Spring MVC 820 | * Struts 821 | * Hibernate 822 | * Design Patterns 823 | * 400+ Questions 824 | * 23 Videos 825 | 826 | 827 | --------------------------------------------------------------------------------