c programming Chapter wise

2nd Semester

<hr/



Chapter 1

From which function C program starts?  2017
Ans:The execution of C program always stars from main () function. This is reason every C program has at least one function called main ().
 How to Declare and Use Constants in the C Language.year 2012 chapter 1
constants are declared using the C language preprocessor and not the compiler. Here’s the format:
#define VOTING_AGE 18
 Executing a C program involves a series of steps. They are,  year 2015

  1. Creating the program.
  2. Compiling the program.
  3. Linking the program with functions that are needed from the C library.
  4. Executing the program.

 

Chapter 2

What is the difference between variable declaration and variable definition?  year 2015 
Declaration associates type to the variable whereas definition gives the value to the variable.
Why n++ executes faster than n+1?year 2015
The expression n++ requires a single machine instruction such as INR to carry out the increment operation whereas, n+1 requires more instructions to carry out this operation.
What are enumerations? year 2015 
Enumerations are list of integer constants with name. Enumerators are defined with the keyword enum.
Basic difference between float and double variable .2015 
Float is of 32 bits (4 byte) in which 23 bits are used to store mantissa and 8 bits are used for storing exponent and 1 bit is used for sign. It can hold about 6 to 9 decimal digits
Double is of 64 bits (8 byte) in which 52 bits are used to store mantissa and 7 bits are used to store exponent and 1 bit is for sign. It can hold about 15 to 17 decimal digits.
What is the difference between float and  double data type? 2018
Float: This data type can hold number with decimal and fractional part.
Double data type: If the precision limit is to be more then float then double data can be used.
What is constant?  2017
Ans:Constants refer to fixed values that the program may not alter during its execution.
These fixed values are also called literals. Constants can be of any of the basic data types like an integer constant, a floating constant, a character constant, or a string literal.
What is type casting in C programming?2017
Ans:Type casting is a way to convert a variable from one data type to another data type.
For example, if we want to store a long value into a simple integer then you can typecast long to int. We can convert values from one type to another explicitly using the cast operator.
Example:
float avg ;
int total = 513;
avg = (float) total/7;
What is a static variable? year 2010
    A static local variables retains its value between the function call and the default value is 0. The following function will print 1 2 3 if called thrice.A static local variables retains its value between the function call and the default value is 0. The following function will print 1 2 3 if called thrice.
 
6.Use of typedf keyword .Chapter 2
The purpose of typedef is to assign alternative names to existing types.
 

Chapter 3

Define Type conversion.year 2013 
It is converting one type of data to another type. It is also known as Type Casting
What is the difference between puts and printf?year 2012 
The similarity is that both are used to display content on the screen.
The difference is puts is used to display only strings. However, printf is used to display integer, float, character, hexa decimal values as well as strings.

Chapter 4

What is the use of == operator?2017
Ans:The use of == operator is to compare two operands. The output of == operator is either 1(true) or 0(false). It is relational operator.
What is wrong with the statement? 2017
scanf (“%”,den1)
Ans:The corrected statement is  scanf (“%d”,&den1)

Chapter 5

Does a break required by default case in switch case statement?2017
Ans:Normally default statement is the last statement in switch case structure. If this is the case we do not require break statement. But if the default statement is in earlier position then we need break statement.
What is the use of gets function? 2017
Ans:The limitation of scanf() function is that it cannot read spaces. It terminates its reading as soon as it finds first space. It cannot read string like “good morning”. To read such types of string we use gets () function
Can we access the same memoy location with different union memers. Chapter 5
You can define a union with many members, but only one member can contain a value at any given time
What is a nested structure?  year 2010
A structure containing an element of another structure as its member is referred so. 

Chapter 6

What is if statement used?  2018
ANS: The if statement is used to select one alternative out of two.
 goto and continue statement in c.year 2012
It is sometimes desirable to skip some statements inside the loop. In such cases, continue statements are used.
goto statement is used for altering the normal sequence of program execution by transferring control to some other part of the program
 What is the relationship between array name and pointer in C ? Chapter 7  year 2010
