Anda di halaman 1dari 21

ARVIND SIRS NOTES IN C PROGRAMMING QUESTION BANK ANSWERS

Explain C tokens (2 marks)

Tokens are basic building blocks of a C program. A token is the smallest element of a C program that is meaningful to the compiler. A token is source-program text that the compiler does not break down The C compiler recognizes the following kinds of tokens: keywords : these are reserved identifiers having predefined meanings identifiers : these tokens name functions, variables, constants, and types literals : these tokens that specify values operators : these tokens are used to combine values in expressions punctuation : these tokens separate or terminate complex constructions special : these tokens have special meaning to the preprocessor or compiler

A token can be a reserved word (such as int or while), an identifier (such as b or sum), a constant (such as 3.14 or "Arvind Kumar"), a delimiter (such as { or ;) or an operator (such as + or =). Example: Consider the following program: main() { int r=10, area; area = 3.14 * r * r; printf("area of circle = %d\n", area); } The tokens in this program are: main ( ) { int r = 10 , area ; 3.14 * identifier left bracket, delimiter right bracket, delimiter left brace, delimiter reserved word identifier equals sign, operator constant comma, delimiter identifier semicolon, delimiter constant asterisk, operator

and so on. Thus a C program is a stream of tokens

Explain C Keywords What is a keyword? State two keywords of C

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : auto break case char const continue default do double else enum extern float for goto if int long register return short signed sizeof static struct switch typedef union unsigned void volatile while

All 32 keywords are in lower case letters. These keywords are reserved and cannot be redefined. Following rules must be kept in mind when using keywords. Keywords are case sensitive. For example, return is a keyword and it must be used as is. So it cannot be used as Return, or RETURN. The keywords have special meaning in the language and cannot be used for any other purpose such as constant name or variable name. State the use of %d and %f . Write a print statement in C using above mentioned symbols

%d and %f are conversion specifiers. They are used with the control string in output function printf() and input function scanf() Consider the following statement printf (radius=%d , area= %f, r,a) ; The control-string uses %d conversion specifier. It describes how the value of n will be printed. The following example shows use of %d. In this example %d is used in scanf() statement to accept an integer value. Again %d is used in the printf() statement to print the integer value. #include<stdio.h> main() { int a; printf (Please enter an integer); scanf(%d,&a) printf (you entered the value %d,a); } The next example shows use of %f . In this example %f is used in scanf() statement to accept a float value. Again %f is used in the printf() statement to print the float value.

#include<stdio.h> main() { int b; printf (Please enter a float number); scanf(%f,&b) printf (you entered the value %f,b); }

Define expressions

Expressions: Expression is a single value or a combination of values and variables which are connected by operators. Following are examples of expression: a + b - c ; x = a ; x > 6 ; The first expression uses arithmetic operators + and , the second expression uses the assignment operator =, and the third expression uses relational operator >. Expression can also be a logical condition that is true or false. In C the true and false condition are represented by integer value 1 and 0 respectively.

What are operators?

Operators: Operators are tokens used to combine values in an expression. The following are examples of operators used in an expression: a + b - c ; x = a ; x > 6 ; The first expression uses arithmetic operators + and , the second expression uses the assignment operator =, and the third expression uses relational operator >. Some operators perform operation on two operands, while others perform operation on only one operand.

Explain the various operators used in C Explain the bit wise operators in C Explain the logical operators used in C What is an operator? Explain unary and binary operators. Explain the increment and decrement operators. State four arithmetic operators and four logical operators of C

The operators used in C are divided into following types: Arithmetic Operators Relation Operator Logical Operator

Arithmetic Operators: The operators used for basic arithmetic are =, +, -, *, and /. Operator + * / % Meaning subtraction addition multiplication division modulus

The operator % is used in integer arithmetic. It is called modulus operator. It gives the remainder when the integer to its left is divided by the integer to its right. Example: 13 % 5 is read as "13 modulo 5" and it has the value 3

The modulus operation gives remainder of an integer division. Hence % cannot be used in type float or double. Unary Operators: These operators perform an operation on a single variable and give a new value. The unary operators are:

Operat or + ++ sizeof

Meaning negative value positive value increment decrement size of variable in memory

Unary Minus: This operator is used to negate a numerical constant, variable or a expression. Examples: 35 2.5 (a * b) a + 35

Increment operator: The operator ++ will increase the value of its operand by 1.. Decrement operator: The operator will decrease the value of its operand by 1. Sizeof operator: This operator will return the size of the operand. The operand can be an expression.

Example: #include<stdio.h> main() { int p=10; float q = 2.5 ; printf (the size of p in byte is %d, sizeof(p)); printf (the size of q in byte is %d, sizeof(q)); }
4

The output of this will be

the size of p in byte is 2 the size of q in byte is 4

Bitwise Boolean Operators: The six bit wise operators used in C are given below. The bit wise operators work bit by bit on operands. The operands must be of integral type. & | ^ ~ << >> AND OR XOR (exclusive OR) NOT ( changes 1 to 0 and 0 to 1) Shift left Shift right

Logical Operators: The logical operators used in C are given below. && || ! logical AND logical OR NOT if any one operand is false, the result is false if any one operand is true, the result is true if the operand is true, the result is false and vice versa

The operators && and || are binary operators i.e. the logical operators require two operands. The ! operator is a unary operator. Consider operands A and B. The truth table for && and || operators are given below A false false true true B false true false true A && B false false false true A false false true true B false true false true A ||B false true true true

These logical operators are used to combine or negate expression containing relational operators. Here's an example of a logical expression. r = ((a&&b) || (c>d)); In the example: r is set equal to 1 if a and b are nonzero, or if c is greater than d. In all other cases, r is set to 0.

Define/Explain Data Types

Every variables used in a C program is defined with a specific type. In Standard C there are four basic data types. They are int, char, float, and double. char int float double characters integers (whole numbers) real numbers higher precision real numbers
5

In addition, C provides structured types: array structs unions - groups of variables of identical types, accessed using integer indices - groups of variables of mixed types, accessed by using named field selectors - variables that can contain values of different types, depending on a field selector

C also allows enumerated types - variables that can take on a small number of different named values

Explain the break statement

The break statement is used in loop statements (for, while, and do-while) . It is possible to force an immediate exit from a loop by using the break statement. A break statement is formed with the keyword break followed by a semicolon.

Example : #include <stdio.h> main() { int x; for(x=1;x<=100;x++) { if(x==10) break; printf(%d, x); } printf (loop terminated); } When the break statement is encountered inside a loop, the loop is immediately terminated and program control resumes at the next statement following the loop. Break is also used in switch statements to terminate execution of statement. When the break statement is executed, execution proceeds to the statement that follows switch statement. Example : #include<stdio.h> main() { int n; printf(\nEnter a number between 1 to 3); scanf(%d,%n); switch (n) case 1 : printf(you entered 1); break; case 2 : printf(you entered 2);
6

break; case 3 : printf(you entered 3); break; default : printf(wrong choice); }

Explain the continue statement

It is possible to bypass the loops normal control structure and force an early iteration of a loop and. This is accomplished by using continue. The continue statement is used in loop statements (for, while, and do-while) to terminate iteration of the loop. The next iteration of loop to takes place, skipping code between itself and conditional expression that controls the loop. A continue statement is formed with the keyword continue followed by a semicolon. Example: The following program prints even numbers between 0 and 100 #include <stdio.h> main() { int x; for(x=0;x<=100;x++) { if(x%2!=0) continue; printf(%d,x); } After a continue statement is executed, execution proceeds to increment clause and continuation test of for loop. Hence only even numbers are printed because an odd one will cause the loop to iterate early. The continue statement can be used in while and do while loops. It will cause control to go directly to the conditional expression and then continue the looping process. The continue statement is useful when data in body of loop is bad, out of bounds or unexpected. Instead of acting on bad data, control can go back to top of loop and get another data value.

Give the selection process of the switch statement Explain switch case statement with syntax

The switch statement is used to select and execute a particular group of statement from several available groups. The selection is based on the value of an expression or variable The syntax of the switch statement is: switch (expression or variable) { case constant1 : statement-block1; break;
7

case constant2 : statement-block2; break; . . . case constantN : statement-blockN; break; case default : statement-block3; } where expression is of simple type such as int, char, or enum . It cannot have float or double type. When switch statement is executed, the expression is evaluated. The resulting value is compared to values of constants 1 through N in order until a matching value is found. If a match is found in constant i then statements-block i through N and the default statements will be executed. Normally, last statement in each statement-block is a break statement so that only one statement-block is executed. The default clause is optional. If it is present then the default-statements are executed whenever value of expression does not match the constant values. Example: The program defines variable v of char type. The program takes input and stores it in variable v. The program will check the value of v and match it with the several cases. If the case matches the corresponding statement is executed. #include<stdio.h> main() { char v; printf(\nEnter a vowel :); scanf(%d,%c); switch (v) case a : printf(you entered a); break; case e : printf(you entered e); break; case i : printf(you entered i); break; case o : printf(you entered o); break; case u : printf(you entered u); break; default : printf(you did not enter a vowel) } Example : In this example, the switch statement will allows execution of different statements depending on the value of n entered by the user. #include<stdio.h> main() { int n;
8

printf(\nEnter a number between 1 to 3); scanf(%d,%n); switch (n) case 1 : printf(you entered 1); break; case 2 : printf(you entered 2); break; case 3 : printf(you entered 3); break; default : printf(wrong choice) }

