0% found this document useful (0 votes)
7 views8 pages

04_Basic Input-Output

This document covers control structures in C programming, focusing on basic input/output functions provided by the C standard library. It details the usage of formatted output with the printf function, including its syntax, control strings, escape sequences, and reference specifiers. Additionally, it introduces the scanf function for reading formatted data from the keyboard and storing it in specified variables.

Uploaded by

sarthakpatel1128
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views8 pages

04_Basic Input-Output

This document covers control structures in C programming, focusing on basic input/output functions provided by the C standard library. It details the usage of formatted output with the printf function, including its syntax, control strings, escape sequences, and reference specifiers. Additionally, it introduces the scanf function for reading formatted data from the keyboard and storing it in specified variables.

Uploaded by

sarthakpatel1128
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

PROGRAMMING  

IN  C     UNIT  III  –  Control  Structures  

BASIC  INPUT  /  OUTPUT  


●   C  standard  library  provides  number  of  input  /  output  functions.  The  input  /  output  
functions  are  grouped  into  two  categories  are  detailed  shown  in  table,  

Functions   Unformatted   Formatted  


putchar()  
Output   printf()  
puts()  
getchar()  
Input   scanf()  
gets()  
 
A.   Formatted  Output  –  printf( ):-­‐    
●   C  provides  the   printf  function  is  used  to  display  information  required  by  the  user  
and  also  print  the  values  of  the  variables.  
●   The  general  form  and  syntax  of  printf  function  cab  be  given  as,  
Function  Name   Function  arguments  

Control  string  
Variable  list    
●   As   shown   in   the   above   syntax   the   printf   statement   consists   of   two   parts   are,  
Function   Name   and   the   Function   Arguments,   enclosed   in   parentheses   and   are  
separated  with  comma.  
●   The   print   formatting   function   named   with   printf   in   C   library   and   is   placed   in  
standard  input  out  header  file  i.e.  stdio.h  
●   The  function  arguments  are  further  divided  in  to  two  parts  are,  
i.   Control  String:-­‐  
●   The  parameter  control  string  in  the   printf  function  is  nothing  but  a  C  string  
that  contains  the  text  that  has  to  be  written  on  standard  output  device.  
●   Control  string  is  further  grouped  with  number  of  optional  parts  are,  
“[escape sequences] [text string] [reference specifiers]”

Escape  Sequences:-­‐    
Escape   sequences   are   actually   non   printing   control   characters   that   being   with  
backslash  (\).  There  are  various  characters  in  escape  sequence  set  that  supported  by  C  
language  are  shown  in  below  table.    
Escape   Escape  
Purpose  of  use   Purpose  of  use  
Sequence   Sequence  
\a Audible  Signal   \?   Question  mark  
\b Backspace   \\   Back  slash  
\t Tab   \’   Single  quote  
\n Newline   \”   Double  Quote  
\v Vertical  tab   \o   Octal  constant  
\f New  Page  or  Clear  screen   \x   Hexadecimal  constant  
\r Carriage  return      
 

    3.48   Prof.  V.  I.  Memon  


PROGRAMMING  IN  C     UNIT  III  –  Control  Structures  

Text  String:-­‐    
Control  string  may  also  contain  text  to  be  printed  like  instructions  to  the  user,  
captions,  identifiers,  or  any  other  text  to  make  the  output  readable.  In  some   printf  
statement  you  may  find  only  a  text  string  that  has  to  be  displayed  on  screen.  

Reference  Specifiers:-­‐    
  Reference  specifiers  are  also  known  as  Placeholders  or  Conversion  Character  or  
Format   Code   or   Format   specifier.   Are   used   at   the   time   of   display   the   value   of   the  
specified  variable  is  substituted  at  the  place  of  placeholder.  The  printf  function  uses  
different   placeholders   to   display   different   type   of   data   items  therefore   placeholders  
have  specific  format  with  specific  parts  are,  
%[flags] [width] [.precision] [length]Specifier

Each  placeholder  string  must  being  with  a  %  sign.  The  %  character  specifies  how  
the  next  variable  in  the  list  of  variables  has  to  be  printed.  After  %  sign  follows.  
 Flags  specify  output  justification  such  as  decimal  point,  numerical  sign,  training  
zeros  or  octal,  or  hexadecimal  prefixes.      
Flags   Description  
- Left-­‐justify  within  the  data  given  field  width  
+ Displays  the  data  with  its  numeric  sign  either  +  or  -­‐  
Used  to  provide  additional  specifiers  like  o,  x,  X,  0,  0x,  or  0X  for  octal  
#
and  hexadecimal  values,  respectively,  for  values  different  than  zero.  
0 The  number  is  left-­‐padded  with  zeros  (0)  instead  of  spaces.    