The name of the array is a constant pointer which contains the memory address  of the first element of the array.

Chapter 8

The advantages of using functions are:year 2012 chapter 8

  • Avoid repetition of codes.
  • Increases program readability.
  • Divide a complex problem into simpler ones.
  • Reduces chances of error.
  • Modifying a program becomes easier by using function

What are the arguments passed to main () function?
ANS: Function call, function declaration, function prototype, function parameter and function return arguments are pass to main ( ) function.
use of memset function.year 2012
The memset() function shall copy c (converted to an unsigned char) into each of the first n bytes of the object pointed to by s.
What is function?  2017
Ans: The function is a self contained block of code that does some specific task. When the size of program is large we divide this large program into smaller programs and these sub-programs are called functions.
Define function.year 2015 
fseek() – It is used to moves the reading control to different positions using fseek function.
ftell() – It tells the byte location of current position in file pointer.
rewind() – It moves the control to beginning of a file.
What is the purpose of the keyword void in a function declaration ?year 2013 
we can say that the purpose of specifying the return type void is that it doesn’t return anything
can function return more than one value ?year 2013 Chapter 8
We cannot return multiple values from a C function. You can either

  1. Return a data structure with multiple values, like a struct or an array.
  2. Pass pointers to the function and modify the values of the pointers inside the function

What are the different ways of passing parameters to the functions? Which to use when?year 2013 Call by value − We send only values to the function as parameters. We choose this if we do not want the actual parameters to be modified with formal parameters but just used.

  • Call by reference − We send address of the actual parameters instead of values. We choose this if we do want the actual parameters to be modified with formal parameters.

Explain modular programming.  year 2010
Dividing the program in to sub programs (modules/function) to achieve the given task is modular approach. More generic functions definition gives the ability to re-use the functions, such as built-in library functions.
Does recurissve function helpto improve the performance of a program .Chapter 8
recursive programming gives the programmer a better way of organizing code in a way that is both maintainable and logically consistent.
 

Chapter 9

use of atoi function chapter .Year
converts a string into an integer numerical representation.
can function return a pointer ?  Year
C programming allows to return an array from a function. Similarly, C also allows to return a pointer from a function.
What is a NULL pointer? What 9 year 2010
A pointer pointing to nothing is called Null Pointer. Eg: char *p=NULL;
 Define double pointer .Chapter 9 year 2010
A pointer which points to another pointer is known as double pointer.
What do you meant by format string? Chapter 9 year 2010
“format string” that allows you to specify lots of information about how a program is formatted.integer /charcter
 What is a pointer on pointer? Chapter 9 year 2010
It’s a pointer variable which can hold the address of another pointer variable.
Eg: int x = 5, *p=&x, **q=&p;
Therefore ‘x’ can be accessed by **q. 
strcat function(String Concatenation)year 2012
the strcat function appends a copy of the string pointed to by s2 to the end of the string pointed to by s1.
Distinguish between malloc() & calloc() memory function. year 2015 
Both allocates memory from heap area/dynamic memory. By default calloc fills the allocated memory with 0’s.
when you pass an array name as an argumet to a function what is actually being passed?year 2013
In passing an array as a parameter to a function it is passed as a reference parameter. What is actually passed is the address of its first element.
What is double pointer? 2017
Ans:We already know that a pointer holds the address of another variable of same type.
When a pointer holds the address of another pointer then such type of pointer is known as pointer-to-pointer or double pointer. The double pointer is declared as:
int  x = 10;
int  *p1 = &x;
int  **p 2= &p1;
What is unary operator? 2018
ANS: The unary operator are symbol, which is used to increase or decrease the value.
Define pointer variable? 2018
ANS: A pointer is a variable that points at, or refers to, another variable ( of same time).Pointer variable does this by holding memory address of another variable.
Write the syntax of gets( ) function?2018
ANS: The syntax of gets () function is: gets(arr);
What are the different meaning of symbol ‘*’ in c language? 2018
ANS: The meanings of ‘*’ symbol is multiplication and typical declaration of pointer.
What does free () function do? 2018
ANS: The free function is used to de-allocate the previously allocated memory by the functions malloc( ), calloc ( ) , etc , and return it to heap so that is can be used for other purposes.

