Writing first c program
for writing a c program let us understand programs structure with basic function let
us see-:
#include<stdio.h>
#include<conio.h>
int main()
{
clrscr();
int a;
scanf("......................."); // taking input by user
printf(".......................\n"); /* print the output on screen */
return 0;
}
#include<stdio.h>
This is the first statement in our code that includes a file called stdio.h. This file has some in-built functions. By simply including this file in our code, we can use these functions directly. stdio basically stands for Standard Input/Output, which means it has functions for input and output of data like reading values from the keyboard and printing the results on the screen. scanf() and printf() these are input and output function in c programs.
main function
int main() Every C program contains a main() function which is the starting point of the program. Int is the return value of the main function. All the statements in the program have been executed just after the main function, the last statement of the program will return an integer value to the operating system. If you do not understand certain things, do not worry you will be understand any further. { } The two curly brackets are used to group all the related statements of the main function.
scanf fuction
Scanf(.......................); The scanf function is defined in the stdio.h file and is used to take the data by
the user through the keyboard.
printf funtion
printf("......................\n "); The printf function is defined in the stdio.h file and is used to print text on the screen. The message that has to be displayed on the screen is enclosed within double quotes and
put inside brackets.
\n is an escape sequence and represents a newline character. It is used to print the message on a new line on the screen.
return function
return 0; This is a return command that is used to return value 0 to the operating system to give an indication that there were no errors during the execution of the program.
#include<stdio.h> header file
#include<conio.h>; In this header file conio.h stand for consold input output header. In the header file clrscr() and getch() function are defined.
clrscr function
clrscr(); The function is defined in #include<conio.h> header file .It is used clear all the data that present on the screen if when program was executed previous time.
getch function
getch(); The function is defined in #include<conio.h> header file. It is used to hold the output on the screen. Some compilers not able to hold the output on the screen without using it for example - turbo c++ .
Note-: Every statement in the main function ends with a semi-colon (;).
Using Comments
Comments are a way of explaining what a program does. C supports two types of comments.
1.) // is used to comment a single statement.
2.) /* is used to comment multiple statements. A /* is ended with */ and all statements that lie between these characters are commented.
//sum of two numbers
#include<stdio.h>
int main()
{
int a,b,sum; // variable declaration
a=5;
b=10;
sum=a+b;
printf("sum is %d",sum); /*sum of two numbers*/
return 0;
return 0;
Format Specifier
The format specifiers are used in C for input and output purposes. Using this concept the compiler can understand that what type of data is in a variable during taking input using the scanf() function and printing using printf() function.
%d
|
signed integer
|
%i
|
unsigned integer
|
%u
|
signed integer
|
%c
|
character
|
%f
|
float values
|
%s
|
string
|
%x
|
octal representation
|
%p
|
pointer
|
%o
|
octal representation
|
Example of program-;
Program on addition of two numbers;
#include<stdio.h>
#include<conio.h>
int main()
{
clrscr();
int a,b,C;
printf("Enter any two numbers");
scanf("%d%d",&a,&b);
C=a+b; // sum of a and b store in C
printf("%d",C);
return 0;
}
Note-:
there are two work of printf function -:
1. print at it is that written in double quates in case:
printf("hello");
2. print the value of of variable at place of %d in case:
printf("%d",a);
3. In the scanf function (&) ampersand help to taken value.
5 Comments
Thanks
ReplyDeleteth
ReplyDeleteth
ReplyDeleteth
ReplyDeleteThat is enormous effort
ReplyDeleteAny query comment in comment box thank you
Emoji