0% found this document useful (0 votes)
0 views76 pages

PPS Module 1

The document provides a comprehensive introduction to C programming, detailing its history, structure, and fundamental concepts such as data types, variables, and program statements. It emphasizes C as a procedural, structured, and mid-level programming language, often referred to as the 'mother language' of modern programming. Key sections include the structure of a C program, variable creation, data types, and the various types of statements and tokens used in C.
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)
0 views76 pages

PPS Module 1

The document provides a comprehensive introduction to C programming, detailing its history, structure, and fundamental concepts such as data types, variables, and program statements. It emphasizes C as a procedural, structured, and mid-level programming language, often referred to as the 'mother language' of modern programming. Key sections include the structure of a C program, variable creation, data types, and the various types of statements and tokens used in C.
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

PROGRAMMING IN

C LANGUAGE
1
Module 1

INTRODUCTION TO C PROGRAMMING
Basics of C programming: Introduction , Structure of C program , Concept of variable.

Datatypes in C, Program Statement , Declaration , Storing the data in memory , C Tokens,


Operators and Expressions , Lvalues and R values , Type Conversion in C
INTRODUCTION TO C
▪ C is a procedural programming language initially developed by Dennis Ritchie in Feb
1972 at AT&T Bell Laboratories.
▪ It is used for creating system applications that directly interact with the hardware devices
such as drivers and kernels.
▪ C programming is considered as the base for other programming languages, that is why
we call c as mother language.
C can be defined as (features/properties):
1. Mother language
2. System programming language
3. Procedure-Oriented programming language
4. Structured programming language
5. Mid-level programming language.
C is a Mother language:
C language is considered as mother language of all modern programming languages
because most of the programming languages follow C syntax and all other languages
are derived from C only.
System Programming language:
C programming can be used to do low-level coding(software coding ) , can be used to
create compilers and Operating systems, kernels.
Ex: Linux Kernal
Procedure Oriented-Programming:
A procedure is known as a function, method, routine, subroutine, etc.
A procedural language specifies a series of steps for the program to solve the
problem.
A procedural language breaks the program into functions, data structures, etc.
C is a procedural language. In C, variables and function prototypes must be declared
before being used.
C as a structured programming language:
A structured programming language is a subset of the procedural language. Structure
means to break a program into parts or blocks so that it may be easy to understand.
In the C language, we break the program into parts using functions. It makes the
program easier to understand and modify.

C as mid-level programming language:


C is considered as a middle-level language because it supports the feature of both low-
level and high-level languages.
Basic hello world program:
#include<stdio.h>
int main(){
printf(“Hello World”);
return 0;
}
HISTORY OF C LANGUAGE
▪ Dennis Richie is the father of C language.
▪ It was developed in 1972 at bell laboratories of AT & T, USA.
▪ In the 1960’s COBOL was used for commercial purposes and FORTAN is used for
scientific and engineering applications. At this stage, people started to develop a
language that is suitable for all possible applications.
▪ Therefore an International Committee was set up to develop ALGOL was released.
But it was not popular because it seemed too general.
▪ To reduce this generality a new language called CPL ( Combined programming
language ) was developed at Cambridge University. Then some other features are
added to this language and a new language called BCPL was developed by Martin
Richard at Cambridge university. Then B language was developed by Ken
Thompson at AT & T labs.

6
▪ Dennis Richie inherited the features of B and BCPL and added his own features and
developed C Language.

LANGUAGE YEAR DEVELOPED BY


ALGOL 1960 International Committee

CPL 1963 Cambridge University

BCPL 1967 Martin Richard

B 1970 Ken Thomson

C 1972 Dennis Richie

7
STRUCTURE OF A C PROGRAM
DOCUMENTATION SECTION
______________________________________________________________
LINK SECTION
______________________________________________________________
DEFINITION SECTION
______________________________________________________________
GLOBAL DECLARATION SECTION
______________________________________________________________
main()
{
Declaration Part
Executable Part
}
______________________________________________________________
Sub program section
Function 1
Function 2

8
Function n
1. DOCUMENTATION SECTION

