Getting started with C programming Public

Module Information

Show less

Show less

Peas in a Pod

Hello! I hope you’ve gotten the hang of using the different types of variables now. Now what happens if we need to store ten integer values? The usual way would be to name all ten variables one by one, like so: int a, b, c, d, e, f, g, h, i, j; Reminds me of kindergarten, but doable. Now what if we had to store 100 integer values? Okay, now that's a problem. Arrays to the rescue! An array is a group of variables of the same type. So, if we have an integer array of size 10, we could store 10 integers together without having to name each of them separately. We just need to name our array. Let's take an example: #include&lt;stdio.h&gt; void main() {     int a[10]={1,2,3,4,5,6,7,8,9,10};     printf("%d %d %d %d", a[1], a[2], a[4], a[7]); }http://codepad.org/pTBSKRCu This program creates an integer array called a, which is of size 10. So, it has the capacity to store 10 integers. The values that we want to store are given inside curly braces, with commas separating them. Did you notice something weird about the output? a[1] should ideally be displaying the first value in the array, but it is displaying the second value. Well, that is because in C, the arrays start from the index 0. So the first value—that is, 1—gets stored ina[0] and not a[1]. We can do everything with array elements that we would usually do with variables. #include&lt;stdio.h&gt; void main() {     int a[5] = {1,4,3,2,2};     printf("%d \n", a[0]+a[1]);     printf("%d", a[4]+a[3]); }http://codepad.org/x24bmHWg Here's a simple program to add all the integers in an array and display the result: #include&lt;stdio.h&gt; void main() {    int a[3] = {1001, 2002, 3003};    printf("%d", a[0]+a[1]+a[2]); }http://codepad.org/4RMO1yPX Just like we have used an integer array, we can use float arrays and character arrays too. #include&lt;stdio.h&gt; void main() {     char first_name[4] = {'A','r','y','a'};     char last_name[5] = {'S','t','a','r','k'};     printf("Initials: %c.%c. \n", first_name[0], last_name[0]);     printf("First Name: %s \n", first_name); }http://codepad.org/5iipaqEn See the second printf? We have used %s to print a whole character array. This is what we call a string—as in, a string of characters. We can do lots of fun stuff with strings, which is something that we will cover later in this course. Tomorrow, we will learn about the most commonly used operators in C. Till then, keep coding!
Show less

Show less

In a Loop

Hello! Welcome back to Getting Started with C Programming! Just a quick reminder: you are already halfway through the course. Yay you! When writing programs, sometimes we need to repeatedly perform the same operation. For example, you want your program to display the numbers from 1 to 100. One way to do that would be to use hundred printf statements. But you are coder, and you are going to use a loop statement. #include&lt;stdio.h&gt; void main() {     int number=1;     while (number&lt;=10)     {         printf("%i \n", number);         number = number + 1;     } }http://codepad.org/zetRhVhe This is one of the simplest loop structures. The computer checks if the number is less than or equal to 10. If it satisfies this condition, the number is displayed, and then incremented by 1. This process continues until the number 10 is displayed. Since the statement for incrementing the number by 1 is present immediately after the display statement, the number becomes 11 and the while loop is again executed. This time, the condition is not satisfied, so we come out of the while loop and go to the next line. As we don't have any other statement after this, the program ends when it reaches the final closing curly bracket. Another commonly used loop structure is the for loop. #include&lt;stdio.h&gt; void main() {     int counter, number=10;     for(counter=1 ; counter&lt;=10 ; counter=counter+1)         printf("%i \n", counter); }http://codepad.org/oCa This program does everything that the previous program did but is more compact. See the for statement. At first, the value 1 is assigned to the variable counter, followed by a semicolon to signal the end of value assignment. Then, the maximum value that counter is allowed to reach is specified. And finally, the last part increments counter by 1 for each iteration of the loop. The assignment of value to counter happens exactly once. Then the condition specified by the for statement is checked, and if the condition is satisfied, the computer performs the statements contained within the for loop—that is, in our case, display the value of counter. Next, the third and final part of the forstatement is performed; that is, counter is increased by 1, and the computer goes back to checking the condition specified by the second part of the for loop. This goes on until the condition is not satisfied, at which point we come out of the for loop and move to the next line in the program code. The for loop may seem slightly confusing at first, but once you get the hang of it, you will find yourself using it more than the while loop. See you tomorrow with more on loops. Till then, happy coding!
Show less

Building a Nest

In today's lesson, we are going to learn how to use one if statement within another, and then follow it up with how to use one for looping within another for loop. There are certain cases where we need to check for multiple conditions, as we have already seen with the AND (&amp;&amp;) operator. However, sometimes, the conditions themselves depend on other conditions. In such cases, we use one if statement within another. Let's take an example. #include&lt;stdio.h&gt; void main() {     int age=24, income=1000;     if(age&gt;=18)     {         if(income&lt;5000)             printf("You are eligible to vote and are exempted from paying tax.");         else             printf("You are eligible to vote.");     }     else         printf("You are not eligible to vote."); }http://codepad.org/8Rh5laje The else on line number 9 is paired with the if in line number 7, and the else in line number 12 is paired with the if in line number 5. Similarly, we can use one for loop within another for loop. #include&lt;stdio.h&gt; void main() {     int column1, column2;     for(column1=1 ; column1&lt;=5 ; column1=column1+1)     {         for(column2=1; column2&lt;=4 ; column2=column2+1)         {             printf("%i %i \n",column1, column2);         }     } }http://codepad.org/kvDENIG5 See the output. At first, the value of column1 is assigned as 1, and now, the value ofcolumn2 changes from 1 to 4. Note that 4 is the maximum value that column2 is allowed to take. Now, column2 is again incremented and becomes 5. However, since column2 is not allowed to take a value of 5, the condition being checked by the second for is not satisfied. This makes us come out of the second for loop and go back to the first for loop. Here, the value of column1 is incremented by 1, and we move to the next statement. In this next statement, the value 1 is again assigned to column2, and the same process of incrementing the value of column2 while the value of column1 remains unchanged continues until the result 2 4 is displayed. After this, the value ofcolumn1 is incremented again by 1. This process continues until the result 5 4 is displayed. After 5 4 is displayed, the value of column2 is incremented by 1, but we know that column2 cannot be 5. So this condition is not satisfied, and we move back to the first for loop. Here, the value of column1 is incremented by 1, but sincecolumn1 cannot hold the value 6, we come out of this for loop as well and move to the next statement, which, in this program, is the closing curly brackets that end the program. The second for loop is inside the first for loop, so we call the second for loop theinner loop and the first for loop the outer loop. The two programs we saw today had just one level of nesting, but you will probably use more than one level of nested statements as you progress to comparatively more advanced programs.
Show less

Show less

Raid the Library!

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&lt;stdio.h&gt; #include&lt;math.h&gt; 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&lt;stdio.h&gt; #include&lt;math.h&gt; 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&lt;stdio.h&gt; #include&lt;string.h&gt; 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&lt;stdio.h&gt; #include&lt;string.h&gt; 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.
Show less