Explain the for-statement. Give the syntax of for-statement

The for statement is a looping construction. It has the following form: for (initialization; condition; increment) { statements } where initialization is a statement that is executed once at beginning of for loop. condition is an expression that can be true (nonzero) or false (zero). This expression is tested prior to each iteration of the loop. The loop terminates when it is false. increment is a statement that is executed after statements. statements is a sequence of statements. If there is only one statement then the braces may be omitted.

Example: The following program will print the numbers 1 to 10 in reverse order #include<stdio.h> main() { int i ; for (i=10 ; i>=0 ; i--) printf(%d\n,i); } Example: The following program will print the sum of numbers from 1 to 10 #include<stdio.h> main() { int i ,sum = 0 ; for (i=1 ; i<=10 ; i++) sum=sum + i ; printf(sum = %d,sum); } Example: The following program will print the sum of all even numbers from 1 to 100
9

#include<stdio.h> main() { int i ,sum = 0 ; for (i=2 ; i<=100 ; i=i+2) sum=sum + i ; printf(sum of even numbers = %d,sum); } Example: The following program will print the first 10 odd numbers #include<stdio.h> main() { int i ; for (i=1 ; i<=20 ; i=i+2) printf(%d\n,i); }

Explain the syntax of while statement

The while statement is a looping construction which has the following form: while (loop condition) { statement block; } where: loop condition is an expression that can be true (nonzero) or false (zero). This expression is tested prior to each iteration of the loop. The loop terminates when it is false. When the condition is false, control will go to the first statement after the curly bracket } statements is a sequence of statements. If there is only one statement then the braces can be omitted.

