C Prog Ass1

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 6

February 2010 Bachelor of Computer Application (BCA) Semester 1

BC0034 Computer Concepts & C Programming 4 Credits


(Book ID: B0678)

Assignment Set 1 (40 Marks)

Answer the following questions. Each question carries 5 marks.

5 x 8 = 40

1. What is the difference between declaration and initialization? Explain the

with example. Declaration In C language every variable should be declared before using it. The declarative statement describes the compiler about the variables we use in the program and its properties such as data type etc. Generally variables are declared as shown by the syntax below. storage class data type v1, v2, vn ; where storage class describes the accessibility of the variable. data type represents the type of the value to be stored in the variable and v1, v2, vn etc represent the declared variables name. Most oftenly the storage class is not used and used occasionally as per the requirement. Example : int a,b,c; Initialization Assigning a value to a variable at the declarative statement is called initialization. We use the assignment operator = to initialize the variable. When a variable is declared, it contains the garbage value. Not all variables require to be initialized. Under special circumstances we initialize them. Once the variable is initialized then the said value will be assigned into it. The initialized variable can be used in any statements later. We cannot initialize a variable unless we declare it. Example : int n, i, prime = 1; is an example for initialization where the variable prime is initialized to 1.

Page 1

2. Write a program to read four floating point numbers and find their sum and average. #include <stdio.h> #include <conio.h> void main() { float a,b,c,d,sum,avg; clrscr(); printf("\nInput any four numbers\n\n"); scanf("%f %f %f %f",&a,&b,&c,&d); sum = a + b + c + d; avg = sum / 4; printf("\nSum = %2.3f\nAverage = %2.3f",sum,avg); getch(); }
3. What are the commonly used input / output functions in C ? How are

they accessed. In C, input and output functions are classified into three categories. They are a) Character I/O functions. b) Formatted I/O functions. c) String I/O functions. Character I/O functions : Functions which can read or print only one character at a time are called Character I/O functions. C supports two character I/O functions. They are getchar( ) and putchar( ). getchar( ) is a character input function and putchar( ) is a character output function. They are used as ch = getchar( ); putchar(ch); assuming ch as a character type variable. Example : ch = getchar( ); putchar(S); Formatted I/O functions : Functions which can read or print format specified values are called formatted I/O functions. These functions can read or print multiple values of different data types at a time. scanf( ) and printf( ) are the two formatted I/O functions. They are used as

Page 2

scanf(control string,&v1,&v2..); printf(control string,&v1,&v2..); where control string is a group of control characters such as %d, %c, %f etc. Apart from these characters we can also use escape sequence characters such as \n, \t, \a etc with printf( ). Example : scanf(%d %f %c,&a,&b,&c); printf(\na = %d\nb = %2.3f\nc = %c,a,b,c); String I/O functions : Functions which can read or print strings are called as String I/O functions. C supports two String I/O functions. They are gets( ) and puts( ). The gets( ) function reads the string and the puts( ) function prints the string. They are used as gets(s); puts(s); where s is an array of characters or a string. Example : char s[30]; gets(s); puts(s); 4. Explain different types of constants. In C, constants are classified into two categories. They are a) Numerical constants : which are numbers such as integers, floating point numbers etc. b) Character constants : which are characters and strings. Numerical constants are further classified into two more categories. They are I. Integer constants : these constants are integer quantities which do not have fractional part. They are further classified into three types. They are i. Decimal constants : any integer represented in base 10 or decimal number system is called as decimal constant. Example : 15 ii. Octal constant : any integer represented in base 8 or octal number system is called as octal constant. They are alwas represented with 0. Example : 0123 iii. Hexa decimal constants : any integer represented in base 16 or hexa decimal number system is called as hexa decimal constant. They are alwas represented with 0x. iv. Example : 0x271
Page 3

II.

Floating point constants : A fractional numerical quantity is called as floating point quantity. Such quantities have mantissa part. Example : 17.231

Character constants are further classified into two more categories. They are I. Character constants : these constants are single characters represented within a pair of single quotes. Example : 'S' II. String constants : A group of characters which are represented within a pair of double quotes are called as string constants. Example : "C Programming 5. What are precedence of operators? How expressions are evaluated using precedences? Operator precedence describes the order in which C reads expressions. For example, the expression a = b + c * 2 contains two operations, an addition and a multiplication. The C compiler evaluate c*2 first, then add b to the result. The operator precedence chart contains the answers. Operators higher in the chart have a higher precedence, meaning that the C compiler evaluates them first. Operators on the same line in the chart have the same precedence, and the "Associativity" column on the right gives their evaluation order. Operator Precedence Chart Operator Type Primary Expression Operators Unary Operators Binary Operators Operator ( ) [ ] . -> expr++ expr-* & + - ! ~ ++expr --expr (typecast) sizeof() */% +>> << < > <= >= == !=
Page 4

Associativity left-to-right right-to-left left-to-right

& ^ | && || Ternary Operator Assignment Operators Comma ?: = += -= *= /= %= >>= <<= &= ^= |= , right-to-left right-to-left left-to-right

6. Explain pre increment and post increment operator with an example. ++ is one of the unary operators commonly used in C. It is also called as incrementation operator. It can be used in two forms. They are a) Prefix form : In this form, the operator is placed before the operand. In such cases, pre incrementation takes place before assigning the result to LHS. Example : int a = 5, b; b = ++a; ++ used in prefix form so that a = 6 and b = 6 after execution b) Postfix form : In this form, the operator is placed after the operand. In such cases, the present value of the operand is assigned to LHS and then post incrementation takes place. Example : int a = 5, b; b = a++; ++ used in postfix form so that a = 6 and b = 5 after execution 7. What are the rules used in naming a variable ? Give examples. In C, variables should be declared explicitly. While declaring them we should consider the following rules. 1. First character of a variable must be an alphabet & rest of the charters may be alphabets or digits. Example : sum1 is a valid variable 1sum is an invalid variable since the first character is a digit.

Page 5

2. Only 32 characters are allowed per variable.

3. Both uppercase & lowercase characters are permitted, but they can't be interchanged. Example : if sum is a declared variable then we cant use SUM instead of sum. 4. Key words cannot be used as variables. Example : we cannot declare a variable called int since it is a key word. 5. Any special characters are not allowed except underscore ( _ ). Example : factorial of n (invalid since it has blank spaces) factorial_of_n (valid since underscore is used)

Page 6

You might also like