6

Suresh Kumar Suthar 2 Roll No: 520776763 3 BCA 1st Semester 4 BC0034 – 01 5 COMPUTER CONCEPTS & C PROGRAMMING

1

7 1. Explain the various data types used in C with suitable examples. Ans: C language has a rich set of data types. Storage representations and machine instruction to handle constants differ from machine to machine. The variety of data types available allow the programmer to select the type appropriate to the needs of the application as well as the machine. C supports three classes of data types: 1. Primary or Fundamental data types: This includes char, int, float, and void data types. 2. Derived data types: This includes pointer and array. 3. User-defined data types: This includes structure and unions.  Integer Types: Integers are whole numbers with a range of values supported by a particular machine. Integers occupy one word of storage, and since the word size of machines vary (16 or 32 bit) the size of the integer that can be stored depends on the computer. If we use a 16 bit word length, the size of the integer value is limited to the range -32768 to +32767 (that is -215 to +215 -1). A signed integer uses one bit for sign and 15 bits for the magnitude of the number. Similarly, a 32 word length can store an integer ranging from -2,147,438,648 to +2,147,483,647. C has three classes of integer storage. Namely short int, int and long int. in both signed and unsigned forms.  Character Types: A single character can be defined as character (char) type data. Characters are usually stored in 8 bits (one byte) of internal storage. The qualifier signed or unsigned may be explicitly applied to char. While unsigned chars have values between 0 and 255, signed char have values from -128 to +127.  Floating Point Types: Floating point or real number are stored in 32 bits (on all 16 bit and 32 bit machines), with 6 digits of precision. Floating point numbers are defined in C by the keyword float. When the accuracy provided by a float number is not sufficient, the type double cab be use to define the number. A double data type number uses 64 bits giving a precision of 14 digits. These are known as double precision numbers. 1 Suresh Kumar Suthar Roll No: 520776763 BC0034 – 01 COMPUTER CONCEPTS & C PROGRAMMING 2 1

7E+308 3.648 to +2. This is usually used to specify the type of functions.1E- 1 Suresh Kumar Suthar Roll No: 520776763 BC0034 – 01 COMPUTER CONCEPTS & C PROGRAMMING 2 2 .768 to +32. Following tables shows the Size and Range of Data Types on a 16-bit Machine: Type char or signed char unsigned char int or signed int unsigned int short int or unsigned short int signed short int long int or signed long int unsigned long int float double long double 4932 Size (bits/bytes) 8/1 8/1 16 / 2 16 / 2 8/1 8/1 32 / 4 32 / 4 64 / 8 64 / 8 80 / 10 Range -128 to 127 0 to 255 -32.294.4E + 38 1.767 0 to 65.147.38 to 3. meaning that it can any of the other standard type.147.295 3.7E-308 to 1. The type of a function is said to be void when it does not return any value to the calling function.4E .483.647 0 to 4.4E-4932 to 1. Void Types: The void types has no values.483. It also play the role of a generic type.967.535 -128 to 127 0 to 255 -2.

2. Operator > >= < <= == != Description greater than greater than or equal to less than less than or equal to equal to not equal to Example 5>4 mark >= score height < 75 height <= input score == mark 5 != 4 3 1 Suresh Kumar Suthar Roll No: 520776763 BC0034 – 01 COMPUTER CONCEPTS & C PROGRAMMING 2 . 7. 3. 5. 4. 8.  Arithmetic operators: C provides all the basic arithmetic operators which are following: Operator + * / % Description Addition or unary plus Subtraction or unary minus Multiplication Division Modulo division Example 5+4 Result 9 5-4 Result 1 5*4 Result 20 5/4 Result 1 5/4 Result 1  Relational operators: C provides all the basic relation operators to compare two or more quantities and depending on their relation taking decision. These operators and their meanings are as below. Ans: C supports a rich set of operators. Discuss the different types of operators used in C with an example for each. 6. They are classified into following categories: Arithmetic operators Relational operators Logical operators Assignment operators Increment and decrement operators Conditional operators Bitwise operators Special operators(Comma ‘. 2.’ and sizeof() operator) 1.