Chapter 10

Difference between Arrays and Structures in C.year 2012 
An array is a collection of related data elements of same type. Structure can have elements of different  types.An array is a derived data type. A structure is a programmer-defined data type. Array allocates static memory.  Structures allocate dynamic
1What is the use of union in C? Chapter 10 year 2012
A union is a special data type available in C that allows to store different data types in the same memory location. You can define a union with many members, but only one member can contain a value at any given time.
How union saves memory?2018
ANS: When a variable of data type union is declared, compiler allocates sufficient memory to hold the largest member in the union.
Define structure.  2017
Ans:structure is a user defined data type available in C that allows to combine data items of different kinds.To define a structure, you must use the struct statement.The struct statement defines a new data type, with more than one member.The format of the struct statement is as follows −
struct structure_name
{
member definition;
member definition;

member definition;
};
 Why, or when, do you need to dynamically allocate memory in C? chapter 10

  • You cannot determine the maximum amount of memory to use at compile time;
  • You want to allocate a very large object;
  • You want to build data structures (containers) without a fixed upper size;

 

Chapter 11

Can we pass argument in macro definition chapter 11
Function-like macros can take arguments, just like true functions.
 How does a constant defined by const differ from the constant defined by preprocessor statement #define?year 2013
A #define is used as immediate constant or as a macro. Where as the constant is a variable whose value can not change. #define can not define externally, and there is no way to use it for availability to the linker. Where as the constant can be global
unable to open stdio.h. what is reason behind it .year 2013 
This error indicates, compiler can’t find or open the file ‘STDIO.H’

Chapter 12

 What is the difference between write and append mode of fopen?year 2013
Write mode either overwrites the existing file or creates a new one if the file is non-existent. Append mode opens the file and sets the file pointer/cursor to the end of the file so that any write operation may start from the very end of that file.
How file is opened in c language? 2018
ANS: The file opening done in c language for either writing data to the file or to read data from already existing file.
Use of fseek function with its complete argument list. Chapter 12 
The fseek() function is used to set the file position indicator for the stream to a new position. This function accepts three arguments.
What is the difference between fprintf, sprintf and printf? Chapter 12

  • fprintf writes output to a file handle (FILE *).
  • sprintf writes output to a buffer that you allocate(char*).
  • printf writes output to the standard output stream(stdout).

 






Pre-Board solution 2018

Asian

 
1.What are the advantages of using library functions? Asian 2018

  • Functionalities, once developed and compiled, is used multiple times in several programs.
  • No need to copy source code all over the place.
  • No need to recompile the same code over and over.
  • Functionalities can be distributed to other programmers without releasing source.

2.Differentiate between the declaration and the definition of variables.Asian 2018 chapter 2
Declaration of variable serves two purpose: It associates a type and an identifer (or name) with the varibale.
Definition of varibale means asking compiler to allocate memory to variable or define storage for the variable.
3.What is an argument? Differentiate between formal arguments and actual arguments?Asian 2018 chapter 8
An argument is a medium or agent which is used to transmit data from one function to another function. Using an argument, we can supply the data from calling function to the called function.
The argumens available in the function definition are called formal aarguments (i.e. parameers). They are preceded by data types. The arguments used in function call statement are known as actual arguments.
4.What is the ouput of the following program?Asian 2018
#include<stdio.h>
main()
{
int a=5;
printf(“%d %d %d”, a, ++a, ++a, a++);
}
 
Output: 888
 5.How array is passed in a function? Explain with an example.Asian 2018 chapter 7
We can pass entire array  to a function. An array name can be named as an argument for the prototype declaration and in function header. When we call the function no need to subscript or square brackets. When we pass array that pass as a call reference because the array name is address for that array. When an array is passed, its base address is passed as call-by-value. The array elements themselves are not copied.
Example:

