The words you are searching are inside this book. To get more targeted content, please make full-text search by clicking here.
Discover the best professional documents and content resources in AnyFlip Document Base.
Search
Published by prabhumandi123, 2023-03-07 08:32:52

C Notes

2. Marks Questions
1. Write any two characteristics of C

Ans: (i) C is as general purpose programming language.
(ii) It is a structured programming language.
2. Write any two applications of C

Ans: C is used to develop the system software . Some of the system and
application software are s follows;
Operating systems
Interpreters
Compilers
Assemblers
Editors
Loaders
Linkers
Application software:
Data Base system
Word processors
3. What are C tokens? List them.
Ans: The smallest and basic units of a C program are called C tokens.
Following are the six tokens in c
Keywords
Identifiers
Constants
Strings
Operators
Special symbols
4. If the computer is an - 8 bit computer, what is the the range of int ?
n-1
Ans: -2 n-1 to +2 -1
8-1
i.e -2 8-1 to +2 -1
-128 to +127
For 16 bit computer
-32768 to 32767
5. What is float?
Ans:It is keyword used to indicate a floating point numbers. It stores a
maximum of 6 digits after the decimal point.
6 What is double?
Ans: Double: This is a keyword used to indicate a double precision floating
point number. It is equivalent to float, but the number of significant
digits stored after the decimal point is double that of float.
Float usually stores a maximum of 6 digits after the decimal point. But
double stores 16 significant digits after the decimal point.







Dr. P. I. Mandi Basaveshwar Science College, Bagalkote 22-23


7. What do you mean by modulus operator?
Ans: The modulus operator(%) is added to the list in C. It is used for
finding the remainder after an integer division.
This operator cannot used with floating point numbers.
8. Write the syntax for input statement and output statement in C
Input statement:
scanf(“Control string”, address list);
Ex. scanf(“%d%f”, &a,&b);

Output statement:
printf(“ control string”,var list);
Ex.. printf(“%d”, number);
9. Write the following mathematical expression in C equivalent

Mathematical Expression C equivalent

((a+b)*(a+b))/((a-b)*(a-b)) or


Pow((a+b),2/pow((a-b),2)

log10(x/y+c)




exp(abs(a))+b


sqrt(a*a+b*b)



alpha=beta+gamma

Sinθ + cosθ Sin(theta)+cos(theta)


Torque=((2*x*y)/(x+y))*g

Torque=


x=exp(abs(y/sqrt(1.0+sin(theta)))))



10. What is ternary operator in C.
Ternary Operator: This operator is a conditional operator in C. There is
only one operator in C. It takes three operands.
Example: c=a<b?a:b
Here c will be assigned the value of a if a is less than b. Otherwise it will

be assigned the value of b.





Dr. P. I. Mandi Basaveshwar Science College, Bagalkote 22-23


11. What is increment operator?
This operator is used to increment the value of an interger quantity by
one.This is represented by ++ symbol. This symbol is placed before or after
the integer variable.
++int var indicates pre increment. The value will be incremented before it
is used
12. What is decrement operator?
Decrement operator:
This operator is used to reduce the value of an interger quantity by one.
This is represented by -- symbol. This symbol is placed before or after the
integer variable.
--int var indicates pre decrement. The value will be decremented before it is
used
(decrement and use)
Int var-- indicates post decrement. The value of int var is used first and
then it will be decremented ( use and decrement).
13. What is comma operator?
Ans: It is used to link two or morerelated expression together.
Ex. Sum=(a=12, b=22, a+b)
14.What is size operator?
Ans: The sizeof() operator returns the size of the operand. It is normally
used to determine the size of arrays ans structures.
Ex. x=sizeof(int);
15. What is an array?
Ans: It is an ordered list of homogeneous data elements.
All these elements are stored in RAM
16. Write the syntax for an array
Data type arayname[size];
17. What is data structure?
Ans: A programming language which stores and organizes a set of data
items is called Data structure.
18. What is function?
Ans: A function is a set of instructions to carryout a particular task.
19. What is function declaration?
Ans: Function declaration means specifying the function as a variable
depending on the return value
20. What is Call by value?
Ans: When the value of arguments are passed from the calling to a
called function, the values are copied into the called function. If any
changes are made to the values in the function, there no in the
original values within the calling function.









Dr. P. I. Mandi Basaveshwar Science College, Bagalkote 22-23


21. What is Call by reference?
Ans: In this method, actual values are not passed, instead their addresses
are passed. There is no copying of values since their memory locations
are referenced.

