Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
58 views49 pages

C Programming

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 49

Translated from Hindi to English - www.onlinedoctranslator.

com

Er.SATISH (Asst.Professor)
(BCA/PGDCA/MCA/M.TECH)
MAIL ID: -SATISHJANAGALJNU@GMAIL.COM
Mobile.9461320821/9799920821
PARMANAND DEGREE COLLEGE (PND), GAJSINGHPUR (RAJ.)
BOOK:- C LANGUAGE PROGRAMMING

Unit 1
INTRODUCTION OF C LANGUAGE
Today, whatever software or application we are working on in computers and mobiles, language is behind all of them and

in other languages,C Language holds an important place. Many software are developed through C Language. Origin of

Operating System Games It has happened.Before introducing C language, let us first understand about the language.

If there is a need for knowledge then let us cometrying to learn about language

What is LANGUAGE?

Language is a medium of communication between two or more people. Through language, people can

exchange their thoughts among themselves. Computer is an electronic machine which has no language of its

own. The computer needs a language to follow our instructions, so the electrical values in the computer A

language was created on its basis. Computer languages are generally of three types.

, Low-Level Language
, Assembly Language
, High-Level Language
,
Low-Level Language: This language is also called machine language. In this language binary code
only 0,1 or Only is used.Hard for this language It is necessary to have knowledge of Yayer in this language.

It takes a lot of time to correct errors in a program and the programs made in it are very complex. Is

Assembly Language: In this language, programs are made using common English words. Machine language is
relatively simple and there are no requirements for creating programs in this language. having knowledge of yair

is necessary

Er.Satish (Asst.Professor) E-mail: -satishjanagaljnu@gmail.com Mobile:- 9461320821/9799920821


High-Level Language: srograms with non-standard English words used in common speech or environment in that language The

programs that are made are very simple which even a common person with computer knowledge can make. Along with this, there

are such programs which can make the code written in English read by the machine.The act of converting language code There are

two types of programs.

interpreter: It converts the code written in the program into machine language in one line. If If
an error occurs in any line, it does not run the codes in the next line.

compilerCompiler: The work of converting the code written in the program into machine language in
one go. and displays the errors in the list as per the error path.

HISTORY OF “C” LANGUAGE


History of ―C‖ Language: The first programs made in C language were not portable. Keeping this
shortcoming in mind, there was a need for a programming language which was portable and in 1960
Cambridge University developed a programming language namedBCPL (Basic Combined Programming
Language) was kept. In 1970, Kan Thompson made some improvements in this language and His name "
Kept B‖ Language. C language was developed in 1972 by American computer scientist Dennis Richie at
AT&T Laboratory. C language is a very powerful language which supports systems and applications. Both
types of software programs can be made in this language in common parlance.of english words is used
so this language is easy to learn and understand. This language is astructural programming language
meaning The programs created in it run in a sequential order.

Er.Satish (Asst.Professor) E-mail: -satishjanagaljnu@gmail.com Mobile:- 9461320821/9799920821


Main features of C language
1 structural language,Structured Programming Language) To solve any big problem in this
language For this it is divided into smaller parts. These small parts are called modules. Hence'

C' is a structured programming language.

2 rosygeral language,Procedural Language ) The language in which statements are written according to rules and sequence It

is called rosygeral language. The order in which instructions or statements are written in 'C' language, and the sequence in which

they are executed (Executec). Hence “C” is a procedural language.

3 transfer language,Portable Language) ‗C' language is a portable language. Athay t aas language

in some oneA program written for a computer runs on a computer without any changes. no or some changeNo

After this the run is done. Hence 'C' is a high level translation language.

4 intermediate level language,Middle Level Language ‗In C language both high level and low level Gurus of

languages are included. This language is also called a middle level language because it is in the middle of two

categories.

5 case sensitive language,Case Sensitive Language ) Know what is used in developing programs in this language wal

small letters (There is a difference between lower case) and capital letters. use of both types of letters It is done on the

basis of the requirement of the program. Opinion in it: To make each program, the opinion statements are written in small

letters only.

6 brevity'Brief description of writing program in C language. Simple and high efficiency (High performance )
wall It happens. Which can be easily understood. They are also easy to test and maintain.

7 high level language,High Level Language) Program written in this language based on Yajur's
problem It happens. Their functions can be used in the program to solve labor problems and the
speed of the prepared program is also relatively faster. Apart from this, the programmer can create
new functions as per his requirement and the program can be developed at a very fast pace.

Some features of C programming


There are an arbitrary number of keywords in C.

C contains many logical and mathematical operators, including bit manipulators.

In C the return values of functions are not always required. A single statement can contain multiple

Er.Satish (Asst.Professor) E-mail: -satishjanagaljnu@gmail.com Mobile:- 9461320821/9799920821


You can also apply for assignments.

# include <stdio.h>
void main()
,
printf("Hello, ComputerHindiNotes.com!");
,
Output-Hello World!

Advantage of C language
1 – C language is a simple and easy language. Which can be used easily. And the biggest
specialty of C language is this. That the code written in C language is very fast. That means the
execution time of C language is fast.
2 – C is a structured programming language and we can create functions in C language. And
ownThe code can be managed even better.
3 – Some keywords have been provided in C language through C 32 reserved. which are such words,
Cannot be used for any other purpose. For whom and worship Sundhay
There are mistakes.

4 – C is a middle level language which is capable of creating high level and low level applications.
This feature is an advantage for programmers who create high level and low level applications. want
to do
5 – After Assembly language, C language is considered to be the fastest language. Therefore, it is faster
than other programming languages. The processing of applications created in C language is very fast.
heeis fast

Disadvantage of C language
C language is a powerful language. But C language also has some problems. Which C language
Creates a limit soI know about some limitations of C language.
1 – There is no run time checking in C language. Identify the type of any variable in C language.
Is not able to do.
2 – C language does not support re-usability (inheritance) and exceptions in C language
cannot be handled at run time.
3 – C language does not support object oriented programming. Like – classes, objects,
interfaces etc.

Usage of C Language

Er.Satish (Asst.Professor) E-mail: -satishjanagaljnu@gmail.com Mobile:- 9461320821/9799920821


C language is used to create system applications or network drivers. Because this code
produces. WhoRuns as fast as code written in assembly language.

Some examples for use in C language are given.


, Operating Systems
, Language Compilers
, Assemblers
, TextEditors
, print spoilers
, Network Drivers
, Modern Programs
, Data Bases
, Language Interpreters
, Utilities

Algorithms
Algorithms are usually destined to be implemented as computer programs. A programming
language offers a set of primitive, standard operators and rules. We will use these elements when
writing our programs in C in this chapter. Algorithms are usually destined to be implemented as
computer programs.