Operator = Description Assignment Operator Example a= 5  Increment and decrement operators(Or Binary Operator): C provides shortcuts to add or subtract constant 1 to a variable.. C has the following three logical operators.. The logical operators && and || are used when we want to test more than one condition and make decision..  Assignment operators: Assignment operators are used to assign the result of an expression to a variable. Operator ++ -Description Increment Decrement Example a=a++ a=a--  Conditional operators: The conditional operator (ternary operator) pair “?:” is available in C to construct conditional expressions of the form Expr1?expr2:expr3 For example a=100. Logical operators: In addition to the relational operators. c=(a>b)?a:b... If (i <5 || I =5) . If ( i !=5) . Operator && || ! Description Logical AND Logical OR Logical NOT Example If(1< i && I <10) ...else statement as follows: 1 Suresh Kumar Suthar Roll No: 520776763 BC0034 – 01 COMPUTER CONCEPTS & C PROGRAMMING 4 2 . b=200. Result: c=200 This is same as if.

sizeof() Description Comma Operator Sizeof() Operator Example Value=(x=10. Operator . The ‘.’ and sizeof() operators. These operators let us work with the individual bits of a variable. one common use is to treat an integer as a set of single-bit flags. else c=b. when used with an operand it returns the numbers of bytes the operand occupies. a constant or a data type qualifier. The sizeof() is a compile time operator and.  Bitwise operators: The bitwise operators operate on integers thought of as binary numbers or strings of bits. x+y) Results: value=15 k= sizeof(235L) 1 Suresh Kumar Suthar Roll No: 520776763 BC0034 – 01 COMPUTER CONCEPTS & C PROGRAMMING 2 5 . The operand may be variable. y=5. Operator & | ^ << >> Description Bitwise AND Bitwise OR Bitwise exclusive OR Shift left Shift right Example  Special operators: C supports some special operators like ‘.’ (comma) operator can be used to link the related expression together.if (a>b) c=a.

