Advanced C 2 Marks
Advanced C 2 Marks
Advanced C 2 Marks
The data items that operators act upon are called operands.
Example:
a+b; In this statement a and b are called operands.
Example :
int result, a=35,b=40;
result = a<b? 10 : 20;
In the above example, if the expression a>b is evaluated to true then 10 is assigned to
result otherwise 20 is assigned to result.
12. What is the difference between Logical AND and Bitwise AND?
Logical AND (&&): Only used in conjunction with two expressions, to test more than one
condition. If both the conditions are true the returns 1. If false then return 0.
17. What is the difference between while loop and do…while loop? (JAN 2011/2013)
In the while loop the condition is first executed. If the condition is true then it executes the
body of the loop. When the condition is false it comes of the loop. In the do…while loop
first the statement is executed and then the condition is checked. The do…while loop will
execute at least one time even though the condition is false at the very first time.
19. How many bytes are occupied by the int, char, float, long int and double?
int - 2 Bytes, char - 1 Byte, float - 4 Bytes, long int - 4 Bytes, double - 8 Bytes
4
24. What are the Escape Sequences present in ‘C’? (JAN 2013)
\n - New Line
\b - Backspace
\t - Form feed
\’ - Single quote
\\ - Backspace
\t - Tab
\r - Carriage return
\a - Alert
\” - Double quotes
}
Here 1 is a non zero, value so the condition is always true. So it is an infinite loop.
26. Write the limitations of getchar( ) and scanf( ) functions for reading strings
(JAN 2009)
getchar( )
read a single character from stdin, then getchar() is the appropriate.
scanf( )
scanf( ) allows to read more than just a single character at a time.
27. What is the difference between scanf() and gets() function? (MAY 2005)
In scanf() when there is a blank was typed, the scanf() assumes that it is an end.
gets() assumes the enter key as end. That is gets() gets a new line (\n) terminated string of
characters from the keyboard and replaces the ‘\n’ with ‘\0’.
35. What are macros? What are its advantages and disadvantages?
Macros are abbreviations for lengthy and frequently used statements. When a macro is called
the entire code is substituted by a single line though the macro definition is of several lines.
The advantage of macro is that it reduces the time taken for control transfer as in case of
function.
39. Write short notes about main ( ) function in “C” program. (MAY 2009)
While Do -While
Executes the statements within the while Executes the statements within the while
block if only the condition is true. block at least once.
The condition is checked at the starting The condition is checked at the end of the
of the loop loop
Top tested Bottom tested
8
47. What are global variable in ‘C’?
This section declares some variables that are used in more than one function. Such variable are
called as global variables.
It should be declared outside all functions.
48. Write a program to swap the values of two variables (without temporary variable).
#include <stdio.h>
#include <conio.h>
void main( )
{
int a =5; b = 10;
clrscr( );
prinf(“Before swapping a = %d b = %d “, a , b);
a = a + b; B = a – b;
a = a – b;
prinf(“After swapping a = %d b = %d”, a,b);
getch( );
}
Output:
Before swapping a = 5 b = 10
After swapping a = 10 b = 5
(Type-name)expression
Example:
int x;
x = int(7.5);
1. What is an array? Write the syntax to declare an array. Also give an example.
An array is a group of related data items that share a common name.
Syntax: data type arrayname[size];
9
Example: int a[10];
5. What will happen when you access the array more than its dimension?
When you access the array more than its dimensions some garbage value is stored in the array.
11. How two dimensional arrays can be initialized? Give example / Define the syntax for
two dimensional array. (Jan 2011)/ May/June 2014
It is an array declared with two subscripts (row and column).
Syntax:
Data-type variablename[row_size][column_size];
Eg:
int ar[3][4]; ar holds 12 elements with 3 rows & 4 columns.
12. Declare a float array of size 5 and assign 5 values to it. (Nov 2015)
float height[5]={56.2,45.2,42.3,21.6,31.2};
14. How strings are represented in ‘C’ language? (JAN 2013) / What are strings? How are
they declared? (Jan 2010)
The group of characters, digit and symbols enclosed within quotes is called as String (or)
character Arrays. Strings are always terminated with ‘\0’ (NULL) character. The compiler
automatically adds ‘\0’ at the end of the strings.
11
17. Why we don’t use ‘&’ symbol, while reading a string through scanf()?
String are represented using character array
Eg: char str[size]
In case of int, char, float, etc we need to provide address which is done by using '&' (address of
operator), whereas in case of character array: the name of the array itself represents the address
of the first element of the array hence no need of '&' just the name is required i.e str
19. Which function is more appropriate for reading a multi word string, gets() or scanf()?
gets() function is more appropriate for reading a multi word string, it collects a string of
characters terminated by a new line from the standard input stream stdin.
The C library supports a function that converts a string of digits into their integer values. The
function takes the form x = atoi(string)
In the above format enum is a keyword, name is given by the programmer by the identifier
rules. Number1, number2,…number are the members of enumerated data type
UNIT III POINTERS AND STRUCUTURES
1. Define function.
A function is a self contained program segment (block of statements) that carries out some
specific, well defined task.
13
Defining a function (or) function definition
Calling the function (or) function call
14. List out any two uses of pointers. (or) Mention some of the advantages of pointers
15
Pointers reduce the length and completing of the program
Pointers improve the efficiency of certain routines
It supports dynamic memory allocation
It provides functions which can modify their calling arguments
17. What is the difference between the indirection operator and the address of operator?
The indirection operator(*) returns the value of the address stored in a pointer. The address of
operator(&) returns the memory address of the variable.
19. Mention some of the library functions dealing with memory allocation.
The library functions dealing with memory allocation are malloc(), calloc(), realloc() and
free().
24. State the equivalent pointer declaration of the array declaration char a[10];
The equivalent pointer declaration is char *a[];
25. What does the error ‘Null Pointer Assignment’ means and what causes this error?
As null pointer points to nothing so accessing a uninitialized pointer or invalid location may
cause an error.
18
34. What is meant by a union? Give example.
A union is a data type in ‘C’ which allows overlay of more than one variable in the same
memory area.
Example:
union emp
{
char name[20];
char eno[10];
}
union emp e1;
Example
typedef int units;
units a,b,c; is equivalent to int a,b,c;
19
Example:
struct stud
{
int rno;
char name[20];
};
struct stud *s1;
40. Difference between static memory allocation and dynamic memory allocation.
S.No. Static memory allocation Dynamic memory allocation
Memory is allocated before the Memory is allocated during the
1 execution of the program execution of the program
begins
Variables remain permanently Allocated only when program unit
2
allocated is active
Memory cannot be resized after Memory can be dynamically
3
the initial allocation expanded and shrunk as necessary
4 Implemented using stacks Implemented using heap
5 Faster execution Slower execution
Memory cannot be reuse when Memory can be freed when it is no
6 it is no longer needed longer needed and reuse or
reallocate during execution
1. Define file
21
A file is a region of storage on hard disks or an auxiliary storage device such as magnetic tape
or disk where a sequence of data is stored. It contains bytes of information.
3. Define stream.
A stream is a source or destination data associated with input / output device. Actually stream
is a pointer representing a block to hold data temporarily during the input / output operations.
11. How will you check end of the file? Give example.
The feof( ) function determines whether the end of the file associated with stream has been
reached. A nonzero value is returned if the file position indicator is at the end of the file; zero is
returned otherwise.
buffer is a pointer to a region of memory that will receive the data from the file. The value of
count determines how many items are read or written, with each item being num_bytes bytes in
length.
17. Write the C code segment to detect error when opening a file.
FILE *fp;
if ((fp = fopen("test", "w"))==NULL) {
printf(''Cannot open file.\n");
exit(1);
}
18. What are command line arguments in C?
It is possible to pass some values from the command line to your C programs when they are
executed. These values are called command line arguments.
The command line arguments are handled using main() function arguments where argc refers
to the number of arguments passed, and argv[] is a pointer array which points to each argument
passed to the program
24
19. Is FILE a built-in data type?
No, it is a structure defined in stdio.h.
20. What is difference between file opening mode r+ and w+ in c?
Both r+ and w+ we can read ,write on file but r+ does not truncate (delete) the content of
file as well it doesn’t create a new file if such file doesn’t exits while in w+ truncate the
content of file as well as create a new file if such file doesn’t exists.
Where fp is a file pointer. Also we can get the same effect by feek(fp,0,0);
25