The documentation section contains a set of comment lines giving the name of the program and
other details. There are two types of comment lines.
To give instructions to the programmer.
Comment Line does not executed.
1. Single Line Comment
2. Multi Line Comment

Single Line Comment Multi Line Comment


o The single line comment starts with // o Multi line comments start with /* and
and continue until the end of line. end with */. All the statements between
o Ex: these symbols are not executed.
o Ex:

9
2. LINK SECTION

▪ The link section provides instructions to the compiler to link the


functions from system library.
▪ C program depends on some header files. Each header file
extension is .h
▪ These header files are included at the beginning of the program in
the c language.
▪ Ex: #include <stdio.h> #include <conio.h>
#include <math.h>
#include <string.h> # - preprocessor

10
3. DEFINITION SECTION
▪ This section is used to define all symbolic constants.
▪ A #define instruction defines value to a symbolic constant. Whenever the symbol
name is encountered in the program, the compiler substitutes the value associated
with the name automatically.
▪ Note: We can’t change the value of constant in program.

▪ Ex: #define pi 3.14


#define a 100

11
4. GLOBAL DECLARATION SECTION
▪ Some variables are declared outside of the main function.
▪ These variables can be used by main function and all functions in the
program.
▪ The default value of the global variable is 0.

12
5. main( ) function:
▪ Every c program must and should have one main( ) function.
▪ This function does not take any arguments.
▪ The main( ) functions consist of statements that are enclosed within the braces.
opening brace { and closing brace }
▪ In these two braces there are two parts
▪ Declaration part
▪ Executable part

▪ In the structure of a C program, this section contains the main function of the code.
The C compiler starts execution from the main() function.
Note: The return type of main( ) can be any data type.

13
6. SUB PROGRAM SECTION
▪ It consists of all user-defined functions. The control of the program is shifted to the
called function whenever they are called from the main or outside the main()
function.
▪ We can write any number of functions to a program.
▪ Functions are useful for code reusability.

main( )

Function 1 Function 2 Function n

14
EXAMPLE PROGRAM

15
WHAT IS A VARIABLE?
▪ A variable is a name given to a memory location, and used to store the data. Its
value can be changed and it can be reused many times.
▪ It is a away to represent memory location through symbol so that it can be easily.
Identified.
Ex:
int a, b;
a=10;
VARIABLE CREATION
Creating Variables includes two steps, first we need to declare the required variables
and then initialize them.
• Variable declaration
• Variable Initialization
Variable Declaration:
Declaration statement is used to specify the name and type of the variable. The
declaration tells the compiler about the existence if the entity in the program and its
location. When you declare a variable it should be initialized.
Syntax:
datatype variable_name;
eg : int x;
Here the variable x is declared of type integer.
eg: int x,y,z;
More than one variables will be separated using comma(,).
Variable Initialization:
A process of assigning values to the variable is called initialization.
Two types of initialization:
▪ Compile Time Initialization
▪ Run Time Initialization
Compile time initialization: A process of putting correct initial values of the
variable directly in the program memory.
Syntax:
Variable name = value ;
Ex: a=10;
(or)
int a=10;
Run time Intialization:
User can give their own values( entering different values during different runs of
program).
It is done by using scanf().
Syntax:
scanf(“format specifier”,address_list);
Ex: scanf(“%d”,&a);
%d is a format specifier tells us the type of data to store and print.
Now the %d in the above scanf() specifies the integer type of data to be store in a
variables.
&a- specifies the address of variable a, that means the value which is reading by the
scanf() will be stored in the address of specifying variable.
DATA TYPES IN C
▪ Data types are defined as the type of data storage format that a
variable can store.
▪ These are used to define a variable before to use in a program.
▪ There are three types of data types available in C
▪ Primary data type
▪ Derived data type
▪ User-defined data type
▪ The difference between these data types is the number of bytes they
occupy and the range of values.

20
1. PRIMARY DATA TYPES
▪ These are the basic data types in C.
▪ There are 4 types of primary data types available. They are:
▪ Integer
▪ Float
▪ Character
▪ void
▪ Using these data types, derived and user defined data types are
obtained.

