SlideShare a Scribd company logo
saStudent Name: Sumit Kumar Singh Course: BCA
Registration Number: 1308009955 LC Code: 01687
Subject: Programming in C Subject Code: 1020
Q.No.1. Explain any 5 category of C operators.
Ans. C language supports many operators, these operators are classified in following categories:
• Arithmetic operators
• Unary operator
• Conditional operator
• Bitwise operator
• Increment and Decrement operators
Arithmetic Operators
The basic operators for performing
The bitwise operators &, |, ^ and ~ operate on integers thought of as binary numbers or strings of
bits. The & operator is bitwise AND, the | operator is bitwise OR, the ^ operator is bitwise
excusive-OR (XOR) and the ~ operator is a bitwise negation or complement. You might define
the 3rd
bit as the “verbose” flag bit by defining
#define VERBOSE 4
Then you can “turn the verbose bit on” in an integer variable flags by executing
flags = flags | VERBOSE;
and turn it off with
flags = flags &~VERBOSE
and test whether it’s set with
if(flags % VERBOSE)
Increment and Decrement Operators
To add or subtract constant 1 to a variable, C provides a set of shortcuts: the auto increment and
auto decrement operators. For instance,
++i add 1 to i
-- j subtract 1 from j
Q.No.2 Write a program to sum integers entered interactively using while loop.
Ans:
#include<stdio.h>
#include <conio.h>
int main(void)
{
long num;
long sum = 0;
int status;
clrscr();
printf("Please enter an integer to be summed. ");
printf("Enter q to quit.n");
status = scanf("%ld", &num);
while (status == 1)
{
sum = sum + num;
printf("Please enter next integer to be summed. ");
printf("Enter q to quit.n");
status = scanf("%ld", &num);
}
printf("Those integers sum to %ld.n", sum);
return 0;
getch();
}
Q.No.3 Write a Program to find average length of several lines of text for illustrated global variables.
Ans:
#include<stdio.h>
#include<conio.h>
// Declare global variables outside of all the functions
int sum=0; // total number of characters
int lines=0; // total number of lines
void main()
{
int n; // number of characters in given line
float avg; // average number of characters per line
clrscr();
void linecount(void); // function declaration
float cal_avg(void);
printf(“Enter the text below:n”);
while((n=linecount())>0)
{
sum+=n;
++lines;
}
avg=cal_avg();
printf(“n tAverage number of characters per line: %5.2f”, avg);
}
void linecount(void)
{
// read a line of text and count the number of characters
char line[80];
int count=0;
while((line[count]=getchar())!=’n’)
++count;
return count;
}
float cal_avg(void)
{
// compute average and return
return (float)sum/lines;
getch();
}
Q.No.4 Explain the basic concept of Pointer addition and subtraction. Also give one example program
for each.
Ans. Addition Pointer
A pointer is a variable which contains the address in memory of another variable. We can have a
Pointer to any variable type. Use of & symbols that gives the “address a variable” and is known
as the unary or monadic operator. * symbol is used for the “contents of an object pointed to by a
pointer” and we call it the indirection or dereference operator. Simple pointer declaration has the
following general format:
Datatype *variablename
For example:
int *ip;
This program performs addition of two numbers using pointers. In our program we have two two
integer variables x, y and two pointer variables p and q. Firstly we assign the addresses of x and
y to p and q respectively and then assign the sum of x and y to variable sum. Note that & is
address of operator and * is value at address operator.
Example for pointers addition:-
#include<stdio.h>
#include<conio.h>
int main()
{
int first, second, *p, *q, sum;
clrcsr();
printf("Enter two integers to addn");
scanf("%d%d", &first, &second);
p = &first;
q = &second;
sum = *p + *q;
printf("Sum of entered numbers = %dn",sum);
return 0;
getch();
}
Subtraction pointer
Example for pointers addition:-
#include<stdio.h>
#include<conio.h>
int main()
{
int first, second, *p, *q, sub; //sub taken as a variable of subtraction pointer.
clrcsr();
printf("Enter two integers to addn");
scanf("%d%d", &first, &second);
p = &first;
q = &second;
sub = *p - *q;
printf("Sum of entered numbers = %dn",sum);
return 0;
getch();
}
Q.No.5. Explain the concept of NULL pointer. Also give one example program.
Ans. Null Pointers
We said that the value of a pointer variable is a pointer to some other
variable. There is one other value a pointer may have: it may be set to a null
pointer. A null pointer is a special pointer value that is known not to point
anywhere. What this means that no other valid pointer, to any other variable
or array cell or anything else, will ever compare equal to a null pointer.
The most straightforward way to “get'' a null pointer in your program is by
using the predefined constant NULL, which is defined for you by several
standard header files, including <stdio.h>, <stdlib.h>, and <string.h>. To
initialize a pointer to a null pointer, you might use code like
#include <stdio.h>
int *ip = NULL;
and to test it for a null pointer before inspecting the value pointed to, you
might use code like
if(ip != NULL)
printf("%dn", *ip);
It is also possible to refer to the null pointer by using a constant 0, and you
will see some code that sets null pointers by simply doing
int *ip = 0;
(In fact, NULL is a preprocessor macro which typically has the value, or
replacement text, 0.)
Furthermore, since the definition of “true'' in C is a value that is not equal to
0, you will see code that tests for non-null pointers with abbreviated code
like
if(ip)
printf("%dn", *ip);
This has the same meaning as our previous example; if(ip) is equivalent to
if(ip != 0) and to if(ip != NULL).
All of these uses are legal, and although the use of the constant NULL is
recommended for clarity, you will come across the other forms, so you
should be able to recognize them.
You can use a null pointer as a placeholder to remind yourself (or, more
importantly, to help your program remember) that a pointer variable does
not point anywhere at the moment and that you should not use the “contents
of'' operator on it (that is, you should not try to inspect what it points to, since
it doesn't point to anything). A function that returns pointer values can return
a null pointer when it is unable to perform its task. (A null pointer used in this
way is analogous to the EOF value that functions like getchar return.)
As an example, let us write our own version of the standard library function
strstr, which looks for one string within another, returning a pointer to the
string if it can, or a null pointer if it cannot.
Example: Here is the function, using the obvious brute-force algorithm:
at every character of the input string, the code checks for a match there of
the pattern string:
#include <stddef.h>
char *mystrstr(char input[], char pat[])
{
char *start, *p1, *p2;
for(start = &input[0]; *start != '0'; start++)
{ / /for each position in input string...
p1 = pat; // prepare to check for pattern string there
p2 = start;
while(*p1 != '0')
{
if(*p1 != *p2) /* characters differ */
break;
p1++;
p2++;
}
if(*p1 == '0') /* found match */
return start;
}
return NULL;
}
Q.No.6. Write a Program to search the specified file, looking for the character using command line
arguments.
Ans. The program to search the specified file, looking for the character using command line
argument
Example program takes two command-line arguments. The first is the name of a file, the second
is a character. The program searches the specified file, looking for the character. If the file
contains at least one of these characters, it reports this fact. This program uses argv to access
the file name and the character for which to search.
/*Search specified file for specified character. */
#include <stdio.h>
#include <stdlib.h>
void main(int argc, char *argv[])
{
FILE *fp; /* file pointer */
char ch;
/* see if correct number of command line arguments */
if(argc !=3)
{
printf("Usage: find <filename> <ch>n");
exit(1);
}
/* open file for input */
if ((fp = fopen(argv[1], "r"))==NULL) {
printf("Cannot open file n");
exit(1);
{
FILE *fp; /* file pointer */
char ch;
/* see if correct number of command line arguments */
if(argc !=3)
{
printf("Usage: find <filename> <ch>n");
exit(1);
}
/* open file for input */
if ((fp = fopen(argv[1], "r"))==NULL) {
printf("Cannot open file n");
exit(1);
Ad

Recommended

C Programming Assignment
C Programming Assignment
Vijayananda Mohire
 
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
C programming(part 3)
C programming(part 3)
Dr. SURBHI SAROHA
 
Lecture 17 - Strings
Lecture 17 - Strings
Md. Imran Hossain Showrov
 
Programming in C (part 2)
Programming in C (part 2)
Dr. SURBHI SAROHA
 
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
Lecture 18 - Pointers
Lecture 18 - Pointers
Md. Imran Hossain Showrov
 
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
C Programming Unit-3
C Programming Unit-3
Vikram Nandini
 
Unit 8. Pointers
Unit 8. Pointers
Ashim Lamichhane
 
C programming session 02
C programming session 02
Dushmanta Nath
 
C programming session 05
C programming session 05
Dushmanta Nath
 
Types of pointer in C
Types of pointer in C
rgnikate
 
C pointers
C pointers
Aravind Mohan
 
C programming session 01
C programming session 01
Dushmanta Nath
 
Lecture 8- Data Input and Output
Lecture 8- Data Input and Output
Md. Imran Hossain Showrov
 
C programming(Part 1)
C programming(Part 1)
Dr. SURBHI SAROHA
 
Lecture 6- Intorduction to C Programming
Lecture 6- Intorduction to C Programming
Md. Imran Hossain Showrov
 
Presentation on pointer.
Presentation on pointer.
Md. Afif Al Mamun
 
Advanced C programming
Advanced C programming
Claus Wu
 
Strings
Strings
Saranya saran
 
Lecturer23 pointersin c.ppt
Lecturer23 pointersin c.ppt
eShikshak
 
Pointers in C
Pointers in C
Vijayananda Ratnam Ch
 
Lập trình C
Lập trình C
Viet NguyenHoang
 
Pointers
Pointers
Swarup Kumar Boro
 
Dynamic Memory Allocation in C
Dynamic Memory Allocation in C
Vijayananda Ratnam Ch
 
C language basics
C language basics
Nikshithas R
 
Ponters
Ponters
Mukund Trivedi
 
C programming
C programming
Karthikeyan A K
 
An imperative study of c
An imperative study of c
Tushar B Kute
 

More Related Content

What's hot (20)

C Programming Unit-3
C Programming Unit-3
Vikram Nandini
 
Unit 8. Pointers
Unit 8. Pointers
Ashim Lamichhane
 
C programming session 02
C programming session 02
Dushmanta Nath
 
C programming session 05
C programming session 05
Dushmanta Nath
 
Types of pointer in C
Types of pointer in C
rgnikate
 
C pointers
C pointers
Aravind Mohan
 
C programming session 01
C programming session 01
Dushmanta Nath
 
Lecture 8- Data Input and Output
Lecture 8- Data Input and Output
Md. Imran Hossain Showrov
 
C programming(Part 1)
C programming(Part 1)
Dr. SURBHI SAROHA
 
Lecture 6- Intorduction to C Programming
Lecture 6- Intorduction to C Programming
Md. Imran Hossain Showrov
 
Presentation on pointer.
Presentation on pointer.
Md. Afif Al Mamun
 
Advanced C programming
Advanced C programming
Claus Wu
 
Strings
Strings
Saranya saran
 
Lecturer23 pointersin c.ppt
Lecturer23 pointersin c.ppt
eShikshak
 
Pointers in C
Pointers in C
Vijayananda Ratnam Ch
 
Lập trình C
Lập trình C
Viet NguyenHoang
 
Pointers
Pointers
Swarup Kumar Boro
 
Dynamic Memory Allocation in C
Dynamic Memory Allocation in C
Vijayananda Ratnam Ch
 
C language basics
C language basics
Nikshithas R
 
Ponters
Ponters
Mukund Trivedi
 

Similar to Assignment c programming (20)

C programming
C programming
Karthikeyan A K
 
An imperative study of c
An imperative study of c
Tushar B Kute
 
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
sudhakargeruganti
 
Pointers in C Language
Pointers in C Language
madan reddy
 
UNIT 4 POINTERS.pptx pointers pptx for basic c language
UNIT 4 POINTERS.pptx pointers pptx for basic c language
wwwskrilikeyou
 
string , pointer
string , pointer
Arafat Bin Reza
 
C librabry 55 functions.pdfljhghmhiguyftg
C librabry 55 functions.pdfljhghmhiguyftg
saimukesh19
 
POEPPSGNHwkejnkweoewnkenjwjewkjewoewkjewijewjk
POEPPSGNHwkejnkweoewnkenjwjewkjewoewkjewijewjk
hanumanthumanideeph6
 
Pointer in C
Pointer in C
bipchulabmki
 
Function in c program
Function in c program
umesh patil
 
Functions and pointers_unit_4
Functions and pointers_unit_4
MKalpanaDevi
 
C programm.pptx
C programm.pptx
SriMurugan16
 
Array Cont
Array Cont
Ashutosh Srivasatava
 
pointers.pptx
pointers.pptx
s170883BesiVyshnavi
 
C function
C function
thirumalaikumar3
 
presentation_pointers_1444076066_140676 (1).ppt
presentation_pointers_1444076066_140676 (1).ppt
georgejustymirobi1
 
c program.ppt
c program.ppt
mouneeshwarans
 
7 functions
7 functions
MomenMostafa
 
13092119343434343432232323121211213435554
13092119343434343432232323121211213435554
simplyamrita2011
 
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
amrit47
 
An imperative study of c
An imperative study of c
Tushar B Kute
 
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
sudhakargeruganti
 
Pointers in C Language
Pointers in C Language
madan reddy
 
UNIT 4 POINTERS.pptx pointers pptx for basic c language
UNIT 4 POINTERS.pptx pointers pptx for basic c language
wwwskrilikeyou
 
C librabry 55 functions.pdfljhghmhiguyftg
C librabry 55 functions.pdfljhghmhiguyftg
saimukesh19
 
POEPPSGNHwkejnkweoewnkenjwjewkjewoewkjewijewjk
POEPPSGNHwkejnkweoewnkenjwjewkjewoewkjewijewjk
hanumanthumanideeph6
 
Function in c program
Function in c program
umesh patil
 
Functions and pointers_unit_4
Functions and pointers_unit_4
MKalpanaDevi
 
presentation_pointers_1444076066_140676 (1).ppt
presentation_pointers_1444076066_140676 (1).ppt
georgejustymirobi1
 
13092119343434343432232323121211213435554
13092119343434343432232323121211213435554
simplyamrita2011
 
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
amrit47
 
Ad

Recently uploaded (20)

HistoPathology Ppt. Arshita Gupta for Diploma
HistoPathology Ppt. Arshita Gupta for Diploma
arshitagupta674
 
Pests of Maize: An comprehensive overview.pptx
Pests of Maize: An comprehensive overview.pptx
Arshad Shaikh
 
LAZY SUNDAY QUIZ "A GENERAL QUIZ" JUNE 2025 SMC QUIZ CLUB, SILCHAR MEDICAL CO...
LAZY SUNDAY QUIZ "A GENERAL QUIZ" JUNE 2025 SMC QUIZ CLUB, SILCHAR MEDICAL CO...
Ultimatewinner0342
 
How to use search fetch method in Odoo 18
How to use search fetch method in Odoo 18
Celine George
 
Photo chemistry Power Point Presentation
Photo chemistry Power Point Presentation
mprpgcwa2024
 
Code Profiling in Odoo 18 - Odoo 18 Slides
Code Profiling in Odoo 18 - Odoo 18 Slides
Celine George
 
INDUCTIVE EFFECT slide for first prof pharamacy students
INDUCTIVE EFFECT slide for first prof pharamacy students
SHABNAM FAIZ
 
Birnagar High School Platinum Jubilee Quiz.pptx
Birnagar High School Platinum Jubilee Quiz.pptx
Sourav Kr Podder
 
Tanja Vujicic - PISA for Schools contact Info
Tanja Vujicic - PISA for Schools contact Info
EduSkills OECD
 
GREAT QUIZ EXCHANGE 2025 - GENERAL QUIZ.pptx
GREAT QUIZ EXCHANGE 2025 - GENERAL QUIZ.pptx
Ronisha Das
 
ECONOMICS, DISASTER MANAGEMENT, ROAD SAFETY - STUDY MATERIAL [10TH]
ECONOMICS, DISASTER MANAGEMENT, ROAD SAFETY - STUDY MATERIAL [10TH]
SHERAZ AHMAD LONE
 
Peer Teaching Observations During School Internship
Peer Teaching Observations During School Internship
AjayaMohanty7
 
A Visual Introduction to the Prophet Jeremiah
A Visual Introduction to the Prophet Jeremiah
Steve Thomason
 
LDMMIA Yoga S10 Free Workshop Grad Level
LDMMIA Yoga S10 Free Workshop Grad Level
LDM & Mia eStudios
 
How to use _name_search() method in Odoo 18
How to use _name_search() method in Odoo 18
Celine George
 
OBSESSIVE COMPULSIVE DISORDER.pptx IN 5TH SEMESTER B.SC NURSING, 2ND YEAR GNM...
OBSESSIVE COMPULSIVE DISORDER.pptx IN 5TH SEMESTER B.SC NURSING, 2ND YEAR GNM...
parmarjuli1412
 
List View Components in Odoo 18 - Odoo Slides
List View Components in Odoo 18 - Odoo Slides
Celine George
 
How payment terms are configured in Odoo 18
How payment terms are configured in Odoo 18
Celine George
 
NSUMD_M1 Library Orientation_June 11, 2025.pptx
NSUMD_M1 Library Orientation_June 11, 2025.pptx
Julie Sarpy
 
THE PSYCHOANALYTIC OF THE BLACK CAT BY EDGAR ALLAN POE (1).pdf
THE PSYCHOANALYTIC OF THE BLACK CAT BY EDGAR ALLAN POE (1).pdf
nabilahk908
 
HistoPathology Ppt. Arshita Gupta for Diploma
HistoPathology Ppt. Arshita Gupta for Diploma
arshitagupta674
 
Pests of Maize: An comprehensive overview.pptx
Pests of Maize: An comprehensive overview.pptx
Arshad Shaikh
 
LAZY SUNDAY QUIZ "A GENERAL QUIZ" JUNE 2025 SMC QUIZ CLUB, SILCHAR MEDICAL CO...
LAZY SUNDAY QUIZ "A GENERAL QUIZ" JUNE 2025 SMC QUIZ CLUB, SILCHAR MEDICAL CO...
Ultimatewinner0342
 
How to use search fetch method in Odoo 18
How to use search fetch method in Odoo 18
Celine George
 
Photo chemistry Power Point Presentation
Photo chemistry Power Point Presentation
mprpgcwa2024
 
Code Profiling in Odoo 18 - Odoo 18 Slides
Code Profiling in Odoo 18 - Odoo 18 Slides
Celine George
 
INDUCTIVE EFFECT slide for first prof pharamacy students
INDUCTIVE EFFECT slide for first prof pharamacy students
SHABNAM FAIZ
 
Birnagar High School Platinum Jubilee Quiz.pptx
Birnagar High School Platinum Jubilee Quiz.pptx
Sourav Kr Podder
 
Tanja Vujicic - PISA for Schools contact Info
Tanja Vujicic - PISA for Schools contact Info
EduSkills OECD
 
GREAT QUIZ EXCHANGE 2025 - GENERAL QUIZ.pptx
GREAT QUIZ EXCHANGE 2025 - GENERAL QUIZ.pptx
Ronisha Das
 
ECONOMICS, DISASTER MANAGEMENT, ROAD SAFETY - STUDY MATERIAL [10TH]
ECONOMICS, DISASTER MANAGEMENT, ROAD SAFETY - STUDY MATERIAL [10TH]
SHERAZ AHMAD LONE
 
Peer Teaching Observations During School Internship
Peer Teaching Observations During School Internship
AjayaMohanty7
 
A Visual Introduction to the Prophet Jeremiah
A Visual Introduction to the Prophet Jeremiah
Steve Thomason
 
LDMMIA Yoga S10 Free Workshop Grad Level
LDMMIA Yoga S10 Free Workshop Grad Level
LDM & Mia eStudios
 
How to use _name_search() method in Odoo 18
How to use _name_search() method in Odoo 18
Celine George
 
OBSESSIVE COMPULSIVE DISORDER.pptx IN 5TH SEMESTER B.SC NURSING, 2ND YEAR GNM...
OBSESSIVE COMPULSIVE DISORDER.pptx IN 5TH SEMESTER B.SC NURSING, 2ND YEAR GNM...
parmarjuli1412
 
List View Components in Odoo 18 - Odoo Slides
List View Components in Odoo 18 - Odoo Slides
Celine George
 
How payment terms are configured in Odoo 18
How payment terms are configured in Odoo 18
Celine George
 
NSUMD_M1 Library Orientation_June 11, 2025.pptx
NSUMD_M1 Library Orientation_June 11, 2025.pptx
Julie Sarpy
 
THE PSYCHOANALYTIC OF THE BLACK CAT BY EDGAR ALLAN POE (1).pdf
THE PSYCHOANALYTIC OF THE BLACK CAT BY EDGAR ALLAN POE (1).pdf
nabilahk908
 
Ad

Assignment c programming

  • 1. saStudent Name: Sumit Kumar Singh Course: BCA Registration Number: 1308009955 LC Code: 01687 Subject: Programming in C Subject Code: 1020 Q.No.1. Explain any 5 category of C operators. Ans. C language supports many operators, these operators are classified in following categories: • Arithmetic operators • Unary operator • Conditional operator • Bitwise operator • Increment and Decrement operators Arithmetic Operators The basic operators for performing The bitwise operators &, |, ^ and ~ operate on integers thought of as binary numbers or strings of bits. The & operator is bitwise AND, the | operator is bitwise OR, the ^ operator is bitwise excusive-OR (XOR) and the ~ operator is a bitwise negation or complement. You might define the 3rd bit as the “verbose” flag bit by defining #define VERBOSE 4 Then you can “turn the verbose bit on” in an integer variable flags by executing flags = flags | VERBOSE; and turn it off with flags = flags &~VERBOSE and test whether it’s set with if(flags % VERBOSE) Increment and Decrement Operators To add or subtract constant 1 to a variable, C provides a set of shortcuts: the auto increment and auto decrement operators. For instance, ++i add 1 to i -- j subtract 1 from j Q.No.2 Write a program to sum integers entered interactively using while loop. Ans: #include<stdio.h> #include <conio.h> int main(void) { long num; long sum = 0; int status; clrscr(); printf("Please enter an integer to be summed. "); printf("Enter q to quit.n");
  • 2. status = scanf("%ld", &num); while (status == 1) { sum = sum + num; printf("Please enter next integer to be summed. "); printf("Enter q to quit.n"); status = scanf("%ld", &num); } printf("Those integers sum to %ld.n", sum); return 0; getch(); } Q.No.3 Write a Program to find average length of several lines of text for illustrated global variables. Ans: #include<stdio.h> #include<conio.h> // Declare global variables outside of all the functions int sum=0; // total number of characters int lines=0; // total number of lines void main() { int n; // number of characters in given line float avg; // average number of characters per line clrscr(); void linecount(void); // function declaration float cal_avg(void); printf(“Enter the text below:n”); while((n=linecount())>0) { sum+=n; ++lines; } avg=cal_avg(); printf(“n tAverage number of characters per line: %5.2f”, avg); } void linecount(void) { // read a line of text and count the number of characters char line[80]; int count=0; while((line[count]=getchar())!=’n’) ++count; return count; } float cal_avg(void) { // compute average and return return (float)sum/lines;
  • 3. getch(); } Q.No.4 Explain the basic concept of Pointer addition and subtraction. Also give one example program for each. Ans. Addition Pointer A pointer is a variable which contains the address in memory of another variable. We can have a Pointer to any variable type. Use of & symbols that gives the “address a variable” and is known as the unary or monadic operator. * symbol is used for the “contents of an object pointed to by a pointer” and we call it the indirection or dereference operator. Simple pointer declaration has the following general format: Datatype *variablename For example: int *ip; This program performs addition of two numbers using pointers. In our program we have two two integer variables x, y and two pointer variables p and q. Firstly we assign the addresses of x and y to p and q respectively and then assign the sum of x and y to variable sum. Note that & is address of operator and * is value at address operator. Example for pointers addition:- #include<stdio.h> #include<conio.h> int main() { int first, second, *p, *q, sum; clrcsr(); printf("Enter two integers to addn"); scanf("%d%d", &first, &second); p = &first; q = &second; sum = *p + *q; printf("Sum of entered numbers = %dn",sum); return 0; getch(); }
  • 4. Subtraction pointer Example for pointers addition:- #include<stdio.h> #include<conio.h> int main() { int first, second, *p, *q, sub; //sub taken as a variable of subtraction pointer. clrcsr(); printf("Enter two integers to addn"); scanf("%d%d", &first, &second); p = &first; q = &second; sub = *p - *q; printf("Sum of entered numbers = %dn",sum); return 0; getch(); } Q.No.5. Explain the concept of NULL pointer. Also give one example program. Ans. Null Pointers We said that the value of a pointer variable is a pointer to some other variable. There is one other value a pointer may have: it may be set to a null pointer. A null pointer is a special pointer value that is known not to point anywhere. What this means that no other valid pointer, to any other variable or array cell or anything else, will ever compare equal to a null pointer. The most straightforward way to “get'' a null pointer in your program is by using the predefined constant NULL, which is defined for you by several standard header files, including <stdio.h>, <stdlib.h>, and <string.h>. To initialize a pointer to a null pointer, you might use code like #include <stdio.h> int *ip = NULL; and to test it for a null pointer before inspecting the value pointed to, you might use code like if(ip != NULL) printf("%dn", *ip); It is also possible to refer to the null pointer by using a constant 0, and you will see some code that sets null pointers by simply doing int *ip = 0; (In fact, NULL is a preprocessor macro which typically has the value, or replacement text, 0.) Furthermore, since the definition of “true'' in C is a value that is not equal to 0, you will see code that tests for non-null pointers with abbreviated code like if(ip) printf("%dn", *ip); This has the same meaning as our previous example; if(ip) is equivalent to if(ip != 0) and to if(ip != NULL).
  • 5. All of these uses are legal, and although the use of the constant NULL is recommended for clarity, you will come across the other forms, so you should be able to recognize them. You can use a null pointer as a placeholder to remind yourself (or, more importantly, to help your program remember) that a pointer variable does not point anywhere at the moment and that you should not use the “contents of'' operator on it (that is, you should not try to inspect what it points to, since it doesn't point to anything). A function that returns pointer values can return a null pointer when it is unable to perform its task. (A null pointer used in this way is analogous to the EOF value that functions like getchar return.) As an example, let us write our own version of the standard library function strstr, which looks for one string within another, returning a pointer to the string if it can, or a null pointer if it cannot. Example: Here is the function, using the obvious brute-force algorithm: at every character of the input string, the code checks for a match there of the pattern string: #include <stddef.h> char *mystrstr(char input[], char pat[]) { char *start, *p1, *p2; for(start = &input[0]; *start != '0'; start++) { / /for each position in input string... p1 = pat; // prepare to check for pattern string there p2 = start; while(*p1 != '0') { if(*p1 != *p2) /* characters differ */ break; p1++; p2++; } if(*p1 == '0') /* found match */ return start; } return NULL; } Q.No.6. Write a Program to search the specified file, looking for the character using command line arguments. Ans. The program to search the specified file, looking for the character using command line argument Example program takes two command-line arguments. The first is the name of a file, the second is a character. The program searches the specified file, looking for the character. If the file contains at least one of these characters, it reports this fact. This program uses argv to access the file name and the character for which to search. /*Search specified file for specified character. */ #include <stdio.h> #include <stdlib.h> void main(int argc, char *argv[])
  • 6. { FILE *fp; /* file pointer */ char ch; /* see if correct number of command line arguments */ if(argc !=3) { printf("Usage: find <filename> <ch>n"); exit(1); } /* open file for input */ if ((fp = fopen(argv[1], "r"))==NULL) { printf("Cannot open file n"); exit(1);
  • 7. { FILE *fp; /* file pointer */ char ch; /* see if correct number of command line arguments */ if(argc !=3) { printf("Usage: find <filename> <ch>n"); exit(1); } /* open file for input */ if ((fp = fopen(argv[1], "r"))==NULL) { printf("Cannot open file n"); exit(1);