#include <stdio.h>
void display(int age)
{
printf("%d", age);
}
int main()
{
int ageArray[] = { 2, 3, 4 };
display(ageArray[2]); //Passing array element ageArray[2] only.
return 0;
}

 
 6.Differentiate between text mode and binary mode.Asian 2018

Text Mode Binary Mode
Number are stored as string of character. Number are stored on a same way as they are stored in computer main memory.
Occupies larger space if file in text mode. Occupies lesser space than in text mode.

 
7.Differentiate among automatic, static and external storage class.Asian 2018
Automatic Storage Class:

  • It is stored in the CPU registers.
  • Its scope is limited to the block where it is defined.
  • The initial value that it contains(if not already assigned) is any garbage value.
  • It remains active till the control is in the block where the variable is declared.
  • The keyword used to define a variable with the register storage class is ‘register’.

External Storage Class:

  • It is stored in the memory.
  • Its scope is global.
  • The initial value that it contains(if not already assigned) is zero.
  • It remains active till the end of the program. Not accessible to other files.
  • The keyword used to define a variable with the external storage class is ‘extern’.
  • When declared outside all the functions in the program its declaration does need to be preceded by the keyword.

Static Storage Class:

  • It is stored in the memory.
  • Its scope is limited to the block where it is defined.
  • The initial value that it contains(if not already assigned) is zero.
  • It remains active between different function calls.
  • The keyword used to define a variable with the static storage class is ‘static’.

8.Define and uses of pointer.Asian 2018 Chapter 8
A pointer is a varibale teat contains an address which is a location of another variable in memory.

  • To create dynamic data structures.
  • To pass and handle variable parameters passed to functions.
  • To access information stored in arrays.
  • Help to avoid compiler confusion for same variable name.
  • Handling array values is easy using pointer.

9.Explain nested macro substitution with an example.Asian 2018 Chapter 11
Macro substitution is a process where an identifier in a program is replaced by a predefined string composed of one or more tokens.We can use one macro inside the definition of another macro. Such macros are known as nested macros.
Examples of macro substitution.
#define                   CUBE(x)                           (SQU(x)*x)
10. Differentiate between linear link list and circular link list. Asian 2018
Circular Link List:
A linked list is called circular if it not NULL terminated and all nodes are connected in the form of a cycle. Below is an example of circular linked list.An empty linked list is considered as circular.
Linear Link List:
Linked List is a linear data structure. Unlike arrays, linked list elements are not stored at contiguous location; the elements are linked using pointers.
In simple words, a linked list consists of nodes where each node contains a data field and a reference(link) to the next node in the list.

NCC

1) What is the difference between the pre-increment operator and the post-increment operator? NCC2018
The pre- increment operator increases the operand’s value by 1 first, and then returns the modified value. On the other hand, post- increment operator stores a copy of the operand value in a temporary location and
then increase the operand value by 1.
2) What are the main difference between a string constant and a character constant?NCC 2018
A string constant is a series of characters enclosed by double quotes, while a character constant is a single character surrounded by single quotes.
3) Why do you normally need to add the break statement into the switch statement?NCC 2018
When one of the cases within the switch statement is selected, the program control will branch to the case and execute all the statements within the selected case and rest of the cases that follow it. Therefore, you might get
more result than you expected. To tell the computer to execute only the statements inside the selected case, you can put a break statement at the end of the case so that the program control flow will exit the switch construct
after the statements within the case executed.
4) What is the difference between while and do-while statement?NCC 2018
The main difference is that in the while statement, at first condition is checked and the body of loop is executed while in do- while statement at first body of loop is executed and then condition is checked.
5 ) Why do you need pointer arithmetic?NCC 2018
The beauty of using pointers is that you can move pointers around to get access to valid data that is saved in those memory locations referenced by pointer. To do so , you can perform the pointer arithmetic to add(or subtract)
an integer to (or from) a pointer.
6) What are the difference between a union and a structure?NCC 2018
Basically, the difference between a union and a structure is that the member in a union is overlaid and they share the same memory location, whereas the member in a structure has their own memory location. A union can be
referenced by using one of its member names.
7) What does it mean if the malloc() function returns a null pointer?NCC 2018
If malloc() function returns a null pointer, it means the function fails to allocate a block of memory whose size is specified by the argument passed to the function. Normally, the failure of the malloc() function is caused by
the fact that there is not enough memory to allocate. You should always check the value returned by the malloc() function to make sure that the function has been successful before you use the block of memory allocated
by the function.
8) When is static variable used?NCC 2018
Static variable is used when only one of the variables is required. So if you declare variable inside the method there is no use of such variable it’s become local to function only.
9) What is preprocessor in C?NCC 2018
Before a C program is compiled in compiler, source code is processed by a program called preprocessor.
10) What are the major difference between sequential file and random access file?NCC 2018
Sequential file is one that is typically written or read from start to finish whereas random access file is one that stores records, all of the same size, and can read or write single records in place, without affecting the rest of the file.