Example: The following program will print the first 10 natural numbers. The loop is executed 10 times. #include<stdio.h> main() { int i=1 ; while (i<=10) { printf(%d\n,i); i++; } }

Explain : Do-While Loops

The do-while statement is a looping construction. It has following form:


10

do { statement block ; } while (loop condition); where statements is a sequence of statements. If there is only one statement then the braces may be omitted. condition is an expression that can be true (nonzero) or false (zero). This expression is tested after each iteration of the loop. The loop terminates when it is false.

Example: The following program will print the first 10 natural numbers. The loop is executed 10 times. #include<stdio.h> main() { int i=1 ; do { printf(%d\n,i); i++; } while (i<=10) }

Give the syntax of the simple if- statement

The simplest form of if statement is as follows: if (condition) statement; The if keyword must be followed by a set of parentheses containing the expression to be tested. The parentheses is followed by a single statement which is executed only if the test expression evaluates to true.

Example: The following program uses the simple if statement: #include<stdio.h> main() { int n; printf(Enter a positive number less than 10); scanf(%d,&n); if (n<=10) printf("Thank you); if (n>10) printf("Sorry. The number is greater than 10); }

Describe the if-else statement


11

The if statement has one of two forms: if (condition) { true-statements } or if (condition) { true-statements } else { false-statements } where condition is an expression that can be true (nonzero) or false (zero). true-statements and false-statements are sequences of statements. If there is only one statement in a sequence then the surrounding braces may be omitted. The second form includes the else clause For both forms, true-statements are executed only if condition is true. For second form the false-statements are executed if condition is false.

