Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
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);