Friday, May 30, 2014

Basic Sytanx of C

Some basic commands you should know while writing program in C .
Let write a C program which prints a line of text.

#include<stdio.h>
int main()
{
printf("This is my first C program");
return 0;
}

stdio.h

While writing program in C you should start with #include<stdio.h>.
This is header file, "stdio" means standard input output header. 

Printf

In C language "printf" command is used to display any message on screen.

scanf

In C language scanf is used to accept the user input from the keyboard. The command of scanf is :
scanf("%d",&base).
Let us take an example. We write a program that will calculate area of triangle and display the answer.
#include<stdio.h>
int main()
{
float area,base,height;
printf("n\Program to calculate the area of triangle");
printf("n\nPlease enter the value of base :")
scanf("%f",&base);
printf("Please enter the value of Height:")
scanf("%f",&height);
area=0.5*base*height;
printf("\n\nHeight=%f",height);
printf("\nBase=%f",base);
printf("\n0.5*Height*Base=Area");
printf("\n0.5*%f*%f=%f\n",height,base,area)'
return 0;
}

"%d" is used for integer data type.
"%f" is used for float data type.
"%c" is used for character data type.
"%s" is used for string data type.

Some other commands to remember

\n

\n is new line character. So whenever you want a newline you have to add a \n.

\t

\(horizontal tab) Moves the active position to the next horizontal tabulation position on the current line.

\a

is alert/bell

No comments:

Post a Comment