SlideShare a Scribd company logo
1 of 7
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)

C Programming Unit-3
C Programming Unit-3C Programming Unit-3
C Programming Unit-3
 
Unit 8. Pointers
Unit 8. PointersUnit 8. Pointers
Unit 8. Pointers
 
C programming session 02
C programming session 02C programming session 02
C programming session 02
 
C programming session 05
C programming session 05C programming session 05
C programming session 05
 
Types of pointer in C
Types of pointer in CTypes of pointer in C
Types of pointer in C
 
C pointers
C pointersC pointers
C pointers
 
C programming session 01
C programming session 01C programming session 01
C programming session 01
 
Lecture 8- Data Input and Output
Lecture 8- Data Input and OutputLecture 8- Data Input and Output
Lecture 8- Data Input and Output
 
C programming(Part 1)
C programming(Part 1)C programming(Part 1)
C programming(Part 1)
 
Lecture 6- Intorduction to C Programming
Lecture 6- Intorduction to C ProgrammingLecture 6- Intorduction to C Programming
Lecture 6- Intorduction to C Programming
 
Presentation on pointer.
Presentation on pointer.Presentation on pointer.
Presentation on pointer.
 
Advanced C programming
Advanced C programmingAdvanced C programming
Advanced C programming
 
Strings
StringsStrings
Strings
 
Lecturer23 pointersin c.ppt
Lecturer23 pointersin c.pptLecturer23 pointersin c.ppt
Lecturer23 pointersin c.ppt
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
Lập trình C
Lập trình CLập trình C
Lập trình C
 
Pointers
PointersPointers
Pointers
 
Dynamic Memory Allocation in C
Dynamic Memory Allocation in CDynamic Memory Allocation in C
Dynamic Memory Allocation in C
 
C language basics
C language basicsC language basics
C language basics
 
Ponters
PontersPonters
Ponters
 

Similar to Assignment c programming

An imperative study of c
An imperative study of cAn imperative study of c
An imperative study of cTushar 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.pdfsudhakargeruganti
 
Pointers in C Language
Pointers in C LanguagePointers in C Language
Pointers in C Languagemadan reddy
 
Function in c program
Function in c programFunction in c program
Function in c programumesh patil
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4MKalpanaDevi
 
presentation_pointers_1444076066_140676 (1).ppt
presentation_pointers_1444076066_140676 (1).pptpresentation_pointers_1444076066_140676 (1).ppt
presentation_pointers_1444076066_140676 (1).pptgeorgejustymirobi1
 
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.docxamrit47
 
1. DSA - Introduction.pptx
1. DSA - Introduction.pptx1. DSA - Introduction.pptx
1. DSA - Introduction.pptxhara69
 
the refernce of programming C notes ppt.pptx
the refernce of programming C notes ppt.pptxthe refernce of programming C notes ppt.pptx
the refernce of programming C notes ppt.pptxAnkitaVerma776806
 

Similar to Assignment c programming (20)

C programming
C programmingC programming
C programming
 
An imperative study of c
An imperative study of cAn imperative study of c
An imperative study of c
 
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
 
Pointers in C Language
Pointers in C LanguagePointers in C Language
Pointers in C Language
 
string , pointer
string , pointerstring , pointer
string , pointer
 
Pointer in C
Pointer in CPointer in C
Pointer in C
 
Function in c program
Function in c programFunction in c program
Function in c program
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
C programm.pptx
C programm.pptxC programm.pptx
C programm.pptx
 
Array Cont
Array ContArray Cont
Array Cont
 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
 
C function
C functionC function
C function
 
presentation_pointers_1444076066_140676 (1).ppt
presentation_pointers_1444076066_140676 (1).pptpresentation_pointers_1444076066_140676 (1).ppt
presentation_pointers_1444076066_140676 (1).ppt
 
c program.ppt
c program.pptc program.ppt
c program.ppt
 
7 functions
7  functions7  functions
7 functions
 
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
 
C Programming Unit-4
C Programming Unit-4C Programming Unit-4
C Programming Unit-4
 
1. DSA - Introduction.pptx
1. DSA - Introduction.pptx1. DSA - Introduction.pptx
1. DSA - Introduction.pptx
 
the refernce of programming C notes ppt.pptx
the refernce of programming C notes ppt.pptxthe refernce of programming C notes ppt.pptx
the refernce of programming C notes ppt.pptx
 
Programming egs
Programming egs Programming egs
Programming egs
 

Recently uploaded

Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxcallscotland1987
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 

Recently uploaded (20)

Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
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);