Professional Documents
Culture Documents
Chapter - 4.ppsx
Chapter - 4.ppsx
Chapter – 4
Output:
1. printf()
Processed Data/
Information/ Result
variable_name=getchar();
Example: char name;
name=getchar();
putchar(variable_name);
Example: char var=‘a’;
putchar(var);
putchar(‘\n’); \\new line
printf("Using getchar()\n");
printf("-------------------\n\n");
printf("Enter the value of a=>");
a=getchar();
printf("\n");
printf("Value of a is=>");
putchar(a);
printf("\n\n\n");
printf("Using scanf()\n");
printf("-------------------\n\n");
printf("Enter the value of b=>");
scanf("%c",&b);
printf("\n");
printf("Value of b is=>%c",b);
getch();
}
printf("Using getchar()\n");
printf("-------------------\n\n");
printf("Enter the value of a=>");
a=getchar();
printf("\n");
printf("Value of a is=>");
putchar(a);
printf("\n\n\n");
printf("Using scanf()\n");
printf("-------------------\n\n");
printf("Enter the value of b=>");
scanf("%d",&b);
printf("\n");
printf("Value of b is=>%d",b);
getch();
}
int a,b,c;
clrscr();
getch();
int a,b,c;
clrscr();
getch();
int a,b,c;
clrscr();
getch();
getch();
}
char n1[5],n2[5],n3[5];
clrscr();
printf("Enter value of n1=>");
scanf(" %5c",n1);
printf("\n n1=%5c",n1);
printf("\n----------------------------------------------\n");
printf("Enter value of n2=>");
scanf(" %s",n2);
printf(" n2=%s",n2);
printf("\n----------------------------------------------\n");
printf("Enter value of no and n3=>");
scanf(" %c",n3);
printf(" n3=%s",n3);
getch();
}
Chapter – 4 : Managing Input and Output Operation
Formatted Input: Inputting Character Strings
char n1[100];
clrscr();
getch();
}
char n2[100];
clrscr();
getch();
}
Chapter – 4 : Managing Input and Output Operation
Formatted Input: Inputting Character Strings
void main()
{
char n1[100];
clrscr();
getch();
}
C. An error is detected or
printf(“control string”,arg1,arg2,…,argn);
Example:
1. printf(“Hello World\n”);
2. printf(“x=%d\n”, x);
printf(“%d”,9876) 9 8 7 6
printf(“%6d”,9876) 9 8 7 6
printf(“%-d”,9876) 9 8 7 6
printf(“%06d”,9876) 0 0 9 8 7 6
The minus(-) and zero (0) are known as flags. Which is used for forceful left-
justified alignment
int a=12345;
clrscr();
printf("a=%d \n",a);
printf("a=%10d \n",a);
printf("a=%010d \n",a);
printf("a=%-d \n",a);
printf("a=%10d \n",-a);
getch();
}
Chapter – 4 : Managing Input and Output Operation
Output of Real Numbers
%w.p f
Minimum field Numbers to be read
width of output after decimal point
printf(“%7.4f”,y) 9 8 . 7 6 5 4
printf(“%7.2f”,y) 9 8 . 7 7
printf(“%-7.2f”,y) 9 8 . 7 7
printf(“%f”,y) 9 8 . 7 6 5 4
printf(“%10.2e”,y) 9 . 8 8 e + 0 1
printf(“%11.4e”,-y) - 9 . 8 7 6 5 e + 0 1
printf(“%-10.2e”,y) 9 . 8 8 e + 0 1
printf(“%e”,y) 9 . 8 7 6 5 4 0 e + 0 1
Another way….
printf(“%*.*f”,width,precision,number);
Example:
float a=98.7654;
clrscr();
printf("a=%f \n",a);
printf("a=%7.4f \n",a);
printf("a=%7.2f \n",a);
printf("a=%-7.2f \n",a);
printf("a=%07.2f \n",a);
printf("a=%*.*f \n",7,2,a);
printf("\n");
printf("a=%10.2e \n",a);
printf("a=%12.4e \n",-a);
printf("a=%-10.2e \n",a);
printf("a=%e \n",a);
getch();
}
Chapter – 4 : Managing Input and Output Operation
Output of Single Character
%w c
Minimum field Character to be
width of output read
By default Right-Justified
Default value of w is 1
By default Right-Justified
%s N E W D E L H I 1 1 0 0 0 1
%20s N E W D E L H I 1 1 0 0 0 1
%20.10s N E W D E L H I
%.5s N E W D
%-20.10s N E W D E L H I
%5s N E W D E L H I 1 1 0 0 0 1
char a='A';
char name[20]="ANIL KUMAR GUPTA";
clrscr();
printf("Output of the characters\n\n");
printf("%c\n%3c\n%5c\n",a,a,a);
printf("%3c\n%c\n\n",a,a);
printf("Output of the strings\n\n");
printf("%s\n",name);
printf("%20s\n",name);
printf("%20.10s\n",name);
printf("%.5s\n",name);
printf("%-20.10s\n",name);
printf("%5s\n",name);
getch();
}
wd printf(“%4d”,123);
How to input a sentence (words with space) in C language?
Chapter – 4
7 : Managing Input and Output Operation