21
INTEGER :
▪ There are different integer data types available. They are int, short int, long int.

TYPE SIZE RANGE


(BYTES)
int 4 -32768 to 32767
short int 2 -32768 to 32768
long int 8 -2147483848 to 2147483847
Unsigned int 4 0 to 65535
Unsigned short int 2 0 to 65535
Unsigned long int 8 0 to 4294967295

22
FLOAT :
▪ These are the values with decimals. It can also hold integer values.
▪ It consists of → float
→ double
→ long double

TYPE SIZE (BYTES) RANGE

float 4 3.4E – 38 to 3.4E + 38

double 8 1.7E – 308 to 1.7E + 308

long double 16 3.4E – 4932 to 3.4E + 4932

23
CHARACTER :

▪ There are two types of char


▪ They are 1) signed char, 2) unsigned char

TYPE SIZE (BYTES) RANGE


signed char 1 -128 to 128
unsigned char 1 0 to 255

24
2. DERIVED DATA TYPES :
These data types are derived from primary data types
Ex: arrays pointers

3. USER DEFINED DATA TYPES :


C allows users to define their own data types which is a combination of primary data
types and user-defined data types.
Ex: structures, unions, enumerations

25
PROGRAM STATEMENTS
C programs are collection of statements, statements is an executable part of the
program it will do some action. In general is an arithematic actions and logical
actions are falls under statements catagories.
There are few statement categories.
1. Expressions Statements.
2. Compound Statements
3. Selection Statements
4. Iterative statements
5. Jump Statements
EXPRESSION STATEMENTS
It is combination of variables ,Constants ,operators ,Function Calls and followed by a
semicolon. Expression can be any operation like Arithmetic operation or Logical
Operation.
▪ Few Examples for expression Statements
▪ X = Y + 10 ;
▪ 20 > 90;
▪ a ? b :c ;
▪ a = 10 + 20 * 30;
▪ ; (This is NULL Statement ).
COMPOUND STATEMENT
▪ Compound statement is combination of several expression statements. Compound
Statement is Enclosed within the Braces { }.
▪ Compound statement is also called as Block Statement.
Note:
There is no need of any semicolon at the end of the compound statement.
Ex:
int a=10,b=20,c;
c = a + b;
printf(“value of c is : %d”,c);
SELECTION STATEMENT
▪ Selection Statements are used in decisions making situations we will look about
selections statements in Later Tutorials. Here is the few examples of Selection
statements.
▪ If
▪ If…….else
▪ Switch
ITERATIVE STATEMENT
▪ These are also Called as Loops. If we want to Execute a part of program many times
we will use [Link] will going to explain each and Every loop in Detail in Later
Tutorials. Here is the List of Basic loops in C language.
▪ For loop
▪ While loop
▪ Do-while loop
JUMP STATEMENT
▪ These are Unconditional statements Jump statements are useful for Transfer the
Control one part of program to other part of Program there are few Jump
Statements in C.
Goto
Continue
Break
Return
Note:
All these selection and iterative and jump statements are Keywords and all are Lower
case characters.
C TOKENS
▪ C tokens are the smallest individual units
▪ We cannot create a sentence using words, Similarly we cannot write a C program
without C Tokens.
▪ So these are the basic building blocks of C Language.
▪ There are 5 types of C Tokens
1. Key Words
2. Identifiers
3. Constants
4. Strings
5. Operators
6. Special symbols

32
1. KEY WORDS
▪ Key words are the reserved and predefined words.
▪ They have some special meaning and we can’t change its meaning.
▪ Each of them are associated with specific feature.
▪ All keywords must be written in lower case only.
▪ Keywords cannot be used as identifiers.
▪ There are 32 keywords in C. They are

auto double int struct

break else long switch


case enum register typedef
char extern return union
continue for signed void
do if static while
default goto sizeof volatile
const float short unsigned

