Hey there!
Yesterday's lesson was all about functions. Today, we are going to look at some important pre-defined functions like printf() that will really come in handy for your programs.
Remember I told you that we need to include stdio.h if we want to useprintf()? Well, it is quite the same for other pre-defined functions too. For the next pre-defined function, we will need to include math.h.
#include<stdio.h>
#include<math.h>
void main()
{
int num = 25;
int result;
result = sqrt(num);
printf("%i",result);
}
http://codepad.org/EKkuGvev
The pre-defined function here is sqrt(). The sqrt() finds out the square root of a number. We have to indicate the number whose square root we want to find within the round brackets.
Another common pre-defined function is pow().
#include<stdio.h>
#include<math.h>
void main()
{
int num = 10, power = 2;
int result;
result = pow(num, power);
printf("%i",result);
}
http://codepad.org/5je45P5U
pow() finds out the result of a number raised to a given power. The number we use as a base is to be written first, followed by the power to which we want to raise it.
For working with strings, we need to include string.h at the beginning of our program.
#include<stdio.h>
#include<string.h>
void main()
{
char word[5]={'h','a','p','p','y'};
int result;
result = strlen(word);
printf("%i", result);
}
http://codepad.org/yHCGc8W6
strlen() finds out the length of the string, which is the number of characters or alphabets in the string. In the above example, it might seem obvious that the length of the string is 5, because we have taken a character array of size 5. However, it is useful when you are working with a large character array that is storing a string of any size that is less than the array size.
#include<stdio.h>
#include<string.h>
void main()
{
char word[10]={'h','a','p','p','y'};
int result;
result = strlen(word);
printf("%i", result);
}
http://codepad.org/zZSrlqvn
There are hundreds of pre-defined functions in C language that will aid you in simplifying the way you code. You don't have to reinvent the wheel—just use it. Some other .h files of interest to you are math.h and stdlib.h.
Congratulations! You made it to the end of the course.
Yay you!
Today's lesson will not talk about code the way we have so far. Instead, we are going to focus on how to continue with your coding journey even after the end of this course.
The first thing that you should know is that these nine days have given you a very selective and controlled glimpse into the vast world of C programming. I have deliberately avoided the “tough” topics and tried to make this introductory course as easy as I could.
This means that you will now be able to take further steps toward learning C with (hopefully) less apprehension, and you will find concepts easier to understand.
However, the over-simplification of most concepts in this course also means that there is a lot more to learn, even about stuff that we have already covered in this course.
The best way to learn coding is through coding. Yes, practice is the only way you will get a real handle on this language—or any other programming language, for that matter. For starters, play around with your code. Use the sample programs in this course and modify bits of them to better understand what is really going on.
I recommend that you join a coding website such as Codechef, HackerRank, etc, where you can complete coding assignments of increasing complexity, as well as go back to that book that was scaring you. I promise it'll seem less scary now.
If you plan to learn C further, it is advisable to get a C compiler right on your computer rather than rely on an online compiler. If you use Linux, you already have the gcc compiler on your computer and can get started straight away. For those of you who use Windows, some great compilers are DevC++ and TurboC++.
Some really good resources are available online for learning C, but the best resource for you to go through at this point would be the book Let Us C by Yashwant Kanetkar.
That's it, folks! I wish you a very happy journey into coding!