Example : The following statement is used to test whether a number entered by the user is positive or negative #include <stdio.h> void main () { int n ; printf ("Enter a non-zero number") ; scanf ("%d", &n); if (n < 0) printf ("number is negative") ; else printf ("The number is positive") ; } Example: Finding the greater of two numbers #include <stdio.h> void main () { int a,b ; printf ("Enter a number") ; scanf ("%d", &a); printf ("Enter another number") ; scanf ("%d", &b); if (a>b)
12

printf ("%d is greater than %d", a,b) ; else printf ("%d is greater than %d", a,b) ; }

State four forms of the if statement and explain any two

The simplest form of if statement is as follows: if (condition) statement; The general form of the if statement includes the else clause. This has the following form: if (condition) statement; else statement; If the programmer wants to execute more than one statement at this point, they may be grouped using curly braces. Such statement group is called compound statement. if (condition) { statement; statement; } In most general form of the if statement is : if (condition) { statement; statement; } else { statement; statement; } Example: The following example uses if statement to find the greatest of three numbers entered by the user. #include <stdio.h> main () { int a, b, c, big ; printf ("Enter three numbers"); scanf ("%d %d %d", &a, &b, &c); if (a > b) && (a > c) big = a ;
13

else { if (b > c) big = b ; else big = c ; } printf ("largest number is %d", big) }

Explain the nesting of if-statement

The if statement can itself contain another if statement. This is known as nesting of if statement. The nested if may appear in a program as : if(condition 1) { if(condition 2) statement 1; else statement 2; } else { if(condition 3) statement 3; else statement 4; } The main thing to remember about nested if is that the else statement always refers to the nearest if statement within the same block. Example : This program uses the nested if to find greatest of three numbers #include <stdio.h> main () { int a, b, c, big ; printf ("Enter three numbers"); scanf ("%d %d %d", &a, &b, &c); if (a > b) { if (a > c) big = a ; else big = c; } else { if (b > c) big = b ; else big = c ; } printf ("largest of %d, %d & %d = %d", a, b, c, big) ;
14

Explain the else-if ladder with example

The construct of else-if ladder is shown below : if(condition1) statement1 ; else if(condition1) statement1 ; else if(condition2) statement2 ; . . . else if(conditionN) statementN ; else statement ; The conditional expressions are evaluated from top downward. When a true condition is found, the associated statement is executed, and rest of ladder is bypassed. If none of conditions is true, then final else statement is executed. The final else acts as a default condition i.e. if all other conditions tests fail, then the last else statement is performed. If there is no final else and all other conditions are false then no action will take place. Example : Following example illustrates the of if-else-if ladder #include <stdio.h> main () { int n; printf("Enter an integer between 1 and 5"); scanf(%d,n); if(n==1) printf("number is one\n"); else if(n==2) printf("number is two\n"); else if(n==3) printf("number is three\n"); else if(n==4) printf("number is four\n"); else if(n==5) printf("number is five\n"); else printf("You didn't follow the rules"); }

Explain the ? : operator (Conditional Operator)

C language uses a combination of ? and : for making two way decisions. This operator is called the conditional operator and it takes three operands. The general form of the conditional operator is : conditional expression ? expression1 : expression2

15

The conditional expression is first evaluated. If result is non-zero, then expression1 is evaluated and its value is returned. If result is zero, then expression2 is evaluated and its value is returned.

Example : Consider the following code : if (x>3) a = 5; else a = 1; The same can be written as : a = (x>3) ? 5 : 1 ; The conditional operator can be nested for more complex assignments. For example consider the following Salary = 4R+20 for R < 20 = 150 for R = 20 = 5R+90 for R > 20 This can be written as: salary = (R=20)? 150:((R<20)? (4*R+20):(5*R+90)) The code is concise, but more difficult to read. Hence it is better to use if else statements when nesting of conditional operator is required. if (R=20) salary else if R < salary else salary

= 150 ; 20 = 4*R+20 ; = 5*R+90 ;

16

C PROGRAMS

Write a C program to accept marks of three subjects, and then print the total and average marks

/* Program: Add three marks and print total and average */ #include<stdio.h> main() { int m1, m2, m3, total; float average; printf(Enter marks of three subjects); scanf(%d %d %d,&m1, &m2, &m3); total = m1 + m2 + m3; printf("total marks is %d", total); average = total/3; printf("average marks is %d", total); }

Write a C program to accept a number and display it in octal and hexadecimal form

