Wednesday, 7 February 2018

printf() and scanf() functions In C

How to write C program?
Example:
#include <stdio.h>   
int main(){   
printf("Welcome to LearningPoint92");   
return 0;  
OR
#include <stdio.h>  
#include<conio.h>
void main(){
clrscr();  
printf("Welcome to LearningPoint92\n");   
getch();
Output:
Welcome to LearningPoint92
Explanation:
#include<stdio.h>: includes the standard input output library functions and printf(),scanf() function is defined in stdio.h
#include<conio.h> : includes the console input output library functions and clrscr(),getch()function is defined in conio.h
void: it does not have return type.
int:it will return integer value.
main() : main() function is the entry point of every program and the flow of programs can be started from main() function.
clrscr():it is used to clear the previous output or screen.
getch():It is used to hold the program execution.
printf() : printf() function is used to print or display data .
\n: It is used to insert a new line characters.
return 0 :return 0 value is used for successful execution and return 1 for unsuccessful execution.

How to compile and run the c program
press ctrl+f9 keys compile and run the program directly.
alt+f5  keys is used to view the user screen.
Alt+Enter for maximize the screen.
press Esc to return to the turbo c++ console.

printf() and scanf() function
printf() and scanf()  functions are predefined library functions which is defined in stdio.h (header file).

 printf() function
 printf() function is used for display the output.
 Syntax
 printf("format string",argument_list); 
 printf("Addition is :%d",(fstNo+ secNo));
 format string can be %d (integer), %f (float), %c (character),,%s (string) etc.

scanf() function
scanf() function is used for taking input from user.
It reads the input data from the console.
Syntax:
scanf("format string",argument_list);  
scanf("%d%d", &fstNo, &secNo);


 Example:
#include <stdio.h>
#include <conio.h>
void main()
{
    int fstNo, secNo;
      clrscr();
     printf("Enter two Numbers:\n");
      scanf("%d%d", &fstNo, &secNo);
     printf("Addition is :%d",(fstNo+ secNo));
      getch();
}
Output:
Enter two Numbers
7
8

Addition is :15

No comments:

Post a Comment

apply function in R

1) apply function: It takes 3 arguments matrix,margin and function.. Example: m<-matrix(c(1,2,3,4),nrow=2,ncol=2) m #1 indicates it is ap...