Nccs

  1. What is the advantage of array over individual variable?

Array is a collection of similar data elements with reference to one name.therefore we can store             data collectively as compared to individual variables.

  1. What is wrong with the statement? myName=”Robin”

char myname[ ]={“robin”} should be used.

  1. What are preprocessor directives?

Before a C program is compiled in a compiler, source code is processed by a program called preprocessor. This process is called preprocessing. Commands used in preprocessor are called preprocessor directives and they begin with “#” symbol.

  1. What does && operator do in a program code?

&& is a boolean operator, which means “and”. For it to become true, both of the statements must be true. If one of them is false, it becomes false.

  1. What is the use of ‘\0’ character?

It is used to show that the string is completed.it marks the end of the string. it is mainly used in string type.by default string contain ‘\0\ character means it show the end of the character in string. end of the array contain ”\0′ to stop the array memory allocation for string name.

  1. What are enumeration?

An enumeration is a complete, ordered listing of all the items in a collection. The term is commonly used in mathematics and computer science to refer to a listing of all of the elements of a set.

  1. Can a pointer access the array?

Yes pointer can access the array.

  1. What are header files and what are its use in C programming?

A header file is a file containing C declarations and macro definitions (see Macros) to be shared between several source files. You request the use of a header file in your program by including it, with the C preprocessing directive ‘ #include ‘. Header files serve two purposes.

  1. What are the differences between a structure and union?

In structure each member get separate space in memory.In union, the total memory space allocated is equal to the member with largest size. All other members share the same memory space.

  1. How using pointers in a programming give advantage?

Major advantages of pointers are: (i) It allows management of structures which are allocated memory dynamically. (ii) It allows passing of arrays and strings to functions more efficiently. (iii) It makes possible to pass address of structure instead of entire structure to the functions.

Group B

1.Programs to read 3*3 matrix and and sum of diagnol of matrix

#include<stdio.h>
main()
{
int i,j,a[3][3],l=0;
printf("enter the matrix:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
if(i==j)
{
l=l+a[i][j];
}
}
}
printf("the sum of diagnol of a matrix is %d",l);
}

 

  1. Program to printt armstrong number between 100 and 200.
#include <stdio.h>
int main()
{
int number, i, digit, sum;
for (i = 100; i <=200; i++)
{
number = i;
sum = 0;
while (number > 0)
{
digit = number % 10;
sum += digit * digit * digit;
number /= 10;
}
if (sum == i)
{
printf("%d\t", i);
}
}
return 0;
}

3.Function driven Program to see the eligibility of vote

#include<stdio.h>
void vote(int);
main()
{
int a;
printf("enter the age :");
scanf("%d",&a);
vote(a);
}
void vote(int a)
{
if(a>=18)
{
printf("voting is eligible");
}
else
{
printf("voting is not eligible");
}
}

