CONSOLE (I/O) FUNCTION

In C programming, console input and output are commonly performed using functions from the standard input/output library (stdio.h). The primary functions for console input and output are printf, scanf, getchar, and puts.

Formatted function

Type

i/p

o/p

Char

Scan f

Print f

Int

Scan f

Print f

Float

Scan f

Print f

String

Scan f

Print f

 Unformatted function

Type

i/p

o/p

Char

Getch()

Getche()

Getcher()

Putch()

Putche()

Putcher()

Int

-

-

Float

-

-

String

Gets()

Puts()

 

Formatted console i/o function

Print f and scan f are the formatted function . here we need to define the format value we are taking as input.

1.printf(“formatted string” , variable list);

2.int a=5;

3.printf(“%d”, a);

The format string that contain.

1.character are simply printed as they are.

2.conversion specification that begins with a “%” sign.

3.scope sequence that begins with a “/” sign.

#include<stdio>

int main()

{

  int avg=364;

  float per=69.2;

  printf(“average =%d/n percentage =%d/n”, avg, per);

  return 0;

}

Output:

average 346     

percentage    69.2

format specification

Datatype

 

Format specifier

Integer

Short signed

Short unsigned

Long signed

Long unsigned

Unsigned hexadecimal

Unsigned octal

%d

%u

%d

%lu

%x

%o

Real

Float

Double

Long double

%f

%lf

%lf

Character

Signed character

Unsigned character

%c

%c

String

 

%s

 

Specifier

Description

W

Digit specifying field with decimal point separating field with from precision

P

Digits specifying pression. Minus sign for left justifying the output in the specified field width.

Ex

#include<stdio.h>

int main()

{

  int weight=63;

  printf(“weight is %d kg/n”, weight);

  printf(“weight is %2d kg/n”, weight);

  printf(“weight is %4d kg/n”, weight);

  printf(“weight is %6d kg/n”, weight);

  printf(“weight is %-6d kg/n”, weight);

  printf(“weight is %1d kg/n”, weight);

  return 0;

}

 Escape sequence -> in c there are multiple sequence character like mention below

Escape seq

Purpose

Escape seq

Purpose

\n

New line

\t

Tab

\b

Back space

Br

Carriage return

\f

Form feed

\a

Alert

\’

Single quote

\”

Double quote

\\

Back slash

 

 

 Sprint and sscanf function

 sprint

#include<stdio.h>

int main()

{

  int i=10;

  char ch =’A’;

  float a=3.14;

  char str[20];

  printf(“%d %c %f\n”, I, ch, a);

  sprint(str, “%d %c %f\n”, I, ch, a);

  printf(“%s\n”, str);

  return 0;

}

 sscanf

#include<stdio.h>

int main()

{

  char input[17]=”GWG is 1 year old”;

  char name[20];

  char name1[10];

  int age;

  sscanf(input ”%s %s %d”, name, name1, age);

  printf(“%s”, name);

  printf(“%s”, name1);

  printf(“%d”, age);

  return 0;

}