33
2. IDENTIFIERS
▪ Identifiers represent a name of various entities like variables, functions, and
program name in a program.
▪ It consists of alphabets and digits (a-z, A-Z, 0-9).
▪ Identifiers allow both uppercase and lowercase letters.
▪ Special characters like @, #, <, etc are not allowed except underscore (_)
▪ An identifier must start with an alphabet or underscore.
▪ Identifiers are case-sensitive.
▪ Keywords can’t be used as identifiers.

34
3. CONSTANTS
▪ Constants are the fixed values which can’t be changed during the execution of the
program.

CONSTANTS

NUMERIC CHARACTER

SINGLE STRING
INTEGER REAL
CHARACTER

HEXA
DECIMAL OCTAL
DECIMAL
35
Integer Constants :
▪ Decimal Integer Constant:
Consists of set of values from 0 to 9, preceding with an optional + or – sign.
Ex: 123 +123 -123
Note: Spaces, commas, non digit characters are not allowed
like 10,000 $ 500 25 000
▪ Octal Integer Constant:
Consists of set of values from 0 to 7, preceding with 0
Ex: 010 ( 8 in decimal )
▪ Hexa Decimal Constant:
Consists of set of values from 0 to F ( 0 to 9, A to F ) and preceding with 0x
Ex: 0x9 (9 in decimal), 0xB (11 in decimal)

36
▪ Real Constants :
These are used to represent floating points ( which have fractions )
Ex: 5.034 0.129
▪ Character Constants :
Single Character Constant:
It consists of a single character enclosed within single quotations (‘ ‘)
Ex: ‘A’ ‘d’
String Constant :
It consists of a sequence of characters enclosed within double quotations (“ “)
Ex: “Hello World” “Welcome 123”

37
4. STRINGS
▪ Strings are used to store a set of characters, which are usually enclosed in double
quotations ( “ “ )
▪ The compiler appends Null Character (‘\0’) at the end of the string by default.
▪ String can consist of alphabets, numerals, and special characters.
▪ String is also an array of characters.
Ex: char str[5] = “Hello”;

str H e l l o \0

Length of str is 5

38
5. OPERATORS
▪ An operator is a symbol that performs certain mathematical or logical operations.
Types of operators:
▪ Unary operators
▪ Binary operators
▪ Ternary operators
Unary operators:
▪ Those operators that requires only single operand to act upon are known as unary
operators.
Example:
Increment operators(a++,a--,++a,--a).
Binary Operators:
Those operators that require two operands to do operation are called binary
operators. Binary operators can further are classified into:
1. Arithmetic operators
[Link] Operators
[Link] Operators
[Link] Operators
[Link] Operator

Ternary Operator:
The operator that requires three operands to do operation is called the ternary operator.
Conditional operator is also called as TERNARY operators.
Eg: (a<b)?printf(“%d is big”,a):printf(“%d is big”,b);
OPERATORS
An operator is a symbol that performs certain mathematical or logical operations.
C supports a rich set of operators. They are
1. Arithmetic operators
2. Relational operators
3. Logical operators
4. Assignment operators
5. Increment and decrement operators
6. Conditional operator or Ternary operator
7. Bitwise operators

41
1. Arithmetic operators:
Arithmetic Operators performs numerical calculations.

OPERATOR MEANING
+ Addition or unary plus
- Subtraction or unary minus
* Multiplication
/ Division
% Modulus or Remainder

▪ In modulo division, the sign of result is always the sign of first operand (dividend).
Ex: 10 % 3 = 1 10 % -3 = 1
-10 % 3 = -1 -10 % -3 = -1

42
2. Relational operators:
▪ Relational operators are used to compare two quantities depending upon their
relation.

OPERATOR MEANING
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
== Equal to
!= Not equal to

▪ Relational operators are complement to each other.


> is complement to <=
< is complement to >=
== is complement to !=
43
3. LOGICAL OPERATORS
▪ In addition to relational operators, C provides logical operators.
Operator Meaning
&& logical AND
|| logical OR
! logical NOT

A B A && B A || B A!
T T T T F
T F F T F
F T F T T
F F F F T

44
4. ASSIGNMENT OPERATOR
▪ The assignment operator is used to assign the value to a variable.
Syntax:
a = 10
variable assignment operator value