Width  specifies  the  minimum  number  of  characters  to  print  after  being  padded  
with   zeros   or   blank   spaces   i.e.   it   specifies   the   minimum   number   of   positions   in   the  
output.   If   data   needs   more   space   than   specified,   then   printf   overrides   the   width  
specified  by  the  user.  Width  is  a  very  important  field  especially  when  you  have  to  align  
output  in  columns.  

Precision  specifies  the  maximum  number  of  character  to  print  


●   For   integer   specifies   (d,   I,   o,   u,   x,   X)   precision   flag   specifies   the   minimum  
number  of  digits  to  be  written.    
●   For  character  strings,  precision  specifies  the  maximum  number  of  characters  
to  be  printed.    
●   For   floating   point   numbers,   the   precision   flag   specifies   the   number   of  
decimal  places  to  be  printed.  Its  format  can  be  given  as  .m  where  m  specifies  
the  number  of  decimal  digits.  

Length  field  can  be  explained  as  given  below.  

Length   Description  
h When  the  argument  is  a  short int  or  unsigned short int.  
l When  the  argument  is  along  int  or  unsigned  long int  for  integer  specifiers.  
L When  the  argument  is  a  long double(used  for  floating  point  specifiers)  

    3.49   Prof.  V.  I.  Memon  


PROGRAMMING  IN  C     UNIT  III  –  Control  Structures  

Specifier   is   used   to   define   the   type   and   the   interpretation   of   the   value   of   the  
corresponding  documents.  
Type   Qualifying  input  
c For  single  character.  
d For  decimal  value.  
f For  floating  point  numbers.  
E, e Floating  point  numbers  in  exponential  format.  
G, g Floating  point  numbers  in  the  shorter  of  e  format.  
i For  single  decimal  number.  
o For  octal  number.  
s For  a  sequence  of  (string  of)  characters.  
u For  unsigned  decimal  value.  
X, x For  hexadecimal  value.  
 
ii.   Variable  List:-­‐  
●   Variable  list  is  nothing  bit  the  list  of  all  variable  names  or  identifiers  in  same  sequence  
as  place  specifiers  in  the  control  string.  
 
Following  are  the  some  of  the  examples  of  the  printf  function.  

printf(“\n Result: %d%c%f”, 12, ‘a’, 2.3);


Result: 12a2.3

printf(“\n Result: %d %c %f”, 12, ‘a’, 2.3);


Result: 12 a 2.3

printf(“\n Result: %d\t%c\t%f”, 12, ‘a’, 2.3);


Result: 12 a 2.3

printf(“\n Result: %d\t%c\t%6.2f”, 12, ‘a’, 245.37154);


Result: 12 a 245.37