20 What is String?
Ans: A string is a one dimensional array of characters. This is stored in
RAM. A string end with a null character(\0)
20 What is the difference between an array and string?

Ans: write the def. of both
22. Which header file is used to use the string handling functions

Ans: header file <string.h> is used.
23. What is pointer?
Ans: Pointer is a variable that can hold the address of other variables,
structures and functions that are used in the program.
It contains only the memory location of the variable rather than its
content.
24. What is point dereferencing?
Ans: Point dereferencing
Process of referencing the contents of variables by pointers, indirectly.
25. What is Pointer Initialization?
Pointer Initialization:
As ordinary variable are initialized within the declaration part, pointer
variables can also be initialized by assigning the address of another
variable that is being used in the program.
Determination of hierarchy of operation and evaluation of
expression

Arithmatic expressions are evaluated from Left to right.
If the expression involves paranthesis, then the expression inside the

paranthesis must be evaluated first.
• * having highest priority than + and -.
• / is having highest priority than + and -, and appears after.
• Both + and – having same priority and + appears before –sign.
Ex.

X=2*((8/3)+4*(5-2))
X=2*(2*+4*(5-2))
= 2*(2+4*3)
=2*(2+12)

=2*14
=28





Dr. P. I. Mandi Basaveshwar Science College, Bagalkote 22-23


5 Marks:
1. Write the characteristics of C


Ans: Characteristics of C:
• C is as general purpose programming language.
• Is a structured programming language.

• Rich set of operators.
• Portability
• Very less number of reserve words.

• It allows manipulations of internal processor registers.
• Any number of statements can be written in a single line.
• Ability to extend itself by adding function to its library.

• Helps in development of system software.
• The modulus operator is added in the list of C arithmetic operators.

• It is used to find the remainder after an integer division.
2. Mention the Applications of C
Because of its portability and efficiency, C is used to develop the system
software . Some of the system and application software are s follows;
Operating systems
Interpreters
Compilers
Assemblers
Editors
Loaders
Linkers
Application software:
Data Base system
Word processors
CAD/CAM applications
Spread sheets
3. What is Increment operator?
This operator is used to increment the value of an interger quantity by
one.This is represented by ++ symbol. This symbol is placed before or
after the integer variable.
++int var indicates pre increment. The value will be incremented before
it is used(Increment and use)
Int var++ indicates post increment. The value of int var is used first and
then it will be incremented( use and increment).


4. What is Decrement operator?
This operator is used to reduce the value of an integer quantity by one.
This is represented by -- symbol. This symbol is placed before or after

Dr. P. I. Mandi Basaveshwar Science College, Bagalkote 22-23


the integer variable.


























































































Dr. P. I. Mandi Basaveshwar Science College, Bagalkote 22-23


--int var indicates pre decrement. The value will be decremented before
it is used(decrement and use)
Int var-- indicates post decrement. The value of int var is used first and
then it will be decremented ( use and decrement).
5. What is an array? Explain Multi-dimensional array
It is an ordered list of homogeneous data elements.
All these elements are stored in RAM
Multi-dimensional array
(two dimensional and three dimensional array)
If the number of subscript is more than one then such arrays are called
Multi-dimensional array.
Thus we can have, two dimensional and three dimensional array.
The dimensionality is understood from the number of pair of square
brackets placed after the array name.
Array[][] - two dimensional array
Array[][][] - three dimensional array
two dimensional array: It is a order table of homogeneous elements. It
is generally referred to as matrix, of some rows and columns.
It is also called as a two subscripted variable.
Syntax: Datatypes arrayname[row][coloumn];
Rows—number of elements to be processed under subscript1.
Coloumn--- number of elements to be processed under subscript2.
Example: 1) int marks[5][3];
2) float matrix[3][3];
6. What is modular programming? Mention the advantages.
Modularization: The process of splitting the lengthier and complex
programs into a number of smaller units is called Modularization.
Programming with such approach is called Modular Programming.
Advantages:
(i) Reusability:
It a particular set of instructions are to be accessed repeatedly from
several different places within the program, then we can make this
group of instructions as one module and call whenever necessary.
This ovaids rewriting of function on every access.
(ii) Debugging is easier:
Since each module is smaller and clearer, the user can easily locate the
errors and correct them.
(iii)Build Library: It allows the programmer to build the library of

the most commonly used subprogram.



7. What are pointers? Write the advantages
Pointer is a variable that can hold the address of other variables,
structures and functions that are used in the program.
It contains only the memory location of the variable rather than its
Dr. P. I. Mandi Basaveshwar Science College, Bagalkote 22-23


content.


























































































