Loading [MathJax]/jax/output/HTML-CSS/fonts/TeX/fontdata.js

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<stdio.h>
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<stdio.h>
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<stdio.h>
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<stdio.h>
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!

Welcome back!

Today's lesson will focus on commonly used operators that you will need for writing Cprograms. But before we do that, we are also going to get familiar with something called if statements.

An if statement checks whether a particular condition is true and performs the corresponding action. Let us take an example:


#include<stdio.h>
void main()
{
    int a=5;
    if (a>4)
        printf("a is greater than 4");
}
http://codepad.org/s8gh7YhE


This program checks if the value of a is greater than 4, and if it is indeed greater than4, it will do whatever is written in the statement immediately afterward and before the next semicolon. You can read it as “if a is greater than 4, display 'a is greater than 4'”.

See the > symbol? It is an operator. The equality symbol (=) is also an operator.

Operators are symbols that tell your computer to perform a specific operation. For example, the + operator performs addition.

List of common operators:

< : less than
<= : less than or equal to
>= : greater than or equal to
!= : not equal to
== : equality comparison

The equality comparison operator (==) is used to compare two values, while the equality operator (=) is used to assign a value. Let me clarify with an example:


#include<stdio.h>
void main()
{
    int a=5;
    if (a==5)
        printf("a is equal to 5");
}
http://codepad.org/1HLFcDO4


Note that when we write a=5 we are assigning the value 5 to the variable a, but when we write a==5, we are checking whether or not the value of a is equal to 5. A common mistake is when people write if(a=5) instead of if(a==5). What happens if you do this is the value 5 gets assigned to the variable a instead of checking whether or not the value of a is equal to 5, which ultimately leads to incorrect results.

Other commonly used operators that you should know about are the logical AND and OR operators.

The AND operator is specified by the symbol && and checks whether both the conditions specified are true, while the OR operator is specified by the symbol ||and checks if at least one of the conditions specified is true.

Let's see an example:


#include<stdio.h>
void main()
{
    int a=5;
    if(a>0 && a<10)
        printf("This is a positive non-zero number less than 10");
}
http://codepad.org/9uVBShRZ


Now what happens if we need to do two different things based on whether or not a condition is satisfied? Well, C has got your back. Just use the else keyword.


#include<stdio.h>
void main()
{
    int a=5;
    if(a==10)
        printf("Number is equal to 10");
    else
        printf("Number is not equal to 10");
}
http://codepad.org/a02qfnNZ


You can make your program do more than one operation if it satisfies a condition. In such cases, you need to enclose all the statements within curly braces.


#include<stdio.h>
void main()
{
    int number1=10, number2=5;
    if(number2!=0)
    {
        printf("%i \n", number1/number2);
        printf("Division successful!");
    }
    else
        printf("Cannot divide by zero!");
}
http://codepad.org/hrdk5oFk


That's all for today. Look out for tomorrow's email for the next lesson. Till then, keep practicing.

Peas in a Pod

Diego Melo
Module by Diego Melo, updated more than 1 year ago
No tags specified