Saturday, June 9, 2012

Program in C Langauge




When we write a program we should follow some rules and regulations.
Rule 1: Every program should start with main().
Rule 2: Variables can't define the everywhere in the program, in the starting only we must define when the function is defined.
Rule 3: When we use the predefined functions/ library functions we must include the header file.
Rule 4: According to the Editors we must mention the header file.

First Program

#include<stdio.h>
main()
{
printf(" Welcome to the C Sample Code ");
getch();
}

Output:
Welcome to the C Sample Code

Explanation

stdio.h it is a header file used for input and output purpose
printf() it is a function used to display the statement in the output screen.
Syntax: printf("statement");
There is a rule for us every program is should start with main() and it is user defined function

Let us See the Next program accepting the integer value and display the value in the output screen


#include<stdio.h>
main()
{
int no;
printf("\n Enter a number: ");
scanf("%d",&no");
printf("\n You Entered Number is :  %d",no);
getch();
}

Output: 
Enter a Number: 34
You Entered Number is :  34

In the above program we used scanf(), %d" and \n.
scanf(): it is a function used to accept the values in the run time of the program.
Syntax: scanf("formatspecifier",&variable_name);

Format Specifier: it is used to accept the value or displaying the value in the program.
Format SpecifierDatatype
%cchar
%ffloat
%ddouble
%dint
%sstring
& is used to specify the address location for the variables (Future we discuss about the variables location in the pointer concept)

Escape Sequences: it is used to format the output screen in the c editor
\n  is used to specify for the new line.
\a  is used to specify for the beep sound.
\b  is used to specify for the backspace.
\t  is used to specify for the tab space.
\" is used to specify for the " in the output screen.
\r is used to specify for the home.
\* */comment lines

Let us see the next program accept two variables and find the sum of two numbers

#include<stdio.h>
main()
{
int a,b,c;
printf("\n Enter two numbers: ");
scanf("%d%d",&a,&b");/* accepting the values */

c =a+b ;/*adding the values of a and b and it is placed in the c variable */
printf("\n Sum of two values is  :  %d",c);
getch();
}

Output: 
Enter two numbers: 4
5
Sum of two values: 9

No comments:

Post a Comment