▪ In addition to assignment operator c provides set of short hand assignment operators.

short hand assignment operator normal assignment operator


a += 1 a=a+1
a %= 1 a=a%1
a *= n + 1 a=a*n+1

45
5. INCREMENT & DECREMENT OPERATOR

▪ The C language provides two operators, increment and decrement operators.


▪ These are classified into 4 types.
1. Postfix increment : a = b++
2. Postfix decrement : a = b - -
3. Prefix increment : a = ++b
4. Prefix decrement : a = --b

POSTFIX POSTFIX PREFIX PREFIX


INCREMENT DECREMENT INCREMENT DECREMENT
If a = 5, If a = 5, If a = 5, If a = 5,
b=a++ b=a-- b= ++a b=--a
O/P : a = 6, b = 5 O/P : a = 4, b = 5 O/P : a = 6, b = 6 O/P : a = 4, b = 4

46
6. CONDITIONAL OPERATORS ( ? : )
▪ The operator which consists of ? Followed by : is known as conditional operator.
▪ It is also known as Ternary operator.
expression 1 ? expression 2 : expression 3
▪ In expression 1 a condition will be given. It has to be evaluated first. If it is TRUE,
then expression 2 is evaluated. If it is FALSE, then expression 3 is evaluated.
▪ Ex:
a > b ? a :b
→ if a = 10, b = 20, OUTPUT = 20
→ if a = 45, b = 30, OUTPUT = 45

47
7. BITWISE OPERATORS
▪ These operators are used to perform operations on bits ( 0’s and 1’s )

OPERATOR MEANING

& Bitwise AND

| Bitwise OR

^ Bitwise Exclusive OR

~ One’s Complement

<< Shift LEFT

>> Shift RIGHT

48
EXAMPLES
1. Bitwise AND ( & ) :
If a = 12 and b = 10, Then a & b = 8
12 → 1 1 0 0 ( binary representation of 12 )
10 → 1 0 1 0 ( binary representation of 10 )
12 & 10 → 1 0 0 0 = 8
2 . Bitwise OR ( | ) :
If a = 12 and b = 10, Then a | b = 14
12 → 1 1 0 0
10 → 1 0 1 0
12 | 10 → 1 1 1 0 = 14

49
3. Bitwise Exclusive OR ( ^ ) :
If a = 12 and b = 10, Then a ^ b = 9
12 → 1 1 0 0 ( binary representation of 12 )
10 → 1 0 1 0
12 ^ 10 → 0 1 1 0 = 6

4. One’s Complement ( ~ ) :
One’s complement of a can be calculated by using - (a+1)
If a = 10, ~ a = -11
~a = - (a+1)
= - (10+1)
= -11

50
5. Shift LEFT ( << ):
The bits are shifted to the left side. The values will be doubled if we shift to the left
side. So, the output is the double of the given input.

51
6. Shift RIGHT ( >> ):
The bits are shifted to the right side. The values will be halved if we shift to the right
side. So, the output is the half of the given input.