Ans: C support many input and output statements. Explain the different types of input and output statements used in C. whitespace characters and nor-whitespace characters. arg1. If there is no more characters available. The control characters are preceded by a % sign and are listed below.argn) The control string consist of control characters. %f. Control Character %c %d %i %e.3. Syntax: scanf( control string. Example: char c. c=”A”. Formatted Input: Input data can be entered into the computer from a standard input device by means of the standard C library function scanf(). Example: char c. putchar(c). c=getchar().…. The function returns the number of data items that have been entered successfully. putchar() function: It writes one character to the standard output (Monitor). arg2. …. It also supports formatted input and output. the special value EOF will be return. This function can be used to enter any combination of numerical values. single character and strings. %g %o %s %x %p %n %u Explanation A single character A decimal integer An integer A floating-point number An octal number A string A hexadecimal number A pointer An integer equal to the number of characters read so far An unsigned integer 6 1 Suresh Kumar Suthar Roll No: 520776763 BC0034 – 01 COMPUTER CONCEPTS & C PROGRAMMING 2 . Basic Character Input & Output: getchar() function: It reads one character from the standard input.

except that its purpose is to display data rather than enter into the computer. float f. scanf(“%d %f %c %s”. char c.100). single characters and strings.argn) The control string characters are listed above (same as input control string) Example: printf(“%d %o %x\n”.str). Formatted Output: Output data can be written from the computer onto a standard output device using the library function printf().3. char str[10]. arg2.100. Syntax: printf(control string.&f. Will print : 3 4 3.”nine”.24. This function can be used to output any combination of numerical values. 100. It similar to the input function scanf().4.8).66000000. Will print : 100 144 64 Printf(“%c %d %f %e %s %d%%\n”.&c.’3’.%[] %% A set of characters A percent sign Example: int i. …. arg1.600000e+7 nine 8% 1 Suresh Kumar Suthar Roll No: 520776763 BC0034 – 01 COMPUTER CONCEPTS & C PROGRAMMING 2 7 .240000 6.&I.

C contains three statements for looping:  While Loop: While loop construct contains the condition first. } }  Do While Loop: Do while loop ensure that the program execute at least once and checks whether the condition at the end of the do-while loop is true or false. As long as the test condition is true the statement will be repeated. Discuss the types of looping structures used in C with examples. This condition causes the termination of the loop.h> void main() { int x.4. while(x<10) { printf(“x is %d /n”.h> #include<conio. Syntax: while(condition) { statement1 Statement2 } Example: : #include<stdio. Ans: Looping: The loop or iteration construct. it ignores these statements. directs a program perform a set of operation again and again until a specified condition is achieved. X=1. Define looping. x++. The control will come out from the loop only when the test condition is false. If the condition is satisfied the control executes the statement following the while loop exile. x). Syntax: do { Statement1 Statement1 }while(condition) 1 Suresh Kumar Suthar Roll No: 520776763 BC0034 – 01 COMPUTER CONCEPTS & C PROGRAMMING 2 8 .

do { printf(“x is %d /n”.h> #include<conio. it is a shorthand method for executing statement in a loop. test condition. counter<=1.h> 1 Suresh Kumar Suthar Roll No: 520776763 BC0034 – 01 COMPUTER CONCEPTS & C PROGRAMMING 2 9 . counter--) { printf(“%d”. x). counter).Example: : void main() #include<stdio. } } #include<stdio.h> { int x.h> #include<conio. } while(x<10) }  For loop construct is used to execute a set of statements for a given number of times. Thus. Syntax: for(initial condition. X=1. for(counter=20. increment or decrement) { Statement1 Statement2 } Example: void main() { int counter. x++.

Write the advantages and disadvantages of functions. including pointers. and debugging. far outweigh any runtime savings. Often the headaches C causes.and much easier to write the function in Vortex. ii) It is easy to locate and isolate a faulty function for further investigations. and in particular. This means that a C programmer can build on what others have already done. Disadvantages of Functions: vi) While adding a user function can speed up code that is best written in C rather than a scripting language. especially for the neophyte. In this programming style. Define functions. dynamic memory allocation. that the code within the function has some useful properties. it may be 1 Suresh Kumar Suthar Roll No: 520776763 BC0034 – 01 COMPUTER CONCEPTS & C PROGRAMMING 2 10 . making debugging a nightmare. When the program passes control to a function the function perform that task and returns control to the instruction following the calling instruction. for small functions it may be just as fast . the high level logic of the overall problem is solved first while the details of each lower-level function are addressed later. iii) A function may be used by many other programs. Since Vortex already has powerful data processing functions. The idea behind a function is that it compartmentalizes part of the program. v) Its interface to the rest of the program is clean and narrow.5. instead of starting all over again from scratch. iv) It facilitates top-down modular programming. viii) There may not be any speed advantage. Bugs in the code might not manifest themselves until well after the C function ends. A function is a “black box” that we’ve locked part of our program into. and the ability to execute external programs. function pointers. Ans: Definition of Function: A function is a routine or a set of instruction or code that performs a specific task and can be processed independently. The most important reason to use the function is make program handling easier as only a small part of the program is dealt with at a time. This factor is particularly critical with microcomputers where memory space is limited. Vortex is pretty fast at most operations. Advantages of Functions: i) The length of a source program can be reduced by using functions at appropriate places. it is not always the best choice for implementation: vii) It requires the programmer to be well versed in C.

