C Programming
C Programming
C Programming
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
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
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.
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
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.
In C the return values of functions are not always required. A single statement can contain multiple
# 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
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.
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!
, Create
, compile
, Execute or Run
, Get the Output
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
, Documentation section
, Link Section
, definition section
, Global declaration section
, Function prototype declaration section
, Main function
, User defined function definition section
Please note that a C program mayn't have all below mentioned sections except main function and link
sections.
The problem which is to be solved by the computer must be precisely defined. In the problem, a
A list of all the tasks related to what life has to do with the group of prospects should be made. TwoA
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
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 -
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
C - Structure of Program
Program Structure:
Is divided.
Preprocessor
main function
,
printf("Hello World");
return 0;
,
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.,
5.return 0;,return 0 This allows the program to close. This '0' returns to the main function
does.
# include <stdio.h>
int main()
,
printf("Hello World!");
return 0;
C - integer
If you want to print integer in output then print it in output with '%d' format specifier.
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.
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.
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
Data items of the same type are used to keep them in consecutive memory locations.
Features:
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.
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.
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 :
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
ifIf the data type of the array was character then there would have been a sequence of 1-1 in the address.
for eg.
for eg.
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
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
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
Syntax:
Types of Constant :
, Integer Constant
, Decimal Constant
, octal constant
, Hexadecimal Constant
, Floating-point / Real Constant
, Character Constant
, String Constant
, Preprocessor
with examples
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
# 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: 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
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
Result of long int can be obtained by converting it into long int or both operands are unsigned long int. will
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. आ
Many inverse and inverse functions are used in 'c' programming. All inverse and inverse functions are Has
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
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.
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.
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.
control statements(structures):-
In Rogrammag language, Rogram'sStatements or structures that are used to coordinate the flow of execution Why
, conditional statements
, loop statements
1:-conditional statements:-
(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
,
(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
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:
,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;
,
,
itloop एक exit-controlled लपू ह.◌ै यह लपू भी while लपू की तरह ही ह परन्त यह पहल स्टटेमेंट को
execute करता ह तथा बाद में condition की जाचं करता ह.◌ै
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;
,
, 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;
Functions in c:-
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 :-
statement1;
statement2;
statement3;
,
,
There are two types of functions in programming language “c”.
, Built-in-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
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.
, Stdio.h
, Math.h
, String.h
, Conio.h
, time.h
, ctype.h
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.
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.
function.
, 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;
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); ,
* n = *n + 20;
printf(―\n value of ‗n' in the called function is = %d‖, n); ,
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.
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'.
everyoneVariable has an address. Only when a variable is declared does it have a memory location. but that
There is a store.
int a = 10;
Variable_or_Location_name a
Variable_value 10 Location
C - Referencing Operator
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
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?
# 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
struct structure_name{
data_type member 1;
data_type member 2;
,
----------------
data_type memeber n;
, Structure Variable Declaration
WhenWhen the definition of structure is written, the structure variable is also written there.
Structure variables are also used inside the main() function.
struct structure_name{
data_type member 1;
data_type member 2;
,
----------------
data_type memeber n;
}structure_variable(s);
struct Employee{
int emp_id;
char emp_name[30];
float salary;
}info;
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};
,
The members of the structure allocate different memory and the size of the entire structure is determined by their
members. is married to
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];
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.
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
WhenWhen the definition of union is written, the structure variable is also written there.
struct union_name{
data_type member 1;
data_type member 2;
,
----------------
data_type memeber n;
}Union_variable(s);
C - Union using Pointer
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
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.
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.
in foalTo read the data, fscanf(), fgets() and fgetc() functions are used.
AsakaThe syntax is given below:
int fscanf(FILE *stream, const char *charPointer[])
In C language, fprintf(), fputs(), fputc() functions are used to write the value.
C - Referencing Operator
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
, scanf (Input)
, printf (Output)
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
for eg.
scanf( "%d %d", &a, &b );
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");
getchar and putchar are both standard input/output (stdio.h) library functions of this header file, hence it is
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
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.
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