52
6. SPECIAL SYMBOLS
The following special symbols are used in C having some special meaning and thus,
cannot be used for some other purpose. Some of these are listed below:
• Brackets[]: Opening and closing brackets are used as array element references.
These indicate single and multidimensional subscripts.
• Parentheses(): These special symbols are used to indicate function calls and
function parameters.
• Braces{}: These opening and ending curly braces mark the start and end of a block
of code containing more than one executable statement.
• Comma (, ): It is used to separate more than one statement like for separating
parameters in function calls.
• Colon(:): It is an operator that essentially invokes something called an initialization
list.
• Semicolon(;): It is known as a statement terminator. It indicates the end of one
logical entity. That’s why each individual statement must be ended with a
semicolon.
• Asterisk (*): It is used to create a pointer variable and for the multiplication of
variables.
• Assignment operator(=): It is used to assign values and for logical operation
validation.
• Pre-processor (#): The preprocessor is a macro processor that is used
automatically by the compiler to transform your program before actual
compilation.
• Period (.): Used to access members of a structure or union.
• Tilde(~): Used as a destructor to free some space from memory
EXPRESSIONS
The combination of Operators and Operands is known as Expression.
Different types of expressions are
1. Simple Expression
2. Complex Expression
3. Unary Expression
4. Ternary or Conditional Expression
5. Postfix Expression
6. Prefix Expression

55
▪ Simple Expression:
Expression containing single operator followed by operands.
Ex: a+b a/b a%b
▪ Complex Expression:
Expression containing more than one operator followed by operands.
Ex: a+b*c a%b*c+d
▪ Unary Expression:
Expression containing single operator followed by single operand.
Ex: +a -b
▪ Ternary or Conditional Expression:
Expression containing question mark ( ? ) followed by ( : )
Ex: a < b ? a :b
▪ Postfix Expression:
Expression containing operand followed by postfix operator.
Ex: a++ a--
▪ Prefix Expression:
Expression containing prefix operator followed by operand.
Ex: ++a --a
56
EXPRESSION EVALUATION
If any expression contains multiple operators, then they are evaluated according to the order
of their Precedence and Associativity.
▪ Operator Precedence :
If an expression has more than one operator, then operator precedence determines which operator is
evaluated first.
Ex: 4 * 5 + 10
In this multiplication is having highest priority over addition. So 4 * 5 is evaluated first and then the result
is added to 10.
▪ Operator Associativity :
It is used when two operators having the same precedence occur in an expression. It can be Right to Left
or Left to Right.
Ex: 10 / 2 * 5
In this / and * have the same priority and their associativity is Left to Right, So 10 / 2 is performed first and
then multiplication.

57
Category Operator Associativity
Postfix ( ) [ ] -> . ++ - - Left to right
Unary + - ! ~ ++ - - (type)* & Right to left
sizeof
Multiplicative */% Left to right
Additive +- Left to right
Shift << >> Left to right
Relational < <= > >= Left to right
Equality == != Left to right
Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right
Logical AND && Left to right
Logical OR || Left to right
Conditional ?: Right to left
Assignment = += -= *= /= %=>>= Right to left
<<= &= ^= |=
58
Comma , Left to right
LVALUE & RVALUE
Lvalue refers to memory location which identifies an object. Lvalue may appear as either
left-hand or right-hand side of an assignment operator(=).
▪ In any assignment statement “Lvalue” must have the capability to store the data.

▪ Lvalue cannot be a function, expression (like a+b), or a constant (like 3, 4, etc.).

Rvalue refers to data value that is stored at some address in memory. An Rvalue is an
expression, that can’t have a value assigned to it, which means Rvalue can appear on the
right but not on the left-hand side of an assignment operator(=).
Ex: int a;
a = 1;
int b = a; //Lvalue can appear on right side
9 = a; // error: assignment is trying to change the value of the assignment operator
59
ESCAPE SEQUENCE CHARACTERS
▪ Escape sequence characters perform alternate operation on the character followed
by \ ( it escapes from original function )
▪ \n new line character
▪ \t horizontal tab space
▪ \v vertical tab space
▪ \a alert
▪ \b backspace
▪ \r carriage return
▪ \” prints “
▪ \’ prints ‘
▪ \? prints ?
▪ \\ prints \

60
TYPE CONVERSION Implicit Type conversion

It is the way of converting one data type into another data type.
There are two types
1. Implicit Type Conversion :
This type of conversion is performed by compiler on its own.
Explicit Type Conversion
2. Explicit Type Conversion :
This type of conversion is performed by the programmer.

61
INPUT AND OUTPUT
Basic screen and Keyboard I/O in C , Non-formatted input and output ,
formatted input and output functions
BASIC INPUT OUTPUT IN C
The input and output functions of C are in stdio.h (Standard Input and Output)
standard library.
Input:
In C, input is taken from keyboard by using scanf( ) method.
Syntax: scanf(“format specifiers”, address-list);
Where format specifiers is a way to tell compiler what type of data stored in the
variable. Address-list consists of all variables address retrieved by using & symbol
separated by using comma.
Ex:
1) scanf(“%d”, &a);
%d represents integer variable and &a represents address of variable a.
2) scanf(“%d%d%d”, &a, &b, &c);
%d%d%d represents 3 integer variables and &a, &b, &c is the address list.
Format Specifier Data Type
%d Integer or decimal
%f Float
%c Character
%s String
%i Unsigned integer
%ld or %li Long
%lf Double
%Lf Long double
%u Unsigned int
%lu Unsigned int or unsigned long
%o Octal representation
%x or %X Hexadecimal representation
%p Pointer
Output:
printf( ) method prints the value passed to it on the console screen.
Syntax:
1. printf(“string/message”);
2. printf(“format specifiers”, variables-list);
Where string/message represents the text which has to be printed,
format specifiers is a way to tell compiler what type of data to be printed,
Address-list consists of all names to be printed separated by comma.
Ex:
1) printf(“Hello World!”); // output: Hello World!
printf( ) prints whatever passed as argument to the function.
2) int a = 5;
printf(“Value of a is %d”, a); // Value of a is 5
To print value of a variable we have use format specifier for int variable (%d)
FORMATTED INPUT & OUTPUT
Formatted I/O functions are used to take various inputs from the user and display
multiple outputs to the user. These types of I/O functions can help to display the
output to the user in different formats using the format specifiers. These I/O supports
all data types like int, float, char, and many more.