1 Suresh Kumar Suthar Roll No: 520776763 BC0034 – 01 COMPUTER CONCEPTS & C PROGRAMMING 2 11 . Other Vortex users won't have the custom function in their taxis executable.faster to <EXEC> the C code in a separate program and parse it in Vortex. ix) It's less portable. A C function means a new Vortex executable must be made if the hardware platform changes. especially as a quick prototype.

} 1 Suresh Kumar Suthar Roll No: 520776763 BC0034 – 01 COMPUTER CONCEPTS & C PROGRAMMING 2 12 . } The example above defines two variables with the same storage class. C provides the following storage-class specifiers: Syntax storage-class-specifier : auto register static extern typedef i) auto .e. { int Count. i.6. All functions have global lifetimes. C calls these two lifetimes “static” and “automatic. Ans: The “storage class” of a variable determines whether the item has a “global” or “local” lifetime. Write a note on storage classes with examples. { register int Miles.Storage Class register is used to define local variables that should be stored in a register instead of RAM. When execution returns. Automatic variables. This means that the variable has a maximum size equal to the register size (usually one word) and cant have the unary '&' operator applied to it (as it does not have a memory location). the variables no longer have meaningful values. are allocated new storage each time execution control passes to the block in which they are defined.” An item with a global lifetime exists and has a value throughout the execution of the program.storage class auto is the default storage class for local variables. or variables with local lifetimes. ii) register . local variables. auto int Month. auto can only be used within functions.

Source 1 -----------extern int count. } The placement of variable and function declarations within source files also affects storage class and visibility.” The exact meaning of each storage-class specifier depends on two factors: • • Whether the declaration appears at the external or internal level Whether the item being declared is a variable or a function 1 Suresh Kumar Suthar Roll No: 520776763 BC0034 – 01 COMPUTER CONCEPTS & C PROGRAMMING 2 13 .such as counters. It should also be noted that defining 'register' goes not mean that the variable will be stored in a register. Declarations outside all function definitions are said to appear at the “external level. When you use 'extern' the variable cannot be initialized as all it does is point the variable name at a storage location that has been previously defined.” Declarations within function definitions appear at the “internal level. } Source 2 -------int count=5.depending on hardware and implementation restrictions. static int Count. int Road. main() { write(). count). iv) extern . static . The two variables below (count and road) both have a static storage class. write() { printf("count is %d\n".Storage Class static is the default storage class for global variables. It means that it might be stored in a register .Register should only be used for variables that require quick access .storage Class extern defines a global variable that is visible to ALL object modules.

so statements like arr ++ are illegal. An array is actually a pointer to the 0th element of the array. Ans: An array is a single. arrays and pointers often seem interchangeable. Since an array is like a pointer. but a pointer is a much more general data structure Due to the so-called equivalence of arrays and pointers. preallocated chunk of contiguous elements (all of the same type). A pointer can point to an array. but arr[n] ++ is legal. A pointer is a reference to any data element (of a particular type) anywhere. if derived from malloc. Array Access arr[0] arr[2] arr[n] Pointer Equivalent *arr *(arr +2) *(arr +n) The array is treated as a constant in the function where it is declared. all the difficult stuff gets done automatically. Distinguish between arrays and pointers with suitable examples. This means that we can modify the values in the array. Dereferencing the array name will give the 0th element. we can pass an array to a function. 1 Suresh Kumar Suthar Roll No: 520776763 BC0034 – 01 COMPUTER CONCEPTS & C PROGRAMMING 2 14 . but it can be reassigned (and the space. A pointer must be assigned to point to space allocated elsewhere. and in particular a pointer to a block of memory assigned by malloc is frequently treated (and can be referenced using []) exactly as if it were a true array. A function which expects to be passed an array can declare that parameter in one of two ways. This gives us a range of equivalent notations for array access. In the following examples. can be resized) at any time. fixed in size and location.7. arr is an array. Since the array is implemented as a hidden pointer. and modify elements of that array without having to worry about referencing and de-referencing. but not the array itself. and can simulate (along with malloc) a dynamically allocated array.

} str1[i]=’\0’. i++.h> main() { char str1[30]. while(str1[i] !=’\0’) { i++. i++) { } return i. printf(“Enter the first string :”). }/*function ends here*/ /*in main() function use these function*/ #inclued<stdio. Ans: Function in C to implement string length: /*function to find the string length*/ int mystrlen(char str[]) /* int type function. for(i=0. j++.j. char str2[]) { int i. it will accept an array of characters and return total number of characters */ { int i. } while(str2[j] !=’\0’) { str1[i]=str2[j]. Write your own function in C to implement string length and string concatenation without using built in functions. str[i] !=’\0’. str2[30]. i=j=0. 1 Suresh Kumar Suthar Roll No: 520776763 BC0034 – 01 COMPUTER CONCEPTS & C PROGRAMMING 2 15 .8. }/*function ends here*/ Function in C to implement string concatenation: /*function to concatenate two string*/ mystrconcat(char str1[].

printf(“After concatenation the first string is : %s\n”.str2).gets(str1). printf(“Length of first string is : %d and length of second string is : %d \n”. gets(str2). mystrlen(str2)). mystrconcat(str1. printf(“\n Enter the second string :”). mystrlen(str1). }/*End of main()*/ 1 Suresh Kumar Suthar Roll No: 520776763 BC0034 – 01 COMPUTER CONCEPTS & C PROGRAMMING 2 16 . str1).

Sign up to vote on this title
UsefulNot useful

Master Your Semester with Scribd & The New York Times

Special offer for students: Only $4.99/month.

Master Your Semester with a Special Offer from Scribd & The New York Times

Cancel anytime.