Tuesday, June 16, 2015

Introduction to C Language and Programming (Part III , How to Read and Store Data in a Variable When Input and What are Operators)



When you want to input data, you have to use variables to store those data. To read those values which we input, we are using scanf function. By using that function, we can read the input and then pass that data to a variable.

To print the values, we can use printf function.

#include <stdio.h>

int main()
{
    int x1;

    printf("Enter a number: ");
    scanf("%d", &x1 );
    printf( "The value is : %d ", x1);
    printf( "\n");
    getchar();
    return 0;
}

Here we have declared x1” as a integer.
We have used “ printf("Enter a number: ");  “ to show your requirement.
Here by using this code scanf("%d", &x1 ); scanf function read the values and store in the variable x1, that we have declared before.
printf( "The value is : %d ", x1); Now we can see the output of the variable.
%d ” – It gives instruction to scanf function to read in an integer.

We are using arguments and mathematics in programming, below there are some examples by using variables to understand.

x = 1+2;    /* Add 1 and 2, then result assign to the x */
b = x * 4; /* Multiply variable x by 4 and assign the final value to b */
c = 10;     /* Assign the number 10 to c */

If you want to check variables are equal or not, you have to use == , it will not assign data to variables and only checks they are equal or not. There are some other operators greater than and less than
> and <

Here are some examples:

y > 10     /* It checks y is greater than 10 */
y > 5       /* It checks y is less than 10 */
z == 100  /* It checks z equals 100 or not*/ 

Try below code and see what's happening

#include <stdio.h>

int main()

{
    int x1,x2,x3;

    printf("Enter a number: ");

    scanf("%d", &x1 );
    printf( "The value is : %d ", x1);
    printf( "\n");

x2 = x1+5;
printf( "After add : %d ", x2);
printf( "\n");

x3 = x2;
printf("Trying this 1: %d ", x3);
printf( "\n");

x3 == 10;
printf("Trying this 2: %d ", x3);
printf( "\n");

    getchar();
    return 0;

}

0 comments:

Post a Comment

Facebook Fan Page

Popular Posts

Powered by Blogger.