# Monthly Archives: May 2016

## List and explain bitwise operators in ‘C’. 10m jun2009

List and explain bitwise operators in ‘C’. 10m

The following table lists the Bitwise operators supported by C. Assume variable ‘A’ holds 60 and variable ‘B’ holds 13, then –

 Operator Description Example & Binary AND Operator copies a bit to the result if it exists in both operands. (A & B) = 12 i.e., 0000 1100 ∣ Binary OR Operator copies a bit if it exists in either operand. (A ∣ B) = 61 i.e., 0011 1101 ^ Binary XOR Operator copies the bit if it is set in one operand but not both. (A ^ B) = 49 i.e., 0011 0001 ~ Binary Ones Complement Operator is unary and has the effect of ‘flipping’ bits. (~A ) = 61 i.e., 1100 0011 in 2′s complement form. << Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. A << 2 = 240 i.e., 1111 0000 >> Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand A >> 2 = 15 i.e., 0000 1111

4. (b) Write a ‘C’ program to find the sum of the given series: 10

S=1 – 2/2! + 3/3! – 4/4! …. n/n!

Similar program can be found on this link http://cssimplified.com/c-programming/write-a-loop-that-calculate-sum-of-the-n-elements-of-the-series-5m-dec2008

5. (b) Write a program to count the number of characters, number of words and number of lines in a given file. 10

Solved program can be found on this link http://cssimplified.com/c-programming/write-a-program-to-count-the-number-of-characters-and-words-in-a-given-file-10m-jun2007

## Explain the meaning and usage of each of the following function prototypes. jun2009

Explain the meaning and usage of each of the following function prototypes: 5×2=10m

(i) getch ( )       (ii) strcmp ( )         (iii) getchar ( )       (iv) gets ( )        (v) puts ( )

(i) getch ( )

• getch ( ) gets a character from console but does not echo to the screen.

Prototype:

• int getch (void);

Usage:

getch reads a single character directly from the keyboard, without echoing to the screen.

(ii) strcmp ( )

• strcmp ( ) compare two strings.

Prototype:

• int strcmp (const  char *s1, const char *s2);

Usage:

strcmp performs an unsigned comparison of s1 and s2.

(iii) getchar ( )

• getchar ( ) is a macro that gets a character from stdin.

Prototype:

• int getchar (void);

Usage:

getchar is a macro defined as getc(stdin) getchar returns the next character on the input stream stdin.

(iv) gets ( )

• gets ( ) gets a string from stdin.

Prototype:

• char *gets (char *s);

Usage:

gets collects a string of characters terminated by a new line from the standard input stream stdin and puts it into s.

(v) puts ( )

• puts ( ) outputs a string to stdout (and appends a newline character).

Prototype:

• int *puts (const char *s);

Usage:

Puts copies the null-terminated string s to the standard output stream stdout andd appends a newline character.

## Write a Program in ‘C’ to copy one file to another. The program should read the filenames at command line. 10m Jun2009

Write a Program in ‘C’ to copy one file to another. The program should read the filenames at command line. 10m Jun2009

Code:

#include<stdio.h>
int main(int argc,char *argv[])
{
FILE *fs,*ft;
int ch;
if(argc!=3)
{
printf(“Invalide numbers of arguments.”);
return 1;
}
fs=fopen(argv[1],”r”);
if(fs==NULL)
{
printf(“Can’t find the source file.”);
return 1;
}
ft=fopen(argv[2],”w”);
if(ft==NULL)
{
printf(“Can’t open target file.”);
fclose(fs);
return 1;
}

while(1)
{
ch=fgetc(fs);
if (feof(fs)) break;
fputc(ch,ft);
}

fclose(fs);
fclose(ft);
return 0;
}

 Source code
```#include<stdio.h>
int main(int argc,char *argv[])
{
FILE *fs,*ft;
int ch;
if(argc!=3)
{
printf("Invalide numbers of arguments.");
return 1;
}
fs=fopen(argv[1],"r");
if(fs==NULL)
{
printf("Can't find the source file.");
return 1;
}
ft=fopen(argv[2],"w");
if(ft==NULL)
{
printf("Can't open target file.");
fclose(fs);
return 1;
}
while(1)
{
ch=fgetc(fs);
if (feof(fs)) break;
fputc(ch,ft);
}
fclose(fs);
fclose(ft);
return 0;
}```

Screen Shots:

## Write a Program in ‘C’ to print the following output ‘n’ rows. 10m june2009

Write a Program in ‘C’ to print the following output ‘n’ rows. For example, if n=3, the following should be output by the program: 10m june2009

#include <stdio.h>
void main()
{
int I,J,N=3,K=0,ODD=1,SPACE;
clrscr();
SPACE=N;
for(I=1;I<=N;I++)
{
for(J=1;J<SPACE;J++)
{
printf(” “);
}
K=0;
for(J=1;J<=ODD;J++)
{
if(J<=I)
{
K++;
}
else
{
K–;
}
printf(“%d “,K);
}
ODD=ODD+2;
SPACE–;
printf(“\n”);
}
I–;
ODD=ODD-2;
I–;
SPACE++;
ODD=ODD-2;
SPACE++;
for(;I>0;I–)
{
for(J=1;J<SPACE;J++)
{
printf(” “);
}
K=0;
for(J=1;J<=ODD;J++)
{
if(J<=I)
{
K++;
}
else
{
K–;
}
printf(“%d “,K);
}
ODD=ODD-2;
SPACE++;
printf(“\n”);
}
getch();
}

 Source code
```#include <stdio.h>
void main()
{
int I,J,N=3,K=0,ODD=1,SPACE;
clrscr();
SPACE=N;
for(I=1;I<=N;I++)
{
for(J=1;J<SPACE;J++)
{
printf(" ");
}
K=0;
for(J=1;J<=ODD;J++)
{
if(J<=I)
{
K++;
}
else
{
K--;
}
printf("%d ",K);
}
ODD=ODD+2;
SPACE--;
printf("\n");
}
I--;
ODD=ODD-2;
I--;
SPACE++;
ODD=ODD-2;
SPACE++;
for(;I>0;I--)
{
for(J=1;J<SPACE;J++)
{
printf(" ");
}
K=0;
for(J=1;J<=ODD;J++)
{
if(J<=I)
{
K++;
}
else
{
K--;
}
printf("%d ",K);
}
ODD=ODD-2;
SPACE++;
printf("\n");
}
getch();
}```

Screen Shots: