0% found this document useful (0 votes)
173 views

C Assignments 1 of Date 8th October)

The two kinds of division that the / operator can do are integer division and floating-point division. Floating-point division is performed if either operand (or both) is floating-point. The built-in operators, >=, ==,!=, &&,, and! always generate 0 for false and 1 for true.

Uploaded by

Pratik Rathod
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
173 views

C Assignments 1 of Date 8th October)

The two kinds of division that the / operator can do are integer division and floating-point division. Floating-point division is performed if either operand (or both) is floating-point. The built-in operators, >=, ==,!=, &&,, and! always generate 0 for false and 1 for true.

Uploaded by

Pratik Rathod
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Review Questions (1).What are the two different kinds of division that the / operator can do?

Under what circumstances does it perform each? Answer: The two kinds of division are integer division and floating-point division. Floating-point division is performed if either operand (or both) is floating-point (that is, if either number being operated on is floating-point). integer division is performed if both operands are integers. (2).What are the definitions of the ``Boolean'' values true and false in C? Answer: False is always represented by a zero value. Any nonzero value is considered true. The built-in operators <, <=, >, >=, ==, !=, &&, ||, and ! always generate 0 for false and 1 for true. (3).Name three uses for the semicolon in C. Answer: Terminating declarations. Terminating statements. Separating the three control expressions in a for loop. (4).What would the equivalent code, using a while loop, be for the example ? for(i = 0; i < 10; i = i + 1) printf("i is %d\n", i); Answer. i = 0; while(i < 10) { printf("i is %d\n", i); i = i + 1; }

(5).What is the numeric value of the expression 3 < 4 ? Answer:

The numeric value of the expression 3 < 4 is 1 (or ``true''). 3 is in fact less than 4.

(6).Under what conditions will this code print ``water''? if(T < 32) printf("ice\n");

else if(T < 212) printf("water\n"); else printf("steam\n"); Answer: If T is greater than or equal to 32 and less than 212.

(7).What would this code print? int x = 3; if(x) printf("yes\n"); else printf("no\n"); Answer: It would print ``yes'', since x is nonzero.

(8).What would this code print? int i; for(i = 0; i < 3; i = i + 1) printf("a\n"); printf("b\n"); printf("c\n"); Answer: As i=0 and i<3, 'a' will be print 3 times.and as soon as the for loop will terminate after satisfing condition statement ,character 'b' will be print. and lastly character 'c' will be print.

(9).What's wrong with this scrap of code? int a[5]; for(i = 1; i <= 5; i = i + 1) a[i] = 0; Answer: The array is of size 5, but the loop is from 1 to 5, so an attempt will be made to access the nonexistent element a[5]. A correct loop over this array would run from 0 to 4.

(10).What would the expression i = i++ do? Answer: The postfix form i++ increments i, but returns the prior, non-incremented value.but here as i=i++ the following action will be taken. for eg:if i =5 then

i=5 i=i+1=5+1=6 I=6.

(11).If we say int i = 5; int *ip = &i; then what is ip? What is its value? Answer: ip is a variable which can point to an int (that is, its value will be a pointer to an int; or informally, we say that ip is ``a pointer to an int''). Its value is a pointer which points to the variable i.

(12).If ip is a pointer to an integer, what does ip++ mean? What does below code do? *ip++ = 0; Answer: ip++ means about the same as it does for any other variable: increment it by 1, that is, as if we had written ip = ip + 1. In the case of a pointer, this means to make it point to the object (the int) one past the one it used to. *ip++ = 0 sets the int variable pointed to by ip to 0, and then increments ip to point to the next int.

Programming Questions (1).Try to get sizes of some fundamental types, pointers and enumerations. Also find their largest and smallest values (Hint: sizeof( ), <limit.h>) Answer: #include<stdio.h> main() { int a; float b; char c; double e; long int f; int* pa = &a; float* pb = &b; char* pc = &c; double* pe = &e; long int* pf = &f;

enum color {Red=1, Green, Blue, Yellow}; enum color mycolor=Blue; clrscr(); printf("Size variables:\na=%d\nb=%d\nc=%d\ne=%d\nf=%d\n",sizeof(a),sizeof(b),sizeof(c),sizeof(e),sizeof(f)); of

printf("Size of pointers:\npa=%d\npb=%d\npc=%d\npe=%d\npf=%d\n",sizeof(pa),sizeof(pb),sizeof(pc),sizeof(pe),sizeof (pf)); printf("Size of color is: %d", sizeof(mycolor)); getchar(); return(0); }

(2).Write a code to check the auto-conversion working between 'unsigned int' and 'signed int'. Try giving all various data and explain the output. Answer: #include<stdio.h> main() { int a,c,d,e,f; unsigned b; printf("enter value of a\n enter value of b\n");

scanf("%d %u",&a,&b); c=a+b; printf("\nc=%u",sizeof(c=a+b)); printf("\nc=%d",sizeof(c=a+b)); d=a-b; printf("\nd=%u",sizeof(d=a-b));

printf ("\nd=%d",sizeof(d=a-b)); e=a*b; printf("\ne=%u",sizeof(e=a*b)); printf("\ne=%d",sizeof(e=a*b));

f=a/b; printf("\nf=%u",sizeof(f=a/b)); printf("\nf=%d",sizeof(f=a/b)); getch(); return(0); }

(3).Write a program to check whether given two strings are same or not? Try using as small code as possible. You need to use pointers. Answer #include<stdio.h> main() { int i; char a[6]={'h','e','l','l','o'}; char b[6]={'h','e','l','l','o'}; char *pa =a; char *pb =b; for(i=0;i!=6;i++) { if(*pa==*pb) { pa++; pb++; if(i==5) { printf("string is same"); } } else { printf("string is not same");

break; } } getchar(); return(0); } (4).Write a program to read lines and print only those containing a certain word. The basic pattern is while(there's another line) { if(line contains word) print the line; } Answer: #include<stdio.h> #include<string.h> main() { char line[20]; char word[10]; char ch; char part[10]; int i, j; clrscr(); printf("Enter a word (max 9 chars)"); scanf("%s", word); getchar();

while(1) { i=0; printf("Enter a line (max 19 chars): "); while((ch=getchar())!='\n') { line[i++] = ch; } line[i]='\0'; for(i=0, j=0; line[i] !='\0'; i++)

{ if(line[i] == ' ' || line[i] == '\t') { part[j] = '\0'; if(strcmpi(part,word)==0) { printf("\nThe line %s contains the word %s", line, word); break; } j=0; } else { part[j++] = line[i]; } } printf("\nDo you want to enter another line(y/n)?: "); ch = getchar(); getch(); if(ch == 'n' || ch == 'N') break; } getch(); return(0); }

(5).Use the strstr function to look for the word. Be sure to include the line at the top of the source file where you call strstr. #include <string.h> Answer: #include <stdio.h> #include <string.h> int main(void) { char *str1 = "I am Jigar Shah", *str2 = "Jigar", *ptr; ptr = strstr(str1, str2); printf("The substring is: %s\n", ptr); getchar(); return 0; }

You might also like