The following formatted I/O functions


1. printf()-refer slide no:66
2. scanf()-refer slide no:64
3. sprintf()
4. sscanf()
3. sprint( ):
sprintf stands for “string print”. This function is similar to printf() function but this function
prints the string into a character array instead of printing it on the console screen.
Syntax:
sprintf(array_name, “format specifiers”, variables-list);
Ex:

In this example, Value of a is %d and value of b is %d is stored in str variable.


Output:
4. sscanf( )
sscanf stands for “string scanf”. This function is similar to scanf() function but this
function reads data from the string instead of the console screen.
Syntax:
sscanf(array_name, “format specifier”, address-list);
Ex:

Note: same string has to be taken in sprintf( ) and sscanf( )


Output:
UNFORMATTED INPUT & OUTPUT
Unformatted I/O functions are used only for character data type or character array/string
and cannot be used for any other datatype. These functions are used to read single input
from the user at the console and it allows to display the value at the console.

The following unformatted I/O functions


1. getch( )
2. getche( )
3. getchar( )
4. putchar( )
5. gets( )
6. puts( )
7. putch( )
1. getch( )

getch( ) function reads a single character from the keyboard by the user but doesn’t
display that character on the console screen and immediately returned without
pressing enter key. This function is declared in conio.h(header file). getch( ) is also
used for hold the screen.

Syntax:

getch( );

(or)

variable = getch( );
2. getche( )

getche( ) function reads a single character from the keyboard by the user and
displays it on the console screen and immediately returns without pressing the enter
key. This function is declared in conio.h(header file).

Syntax:

getche( ); (or) Variable = getche( );


3. getchar( )

The getchar( ) function is used to read only a first single character from the keyboard
whether multiple characters is typed by the user and this function reads one
character at one time until and unless the enter key is pressed. This function is
declared in stdio.h(header file)

Syntax:

Variable = getchar( );
4. putchar( ):

The putchar( ) function is used to display a single character at a time by passing that
character directly to it or by passing a variable that has already stored a character.
This function is declared in stdio.h(header file)

Syntax:

putchar(variable);
5. gets( )
gets( ) function reads a group of characters or strings from the keyboard by the user
and these characters get stored in a character array. This function allows us to write
space-separated texts or strings. This function is declared in stdio.h(header file).

Syntax:

gets(str);
6. puts( )

puts( ) function is used to display a group of characters or strings which is already


stored in a character array. This function is declared in stdio.h(header file).

Syntax:

puts(variable);
7. putch( )

putch( ) function is used to display a single character which is given by the user and
that character prints at the current cursor location. This function is declared in
conio.h(header file)

Syntax:

putch(variable);

You might also like