Dr. P. I. Mandi Basaveshwar Science College, Bagalkote 22-23


Advantages:
• To point to different data structures
• Manipulation of data at different memory locations is easier
• To achieve clarity and simplicity
• More compact and efficient coding
• Dynamic memory location
• To return multiple values via functions


8. What are Local and Global Variables?

Variable whose existence is known only in the main program or
function are called Local Variables.
Local variables are declared within the main program or a function.
Variables whose existence is known in the main as well as other
functions are called global variables.
Global variables are declared outside the main() and functions.

9. What do you mean by Arguments and parameters?
Arguments and Parameters:
These are the variables used in a program and a function.
Arguments:
Variables uses in the function reference are called arguments. These are
written within the parentheses followed by the name of the function. They
are also called actual parameters, they are accepted in the main program.
Parameters:
Variable used in the function definition are called Parameters. They are
also referred as formal parameters, because they are the accepted values.
They receive value from the calling function. Parameters must be written
within the parentheses followed by the name of the function, in the function
definition.
































Dr. P. I. Mandi Basaveshwar Science College, Bagalkote 22-23


10 Marks Questions


1. Structure of C program


Preprocessor statements
Global declarations;
main()
{
declarations;
statements;
}
user defined functions

Preprocessor statements: These statements starts with # symbol, are also
called preprocessor directives. It includes header files and also symbolic
constants into C program.
Example : #include <stdio.h>
#include<math.h>
Global declarations: Variables or functions whose existence is known in the
main() function and other user defined functions, called global
variables(functions) and their declarations are called global declarations.
These declaration should be made before main().
main(): Execution of main program starts from main(). No C program should
be executed without main(). It should not be terminated by semicolon. There
must be only and one main() function in every C program.
Braces: Every C program uses a pair of curly braces{ }. The left braces
indicates the beginning of main()function. Right indicates the end of the
main() function. The braces are used to indicate the beginning and end of user
– defined functions and compound statements.
Declarations: It is a part of C program where all the variables, arrays,
functions etc., used I n the C program and are declared and may be intialised
with their data types.
Statements: These are instructions to the computer to perform specific
operations. They may be input – output statements, arithmetic statements,
control statements and other statements.
User Defined functions: These are subprograms. Generally subprogram is a
function. And they contain a set of statements to perform a particular task.
These are written by the user.















Dr. P. I. Mandi Basaveshwar Science College, Bagalkote 22-23


2. C operators:
C has rich set of operators, They may operate on a single operand or
two operands.











Unary Operators: An operator that acts upon only one operand is known as
Unary operator.
Binary Operators: These operators act upon two operands. The binary
operators further classified into four categories








Arithmaatic Operators: These are used to perform the basic arithmetic
operations such as addition, substractio, multiplication and division. C doesnot
provide exponential operator. This cn be performed by pow(), which is in the
library function.
The modulus operator is added in the list of C arithmetic operators.
It is used to find the remainder after an integer division.
Increment operator:
This operator is used to increment the value of an interger quantity by
one.This is represented by ++ symbol. This symbol is placed before or after the
integer variable.
++int var indicates pre increment. The value will be incremented before it is
used(Increment and use)
Int var++ indicates post increment. The value of int var is used first and then it
will be incremented( use and increment).



Decrement operator:
This operator is used to reduce the value of an interger quantity by one. This is
represented by -- symbol. This symbol is placed before or after the integer
variable.
--int var indicates pre decrement. The value will be decremented before it is
Dr. P. I. Mandi Basaveshwar Science College, Bagalkote 22-23


used(decrement and use)


























































































Dr. P. I. Mandi Basaveshwar Science College, Bagalkote 22-23


int var-- indicates post decrement. The value of int var is used first and then it
will be decremented ( use and decrement).

Relational operators:
These are used to compare two operands.They define the relationalship
existing between two constants or variables. They result in either a TRUE or
FALSE value.
Operator Meaning Precedence Associatively
< Less than 1 L to R
<= Less than or equal 1 L to R
> Greater than 1 L to R
>= Greater than or equal 1 L to R
== Equal to 2 L to R
!= Not equal 2 L to R

Logical Operator:
These operators are used to take decisions. There are three such operators.
AND, OR and NOT. The first two are binary operators and the last one is
unary operator. Result of these operators is either TRUE or FALSE. Logical
operators are used to connect one or more relational expression.

Operator Meaning Precedence Associatively
&& Logical AND 2 L to R
|| Logical OR 3 L to R
! Logical NOT 1 L to R