4.Program to take 10 integers value and displying in reverse order

#include<stdio.h>
void vote(int);
main()
{
int a[10],b[10],i,j=9;
printf("enter the element in A :");
for(i=0;i<=9;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<=9;i++)
{
b[i]=a[j];
j--;
}
printf("displaying in reverse order:");
for(i=0;i<=9;i++)
{
printf("%d",b[i]);
}
}

5.sorting 10 number in descending order using pointer

#include<stdio.h>
#include<stdlib.h>
int main()
{
int a[10],*p;
int i,j,temp;
p=a;
printf("Enter %d Elements:");
for(i=0;i<10;i++)
{
scanf("%d",p+i);
}
for(i=0;i<10;i++)
{
for(j=i+1;j<10;j++)
{
if(*(p+i)<*(p+j))
{
temp=*(p+i);
*(p+i)=*(p+j);
*(p+j)=temp;
}
}
}
printf("After sorting it in descending order:");
for(i=0;i<10;i++)
{
printf("%d",*(p+i));
}
return 0;
}

6.Progrm to count number of ‘a’ in a string .

#include<stdio.h>
void vote(int);
main()
{
char a[10];
int i,l=0;
printf("enter the string:");
gets(a);
for(i=0;a[i]!='\0';i++)
{
if(a[i]=='a' || a[i]=='A')
{
l++;
}
}
printf("%d is the number of 'a' in a string",l);
}

 
7.Write a structure to post and salary of employee(more than 10) and store their information in file employee.dat

  • Function to print name of all employee having salary lesss than 20000
  • Function to add 1000 to all the employees having salary less than 10000and display their information
< #include<stdio.h>
void b();
void c();
struct employee
{
char name[20];
char post[20];
int salary;
}a[15];
main()
 {
FILE *fp;
fp=fopen("employee.txt","w");
int i=0;
while(i<15)
{
printf("enter the name , post , and salary:");
scanf("%s",a[i].name);
scanf("%s",a[i].post);
fflush(stdin);
scanf("%d",&a[i].salary);
i++;
fwrite(&a[i],sizeof(a[i]),1,fp);
}
b();
c();
fclose(fp);
}
void b()
{
FILE *fp;
fp=fopen("employee.txt","r");
int i=0;
while(i<15)
{
fread(&a[i],sizeof(a[i]),1,fp);
if(a[i].salary<20000)
{
printf("name:%s\n",a[i].name);
}
i++;
}
fclose(fp);
}
void c()
{
FILE *fp;
fp=fopen("employee.txt","a");
int i=0;
while(i<15)
{
fread(&a[i],sizeof(a[i]),1,fp);
if(a[i].salary<10000)
{
a[i].salary=a[i].salary+1000;
printf("name:%s\n",a[i].name);
printf("position:%s\n",a[i].post);
printf("salary:%d\n",a[i].salary);
}
i++;
}
fclose(fp);
}

 

Thames

1.Why is stdio.h file used in c-program?
stdio.h is the header file for standard input and output. we use stdio.h because it is a header file
which contains the function of input/output like scanf and printf. We can&#39;t input or output any variable
without using stdio.h.
2.what is preprocessor in c?
The c preprocessor is a macro processor that is used automatically by C compiler to transform your
program before actual compilation.
3.Define variable?
Variable is named location in a memory where a program can manipulate the data.
4.Why semicolon used in c-program.
In c program semicolon is used to separate multiple statements. It is also called terminators and are
required after every statement.
5.what is function.
A function is a group of statements that together perform a task. Function declaration tells the compiler
about a function’s name, return type, parameters etc.
6.what is use of \n in c program?
“\n” is used in c to give a vertical space.
7.what is pointer variable?
Pointer variable refers to a variable which stores the address of specific data location.
8.why do we use comments?
Comments are non executable statements. It is a programmer-readable explanation or annotation in source
code of a computer program.
10.why do we use puts() for string?
It is used to display a string on a standard output device. It automatically inserts a newline character at the
end of each string it display.