0% found this document useful (0 votes)
6 views

Input Output Functions in C

Uploaded by

Kirtimaya Swain
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Input Output Functions in C

Uploaded by

Kirtimaya Swain
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

06-09-2024

Input Output Functions


in C
Dr. Madhusmita Sahu
Associate Professor
Department of Computer Science and Information Technology

Topics to be discussed

 Header File
 Standard Library
 Syntax
 Semantics
 Operations

1
06-09-2024

Header File

 A header file is a file with extension .h which contains C


function declarations and macro definitions to be shared
between several source files.
 There are two types of header files:
 the files that the programmer writes and
 the files that comes with your compiler.
 You request to use a header file in your program by
including it with the C preprocessing directive #include

Header File

 Including a header file is equal to copying the content of


the header file but we do not do it because it will be
error-prone and it is not a good idea to copy the content
of a header file in the source files, especially if we have
multiple source files in a program.
 A simple practice in C or C++ programs is that we keep all
the constants, macros, system wide global variables, and
function prototypes in the header files and include that
header file wherever it is required.

2
06-09-2024

Purposes of Header files

 System header files declare the interfaces to parts of the


operating system.
 You include them in your program to supply the definitions and
declarations you need to invoke system calls and libraries.
 Your own header files contain declarations for interfaces
between the source files of your program.
 Each time you have a group of related declarations and macro
definitions all or most of which are needed in several different
source files, it is a good idea to create a header file for them.

Standard Library

 The C standard library is a standardized collection of header files and


library routines used to implement common operations, such as
input/output and character string handling.
 Unlike other languages (such as COBOL, Fortran, and PL/I) C does not
include built-in keywords for these tasks, so nearly all C programs rely
on the standard library to operate.
 The C Standard Library is a set of C built-in functions, constants and
header files like <stdio.h>, <stdlib.h>, <math.h>, etc.
 This library will work as a reference manual for C programmers.

3
06-09-2024

Syntax

 In linguistics, syntax is the set of rules, principles, and


processes that govern the structure of sentences in a
given language, specifically word order and punctuation.
 In computer science, the syntax of a computer language is
the set of rules that defines the combinations of symbols
that are considered to be a correctly structured document
or fragment in that language.

Semantics

 Semantics is the study of the meaning of language.


 It also deals with varieties and changes in the meaning of words, phrases,
sentences and text.
 In programming language theory, semantics is the field concerned with the
rigorous mathematical study of the meaning of programming languages.
 It does so by evaluating the meaning of syntactically legal strings defined by
a specific programming language, showing the computation involved.
 In such a case that the evaluation would be of syntactically illegal strings,
the result would be non-computation.
 Semantics describes the processes a computer follows when executing a
program in that specific language.
 This can be shown by describing the relationship between the input and
output of a program, or an explanation of how the program will execute on a
certain platform, hence creating a model of computation.

4
06-09-2024

Difference between syntax and


semantics
 Syntax is about the structure or the grammar of the language.
 It answers the question: how do I construct a valid sentence? All
languages, even English and other human (aka "natural") languages
have grammars, that is, rules that define whether or not the sentence
is properly constructed.
 Here are some C language syntax rules:
 separate statements with a semi-colon
 enclose the conditional expression of an If statement inside parentheses
 group multiple statements into a single statement by enclosing in curly
braces
 data types and variables must be declared before the first executable
statement

Difference between syntax and semantics


 Semantics is about the meaning of the sentence.
 It answers the questions:
 is this sentence valid?
 If so, what does the sentence mean?
 For example:
 x++; // increment
 foo(xyz, --b, &qrs); // call foo
 are syntactically valid C statements.
 But what do they mean?
 Is it even valid to attempt to transform these statements into an
executable sequence of instructions?
 These questions are at the heart of semantics.

5
06-09-2024

Difference between syntax and semantics


 Consider the ++ operator in the first statement.
 First of all, is it even valid to attempt this?
 If x is a float data type, this statement has no meaning (according to
the C language rules) and thus it is an error even though the
statement is syntactically correct.
 If x is a pointer to some data type, the meaning of the statement is
to "add sizeof(some data type) to the value at address x and store
the result into the location at address x".
 If x is a scalar, the meaning of the statement is "add one to the value
at address x and store the result into the location at address x".

Difference between syntax and semantics


 Finally, note that some semantics cannot be determined at
compile-time and must therefore must be evaluated at run-time.
 In the ++ operator example, if x is already at the maximum value
for its data type, what happens when you try to add 1 to it?
 Another example: what happens if your program attempts to
dereference a pointer whose value is NULL?
 In summary, syntax is the concept that concerns itself only
whether or not the sentence is valid for the grammar of the
language.
 Semantics is about whether or not the sentence has a valid