Logical AND is used to perform multiplication operation.
Logical OR is used to perform addition operation.
Logical OR is used to obtain the compliment of the operand.

Bitwise Operators:


All data items are stored in the computer memory as a sequence of bits(0’s
and 1’s). Manipulations of individual bits are carried out in machine
language or assembly language. These operators are work with int and char

type data. They cannot be used with floating point numbers.







Ternary Operator:

This operator is a conditional operator in C. There is only one operator in
C. It takes three operands.
Dr. P. I. Mandi Basaveshwar Science College, Bagalkote 22-23


Example: c=a<b?a:b
Here c will be assigned the value of a if a is less than b. Otherwise it will be
assigned the value of b.





3. Arrays:

It is defined as an ordered list of homogeneous data elements.These elements
may be of type int, float, char, or double.
All these elements are stored in consecutive memory locations(on RAM)
Array is referenced by a subscript(or index) enclosed in a pair of square
brackets. This subscript indicates the position of an individual data item in an
array.
For example, in an array names even, individual data items are shown
below
Even[0] Even[1] Even[2] Even[3] Even[4] Even[5]
Array must be declared before it appears in the program, using data types
and size of the array must be specified.
Classification of Array:
Generally, arrays are classified into
One dimensional array:
It is a linear list of fixed number of data items of the same data.
One dimensional array, it is similar to a row or coloumn matrix
All the data items are accessed using the same name using a single
subscript.
Syntax:
Data_type arrayname[size];
Array name ---name of an array
Size ------------ no.of elements of data type
Example:
int list[10];
char name[20];
float xyz[6];
double p[100];
Initialising One dimensional array:
Initialising means assigning some value to the variable that undergoes
processing.
1) int evennum[4]={2,4,6,8};
2) float abc[6]={1.21,3.45,5.6,7.32,2.22,6.73}

Multi-dimensional array(two dimensional and three dimensional array)
If the number of subscript is more than one then such arrays are called
Multi-dimensional array.
Thus we can have, two dimensional and three dimensional array

Dr. P. I. Mandi Basaveshwar Science College, Bagalkote 22-23


two dimensional array: It is a order table of homogeneous elements. It is


























































































Dr. P. I. Mandi Basaveshwar Science College, Bagalkote 22-23


generally referred to as matrix, of some rows and columns.
Syntax:
Datatypes arrayname[row][coloumn];
Example: 1) int marks[5][3];
2) float matrix[3][3];




4. String handling functions:
C supports a number of string handling functions. These functions are defined
in the header file <string.h>. Therefore whenever the string handling
functions are used in a program the preprocessor statement
#include<string.h> should be included in a program.
Some of the strings handling functions are
1. strcmp( )
2. strcpy( )
3. strcat( )
4. strlen( )
5. strncmp( )
6. strnncpy( )
7. strnncat( )
8. strnlwr( )
9. strupr()
10.strchr( )

1. strcmp( )-function
This function compares two strings character by character(ASCII
comparison)returns one of the three values {-1,0,1}
Syntax is:
Strcmp(string1,string2);
Example:p=strcmp(“ABC”,”abc”);
2. strcpy( )-function
The function is used to copy one string to other.
Its syntax:
Strcpy(string1,string2);
Example:strcpy(“lotus”,Jasmine”)

This copies the string jasmine to lotus. Here the content of lotus is lost.

3. strcat( )-function
This function is used to concatenate two strings. That is, it appends one
string at the end of the specified string.
Its syntax:
Strcat(string1,string2);
Here the string2 is appended to the end of string1.

Dr. P. I. Mandi Basaveshwar Science College, Bagalkote 22-23


Example: strcat(“program”,”ming”);


























































































Dr. P. I. Mandi Basaveshwar Science College, Bagalkote 22-23


The resultant string will be programming.
4. strlen( )-function
This returns the number of characters in the string (i.e string length).
Strlen(“programming”);
The length of programming is 11. Since there are 11 characters in it.


5. strncmp( )-function
This compares the first n characters of two input strings.
Its syntax is:
Strncmp(string1,srting2,n);
Where n is an integer.
Example: strncmp(“kudala”,”sangama”,4);
This compare the first 4 characters in a strings “kudala” and
“sangama”respectively.

6. strncpy( )-function
This copies first n characters of the second string to the first string.
Syntax:
Strncpy(string1,string2,n);
Example: strncpy(“sachin”.Tendulkar”,6);
The resultant string1 will be only Tendul.
7. strncat( )-function
This appends first n characters of the second string at the end of the
first string.
Syntax:
strncat( string1,string2,n);
Example: strncat(“system”,”software engg”,8);
Thus result would be systemsoftware
8. strlwr( )-function
This function is used to convert any uppercase letters in the string to
the lower case.
Syntax: strlwr(string);
Example: strlwr(“Bangalore”);
The resultant string is Bangalore





