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

More Related Content

What's hot (20)

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

Similar to Assignment c programming (20)

C programming
C programmingC programming
C programming
Karthikeyan A K
 
An imperative study of c
An imperative study of cAn 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.pdfEASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
sudhakargeruganti
 
Pointers in C Language
Pointers in C LanguagePointers 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 languageUNIT 4 POINTERS.pptx pointers pptx for basic c language
UNIT 4 POINTERS.pptx pointers pptx for basic c language
wwwskrilikeyou
 
string , pointer
string , pointerstring , pointer
string , pointer
Arafat Bin Reza
 
C librabry 55 functions.pdfljhghmhiguyftg
C librabry 55 functions.pdfljhghmhiguyftgC librabry 55 functions.pdfljhghmhiguyftg
C librabry 55 functions.pdfljhghmhiguyftg
saimukesh19
 
POEPPSGNHwkejnkweoewnkenjwjewkjewoewkjewijewjk
POEPPSGNHwkejnkweoewnkenjwjewkjewoewkjewijewjkPOEPPSGNHwkejnkweoewnkenjwjewkjewoewkjewijewjk
POEPPSGNHwkejnkweoewnkenjwjewkjewoewkjewijewjk
hanumanthumanideeph6
 
Pointer in C
Pointer in CPointer in C
Pointer in C
bipchulabmki
 
Function in c program
Function in c programFunction in c program
Function in c program
umesh patil
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
MKalpanaDevi
 
C programm.pptx
C programm.pptxC programm.pptx
C programm.pptx
SriMurugan16
 
Array Cont
Array ContArray Cont
Array Cont
Ashutosh Srivasatava
 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
s170883BesiVyshnavi
 
C function
C functionC function
C function
thirumalaikumar3
 
presentation_pointers_1444076066_140676 (1).ppt
presentation_pointers_1444076066_140676 (1).pptpresentation_pointers_1444076066_140676 (1).ppt
presentation_pointers_1444076066_140676 (1).ppt
georgejustymirobi1
 
c program.ppt
c program.pptc program.ppt
c program.ppt
mouneeshwarans
 
7 functions
7  functions7  functions
7 functions
MomenMostafa
 
13092119343434343432232323121211213435554
1309211934343434343223232312121121343555413092119343434343432232323121211213435554
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.docxPROVIDE 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 cAn 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.pdfEASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
sudhakargeruganti
 
Pointers in C Language
Pointers in C LanguagePointers 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 languageUNIT 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.pdfljhghmhiguyftgC librabry 55 functions.pdfljhghmhiguyftg
C librabry 55 functions.pdfljhghmhiguyftg
saimukesh19
 
POEPPSGNHwkejnkweoewnkenjwjewkjewoewkjewijewjk
POEPPSGNHwkejnkweoewnkenjwjewkjewoewkjewijewjkPOEPPSGNHwkejnkweoewnkenjwjewkjewoewkjewijewjk
POEPPSGNHwkejnkweoewnkenjwjewkjewoewkjewijewjk
hanumanthumanideeph6
 
Function in c program
Function in c programFunction in c program
Function in c program
umesh patil
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
MKalpanaDevi
 
presentation_pointers_1444076066_140676 (1).ppt
presentation_pointers_1444076066_140676 (1).pptpresentation_pointers_1444076066_140676 (1).ppt
presentation_pointers_1444076066_140676 (1).ppt
georgejustymirobi1
 
13092119343434343432232323121211213435554
1309211934343434343223232312121121343555413092119343434343432232323121211213435554
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.docxPROVIDE 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)

One Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learningOne Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learning
momer9505
 
Odoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo SlidesOdoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo Slides
Celine George
 
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam SuccessUltimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Mark Soia
 
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 AccountingHow to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
Celine George
 
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Library Association of Ireland
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 4-30-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 4-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-30-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
apa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdfapa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdf
Ishika Ghosh
 
How to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of saleHow to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of sale
Celine George
 
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Library Association of Ireland
 
Presentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem KayaPresentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
Unit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdfUnit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdf
KanchanPatil34
 
Handling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptxHandling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptx
AuthorAIDNationalRes
 
Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdfBiophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
PKLI-Institute of Nursing and Allied Health Sciences Lahore , Pakistan.
 
To study Digestive system of insect.pptx
To study Digestive system of insect.pptxTo study Digestive system of insect.pptx
To study Digestive system of insect.pptx
Arshad Shaikh
 
Social Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy StudentsSocial Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy Students
DrNidhiAgarwal
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar RabbiPresentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Md Shaifullar Rabbi
 
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACYUNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
DR.PRISCILLA MARY J
 
Anti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptxAnti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptx
Mayuri Chavan
 
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptxSCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
Ronisha Das
 
One Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learningOne Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learning
momer9505
 
Odoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo SlidesOdoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo Slides
Celine George
 
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam SuccessUltimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Mark Soia
 
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 AccountingHow to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
Celine George
 
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Library Association of Ireland
 
apa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdfapa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdf
Ishika Ghosh
 
How to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of saleHow to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of sale
Celine George
 
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Library Association of Ireland
 
Presentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem KayaPresentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
Unit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdfUnit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdf
KanchanPatil34
 
Handling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptxHandling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptx
AuthorAIDNationalRes
 
To study Digestive system of insect.pptx
To study Digestive system of insect.pptxTo study Digestive system of insect.pptx
To study Digestive system of insect.pptx
Arshad Shaikh
 
Social Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy StudentsSocial Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy Students
DrNidhiAgarwal
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar RabbiPresentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Md Shaifullar Rabbi
 
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACYUNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
DR.PRISCILLA MARY J
 
Anti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptxAnti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptx
Mayuri Chavan
 
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptxSCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
Ronisha Das
 
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);