Algorithm (Al-go-rith-um) It is a Procedure (Step by Step Process) or it is a Formula which one of


Solves the problem. It is a procedure which has limited rules which are called instructions. is
also called

Er.Satish (Asst.Professor) E-mail: -satishjanagaljnu@gmail.com Mobile:- 9461320821/9799920821


C – Programming Basics
1- C programming basic commands to write a C program 2-A
simple C program with output and explanation 3-Steps to
write C programs and get the output 4-Creation,
Compilation and Execution of a C program
* How to install C compiler and IDE tool to run C programming codes 5-
Basic structure of a C program
* Example C program to compare all the sections
* Description for each section of the C program
6-C programs ( Click here for more C programs ) with definition and output – C program for Prime
number, Factorial, Fibonacci series, Palindrome, Swapping 2 numbers with and without temp
variable, sample calculator program and sample bank application program etc.

Er.Satish (Asst.Professor) E-mail: -satishjanagaljnu@gmail.com Mobile:- 9461320821/9799920821


1. C PROGRAMMING BASICS TO WRITE AC PROGRAM:
Below are few commands and syntax used in C programming to write a simple C program. Let's see
all the sections of a simple C program line by line.

2. A SIMPLE C PROGRAM:
Below C program is a very simple and basic program in C programming language. This C program
displays ―Hello World!‖ in the output window. And, all syntax and commands in C programming are
case sensitive. Also, each statement should be ended with semicolon (;) which is a statement terminator.

# include <stdio.h>

int main()
,
/* Our first simple C basic program */
printf("Hello World!");
getch();
return 0;
,
OUTPUT:
Hello World!

3. STEPS TO WRITE C PROGRAMS AND GET THE OUTPUT:


Below are the steps to be followed for any C program to create and get the output. This is common
to all C program and there is no exception whether its a very small C program or very large C
program.

, Create
, compile
, Execute or Run
, Get the Output

Er.Satish (Asst.Professor) E-mail: -satishjanagaljnu@gmail.com Mobile:- 9461320821/9799920821


4.CREATION,COMPILATION AND EXECUTION OF AC PROGRAM:
If you want to create, compile and execute C programs by your own, you have to install C compiler in
your machine. Then, you can start to execute your own C programs in your machine. You can refer
below link for how to install C compiler and compile and execute C programs in your machine.

Once C compiler is installed in your machine, you can create, compile and execute C programs as
shown in below link.
If you don't want to install C/C++ compilers in your machine, you can refer online compilers which
will compile and execute C/C++ and many other programming languages online and display
outputs on the screen. Please search for online C/C++ compilers in Google for more details.
C – Environment Setup Using IDE tool C –
Environment Setup Using GCC compiler

5.BASIC STRUCTURE OF AC PROGRAM:


Structure of C program is defined by set of rules called protocol, to be followed by programmer
while writing C program. All C programs are having sections/parts which are mentioned below.

, Documentation section
, Link Section
, definition section
, Global declaration section
, Function prototype declaration section
, Main function
, User defined function definition section

Example C Program to Compare All the Sections:


# include <stdio.h> /* Link section */
int total = 0; /* Global declaration, definition section */ /*
int sum(int, int); Function declaration section */
int main() /* Main function */
,
printf ("This is a C basic program\n");
total = sum(1, 1);
printf ("Sum of two numbers : %d \n", total);
return 0;

Er.Satish (Asst.Professor) E-mail: -satishjanagaljnu@gmail.com Mobile:- 9461320821/9799920821


,
int sum (int a, int b) /* User defined function */ {

return a + b; /* definition section */


,
OUTPUT:
This is a C basic program Sum of two numbers : 2

ESCRIPTION FOR EACH SECTION OF THE C PROGRAM:


Let us see about each section of a C basic program in detail below.

Please note that a C program mayn't have all below mentioned sections except main function and link
sections.

6. Example C Programs with Definition, Example Program


and Output:
If you have enough basic knowledge on C programming language and all concepts, you can refer
following C programs.
Please click here ―C programs‖ for referring below programs. C
program for prime number
C program for Factorial
C program for Fibonacci series

C program for Palindrome


C program for Swapping 2 numbers with and without temp variable
Sample calculator program and bank application program
etc.
Decision table (What is decision table?)

Er.Satish (Asst.Professor) E-mail: -satishjanagaljnu@gmail.com Mobile:- 9461320821/9799920821


Decision table: Decision table expresses the word statement of a problem in the form of a table.
and clearly defines aTo define complex program logically hopeCan be considered a powerful
tool. As is clear from the name, the decision table shows what decisions the computer takes or
what actions it takes as a result of the input data. When the computer has to take a large
number of decisions or When a program has a large number of different branches happens,
then personallyDecision table is useful. In these situations, decision table is useful instead of
Flowchart.

OneTo make a decision table the following steps have to be followed -

The problem which is to be solved by the computer must be precisely defined. In the problem, a

list of all the possibilities related to the test should be made.

A list of all the tasks related to what life has to do with the group of prospects should be made. TwoA

decision table should be made using the list.

majorityThe decision table consists of six parts. The first part of the decision table
contains the name and/or number of the table. it occursThe second part of the table,
called the condition stub, contains all the conditions that exist in the program logic. tells
the possibilitiesThe work done in the third part of the decision table is in the form of
statement, hopeAction Stub Is

Advantages of Decision table

Decision table is used instead of flowchart for the following


reasons - itProvides outline for statements.
They are easier to create and modify than flowcharts.
Decision tables provide more compact documentation. A small table is equivalent to a flowchart
containing several papers. Following a particular path in a column, several pages of the
flowchart It is easier than
itIt is easy to check that all the conditions of the test have been taken into account.

OneWhile drawing a decision table, we use a standard format, here is its explanation
Can be done easily.
Disadvantages of Decision table (Disadvantages of decision table)

Despite many benefits,Decision tables are not very popular and are used much like charts. It is not
brought because -

Er.Satish (Asst.Professor) E-mail: -satishjanagaljnu@gmail.com Mobile:- 9461320821/9799920821


OneFlowcharts can clearly express the sequence of events required to solve a problem.
Do.
Flowcharts are more popular and many programmers and users value them.

Execution table

Execution table: Outputs or results generated by following the algorithm or flowchart step by step. OfThe
sequence is called Execution table. This is the basis for any run time error in the flowchart or algorithm.
Ryu is a useful technique in examiningCan also be applied to a program. After making a flowchart or
writing an algorithm or program, it is executed with this collection of data. This process ToAlso called dry
run. In this stage the work of program instructions There is a rebirth.

Pseudo code

Definition of Pseudo Code:- Pseudo-code is used in computer programming and algorithms to complete
a program. Logic is used to write i.e. tWritten in simple English with pseudocode
It is known that the programHow will the source code be written?

in pseudocodeWords like if, else, print, input, output are also used due to which they are not used.
The process of writing programming code is quite quick and easy to complete. For any problem The
process of writing pseudo code is done after creating the algorithm and flowchart, i.e. t we
Algorithms and flowcharts can also be called the main basis of pseudocode because only the instructions written in

algorithms and flowcharts are used to create pseudocode.

C - Structure of Program

Program Structure:

Preprocessor # include <stdio.h>


return_type & main function int main()
Opening curly brace ,

Er.Satish (Asst.Professor) E-mail: -satishjanagaljnu@gmail.com Mobile:- 9461320821/9799920821


printf i/o function printf("Hello World");
return 0; return 0;
closing curly brace ,
thisProgram is the first and easiest program in C programming. The above program is divided into six parts.

Is divided.
Preprocessor
main function
,
printf("Hello World");
return 0;
,

1.Preprocessor,Preprocessors/header are written first in the program. Y preprocessors are


divided for different purposes, for eg. There are printf and scanf functions in stdio.h. getch function
is provided in conio.h.

2.int main(),over hereThe return_type of main function is integer. The return_type of main function is
also written as void but void does not return any value. Program starts with main() function It happens.

3.{,everyFunction codes or statements are written after the curly brace is open.,

4.printf("Hello World");,The statement written in printf() function is printed in the output.


goes hopeThe statement is written inside two double quotes (" ").

5.return 0;,return 0 This allows the program to close. This '0' returns to the main function
does.

6.},here} hopeThe main function is closed with curly brace.