9. strupr( )-function
This function is used to convert any lowercase letters in the string to
the uppercase case.
Syntax: strupr(string);
Example: strupr(“bangalore”);
The resultant string is BANGALORE

Dr. P. I. Mandi Basaveshwar Science College, Bagalkote 22-23


10. strchr( )-function:


























































































Dr. P. I. Mandi Basaveshwar Science College, Bagalkote 22-23


This function searches for a specified character in the string. It
returns NULL if the desired character is not found in the string.
Its syntax is:
Strchr(string,desired char);
Example:strchr(“vajapeyi”,p);
Since p is present in vajapeyi. The search for the character p is
successful.


5. Functions:

Modularization: The process of splitting the lengthier and complex
programs into a number of smaller units is called Modularization.
Programming with such approach is called Modular Programming.
Advantages:
(i) Reusability: It a particular set of instructions are to be accessed
repeatedly from several different places within the program, then
we can make this group of instructions as one module and call
whenever necessary. This ovaids rewriting of function on every
access.
(ii) Debugging is easier: Since each module is smaller and clearer, the
user can easily locate the errors and correct them.
(iii) Build Library: It allows the programmer to build the library of the
most commonly used subprogram.
Arguments and Parameters:
These are the variables used in a program and a function.
Arguments:
Variables uses in the function reference are called arguments. These are
written within the parentheses followed by the name of the function. They are
also called actual parameters, they are accepted in the main program.
Parameters:
Variable used in the function definition are called Parmeters. They are also
referred as formal parameters, because they are the accepted values. They
receive value from the calling function. Parameters must be written within the
parentheses followed by the name of the function, in the function definition





Example: Write a program to accept two integers and compute their sum via
function addnum().
#include<stdio.h>
#include<conio.h>
main()
{
int n1, n2,result;

Dr. P. I. Mandi Basaveshwar Science College, Bagalkote 22-23


printf(Enter the two numbers”);


























































































Dr. P. I. Mandi Basaveshwar Science College, Bagalkote 22-23


scanf(“%d%d”,&n1,&n2);
result=addnums(n1,n2); /*Function reference*/
printf(“The sum of %d and %d =%d\n”, n1,n2,result);
getch();
}
int addnum(val1,val) /*Function to add two numbers*/
int val1,val2;
{
int sum;
sum=val1+val2; In this program n1 and n2 are arguments,
return(sum); and val1 and val2 are parameters
}

6. Local and Global Variables:

Variable whose existence is known only in the main program or function are
called Local Variables.
Local variables are declared within the main program or a function.
Variables whose existence is known in the main as well as other functions are
called global variables.
Global variables are declared outside the main() and functions.
Example:
#include<stdio.h>
int i=10; /* Global declaration*/
main()
{
int j;
printf(“i=%d\n”,i);
j=value(i); /*Function reference*/
printf(“j=%d\n”,j);
}
/* Function to compute value*/
int value(i)
int i;
{
int k;
k=i+10;
return(k);
}
The statement int i-10; that appears before the main(), is a global
declaration. The values of i is accessed by the main program a well as the
function value(). The variable k is a local variable in the function. It has no
existence in the main program.
The variable j is local main() but has no scope in the function value().
The output of the program is
i=10 j-20

Dr. P. I. Mandi Basaveshwar Science College, Bagalkote 22-23


7. Pointers
Pointer is a variable that can hold the address of other variables, structures
and functions that are used in the program.
It contains only the memory location of the variable rather than its content.
Advantages:
1. To point to different data structures
2. Manipulation of data at different memory locations is easier
3. To achieve clarity and simplicity
4. More compact and efficient coding
5. Dynamic memory location
6. To return multiple values via functions




Pointer Dereferencing:
Process of referencing the contents of variables by pointers, indirectly.

Pointer Initialization:
As ordinary variable are initialized within the declaration part, pointer
variables can also be initialized by assigning the address of another variable
that is being used in the program.

Program for pointer initialization
#include<stdio.h>
main()
{
int m=124,*pm;
pm=&m;
printf(“ The value of m=%d\n”, 8pm);
*pm=321;
printf(“The value of m after pointer intialistion=%d\n”,*pm);
}























Dr. P. I. Mandi Basaveshwar Science College, Bagalkote 22-23


Click to View FlipBook Version