P. 1
SEM 1 BC0034 Computer Concepts C Programming

SEM 1 BC0034 Computer Concepts C Programming

|Views: 148|Likes:
Published by system9624

More info:

Published by: system9624 on May 13, 2012
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less






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


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

295 3.647 0 to 4.4E-4932 to 1.483.4E + 38 1.294.483.767 0 to 65. 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.147.648 to +2. Void Types: The void types has no values.147.1E- 1 Suresh Kumar Suthar Roll No: 520776763 BC0034 – 01 COMPUTER CONCEPTS & C PROGRAMMING 2 2 .535 -128 to 127 0 to 255 -2. meaning that it can any of the other standard type.7E-308 to 1. This is usually used to specify the type of functions.38 to 3.967. The type of a function is said to be void when it does not return any value to the calling function. It also play the role of a generic type.4E .7E+308 3.768 to +32.

Discuss the different types of operators used in C with an example for each. 4.  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. Ans: C supports a rich set of operators. 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 . 8.2. 3. 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 ‘. These operators and their meanings are as below. 7. 5.’ and sizeof() operator) 1. 6.

C has the following three logical operators. Logical operators: In addition to the relational operators. c=(a>b)?a:b.  Assignment operators: Assignment operators are used to assign the result of an expression to a variable.. b=200.. 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. The logical operators && and || are used when we want to test more than one condition and make decision.. Operator && || ! Description Logical AND Logical OR Logical NOT Example If(1< i && I <10) . Result: c=200 This is same as if.. 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... If (i <5 || I =5) .else statement as follows: 1 Suresh Kumar Suthar Roll No: 520776763 BC0034 – 01 COMPUTER CONCEPTS & C PROGRAMMING 4 2 .. If ( i !=5) .

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

the special value EOF will be return. This function can be used to enter any combination of numerical values. Basic Character Input & Output: getchar() function: It reads one character from the standard input. putchar() function: It writes one character to the standard output (Monitor). Example: char c. Formatted Input: Input data can be entered into the computer from a standard input device by means of the standard C library function scanf(). arg2. c=getchar(). putchar(c). %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 . If there is no more characters available. Syntax: scanf( control string. single character and strings.argn) The control string consist of control characters. Explain the different types of input and output statements used in C.3. The control characters are preceded by a % sign and are listed below. The function returns the number of data items that have been entered successfully.…. Control Character %c %d %i %e. c=”A”. It also supports formatted input and output. …. arg1. Example: char c. Ans: C support many input and output statements. whitespace characters and nor-whitespace characters. %f.

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

The control will come out from the loop only when the test condition is false.h> #include<conio.h> void main() { int x. C contains three statements for looping:  While Loop: While loop construct contains the condition first. X=1. directs a program perform a set of operation again and again until a specified condition is achieved. while(x<10) { printf(“x is %d /n”. Ans: Looping: The loop or iteration construct. Discuss the types of looping structures used in C with examples.4. This condition causes the termination of the loop. } }  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. x++. 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 . it ignores these statements. Syntax: while(condition) { statement1 Statement2 } Example: : #include<stdio. As long as the test condition is true the statement will be repeated. Define looping.

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

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

ix) It's less portable. Other Vortex users won't have the custom function in their taxis executable. 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 11 .faster to <EXEC> the C code in a separate program and parse it in Vortex.

6. { register int Miles. All functions have global lifetimes. } 1 Suresh Kumar Suthar Roll No: 520776763 BC0034 – 01 COMPUTER CONCEPTS & C PROGRAMMING 2 12 . 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).Storage Class register is used to define local variables that should be stored in a register instead of RAM. C calls these two lifetimes “static” and “automatic. Automatic variables. are allocated new storage each time execution control passes to the block in which they are defined.storage class auto is the default storage class for local variables. C provides the following storage-class specifiers: Syntax storage-class-specifier : auto register static extern typedef i) auto . local variables. When execution returns. auto can only be used within functions. } The example above defines two variables with the same storage class. auto int Month.” An item with a global lifetime exists and has a value throughout the execution of the program. ii) register . or variables with local lifetimes. { int Count. 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. i. the variables no longer have meaningful values.e.

storage Class extern defines a global variable that is visible to ALL object modules. count). write() { printf("count is %d\n". Source 1 -----------extern int count.” 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 . int Road. main() { write(). It should also be noted that defining 'register' goes not mean that the variable will be stored in a register.such as counters.depending on hardware and implementation restrictions. static int Count. } Source 2 -------int count=5. } The placement of variable and function declarations within source files also affects storage class and visibility.” Declarations within function definitions appear at the “internal 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.Storage Class static is the default storage class for global variables. Declarations outside all function definitions are said to appear at the “external level. The two variables below (count and road) both have a static storage class. iv) extern . static .Register should only be used for variables that require quick access . It means that it might be stored in a register .

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

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

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

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->