Hello Program Source Code:

# include <stdio.h>
int main()
,
printf("Hello World!");
return 0;

Er.Satish (Asst.Professor) E-mail: -satishjanagaljnu@gmail.com Mobile:- 9461320821/9799920821


,
Output :
Hello World!

C - integer

To declare variables in Integer Data Type, we use 'int' keyword.

Integer Data Type can store all numeric values.

If you want to print integer in output then print it in output with '%d' format specifier.

Integer Data Type can be of 2, 4 and 8 bytes.

ifIf the processor of the computer is 16-bit then the size of int is 2 bytes.

ifIf the processor of the computer is 32-bit then the size of int is 4 bytes.

IfIf the computer processor is 64-bit then the size of int is 8 bytes.

Integer Data Types, Storage Size and Range

Data Types Storage Size Range


int (16-bit) 2 bytes - 32,768 to 32,767
int (32-bit) 4 bytes - 2,147,483,648 to 2,147,483,647
int (64-bit) 8 bytes - 9,223,372,036,854,775,807 0
unsigned int (16-bit) 2 bytes to 65,535
short int 2 bytes - 32,768 to 32,767
unsigned short int 2 bytes 0 to 65,535
signed short int 2 bytes – 32,768 to 32,767
long int 4 bytes – 2,147,483,647 to 2,147,483,647 0
unsigned long int 4 bytes to 4,294,967,295
signed long int 4 bytes – 2,147,483,648 to 2,147,483,647

C character

To declare variables in Character Data Type, we use 'char' keyword. If the character is to be
printed in the output then it is printed in the output with '%c' format specifier.

Er.Satish (Asst.Professor) E-mail: -satishjanagaljnu@gmail.com Mobile:- 9461320821/9799920821


only oneCan declare character. for eg. 'H'
ifMultiple character means if you want to print a whole string then use '%s'.
Character Data Type is 1 byte.

C - float

To declare variables in floating-point data type, we use 'float' keyword. If you want
to print float in output then print it in output with '%f' format specifier. Floating-
pont data type is of 4 bytes. ,
for eg.

