Programming, website development forum Get latest updates by RSS Follow TechnicalTalk on Twitter Follow TechnicalTalk on Facebook 
HomeSearchRecent PostsLoginRegisterContact Us

Username  
Password    
  Forgot your password?  

Pages: [1]   Go Down
 
  Email this topic  |  Print
0 Members and 1 Guest are viewing this topic.

C programming in simple English with examples - Lesson 2

 
webmaster forum
Kevin  Offline
A computer Science student
Dream - To become a computer expert
Activity
20%
 
Skilled Coder
Real name: Kevin
Location: India
Gender: Male
Age: 18
Posts: 199
Topics: 51
kevin.cmaker
August 23, 2011, 12:41:33 PM



To learn the first lesson please visit, C program tutorial in simple English with examples - Lesson 1


Interaction with computer using C program:

In the last lesson, we learnt about how to make the computer to do a task without giving any input during the execution of the program. In this lesson, we are going to learn how to give an input to the computer and make it answer according to our input. Before that, let us take a look at variables and constants.



What are variables?

As we all know, variables are those whose values can be changed. Actually, a variable is most probably assigned (equals) a constant which is many times a number.

There are mainly two types of variables. They are

  • Numeric variable (holds numeric values)
  • Character variable (holds characters) - all digits between 0 and 9, alphabets from a to z and special characters like #, $, _, & are characters.

Examples of variables:

(i) age_of_people

(ii) a, b, c, etc.

(iii) favorite_juice

Note:

(i) Variables should not contain blank spaces in-between the characters. Eg: charge of an electron is not a variable, while charge_of_an_electron is a variable.

(ii) To avoid confusion, the empty spaces are generally replaced by underscores _ .

(iii) A variable name can start with an alphabet or with an underscore but not with a number.

  • mark_of_the_student is a variable.
  • _fathers_name is a variable.
  • 9rupee is not a variable as it starts with a number.



What are constants?

Constants are those whose values are fixed and cannot be changed.

Examples of constants:

  • The value of pi (3.14)
  • The mass of an electron (9.1x10-31kg)

But, what is the significance of these constants in C programming? Well, it has much importance in C. One of its significance is that if we are using a value with the name (charge_of_an_electron) through out a program as the charge of an electron, the value must be a constant through out the program and hence we will be warned if we change its value by mistake in any latter part of the program.



Example 1:

Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int a;          /*int refers to integer*/
int b;          /*in all the three statements, we are informing the computer that a, b and c are integer values*/
int c;        

a=5;
b=4;
c=a+b;          /*computer adds the values of a and b and saves it in c*/

printf("%d", c);          /*to display the saved value of c on the screen*/
getch();                    /*to hold the screen waiting to press any key to close the application*/
return 0;
}

Output:
Quote
9
In the above program, first we are declaring all the three variables as integers. Then the value of c is calculated out by adding a and b. The printf() function is used to display the result (value of c). A notable change in this program is in the printf() function.

Consider  printf("%d", c). The %d (d = integer) is used to say the computer that the value to be printed is an integer. But, yet it doesn't know what the value of the integer is, since there are three different integers a, b, c. Hence, c represents the value (a+b) to be typed.

Also note that, instead of declaring all the three variables a, b, c separately they can also be declared in the same line as follows.

Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int a, b, c;
               
a=5;
b=4;
c=a+b;        

printf("%d", c);        
getch();            
return 0;
}

This gives the same output as above. Note that the declarations should always come at the top.




Getting output by giving input : The scanf() function


Example 2:

So far, we had not been giving any input value during the execution of the program. But programming as such will not be much interesting unless we order the computer to do tasks by giving some input. Only for this, the scan() function is going to be used.

Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int a, b;       /*declaration part*/

scanf("%d", &a);       /*the system will wait to receive an input value from us*/
printf("The value that you have entered is %d", a);         /*this will print the value which was received from us*/

getch();
return 0;
}
Consider the value that you enter is 100

Output:
Quote
100
The value that you have entered is 100
In the above example, scanf() is used to scan (receive) the value from the programmer.

(i) %d means that the value to be given in is an integer.

(ii) &a indicates that the input value is going to be saved as a (In this case, a=100).

(iii) Also note that in scanf() function, we use & but not in printf() function.

Example 3:

We can also scan multiple values as follows:

Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int a, b, c;  
            
printf("Enter the first value : ");
scanf("%d", &a);    /*the %d value will be replaced by the value of a*/  
      
printf("Enter the second value : ");
scanf("%d", &b);    /*the %d value will be replaced by the value of b*/

c=a*b;              /*multiplies a and b*/

printf("The value of c is %d", c);

getch();

return 0;
}

Consider the input values are a=7 and b=8.

Output:
Quote
Enter the first value 7 Enter the second value 8
The value of c is 56



Interesting fact:

To have the screen cleared (removing all the inputs and outputs given previously), you can use the clrscr(); command in the beginning, just below the declaration part.


Code:
#include<stdio.h>
#include<conio.h>
int main()
{
  int a, b, c;

  clrscr(); /*Removes unwanted entries that were entered before*/

printf("Enter the first value ");
  scanf("%d", &a);

printf("Enter the second value ");
        scanf("%d", &b);

  c=a*b;
  printf("The value of c is %d", c);

  getch();
 
  return 0;
}



Other detailed concepts in scanf() will be given in the next lesson. Thanks for reading!

To learn the next lesson, please visit C program tutorial in simple English with examples - Lesson 3





You can find more information here :-



*** Please do not forget to leave your comments about this article ***

« Last Edit: January 04, 2012, 07:36:49 AM by Admin »

Glad to help.

A TechnicalTalk member!
 
  Email this topic  |  Print
Pages: [1]   Go Up
 
Jump to:  



Powered by SMF 1.1.15 | SMF © 2011, Simple Machines


Google visited last this page February 04, 2012, 05:23:00 PM