printf(“\n Result: %5d\t%x\t%#x”, 234, 234, 234);


Result: 234 EA 0xEA

printf(“\n The number is %4d”, 12);


The number is 12

printf(“\n The number is %4d”, 1234);


The number is 1234

printf(“\n The number is %4d”, 123456);


The number is 1234

printf(“\n The number is %06d”, 1234);


 

    3.50   Prof.  V.  I.  Memon  


PROGRAMMING  IN  C     UNIT  III  –  Control  Structures  

The number is 001234

printf(“\n The number is %-6d5”, 1234);


The number is 1234 5 //You will see the two blank spaces
in between 1234 & 5 due to -6
digit used

printf(“\n The price of this item %04.2d Rupees”,123.4567);


The price of this item 0123.45

printf(“\n This is \’so\’ wonderful”);


This is ’so’ wonderful

printf(“\n This is \”so\” wonderful”);


This is “so” wonderful

printf(“\n This is \\ so wonderful”);


This is \ so wonderful

printf(“\na=|%-+7.2f| b=%0+7.2f| c=%-0+7.2f”, 1.2, 1.2, 1.2);


a=+1.20 b=0001.20 c=1.20 /*here –means left justify, +means
display the sign, 7 specifies the
width, and 2 specifies the
precision */

char ch = ‘A’
printf(“\n %c \n%3c \n%5c”, ch, ch, ch);
A
A
A

char str[] = “Good Morning”


printf(“\n %s”, str);
printf(“\n %20s”, str);
printf(“\n %20.10s”, str);
printf(“\n %.7s”, str);
printf(“\n %-20.10s”, str);
printf(“\n %7s”, str);
Good Morning
Good Morning
Good Morni
Good Mo
Good Morni
Good Morning

 
 
 
 

    3.51   Prof.  V.  I.  Memon  


PROGRAMMING  IN  C     UNIT  III  –  Control  Structures  

B.   Formatted  Output  –  scanf(  ):-­‐    


●   C  provides  the   scanf  function  which  stands  for  scan  formatting  and  is  used  to  read  
formatted   data   from   the   keyboard   and   then   stores   the   data   in   specified   program  
variable.  
●   The  general  form  and  syntax  of  scanf  function  cab  be  given  as,  

Function  Name   Function  arguments  

Control  string   Variable  list  


 
●   As  shown  in  the  above  syntax  the  scanf  statement  consists  of  two  parts  are,  Function  
Name  and  the  Function  Arguments,  enclosed  in  parentheses  and  are  separated  with  
comma.  
●   The  scan  formatting  function  named  with  scanf  in  C  library  and  is  placed  in  standard  
input  out  header  file  i.e.  stdio.h  
●   The  function  arguments  are  further  divided  in  to  two  parts  are,  

i.   Control  String:-­‐  
●   The  parameter  control  string  specify  the  type  and  format  of  the  data  that  has  to  
be  obtained  from  the  arguments   Name, RollNo and Percentage i.e.  the  
arguments  are  actually  the  variable  addresses  where  each  piece  of  data  are  to  
be  stored.  
●   The  prototype  of  the  Control  string  can  be  given  as,  

“%[*][width][modifiers]type”

Here   *   is   an   optional   argument   that   suppresses   assignment   of   the   input   field,   i.e.   it  
indicates   data   should   be   read   from   the   stream   but   ignored   (not   stored   in   memory  
location)  

Width  is  an  optional  argument  that  specifies  the  maximum  number  of  characters  to  be  
read.  However,  fewer  characters  will  be  read  if  the  scanf  function  encounters  a  white  
space  or  an  inconvertible  character  because  the  moment   scanf  encounters  a  white  
space  character  it  will  stop  processing  further.    

Modifiers  are  an  optional  argument  that  can  be  h,  l,  or  L  for  the  data  pointer  by  the  
corresponding  additional  arguments.    

Modifier
Description  
s  
h When  the  argument  is  a  short int  or  unsigned short int.  
l When  the  argument  is  along  int  or  unsigned  long int  for  integer  specifiers.  
L When  the  argument  is  a  long double(used  for  floating  point  specifiers)  

    3.52   Prof.  V.  I.  Memon  


PROGRAMMING  IN  C     UNIT  III  –  Control  Structures  

Type  specifies  the  type  of  data  that  has  to  be  read.  It  also  indicates  how  this  data  is  
expected  to  be  read  from  the  user.  The  type  specifiers  for  scanf  function  are  given  in  
below  table.  

 Type   Qualifying  input  


c For  single  character.  
d For  decimal  value.  
f For  floating  point  numbers.  
E, e Floating  point  numbers  in  exponential  format.  
G, g Floating  point  numbers  in  the  shorter  of  e  format.  
i For  single  decimal  number.  
o For  octal  number.  
s For  a  sequence  of  (string  of)  characters.  
u For  unsigned  decimal  value.  
X, x For  hexadecimal  value.  
 
●   The  scanf  function  ignores  any  blank  spaces,  tabs,  and  newlines  entered  by  the  user.  
Function  simply  returns  the  number  of  input  fields  successfully  scanned  and  stored.    
●   The   scanf   function   is   used   to   store   values   in   memory   locations   associated   with  
variables.  For  this  the  function  should  have  address  of  the  variables.    
●   The  address  of  the  variable  is  denoted  by  an  ‘&’  sign  followed  by  the  name  of  the  
variable.  
●    There  are  some  rules  to  use  a  scanf  function  in  our  C  programs  are,  
Rule  1:  The  scanf  function  works  until,  
(a)   The  maximum  number  of  characters  has  been  processed.  
(b)   A  white  space  character  is  encountered,  or  
(c)   An  error  is  detected.  
Rule  2:  Every  variable  that  has  to  be  processed  must  have  a  conversion  specification  
associated  with  it.  Therefore,  the  following  scanf  statement  will  generate  an  error  
as  num3  has  no  conversion  specification.  
scanf(“%d %d”, &num1, &num2, &num3);

Rule  3:  There  must  be  a  variable  address  for  each  conversion  specification.  Therefore,  
the  following  scanf  statement  will  generate  an  error  as  no  variable  address  is  given  
for  the  third  conversion  specification.  
scanf(“%d %d %d”, &num1, &num2,);

Remember  that  the  ampersand  operator  (&)  before  each  variable  name  specifies  the  
address  of  that  variable  name.  
Rule  4:  A  fatal  error  would  be  generated  if  the  format  string  is  ended  with  a  white  
space  character.    
Rule  5:  The  data  entered  by  the  user  must  match  the  character  specified  in  the  control  
string,  otherwise  an  error  will  be  generated  and  scanf  will  stop  its  processing.  For  
example,  consider  the  scanf  statement  given  below.  
scanf(“%d / %d”, &num1, &num2,);

    3.53   Prof.  V.  I.  Memon  


PROGRAMMING  IN  C     UNIT  III  –  Control  Structures  

Here,  the  slashes  in  the  control  string  are  neither  white  space  characters  not  a  part  of  
conversion  specification,  so  the  user  must  enter  data  of  the  form  21/46.      
 
Rule  6:  Input  data  values  must  be  separated  by  spaces.  
Rule  7:  Any  unread  data  value  will  be  considered  as  a  part  of  the  data  input  in  the  next  
call  to  scanf.  
Rule  8:  When  the  field  width  specifier  is  used,  it  should  be  large  enough  to  contain  
the  input  data  size.  Look  at  the  code  given  below  that  show  how  we  input  values  in  
variable  of  different  data  types.  

    int num;
scanf(“ %d ”, &num);
The  scanf  function  reads  an  integer  value  into  the  address  or  the  memory  
location  pointed  by  num.

float salary;
scanf(“ %f ”, &salary);  
The  scanf  function  reads  a  floating  point  number  into  the  address  or  the  
memory  location  pointed  by  salary.

char ch;
scanf(“ %c ”, &ch);  
The   scanf   function   reads   a   single   character   into   the   address   or   the  
memory  location  pointed  by  salary.

char str[10];
scanf(“ %s ”, str);  
The   scanf  function  reads  a  string  or  a  sequence  of  characters  into  the  
address  or  the  memory  location  pointed  by  str. Note  that  in  case  of  
string,  we  do  not  use  the  &  sign  in  the  scanf  function.  
Look  at  the  code  given  below  which  combines  reading  of  variable  of  different  data  types  in  one  
single  statement.  

int num;
float salary;
char ch;
char str[10];
scanf(“ %d %f %c %s ”, &num, &salary, &ch, str);
 
Following  are  the  some  of  the  examples  of  the  scanf  /  printf  function.  

int num;
scanf(“ %d ”, &num);
printf(“\n Number: %d”, num);
32455
Number: 32455

float salary;
scanf(“ %f ”, &salary);
printf(“\n Salary: %.2f”, salary);
 

    3.54   Prof.  V.  I.  Memon  


PROGRAMMING  IN  C     UNIT  III  –  Control  Structures  

12345.500
Salary: 12345.50

char ch;
scanf(“ %c ”, &ch);
printf(“\n Character: %c”, ch);
A
Character: A

char str[10];
scanf(“ %s ”, str);
printf(“\n String: %s”, str);
ABCDef
String: ABCDef

1.   Program  64:  Find  out  the  output  of  the  program.   Output  Window
1 #include <stdio.h>
2 #include<conio.h> Enter two four Digit
3 int main() Numbers: 1234 5678
4 {
5 int a, b;
The two four Digit Numbers
6 printf(“\nEnter two four Digit Numbers:”);
are: 12 and 34
7 scanf(“%2d %4d”, &a, &b);
8 printf(“\nThe two four Digit Numbers are: %d
and %d”, a, b);
9 getch();
10 return 0;
11 }
 
2.   Program  65:  Program  to  calculate  the  area  of  a  triangle  using  Hero’s  Formula.   Output  Window
1 #include <stdio.h>
2 #include<conio.h>
3 #include<math.h> Enter the length of three
4 int main() sides of the triangle:33
5 { 40 50
6 float a, b, c, area, s;
The Area of Triangle:=
7 printf(“\nEnter the length of three sides of the
658.306519
triangle:”);
8 scanf(“%f %f %f”, &a, &b, &c);
9 s = (a + b + c) / 2;
10 area = sqrt(s * (s – a) * (s – b) * (s – c));
11 printf(“\nThe Area of Triangle:= %f”, area);
12 getch();
13 return 0;
14 }
 
 
 

    3.55   Prof.  V.  I.  Memon  

You might also like