# include <stdio.h>
int main() {
float a=5, b=3.145; // Type Casting convert integer value to print in float value
printf("Value of Float variable : %f \n", a);
printf("Value of b : %f \n", b);
return 0;
,
output Value of Float variable : 5.000000
Value of b : 3.145000

Floating-point Data Type, Storage Size and Range

Data Types Storage Size Range Digits of Precision


float 4 bytes 1.2E-38 to 3.4E+38 6

Data items of the same type are used to keep them in consecutive memory locations.

Features:

1. one type of datato save


2.Only the first adjuncts of the array need to be remembered.

3. If we want to store the marks of all the students of a class thenbest use of array It is a good
option.
Demerits:

1. ktukunkThe size of the array is fixed, hence many times a lot of memory space is
wasted. 2. Once the material is fixed, it cannot be worked or increased further.

Er.Satish (Asst.Professor) E-mail: -satishjanagaljnu@gmail.com Mobile:- 9461320821/9799920821


Double Data Type

To declare variables in Double Data Type, 'double' keyword is used.


If you want to print an integer in the output, you print it in the output with '%f', '%e', '%E', '%lf' format

specifiers. Double Data Type is of 8 bytes. ,

There is no difference between Double and Floating-point Data Types.

C - void
void means null value.
There is no value in void.
thisData type is used for function and parameter/argument of function. or
someoneDoes not return value.

for eg.

# include <stdio.h>
void hello(void); //function with no return value and parameter
main()
, Hello();
,
void hello(void)
, printf("Hello World");
,
Output - Hello World

Unit 2
C - Array
Array is a collection of variables of the same data type.
The data type of Array can be any.
The variables of the array are stored at their respective memory locations.

Initialization of Array starts from '0'. 0


1234
The first element of the array has the lowest memory address.
Why is Array used?
if anyIf you want to store the names of students, you have to create separate variables for them. for
eg.
char stud1 = "Rakesh" , stud2 = "Uday", stud3 = "Raj" ;

Er.Satish (Asst.Professor) E-mail: -satishjanagaljnu@gmail.com Mobile:- 9461320821/9799920821


Only one array_variable has to be created for all these names in the array.
for eg.
char arr[] = { "Rakesh", "Uday", "Raj" };

There are two types of Array.

, Single/One Dimensional Array


, Multi Dimensional Array
,
Single/One Dimensional Array

Syntax for Single Dimensional Array Declaration


data_type array_name[size_of_array];

for eg.
int arr[5];
Syntax for Single Dimensional Array Initialization
data_type array_name[size_of_array] = {value1, value2,...,value n};

for eg.
int arr[5] = {1, 2, 3, 4, 5};
Example for One/Single Dimensional Array
Source Code :

Er.Satish (Asst.Professor) E-mail: -satishjanagaljnu@gmail.com Mobile:- 9461320821/9799920821


Array Memory Representation

over hereThis is done by 4-4 in the address because since the system is 32-bit, the size of the integer is 4bytes. so

thereforeMeaning of 4-4 in Memory Address.

ifIf the data type of the array was character then there would have been a sequence of 1-1 in the address.

index of array arr[0] arr[1] arr[2] arr[3] arr[4]


Array Elements 1 2 3 4 5
Memory Address 6356728 6356732 6356736 6356740 6356744

Multi Dimensional Array

Syntax for Multi Dimensional Array Declaration

data_type array_name[ row_size ][ column_size ];

for eg.

Syntax for Multi Dimensional Array Initialization

for eg.

int arr [3][4] = {1,2,3,4,5,6,7,8,9,10,11,12}; OR

int arr [3][4] = {{1,2,3,4},{5,6,7,8},{9,10,11,12}};

Er.Satish (Asst.Professor) E-mail: -satishjanagaljnu@gmail.com Mobile:- 9461320821/9799920821


Example for Multi Dimensional Array

Source Code:

# include <stdio.h>
int main()
,
int arr[3][4] = { {1,2,3,4}, {5,6,7,8}, {9,10,11,12} }; int
i,j;
for ( i = 0; i < 3; i++ )
, for ( j = 0; j < 4; j++ ) printf("[%d]
[%d]=%d\n",i,j,arr[i][j]);
,
return 0;
,
Output :

[0][0]=1
[0][1]=2
[0][2]=3
[0][3]=4
[1][0]=5
[1][1]=6
[1] [2] = 7
[1] [3] = 8
[2][0]=9
[2][1]=10
[2] [2] = 11
[2] [3] = 12

Er.Satish (Asst.Professor) E-mail: -satishjanagaljnu@gmail.com Mobile:- 9461320821/9799920821


C-Pointer

everyoneVariable has a memory address and to see the memory address the programmer needs to
'&'(address operator) has to be placed before the variable. for
eg.
int a ;
printf("Address of a : %d", &a);
Output:
Address of : 6356748 // 6356748 is address of variable
AssiThe value of the variable is accessed with the help of memory address, these are called 'Pointers'.
Pointer holds the address of another variable.
someoneIf you want to hold the address of an integer data type variable, then the pointer will also
be of the same data type as the variable, meaning if the variable is int then the pointer will also be
int, if the variable is char then the pointer variable will also be char. Pointer does not hold any value,
but only holds its address and the value of the variable is printed with this address. Ha |

C Structure

Structure is a collection of different data types.


ifIf you want to use structure then use 'struct' keyword. The
structure is similar to that of an array.
Array is a collection of similar data types and Structure is a collection of different data types. The
declaration of every variable in the structure is called 'member'.
Structure allocates separate memory for each member.
C - Union

Union is a collection of different data types.


ifIf you want to use union then use 'union' keyword. Union is
similar to this structure.
The declaration of every variable in the union is called 'member'.

Er.Satish (Asst.Professor) E-mail: -satishjanagaljnu@gmail.com Mobile:- 9461320821/9799920821


Union does not allocate separate memory for each member.
Union members share the same memory location.
In a union, the member who is bigger in size is the size of the entire union.
C - What is Variable

thisIt stores the values of data types internally.


Variable is also the name of a memory location.
Rules for variables

Variable Y is case-sensitive. for eg int a and int A are different variables.


Variable starts with any alphabet (az, AZ) or underscore ( _ ). Name of
variables can be alphanumeric. For eg. a1 = 5, var1, var2
Variable does not allow space. Variable name
does not contain any C keywords.
C Variable Declaration and initialization

Variable Declaration

WhenWhen a variable is declared, it allocates memory associated with the data type of the
variable. After the variable is declared, it takes its own internal Garbage Value.
Garbage Value: Garbage Value variable is given by the compiler.
Source Code:

# include <stdio.h>
int main(){
int a;
printf("Value of a : %d", a);
return 0;
,
Output :
Value of a : 27
C - Constants

Constants

The value of constant is fixed.


Constant can be of any data type.

Er.Satish (Asst.Professor) E-mail: -satishjanagaljnu@gmail.com Mobile:- 9461320821/9799920821


Constants are also called Literals.

There is also Constant Pointer.

Syntax:

const data_type variable_name = value (optional) ;

Types of Constant :

, Integer Constant
, Decimal Constant
, octal constant
, Hexadecimal Constant
, Floating-point / Real Constant
, Character Constant
, String Constant
, Preprocessor

with examples

Constant Types Examples


integer e.g. 2, 10, -2
Decimal (Integer) e.g. 2, 10, -2
Octal (Integer) e.g. 02, 010
Hexadecimal (Integer) e.g. 0x12, 0x1f
Floating-point/Real e.g. -2.4, 4.8
Characters e.g. 'i', 'j'
string e.g. "Hello", "Hi"
Preprocessor e.g. #define a 5

Integer Constant Types:

, Decimal Integer Constant


, Octal Integer Constant
, Hexadecimal Integer Constant
Integer Constants

Integer Constant works like a normal variable.


Integer Constant can be positive (+) or negative (-).
The range of Integer Constant is -32768 to 32767.

Er.Satish (Asst.Professor) E-mail: -satishjanagaljnu@gmail.com Mobile:- 9461320821/9799920821


Character Constants

Character Constant works like a normal variable.


Character Constant takes only single character. ,
The character constant is also used with escape sequences.
Floating-point Constant

Floating-point constant works just like a normal float variable.


Floating-point constant has a decimal point (.).
ifIf the value of Floatint-point Constant is of integer type then it takes Decimal point (.).

String Constant

The value of String Constant is written inside Double Quotation Mark (" ").
String Constant takes single and multiple characters.
String ConstaPreprocessor Constant
Constant value can be kept even with Preprocessor.

Preprocessor Constant

for eg.Source Code:

# include <stdio.h>
# define a 5 // Constant value with preprocessor
# define b 10 // Constant value with preprocessor
int main(){
printf("Value of a :%d\n",a);
printf("Value of b :%d",b);
return 0;
,
output
Value of a :5
Value of b : Also used with 10nt Escape Sequences.

What is Type Conversion and Type Casting

What is Type Conversion and Type Casting: Actually Type Conversion and Type Casting are not two
different concepts but are two different names of the same concept. Values of different data types are
stored in the computer and processed. Many times such a situation occurs while interacting with values.
becomes Paida, in which one of theConverting a value of a data type to a value of another data type

Er.Satish (Asst.Professor) E-mail: -satishjanagaljnu@gmail.com Mobile:- 9461320821/9799920821


someoneCalculation has to be performed. To deal with such situations we use C language. two ways
Provides Type Conversion Mechanism on the basis of which we can convert the value of a type DasurWe
fulfill our requirement by concatenating the value of Type. Both these methods are as follows:

Automatic Type Conversion

In “C” language, you can mix different types of variables and constants, but at the time of execution
thisExpressions follow a special rule. We know that in Expressions the compiler will morePerforms
operations with operands and stores its value in an operand. If the operand differentIf the data is of
type, then the operand of lower type is converted into the operand of upper type and the result is
always of higher type.

AllVariables of short and char type are automatically converted to int type.

if oneIf the operand is of Long Double type then during execution the other operand will also be Long
Double. shaped likeIt is converted into operand and the result is of Long Double type.

if oneIf the operand is of double type then the second is also converted to double and the result is double.

Government is achieved.

if oneIf the operand is of Float type, then the other one is also converted to Float type and the output is of Float type. It

happens

if oneIf the operand is unsigned long int then the value will also be converted to unsigned long int and the result is

received in unsigned long int only.

Result of long int can be obtained by converting it into long int or both operands are unsigned long int. will

turn intoResult will be received in unsigned long int.

if oneIf the operand is unsigned long int then the second string is also changed to unsigned and the result is obtained as unsigned int. will

be.

The data type of the variable on the left side of the assignment operator is the expression EC.
shapedWill return data.

If we understand from example, we mean three variables respectively.int, long and double are of type:

int a;

long B;
double c;
Manual Type Conversion OR Casting
Er.Satish (Asst.Professor) E-mail: -satishjanagaljnu@gmail.com Mobile:- 9461320821/9799920821
When we have to do special kind of calculations, then weDo not use Automatic Type Conversion In
such a situation, we need toType conversion has to be done. In any expression like this someone
Calculates by changing the declared data type of the identifier and converting it to a new data type.
are like thisConversion is called casting. To perform calculation by converting the data type of a
variable into some other data type, we have to first specify the data type of the variable in which the
value of the variable is to be converted. आ

input output function

Many inverse and inverse functions are used in 'c' programming. All inverse and inverse functions are Has

been divided into two categories.

- Famtde Aanptu Aitptufunction


, Unformatted aanapattu aitapattu function

formats input outputfunction⇒

for anapatu and aitapatuIt is necessary to define the format of the value which is going to
be used in such a function in which it is called Anaptu or Aitpatu function.

asscanf(), printf()

printf() function is a one-page formatted function and scanf() is a one-page formatted function.
Anamscanf() The values are interpolated depending on the type of data type. Offormatted
meaning ta taapIt is mandatory to create specifier

asint a; scanf("%d",&a);

in aboveBy scanf() we are getting value in a. a is an integer variable hence integer in the function. data
type data typespecifier is getting value in a by scanf() in above. a is an integer variabale hence type of
integer data type in function specifier value in a by scanf() in above. getting into troublea is an integer
variable, so the function also has the type specifier %d of the integer data type. has been imposedc' is the
following data type specifier in programming like %

%d , int
%c , characters
%f , float
%s , strings

Er.Satish (Asst.Professor) E-mail: -satishjanagaljnu@gmail.com Mobile:- 9461320821/9799920821


printf() function is a formatted output function which outputs the value of all records.
Hope is killedThe format (type specifier) of the value to be returned is also defined in
the function.

asint a=50; printf("%d",&a);

In the above examplea is an integer variable whose value is 20. To get the octave of the value of
a forprintf() function has been used and type specifier %d of integer has also been used.

Unformatted input output function⇒

for anapatu and aitapatuSuch functions used in which the format of the value to be returned or
returned is not defined, they are called Unformatted return or return functions.

asgetchar(); getche(); gets(); putchar(); puts();

getchar() This is an unformatted invert function. This function can be used to invert a character
value. did this forFunction can manipulate a character value so this function must manipulate the
oneThere is no need to define the format of the value.

aschar a; a=getchar();

Branching Statements

at many places in the programDepending on the condition, the statement has to be executed from a
different point leaving the normal flow of control. The statements used for this type of selective
execution are called Conditional Statements or Branching Statements.

Er.Satish (Asst.Professor) E-mail: -satishjanagaljnu@gmail.com Mobile:- 9461320821/9799920821


Unit 3

control statements(structures):-
In Rogrammag language, Rogram'sStatements or structures that are used to coordinate the flow of execution Why

do they go?control statements(structures) are called

Control statements are of two types:

, conditional statements
, loop statements

1:-conditional statements:-

These are of the following types:

(a):-if statement:-

If statement is a control statement that is used to test a specific condition. This condition
exists only onceExecutes when the condition is true.

syntax-
if(condition)
,
statements
,

Er.Satish (Asst.Professor) E-mail:-satish ja Email:- 9461320821/ 9799920821


image Fig:+if flow chart

(b):–if-else statements:-

any special conditionIf-else statement is used to test if the condition is true. Soif
statement is executed if conditionIf false then execute else statement goes

syntax-
if(condition)
,
statement
,
otherwise

,
statement
,
image fig:-if else flow chart

(c):- switch statements:-

switch statement is a multi-way branch statement.

itstatement is also a selection statement that defines various paths for the execution of a program.
does
itServes as an alternative to the if-else statement.
syntax :-
switch(variable)
,
case constant 1;
statement(s);
break:

Er.Satish (Asst.Professor) E-mail:-satishjanagalj n 821/9799920 821


case constant 2;
statement(s);
break;
case constant 3;
statement(s);
break;
,
default
statement(s);
,

,2):-Loop statements:-

Through loops we can execute any one statement or many statements more than once
when untilThe condition is not satisfied.
loops statements are of the following types:
, while loop
, do-while loop
, for loop

1:-while loop:-

while loop is an entry-controlled loop. In this the statement continues to execute until One
The condition does not become true. It checks the condition first and then the statement. To
executes

syntax :-
while(condition)
,
statement1;
statement2;
,
,

Er.Satish (Asst.Professor) E-mail:-satish j 99920821


2:-do-while loop:-

itloop एक exit-controlled लपू ह.◌ै यह लपू भी while लपू की तरह ही ह परन्त यह पहल स्टटेमेंट को
execute करता ह तथा बाद में condition की जाचं करता ह.◌ै

itloop ensures that the program is executed at least once.

syntax :-
do
,
Statement1;
Statement2;
,
,
While(condition);

3:-For loop:-

this oneEntry is controlled loop. In this the statement is executed until the condition is true. it goesloop
has three components initialization statement, boolean statement and increment/decrement statement.
is made up of

syntax :-
for(initial condition, test condition; incrementor or decrementor)
,
statement1;
statement2;
,

Er.Satish (Asst.Professor) E-mail:-satishj a 799920821


Other statements:- Apart from these, there are some other statements which are as follows-

, 1 :-break statement
, 2:- Continue statement
1:-Break statement:-

Use of this statementIt is used to end the sequence of statements in switch statement. and
someoneThis is done to exit the loop immediately.

syntax:- break;

2:-continue statement:-

continue statement is used when we want to run the loop continuously with different iterations.
want and need ValueWant to skip other statements of loop for iteration.

syntax:- continue;

Er.Satish (Asst.Professor) E-mail: -satishjanagaljnu@gmail.com Mobile:- 9461320821/9799920821


Fig:-continue flowchart

functions in “c programming language”

Functions in c:-

Function is a piece of code. In other words, it is like a sub-program in a program. works


like that

While creating a program, sometimes we need to write some codes.The result obtained from
execution has to be used repeatedly in the program, in such a situation codes are not written again
and again. As a buskal functionIt is defined outside the main() function and used in one place. and
derived from functionThis function is called to use the result in the program.

The advantage of functions is that they save both our time and space.

of functionsyntax :-

function name(arg1, arg2, arg3….) {

statement1;
statement2;
statement3;
,
,
There are two types of functions in programming language “c”.

, Built-in-functions

Er.Satish (Asst.Professor) E-mail: -satishjanagaljnu@gmail.com Mobile:- 9461320821/9799920821


, user defined functions

1 :-Built-in-function

There are sublattice-functions and functions whoseThe prototype is preserved in the header file of the programming

language “C”. These functions can be called by simply typing their name in the program (call) is made and executed in the

program Examples of this happen:-scanf();, printf();, strcat(); Acad.

thisFunction – are also called library functions of “C” programming language. All these functions are
related to some Special “C” is Lauri Fall C. Ya Special Lauri Fallare called header files and their
extension name is .h. Hence, all the files located in the “C” directory and whose extended name is .h,
are called hair falls.

Some of his major mistakes in “C” are as follows –

, Stdio.h
, Math.h
, String.h
, Conio.h
, time.h
, ctype.h

2:- user defined function (Yasur Safaand Function):-

These are the defined functions which are created by the user at the time of writing the program i.e. they
are defined. Whatever the user needs, he can create the function as per his requirement.create can

A program for writing programs in the programming language “C”.main() function is required this function also This is the

most defined function of the program.Execution also starts from such program.

Name of function used in “C”Anything other than keywords can be entered.


after function ()It is necessary to put braces, it is an indicator of function. any
function into another function and intocan be called

Parameter Passing – What is parameter passing?

When someone in the programWhen the function is executed then the execution control is the calling
function. Sis transferred to the called function and executes the function definition there, and finally he
backGoes into calling function.

Er.Satish (Asst.Professor) E-mail: -satishjanagaljnu@gmail.com Mobile:- 9461320821/9799920821


WhenWhen execution control is transferred from the calling function to the called function, it is either
Jesus moreData values can be taken. These data values are called parameters.
So we can say that “Parameters are those data values that are passed from the calling function to the called

function.

In C, there are two types of parameters:

, Actual parameter
, formal parameter
Actual parameters are the parameters which are specified in the calling function.
Formal parameters are the parameters which are declared in the called function.
In C programming language, there are two ways to pass parameters, which are as follows -

, Call by value
, Call by reference

Call by value

In call by value, the values of actual parameters are copied into formal parameters and these
formal parameters are used in the called function.
"In the skyIf any change is made in the formal parameters, it does not affect the actual parameters. It
means that whenWhen execution control goes back to the calling function, the values of the actual
parameters remain the same. Madam, there will be no change in the prize.

Example:-
# include <stdio.h>
int sum(int n);
void main()
,
int a = 5;
printf(―\n The value of ‗a' before the calling function is = %d‖, a); a =
sum(a);
printf(―\n The value of ‗a' after calling the function is = %d‖, a); ,

int sum(int n)
,
n = n + 20;
printf(―\n Value of ‗n' in the called function is = %d‖, n);
return n;

Er.Satish (Asst.Professor) E-mail: -satishjanagaljnu@gmail.com Mobile:- 9461320821/9799920821


,

Call by reference

In call by reference, the memory address of the actual parameter is copied into the formal parameter.
This address is used to access the memory location of the actual parameter in the called function. goes

"If in thisIf any change is made in the formal parameter, it affects the actual parameters. Is"

Example:-
# include <stdio.h>
int sum(int*n);
void main()
,
int a = 5;
printf(―\n The value of ‗a' before the calling function is = %d‖, a);
sum(&a);
printf(―\n The value of ‗a' after calling the function is = %d‖, a); ,

int sum (int *n) {

* n = *n + 20;
printf(―\n value of ‗n' in the called function is = %d‖, n); ,

Variable Scope in C Programming Language


Er.Satish (Asst.Professor) E-mail: -satishjanagaljnu@gmail.com Mobile:- 9461320821/9799920821
According to the scope the variables are divided into two categories.Local Variable – Local
variables These are defined in small blocks of the program such as functions or control
statements.

Local Variable,When we define a variable inside a function then this local They are called
variables and we cannot manipulate them with actions outside the function.

Global Variable,Variables that are defined outside the abstract function are calledglobal variable It is said We
can implement this variable anywhere in the program.Can access.

Formal Parameter,When we define a functionin the function signature If we define


a variable then thisFormal parameter is called

Unit 4
C - Pointer Introduction

everyoneVariable has a memory address and to show the memory address the programmer has to
put '&' (address operator) before the variable.

for eg.

int a ;
printf("Address of a : %d", &a);
Output:
Address of : 6356748 // 6356748 is address of variable

AssiThe value of the variable is accessed with the help of memory address, these are called 'Pointers'.

Pointer holds the address of another variable.

everyoneVariable has an address. Only when a variable is declared does it have a memory location. but that

There is a store.

Er.Satish (Asst.Professor) E-mail: -satishjanagaljnu@gmail.com Mobile:- 9461320821/9799920821


For Example

int a = 10;
Variable_or_Location_name a
Variable_value 10 Location

Memory AddC - Referencing Operator

C - Referencing Operator

Referencing means holding the address of another variable.

everyTo hold the address of the variable whose address is to be held and the pointer in which it is to
be held, the data type of both should be the same.

for eg.

int a = 10;
int*ptr;
ptr = &a; //address of a stored in ptrress 6356748

C - Dereferencing Operator

In dereferencing, asterisk ( * ) is used but in pointer it is also called Dereferencing Operator.

In dereferencing the value stored in the pointer is accessed.

int a = 10;
int*ptr;
ptr = &a;
int b = *ptr; // ptr means dereferencing
printf("Value of a : %d", b);
Output :
Value of a : 10

C - What is String?

Strings are groups of characters.


Strings are one-dimensional arrays that contain only characters.
The first character of String is 'NULL'(\0).

Er.Satish (Asst.Professor) E-mail: -satishjanagaljnu@gmail.com Mobile:- 9461320821/9799920821


if completeIf string is to be written then it is written in double quotes (" "). If each character If you want to
write then thisIt is written in single quotes (' '). The data type of String is character (char).

Example for Single Character String

char str1[6] = {'H', 'e', 'l', 'l', 'o', '\0'};


Example for Multiple Character String
char str2[6] = "Hello";

C - String Library Functions

# include <stdio.h>
# include <string.h>
int main(){
char str1[20] = "Welcome";
char str2[20] = "Friend";
strcat(str1, str2);
printf("Concatenation String : %s", str1);
return 0;
,
C - Introduction to Structure

Structure is a collection of different data types.


ifIf you want to use structure then use 'struct' keyword. The
structure is similar to that of an array.
Array is a collection of similar data types and Structure is a collection of different data types.

Er.Satish (Asst.Professor) E-mail: -satishjanagaljnu@gmail.com Mobile:- 9461320821/9799920821


The declaration of every variable in the structure is called 'member'.
Structure allocates separate memory for each member.

Syntax for Structure Definition

struct structure_name{
data_type member 1;
data_type member 2;
,
----------------
data_type memeber n;
, Structure Variable Declaration

There are two ways to declare variables of structure.

WhenWhen the definition of structure is written, the structure variable is also written there.
Structure variables are also used inside the main() function.

Syntax for Structure Variable outside of Structure Definition

struct structure_name{
data_type member 1;
data_type member 2;
,
----------------
data_type memeber n;
}structure_variable(s);

Example for Structure Variable in main() Function

struct Employee{
int emp_id;
char emp_name[30];
float salary;
}info;

How are the members or elements of the structure accessed?


Syntax for Accessing Structure members
structure_variable_name . member_of_structure = value(optional);
Example for Accessing Structure members

Er.Satish (Asst.Professor) E-mail: -satishjanagaljnu@gmail.com Mobile:- 9461320821/9799920821


info.emp_id = 10;

Structure Initialization

There are two types of Structure Initialization.

Type 1:
struct Employee{
int emp_id;
char emp_name[30];
float salary;
,
int main(){
struct Employee info;

info.emp_id = 34;
strcpy( info.emp_name, "Raj Biradar");
info.salary = 20000.00;
,

Type 2:
struct Employee{
int emp_id;
char emp_name[30];
float salary;
,
int main(){
struct Employee info = {34, "Raj Biradar", 20000.00};
,

Structure Memory Representation

Er.Satish (Asst.Professor) E-mail: -satishjanagaljnu@gmail.com Mobile:- 9461320821/9799920821


If seen in memory representation, since the system is 32-bit, the size of the integer is '4 Bytes'. The
size of the character is equal to the size given in the bracket and the size of float is only 4 bytes. The
size of the structure is equal to the size of all the given members. for eg. 4 (int) + 20 (char) + 4 (float)
= 28 Bytes.

Structure working with size(sizeof)

The members of the structure allocate different memory and the size of the entire structure is determined by their

members. is married to

C - Structure using Pointer

Members of Structure are accessed in two ways. ,


(dot operator)
- > (pointer operator)
Accessing Members using pointer Operator
Source Code :
# include <stdio.h>
# include <string.h>
struct Employee {
int emp_id;
char emp_name[30];
float salary;
,
int main(){
struct Employee info;
struct Employee *ptr;
ptr = &info;

Er.Satish (Asst.Professor) E-mail: -satishjanagaljnu@gmail.com Mobile:- 9461320821/9799920821


ptr->emp_id = 34;
strcpy( ptr->emp_name, "Raj Biradar");
ptr->salary = 20000.00;
printf( "Employee id is : %d\n", ptr->emp_id);
printf( "Employee name is %s\n", ptr->emp_name);
printf( "Employee salary is : %f", ptr->salary); return
0;
,
< /pre>
Output :
Employee id is : 34 Employee name
is Raj Biradar Employee salary is :
20000.000000

C- Array of Structure
Syntax for Array Structure Declaration
struct structure_name structure_variable_name[array_size];
Example for Array Structure Declaration
struct Employee info[3];

C Type Casting Introduction

In Type Casting one variable is converted into another variable.


EXP. If a variable is converted from integer to float or from float to integer, then it is called Type
Casting.
Pointers and Arrays

Pointers and Arrays are very closely related. One thing should always be remembered. Be aware thatArray name always

Represents the Base Address of the Array. Therefore intArray + j Expression is an address of the
memory location of the required value. You can think that intArray + 3 Expression intArray OfIn
address3 will add.
C - Pointer And Array

Pointer and Array is a method of Pointer. Find the addresses of multiple elements of an
array only oneis held in pointer.

Er.Satish (Asst.Professor) E-mail: -satishjanagaljnu@gmail.com Mobile:- 9461320821/9799920821


for eg int num[5] = {1, 2, 3, 4, 5};
over hereTo store the values of the elements of the array in the pointer, it is necessary to give the base address to the pointer. If

base address is not specified so thatWill give output, but it will not print anything in the output.
for example

ptr = num ;
num[0] or num is the base address. You can use either of these.

SocCode:

# include <stdio.h>
# include <conio.h>
void main () {
int i,*ptr;
int num[5] = {1, 2, 3, 4, 5};
ptr = num;
for (i=0; i<5; i++) {

printf("
pointer of array = %d", *ptr);
ptr++;
,
getch();
,
Aitpattu:
pointer of array = 1
pointer of array = 2
pointer of array = 3
pointer of array = 4
pointer of array = 5

Er.Satish (Asst.Professor) E-mail: -satishjanagaljnu@gmail.com Mobile:- 9461320821/9799920821


Unit 5
C - What is Union?

Union is a collection of different data types.


ifIf you want to use union then use 'union' keyword. Union is
similar to this structure.
The declaration of every variable in the union is called 'member'.
Union does not allocate separate memory for each member. Union
members share the same memory location.
In a union, the member who is bigger in size is the size of the entire union.
Union Variable Declaration

There are two ways to declare variables of structure.

WhenWhen the definition of union is written, the structure variable is also written there.

The union variable is also used inside the main() function.


Syntax for Union Variable outside of Union Definition

struct union_name{
data_type member 1;
data_type member 2;
,
----------------
data_type memeber n;
}Union_variable(s);
C - Union using Pointer

Union members are accessed in two ways. , (dot


operator)
- > (pointer operator)
Accessing Members using pointer Operator

Er.Satish (Asst.Professor) E-mail: -satishjanagaljnu@gmail.com Mobile:- 9461320821/9799920821


program to show the use of structure

To declare (define) structure we use “struct” keyword. Help of struct keyword s up


differentYou create a new data type by grouping variables of data types.

File Handling in C,foal head What is it?

In C language, File is a container which is used to store data. File Handling is a process in
which data is stored in a file using a program.
In C, we can perform multiple operations on a file using file handling. ) can do as –files

ToCreating, updating, reading, and deleting etc. One


The following operations can be performed on the file:
- newCreating the file.
- to the foalTo open.
- to the foalTo read.
- to the foalTo write.
- to the foalTo delete.
- to the foalto close.
,

Functions for File Handling –


No. Function Description
1 fopen() hopeThe function of the function is to open the file.

Er.Satish (Asst.Professor) E-mail: -satishjanagaljnu@gmail.com Mobile:- 9461320821/9799920821


2 fprintf() Through this function weCan write data in the
3 fscanf() file. by himData is read from the file. One in
4 fputc() the fall by using hopecharacter is written
5 fgetc() itReads one character from the file.
6 fclose() Foal through this functionis closed it is
7 fseek() givenSets the file pointer at position.
8 fputw() itwrites integer to file itReads
9 fgetw() integer from file. itReturns the
10 ftell() current position. itSets the file pointer at
11 rewind() the beginning of the file.

Opening the file –

someoneBefore we can read, write, or update a file, we have to open the file.
forfopen() function is used. Its syntax is given below: FILE
*fopen( const char * filename, const char * mode );
The fopen() function takes two parameters. First is filename and second is mode.

WeFollowing modes can be used in fopen() function to open the file:

mode Description
r itOpens the text folder in read mode.
w itOpens the text file in write mode.
a itOpens the text file in append mode.
r+ itOpens the text folder in both read and write modes.
w+ itOpens the text folder in both read and write modes. it
a+ Opens the text folder in both read and write modes.
rb itOpens the binary file in read mode. it
wb Opens the binary file in write mode.
ab itOpens the binary file in append mode.
rb+ itOpens the binary file in both read and write modes. it
wb+ Opens the binary file in both read and write modes. it
ab+ Opens the binary file in both read and write modes.

closing the file –

Er.Satish (Asst.Professor) E-mail: -satishjanagaljnu@gmail.com Mobile:- 9461320821/9799920821


After doing any work in the fall we should always remember the fall.should close To close the file
fclose() function is used. Its syntax is as follows: -
fclose(fptr);

Reading data from file –

in foalTo read the data, fscanf(), fgets() and fgetc() functions are used.
AsakaThe syntax is given below:
int fscanf(FILE *stream, const char *charPointer[])

Writing the file –

In C language, fprintf(), fputs(), fputc() functions are used to write the value.

int fprintf(FILE *stream, char *string[])

C - Referencing Operator

Referencing means holding the address of another variable.

everyTo hold the address of the variable whose address is to be held and the pointer in which it is to
be held, the data type of both should be the same.

for eg.

int a = 10;
int*ptr;
ptr = &a; //address of a stored in ptr

C - Input and Output

There are two main in-built functions of Input and Output.

, scanf (Input)

, printf (Output)

1. scanf (Input Function)

Er.Satish (Asst.Professor) E-mail: -satishjanagaljnu@gmail.com Mobile:- 9461320821/9799920821


scanf is a standard library function.
Any value like numeric, characters and string is input through scanf. To use
scanf in the program, stdio.h or header file has to be included.
scanf is used to read data from the keyboard. Syntax
for scanf() function scanf("format_specifier(s)",
&variable_list(s) );

having a good timeIn the example two format specifiers and two variables & (address operator or ampersand)

For together. everyData types have different format specifiers. Here %d is the format specifier of integer data

type. To read data from keyboard, '&' has to be given along with the variable. If input has to be given then the

compiler has to tell the address of the variable.

for eg.
scanf( "%d %d", &a, &b );

2. printf (Output Function)

printf is a standard library function.


printf is used to write data on the screen.
To use printf in the program, stdio.h or header file has to be included. Syntax
for scanf() function
printf("String / format_specifier(s) / escape sequence(s)", variable_list(s) );

for string
only herestring has been used. String is written in double quotes(" ") and then
printf("Hello World !"); for format
specifier and variable
thisscanf prints the input numeric (%d), character(%c), float(%f) or string(%s) as output.
Ha |
over hereThe address(&) of the variable is not given.
printf("%d",a);
for escape sequence
over here\n(newline) or escape sequence is taken in printf,
printf("\n");

and something elseInput/Output Library Functions

Er.Satish (Asst.Professor) E-mail: -satishjanagaljnu@gmail.com Mobile:- 9461320821/9799920821


getchar() and putchar() (for single character)
gets() and puts() (for string)

1. getchar(Input) and putchar(Output) (for single character)

getchar and putchar are both standard input/output (stdio.h) library functions of this header file, hence it is

necessary to #include <stdio.h> or provide a preprocessor.

Getchar is used to allow input from the user i.e. one character.
ifIf user gives more than one character in input then putchar will print only one character in output first.
does.

For Example

Source Code:
# include <stdio.h>
int main( ) {
int i;
printf( "Enter a character : "); i
= getchar();
printf( "Entered character is : ");
putchar(i);
return 0;
,

Output :
Enter a character :hello
Entered character is : h

2. gets() and puts() (for string)

gets and puts are both standard input/output (stdio.h) library functions of this header file, so
# include <stdio.h> or it is necessary to provide preprocessor.

Gets is used to allow taking input from the user i.e. string. puts prints the string for input
as output.

Er.Satish (Asst.Professor) E-mail: -satishjanagaljnu@gmail.com Mobile:- 9461320821/9799920821


For Example

Source Code:
# include <stdio.h>
int main(){
char name[20];
printf( "Enter your name: \n");
gets(name);
printf( "You name is : ");
puts(name);
return 0;
,
Output :
Enter your name : Rakesh
Your name is : Rakesh

Er.Satish (Asst.Professor) E-mail: -satishjanagaljnu@gmail.com Mobile:- 9461320821/9799920821

You might also like