meaning.

6
06-09-2024

Types of Operations

Operation

Input Output

Input/Output operations
 I/O operation in C language is performed through set of library
function supplied with complier.
 There are set of header file which provides various library
function
 A common used header file in C programming is stdio.h .
 It is called standard input output header file.
 The set of library functions that perform input-output operation
is known as standard input/output library (stdio.h)
 Inside this header file there are two common functions.
 printf() and scanf()

7
06-09-2024

Reading a Character

 getchar();

 Accepts any character keyed in including


 return (enter)
 tab space

 Ex:
char variable_name;
variable_name=getchar();

Writing a Character

 putchar(var_name);

 Displays char represented by var_name on the terminal

 Ex:
char c=getchar();
putchar(c);

8
06-09-2024

scanf()
 This function is used for input purpose.
 This function is used to read some data from the keyboard
and store it in the variable.
 Syntax: scanf(“control string”, address of the variable);
 Ex:-
 int x; /* Declaration of an integer variable */
 scanf(“%d”, &x); /* Input from keyboard for the variable x
*/
 printf(“%d”, x); /* To print the value of the variable x */

scanf()

 Each variable must have a field specification


 For each field specification there must be
variable address
 The scanf reads until
A white space is found in numeric specification
 the maximum number of characters have been read
 An error is detected
 The end of file is reached

9
06-09-2024

Conversion Specifications
•The format specifier or the Conversion specification used by the printf() and scanf()
function specifies the type and size of data.
•Each format specifier must begin with a % sign .
Specifier meaning
%c a single character
%d or %i decimal integer
%f or %e or %g floating point number
%lf long range floating point
(double)
%Lf long double
%h short int
%s string
%u unsigned decimal integer
%o octal integer
%x hexadecimal
%[…] Read a string of words

Examples

 int marks;
 scanf(“%d”,&marks);

 int basic,da;
 scanf(“%d%d”,&basic,&da);

 float x;
 scanf(“%f”,&x);

 double y;
 scanf(“%lf”,&y);

10
06-09-2024

N.B
 The modifier h can be used before conversion specifications d,i,o,u,x to specify
short integer.
 The modifier l can be used before them to specify a long integer.
 The modifier l can be used before conversion specification f,e,g to specify
double .
 The modifier L can be used before f,e,g to specify long double.

Example:
%ld
%hd
%Lf
%hx

printf()

 This function is used for output purpose. This function


takes an argument and display it in the console.
 syntax: printf(“format specifier”, variable);
 Ex: printf(“Hello”); /* Used to display the string Hello*/

11
06-09-2024

Examples

 printf(“Programming in C”);
 printf(“\n”);
 printf(“%d”,x);
 printf(“x=%d\n”,x);
 printf(“The value of a is %d”,a);

 printf does not supply new line automatically.


 Thus ‘\n’ is used

Integer Examples

 printf(“%d”,9678); 9 6 7 8

 printf(“%6d”,9678); 9 6 7 8

 printf(“%2d”,9678); 9 6 7 8

 printf(“%-6d”,9678); 9 6 7 8

 printf(“%06d”,9678); 0 0 9 6 7 8

12
06-09-2024

Real Examples

 Syntax: %w.pf
 w indicates the number of digits used for display
 p indicates the number of digits to be displayed after decimal
 Let y=98.7654;

 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

String Examples

 Syntax: %w.ps
 w specifies width of field
 p specifies only first p characters of string are displayed

 Ex:
 char a[20]=“Hello World”;
 printf(“%s”,a);
H e l l o W o r l d

13
06-09-2024

Statements

 In a C program instructions are written in the form of


statements.
 Statements can be categorized as :
 i. Expression statements
 ii. Compound statements
 iii. Selection statements( if, if.. .else, switch)
 iv. Iterative statements (for, while, do…while)
 v. Jump Statements(goto, continue, break, return)
 vi. Label statements(case, default, label statement
used in goto)

Comments

 Comments are used for increasing readability of the


program.
 They explain the purpose of the program and are helpful
in understanding the program.
 Comments are written inside /* and */ (Multiline
comment)
 We can write comments anywhere in a program.
Example:
/* This is a C program to calculate simple interest*/

14
06-09-2024

Example 1

/*This program prints Hello World*/


#include<stdio.h>
main()
{
printf(“Hello World”);
}

Example 2
/*This program calculates area of a rectangle*/
#include<stdio.h>
main()
{
float length,breadth,area;
printf(“Enter length: ”);
scanf(“%f”,&length);
printf(“Enter breadth: ”);
scanf(“%f”,&breadth);
area=length*breadth;
printf(“Area of rectangle is: %f”,area);
}

15

You might also like