PROGRAMMING AND PROBLEM SOLVING THROUGH ‘C’ LANGUAGE

1.8. Consider the following declarations.
union id {
char color;
int size;
}
struct {
char country;
int date;
union id i;
} flag;
To assign a color to a flag, the correct statement would be
A) flag.color = ‘W’;
B) flag.i.color = ‘W’;
C) flag.color = ‘White’;
D) flag.i.color = ‘White’;

PROGRAMMING AND PROBLEM SOLVING THROUGH ‘C’ LANGUAGE

1.5. What will assign in s, when we use the following pair of statements in c-program:
char *s;
s = “my car color is : white”;
A) first character of the string constant
B) complete string
C) address of the string storage
D) is a logical error

PROGRAMMING AND PROBLEM SOLVING THROUGH ‘C’ LANGUAGE

1.1. In a for loop with a multi statement loop body, semicolons should appear following:
A) the for statement itself
B) the crossing brace in the multiple statement loop body
C) each statement within the loop body and the test expression
D) each statement within the loop only

Command Line Example In C

prog1.c

void main(int argc,char *argv[])
{int i;
printf("%d",argc);
for (i=1;i<argc;i++)
printf("%s\n",argv[i]);
}
c:\prog1.exe 1  4 5 6 6 56 7

showfile.c

#include<process.h>
#include<stdio.h>
void main(int argc, char *argv[])
{ char ch;
FILE *src;
src=fopen(argv[1],"r");
do{
ch=fgetc(src);
if(ch==EOF)
break;
putchar(ch);
}while(1);
}
c:\showfile.exe filename.txt

list.c

#include<process.h>
void main(int argc, char *argv[])
{ char com[20];
if(argc==1)
system("dir");
else
if(argc==2)
{
strcpy(com,"dir ");
strcat(com,argv[1]);
system(com );
}
}
c:\list.exe foldername

copyfile.c

#include<process.h>
#include<stdio.h>
void main(int argc, char *argv[])
{ char ch;
FILE *src,*trgt;
src=fopen(argv[1],"r");
trgt=fopen(argv[2],"w");
do{
ch=fgetc(src);
if(ch==EOF)
break;
fputc(ch,trgt);
}while(1);
}
c:\filecopy.exe file1.txt newfile.txt

Circular Queue Example C

#include<stdio.h>
#include<process.h>
#include<stdlib.h>
#include<math.h>
#define MAX 3
typedef int itemtype;
typedef struct queue* qptr;
struct queue
{
itemtype data[MAX];
int front,rear;
};
void init(qptr q);
void add(qptr q,itemtype x);
void removeq(qptr q);
int full(qptr q);
int empty(qptr q);
int count(qptr q);

void init(qptr q)
{
q->front=0;
q->rear=-1;
}

void add(qptr q,itemtype x)
{
if(!full(q))
{
q->rear=(q->rear+1)%MAX;
q->data[q->rear]=x;
}
else
{
printf("\nqueue is full...\n");
}
}

void removeq(qptr q)
{
if(!empty(q))
{
	printf("\n%d removed \n",q->data[q->front]);
	q->data[q->front]=-1;
	printf("\n%d after remove \n",q->data[q->front]);
	q->front=(q->front+1)%MAX;
}
else
{
printf("\nqueue is empty\n");
}
}

int full(qptr q)
{
if(count(q)==MAX)
return 1;
else
return 0;
}

int empty(qptr q)
{
return (!count(q));
}

int count(qptr q)
{
	if((q->front>q->rear)&&((q->data[q->rear]!=-1)&&(q->rear!=-1)))
		return (MAX-((q->front-q->rear)-1));
	else if((q->front<=q->rear)&&(q->data[q->rear]!=-1))
		return abs(((q->front-q->rear)-1));
	else
		return 0;
}



int main()
{
qptr q;
int ch,x;
char choice;
q=(qptr)malloc(sizeof(struct queue));
init(q);
do
{
printf("\n || MENU || \n");
printf("\n 1. add in cqueue");                 
printf("\n 2. remove from cqueue");
printf("\n 3. count"); 
printf("\n\n Enter your choice (1-3) : ");
scanf("%d",&ch);
if(ch==1)
{
printf("\n Enter value in queue : ");
scanf("%d",&x);
add(q,x);
}
else if(ch==2)
{
removeq(q);
}
else if(ch==3)
{
printf("\n count = %d \n",count(q));
}
else
{
printf("\n Invalid choice...\n");
}
	fflush(stdin);
	printf("\n want to continue (y/n) : ");
	scanf("%c",&choice);
}while(choice=='y'||choice=='Y');
system("pause");
}