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);

More Related Content

What's hot (20)

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

Similar to Assignment c programming (20)

PPTX
Very interesting C programming Technical Questions
Vanathi24
 
DOCX
C Programming
Raj vardhan
 
PPT
Unit i intro-operators
HINAPARVEENAlXC
 
PPTX
Advance topics of C language
Mehwish Mehmood
 
PPT
13092119343434343432232323121211213435554
simplyamrita2011
 
PPTX
C programming language
Abin Rimal
 
DOCX
Programming in c
Ashutosh Srivasatava
 
DOC
2. operator
Shankar Gangaju
 
PDF
Learn C program in Complete c programing string and its functions like array...
paraliwarrior
 
PPTX
Introduction to C Programming - R.D.Sivakumar
Sivakumar R D .
 
PPT
c_tutorial_2.ppt
gitesh_nagar
 
PDF
2 EPT 162 Lecture 2
Don Dooley
 
DOCX
C interview question answer 2
Amit Kapoor
 
PDF
C programming | Class 8 | III Term
Andrew Raj
 
PPTX
Variable declaration
Mark Leo Tarectecan
 
PPT
Basics of c
DebanjanSarkar11
 
PDF
PSPC--UNIT-5.pdf
ArshiniGubbala3
 
Very interesting C programming Technical Questions
Vanathi24
 
C Programming
Raj vardhan
 
Unit i intro-operators
HINAPARVEENAlXC
 
Advance topics of C language
Mehwish Mehmood
 
13092119343434343432232323121211213435554
simplyamrita2011
 
C programming language
Abin Rimal
 
Programming in c
Ashutosh Srivasatava
 
2. operator
Shankar Gangaju
 
Learn C program in Complete c programing string and its functions like array...
paraliwarrior
 
Introduction to C Programming - R.D.Sivakumar
Sivakumar R D .
 
c_tutorial_2.ppt
gitesh_nagar
 
2 EPT 162 Lecture 2
Don Dooley
 
C interview question answer 2
Amit Kapoor
 
C programming | Class 8 | III Term
Andrew Raj
 
Variable declaration
Mark Leo Tarectecan
 
Basics of c
DebanjanSarkar11
 
PSPC--UNIT-5.pdf
ArshiniGubbala3
 
Ad

Recently uploaded (20)

PPTX
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
PPTX
GRADE-3-PPT-EVE-2025-ENG-Q1-LESSON-1.pptx
EveOdrapngimapNarido
 
PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
PPTX
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
PPTX
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PPTX
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
PPTX
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
PPTX
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
PPTX
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PDF
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
PPTX
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
GRADE-3-PPT-EVE-2025-ENG-Q1-LESSON-1.pptx
EveOdrapngimapNarido
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
Dimensions of Societal Planning in Commonism
StefanMz
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
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);