/* Program to print octal and hexadecimal numbers */ #include<stdio.h> main() { int n; printf(\nEnter a decimal number:); scanf(%d,&n); printf(\nNumber is %d in decimal, %o in octal, %x in hexadecimal); }

Write C program using conditional operator to determine whether year entered through at the keyboard is a leap year or not

/* Program : To test for a leap year */ #include<stdio.h> main() { int year; printf(Enter an year: ); scanf(%d,&year); if (year%4 == 0 && year%100 != 0 || year%400 == 0) printf(year is a leap year); else printf(year is not a leap year); } Write a C program to find whether a number is odd or even /* Program for odd-even test */ #include<stdio.h> main()

17

{ int n; printf(Enter an integer: ); scanf(%d,&n); if (n%2 == 0) printf(number is even); else printf(number is odd); } Write a C program to find whether number is divisible by 7 or not /* Program : To test divisibility by 7 */ #include<stdio.h> main() { int n; printf(Enter an integer: ); scanf(%d,&n); if (n%7 == 0) printf(number is divisible by 7); else printf(number is not divisible by 7); }

Write a C program to find the factorial of a number /* Program : To find the factorial of a number */ #include<stdio.h> main() { int n, fact=1; printf(Enter a positive integer: ); scanf(%d,&n); for (i=1;i<=n;i++) fact = fact * i; printf(\nfactorial of %d is %d, n,fact); }

Write a C program to find whether a number is prime of not

/* Program : Prime number test*/ #include<stdio.h> main() { int i,n, isprime=0; printf("Enter the number to be checked"); scanf("%d",&n); for(i=2;i<=n;i++) { if(n%i==0) isprime=-1; } If (isprime == 0 ) printf("number is prime") ;
18

else printf("number is not prime"); } /* Program : Prime number test*/ #include <stdio.h> main() { int n, count ; printf (Enter a number); scanf("%d",&n); for(i=2;i<n;i++) { if(n%i==0) count++; } if(count>1) printf("not prime no."); else printf("prime"); }

Write a C program to print sum of digits in 3 digit numbers

/* Program : Add digits of number */ #include <stdio.h> main() { int n, u, t, h; printf(Enter a 3 digit integer: ); scanf(%d,&n); u = n%10; n = n/10; t = n%10; n = n/10; h = n%10; printf(\n Sum of digits : %d, u+t+h); }

Write a C program to reverse the three digit number entered as input through keyboard

/* Program : Reverse 3 digit number */ #include <stdio.h> main() { int n, u, t, h; printf(Enter a 3 digit integer: ); scanf(%d,&n); u = n%10; n = n/10; t = n%10; n = n/10;
19

h = n%10; printf(\nReversed number : %d%d%d, u,t,h); }

Write a C program to reverse digits of a number entered as input through keyboard

/* Program : Reverse digits of any number */ #include <stdio.h> main() { int n, digit; printf(Enter a 3 digit integer: ); scanf(%d,&n); printf(\nThe reversed digits are: ); do { digit=n%10; printf(%d,digit); n = n/10; } while (n>0); printf(\n); }

Write a C program using if else ladder to grade student according to following rules. Marks 70 to 100 60 to 69 50 to 59 40 to 49 0 to 39 Grade Distinction I class II class pass class fail

/* Program : Grading of students */ #include <stdio.h> main () { int marks ; printf("Enter marks\n") ; scanf("%d",&marks); if (marks<=100 && marks>=70) printf ("\n Distinction"); else if (marks>=60) printf("\n First class") ; else if (marks>=50) printf ("\n second class") ; else if (marks>=35) printf ("\n pass class") ; else printf ("Fail") ; }

Write a C program to find sum of series : 1+3+5+7++N

/* Program : Sum of series */


20

#include <stdio.h> main () { int i,n, sum = 0 ; printf("Enter value of n: ") ; scanf("%d",&n); for (i=0;i<=n; i=i+2) sum = sum + i; printf ("sum = %d , sum"); }

Write a C program to find sum of series : 0+1+3+6+10+15+21+upto N numbers

/* Program : Sum of series */ #include <stdio.h> main () { int i,n, sum = 0,term=0 ; printf("Enter value of n: ") ; scanf("%d",&n); for (i=0;i<=n-1; i++) { term = term + i; sum = sum + term ; } printf ("sum = %d , sum"); }

21

Anda mungkin juga menyukai