C Tutorials
- What is C?
- History Of C?
- Feature of C?
- How to Install C?
- How to write c program?
- First c program
- Flow of C program
- printf() and scanf() in C
- C Program Structure
- Variables in C
- Data Types in C
- Keywords in C
- C Operators
- Comments in C
- Escape Sequence in C
- Escape Sequence in C
- Constants in C
- C - Storage Classes
- C - Decision Making
- Switch statement in C
- C Loops
- Loop Control Statements
- Type Casting in C
- functions in C
- call by value Or call by reference
- Recursion in C
- Storage Classes in C
- Array
- String
- Pointer
- Pointer And Array
- Pointer with function
- Structure
- Union
- File Handling
- Preprocessor Directives
Important Links
printf() and scanf() in C
The printf() function is a predefined standard 'c' function.
The printf() function is used for output. It prints the given statement to the console.
The printf() function prints all types of data values to the console. it requires conversion symbol and variable names to print the data.
Syntax of printf() function
printf("format string",argument_list);
The format string can be %d (integer), %c (character), %s (string), %f (float) etc.
Example :-
#include<stdio.h>
#include<conio.h>
void main()
{
int x = 2;
float y = 3.14;
char z = "M";
printf("%d %f %c",x,y,z);
}
output:
2 3.14 M
Example :-
#include<stdio.h>
#include<conio.h>
void main()
{
int x = 65;
printf("%c %d",x,x);
}
output:
A 65
scanf() function :-
The scanf() function is used for input. The scanf() function reads all types of data values. it is used for runtime assignment of varilables. It reads the input data from the console.
scanf("format string",argument_list);
scanf("%d %f %c",&a,&b,&c);
The format string can be %d (integer), %c (character), %s (string), %f (float) etc.
The scanf() funtcion requires '&' operator called address operator. the address operator prints the memory location of the variable. the scanf() function also returns values. the return value is equal to the number of values correctly used.
Example :-
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
printf("Enter value of A : ");
scanf("%d", &a);
printf("%d",a);
getch();
}
Example :-
It gets input from the user and prints the cube of the given number.
#include<stdio.h>
int main(){
int number;
printf("enter a number:");
scanf("%d",&number);
printf("cube of number is:%d ",number*number*number);
return 0;
}
Output :-
cube of number is:125
The scanf("%d",&number) statement reads integer number from the console and stores the given value in number variable.
The printf("cube of number is:%d ",number*number*number) statement prints the cube of number on the console.
Program to print sum of 2 numbers :-
#include<stdio.h>
int main(){
int x=0,y=0,result=0;
printf("enter first number:");
scanf("%d",&x);
printf("enter second number:");
scanf("%d",&y);
result=x+y;
printf("sum of 2 numbers:%d ",result);
return 0;
}
Output :-
enter first number:9 enter second number:9 sum of 2 numbers:18