C Language Notes PDF
C Language Notes PDF
C Programming Language
Unit-1
Short Question:
do if static while
2
Ans: In C language, identifiers are the names given to variables, constants, functions
and user-define data. These identifier are defined against a set of rules.
Here myvariable is the name or identifier for the variable which stores the value
"Studytonight" in it.
------------------------------------------------------------------------------------------
Ans: The alphabets, numbers and special symbols when properly combined form
constants and variables.
Constants: A constant is a quantity that does not change. This quantity can be stored
in the memory of the computer. A constant is always a fixed value, which we can not
change.
Variable: A variable is the name given to the location in memory where this constant
is stored. The content of the variable can change but variable in itself can not change.
3x + y = 20
Since 3 and 20can not change. So, they are called as constant. And the quantities x &
y can change. So, these are called as variable.
-------------------------------------------------------------------------------------------
The standard input-output header file, named stdio.h contains the definition of the
functions printf()and scanf(), which are used to display output on screen and to take
input from user respectively.
printf() function returns the number of characters printed by it. %d inside the printf()
is known as format string and this informs the compiler, what type of output to
expect.
scanf() returns the number of characters read by it. %d inside the scanf() is known
as format string and this informs the scanf() function, what type of input to expect
4
----------------------------------------------------------------------------------
For example:
---------------------------------------------------------------------------------------------
A symbolic constant is an "variable" whose value does not change during the entire
lifetime of the program.
------------------------------------------------------------------------------------------
5
For example: 100-2*30 would yield 40, because it is evaluated as 100 – (2*30) and
not (100-2)*30. The reason is that multiplication * has higher precedence than
subtraction(-).
Associativity in C :
Associativity is used when there are two or more operators of same precedence or
priority is present in an expression.
-----------------------------------------------------------------------------------------
Identifier Variable
An identifier can be a variable, but not All variable names are identifiers.
all indentifiers are variables.
------------------------------------------------------------------------------------
7
Long Question:
Q:1 Describe in detail how C language evolve and came into existence. What are
the various C standards ?
History of C language:
8
Cpl (Combined Programing Language): It was based on Algol 60. to which it made
many important additions applicable. CPL language was made for research in both
programming concepts & the design of compilers.
It has never been fully implemented because of its size & complexity.
C Language:It came after B language. So, the name was chosen as C Language. It
was developed by Dennis Ritchie at AT& T bell Lab in 1973.The idea behind creating
C language was to create an easy language which requires a simple compiler and
enables programmers to efficiently interact with the machine/system, just like
machine instructions.
The C programming language features were derived from an earlier language called
B , BCPL. BCPL and B are "type less" languages whereas C provides a variety of
data types. It uses many concepts from these languages while introduced many new
concepts such as data types, structure, pointer etc.
C11 standard adds new features to C programming language and library like
type generic macros, structures, multi-threading and bounds-checked functions.
---------------------------------------------------------------------------------------------
Features of C language:
C language is one of the powerful language. The various features of C language are as
follows:-
o Portable: C is portable language; this means that C programs written for one
computer system can be run on another system, with little or no modification.
10
o Robust: It is robust language whose rich setup of built in functions and operator
can be used to write any complex program.
o Ease of Modification:You can easily run a C program in any computer with little
change.
o The C compiler combines the capabilities of an assembly language with the feature
of high level language. Therefore it is well suited for writing both system software
and business package.
o C language is the most widely used language in operating systems and embedded
system development today.
Importance of C:
11
Operating system is one of the very important thing in any device and most
of the Operating systems developed by with the help of C language because
it produces code that runs nearly as fast as code written in any assembly
language.
Programs written in C language takes very less time to execute and almost
executes at the speed of assembly language instructions.
Initially C language was mainly used for writing system level programs, like
designing operating systems, but there are other applications as well which
can be very well designed and developed using C language, like Text
Editors, Compilers, Network Drivers etc.
------------------------------------------------------------------------------------------------------------------------
Ans: Structure of C:
Then we can use program statements and all C statement must end with a
semicolon.
For Example:
# (hash): It is used to point out lines in a program that are not program statements but
Pre-Processor Directives.
include: Itwill add additional code to your program that help you. include is a
preprocesser directive, as the word itself suggests. It is used to tell the compiler to
include all the functions, and stuff from a header file. Pre-Processor Directives are
executed before the actual compilation of code begins.
stdio.h: We use stdio.hso that compiler come to now that you are going to use
function such as ( printf , scanf ,....etc ) all of this functions are define in that library
as you define main in your program
13
console.h: This header declares several useful library functions for performing
"console input and output" from a program. Some of the most commonly used
functions of conio.h are clrscr, getch, getche
void main(): main() is the main function of c from where execution of c program
starts. & void main() means it does not have return type. That means it does not return
anything.
Printf() : Printf() function is used to to print formatted output onto the screen.
Use of & :If an input need an address to reach the location then we use ampersand.
The ampersand is a reference parameter. It passes a reference to the argument. Or its
is a address parameter.
Use of getch():It is used to hold the output screen until you hit a character . It is used
to catch a character from the keyboard. And that character would not be seen in the
output screen.
---------------------------------------------------------------------------------------------
Ans:
Derived data types are nothing but primary datatypes butlittle twisted or grouped
together like:
Array
Structure
Union
pointer.
a) Integer type: Integers are used to store whole numbers. Integers are whole
numbers that can have both positive and negative values but no decimal values.
Example: 0, -5, 10
For example:
int id;
b) Floating point type: Floating types are used to store real numbers. Floating type
variables can hold real numbers such as: 2.34, -9.38, 5.0 etc. You can declare a
floating point variable in C by using either float or double keyword.
For example:
float balance;
double bookPrice;
c) Character type: Character types are used to store characters value. Keyword char
is used for declaring character type variables.
For example:
char test = 'h';
16
d) void type: void type means no value. This is usually used to specify the type of
functions which returns nothing. We will get acquainted to this datatype as we start
learning more advanced topics in C language, like functions, pointers etc.
------------------------------------------------------------------------------------------------
Q:5 How can you declare and initialize a variable in C language? Also discuss
the
scope of a variable.
Declaration of variables must be done before they are used in the program.
Declaration does the following things:-
Defining a variable means the compiler has to now assign a memory space to the
variable because it will be used in the program.. You can directly define a variable
inside the main() function and use it.
To define a function we must provide the datatype and the variable name. We can
even define multiple variables of same datatype in a single line by using comma to
separate them.
Example:
int a;
Float b,c;
18
Int a = 10;
Scope of a variable means where the variable is available within the program.
Local
Global
Local Variable: Local variable are defined inside a function. And the scope of local
variable is available only to certain selected statements in the program. Its scope is
restricted to the function within which it is defined.
Global Variable: Global variable are declared outside all functions. The scope of
global variable is available to all the statements in the program. It can be used by all
the functions of the program.
----------------------------------------------------------------------------------------
Ans:
Operators in C Language:
C language supports a rich set of built-in operators. An operator is a symbol that tells
the compiler to perform a certain mathematical or logical manipulation. Operators are
used in programs to manipulate data and variables.
19
Arithmetic operators
Relational operators
Logical operators
Bitwise operators
Assignment operators
Conditional operators
Special operators
1) Arithmetic operators:
These are the operators used to perform arithmetic/mathematical operations on
operands.C supports all the basic arithmetic operators.
Operator Description
% remainder of division
C Programming has two very useful operators increment (++) and decrement (–).
Increment operators are used to increase the value of variable by one and decrement
operators are used to decrease the value of the variable by one in C Programs.
3) Unary Operator:
The operator which works on only one operand is known as unary operator.
4) Relational operators:
Relational operators are used for comparison of the values of two operands.
21
Operator Description
> Check if operand on the left is greater than operand on the right
5) Logical operators:
Logical Operators are used to combine two or more conditions/constraints or to
complement the evaluation of the original condition in consideration. The result of the
operation of a logical operator is a boolean value either true or false.
|| Logical OR (a || b) is true
6) Bitwise operators:
The Bitwise operators is used to perform bit-level operations on the operands. The
operators are first converted to bit-level and then calculation is performed on the
operands. The mathematical operations such as addition , subtraction , multiplication
etc. can be performed at bit-level for faster processing.
Bitwise operators perform manipulations of data at bit level. These operators also
perform shifting of bits from right to left. Bitwise operators are not applied
to float or double.
Operator Description
| Bitwise OR
^ Bitwise exclusive OR
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
23
1 1 1 1 0
The bitwise shift operator, shifts the bit value. The left operand specifies the value to
be shifted and the right operand specifies the number of positions that the bits in the
value have to be shifted. Both operands have the same precedence.
Example :
a = 0001000
b=2
a << b = 0100000
a >> b = 0000010
7) Assignment Operators:
Assignment operators are used to assign value to a variable. The left side operand of
the assignment operator is a variable and right side operand of the assignment
operator is a value. The value on the right side must be of the same data-type of
variable on the left side otherwise the compiler will raise an error.
+= adds right operand to the left operand and assign the a+=b is same as
result to left a=a+b
-= subtracts right operand from the left operand and a-=b is same as
24
*= mutiply left operand with the right operand and assign a*=b is same as
the result to left operand a=a*b
/= divides left operand with the right operand and assign a/=b is same as
the result to left operand a=a/b
%= a%=b is same as
calculate modulus using two operands and assign the a=a%b
8) Conditional operator:
Conditional operator is of the form Expression1 ?Expression2 : Expression3 .
Here, Expression1 is the condition to be evaluated. If the condition(Expression1)
is True then we will execute and return the result of Expression2 otherwise if the
condition(Expression1) is false then we will execute and return the result of
Expression3
? : Operator
Explanation:
The first expression (expression 1) generally returns either true or false, based on
which it is decided whether (expression 2) will be executed or (expression 3)
If (expression 1) returns true then the expression on the left side of " : " i.e
(expression 2) is executed.
If (expression 1) returns false then the expression on the right side of " : " i.e
(expression 3) is executed.
9) Ternary Operator:
Ternary operator are those operators which operates on 3 operands. So, conditional
operators are also called as Ternary operator.
sizeof Returns the size of an variable sizeof(x) return size of the variable x
& Returns the address of an variable &x ; return address of the variable x
--------------------------------------------------------------------------------------
Done by the compiler on its own, without any external trigger from the user.
Generally takes place when in an expression more than one data type is
present. In such condition type conversion (type promotion) takes place to
avoid lose of data.
All the data types of the variables are upgraded to the data type of the
variable with largest data type.
#include<stdio.h>
int main()
{
int x = 10;
float z = x + 1.0;
printf("z = %f",z);
return0;
}
Output:
z = 11.000000
27
It is user defined.
Here the user can type cast the result to make it of a particular data type.
The syntax in C:
(type) expression
Here, type indicated the data type to which the final result is converted.
For Example:
sum = (int)x + 1;
printf("sum = %d", sum);
return0;
}
Output:
28
sum = 2
--------------------------------------------------------------------------------------------
1
C Programming
Unit-2
Short Questions:
getch();
}
Output:
0, 0
0, 1
1, 0
1, 1
2
-------------------------------------------------------------------------------
Ans:
goto statement:
Goto is avoided because: The goto statement is rarely used because it makes
program confusing, less readable and complex. Also, when this is used, the
control of the program won’t be easy to trace, hence it makes testing and
debugging difficult.
goto label_name;
..
..
label_name: C-statements
----------------------------------------------------------------------------------
.In the while loop, first the condition is checked and then the statements in
while loop are executed whereas do while loop executes the statements
inside the body of do-while first and then check the condition.
3
So in while loop, if a condition is false at the first place then the while
loop would not run at all whereas the do while loop would run once.
--------------------------------------------------------------------------------------
Q:4 Name the Decision Control Structure and Loop Control Structure.
Ans: Decision control structure are:
IF
IF Else
Nested IF Else
Else if
Switch
---------------------------------------------------------------------------------------
4
Long Questions:
Ans:
1) IF Statement:
The statements inside the body of “if” only execute if the given condition returns
true. If the condition returns false then the statements inside “if” are skipped.
Syntax:
if (condition)
{
//Block of C statements here
Example of if statement
#include <stdio.h>
#include <conio.h>
Void main()
{
int x = 10;
int y = 20;
if (x<y)
{
printf("Variable x is less than y");
}
getch();
}
5
Output:
2) If else statement:
If condition returns true then the statements inside the body of “if” are executed
and the statements inside body of “else” are skipped.
If condition returns false then the statements inside the body of “if” are skipped
and the statements in “else” are executed.
if(condition) {
// Statements inside body of if
}
else {
//Statements inside body of else
}
#include <stdio.h>
#include <conio.h>
void main()
{
int x ;
int y ;
if (x<y)
6
{
printf(" x is less than y");
}
else
getch();
}
Output:
If user provide the value of x =10 and y=20, then output will be:
X is less than y
And suppose, if user provide the value of x=20 and y=10, then output will be:
X is greater than y
------------------------------------------------------------------------------------------------
Q:2 Explain Nested If Else statement and Else If statement with example.
Ans:
1) Nested If..else statement:
When an if else statement is present inside the body of another “if” or “else” then
this is called nested if else.
7
if(condition)
{
statement 1
if(condition2)
{
Statement 2
}
else
{
Statement 3
}
}
else
{
Statements 4
}
void main()
{
int x, y;
printf("Input the value of x and y");
scanf("%d %d",&x,&y);
if (x != y)
{
printf("x is not equal to y);
if (x > y)
{
printf("x is greater than y”);
}
else
8
{
printf("y is greater than x");
}
}
else
{
printf("x is equal to y");
}
getch();
}
Output:
x is not equal to y
y is greater than x
------------------------------------
2) Else If statement:
The else..if statement is useful when you need to check multiple conditions within
the program, nesting of if-else blocks can be avoided using else..if statement.
if (condition1)
{
Statement 1
}
else if(condition2)
{
Statement 2
}
9
else if (condition3)
{
Statement 3
}
.
.
else
{
//These statements would execute if all the conditions return
false.
}
Lets take the same example that we have seen above while discussing nested
if..else. We will rewrite the same program using else..if statements.
#include <stdio.h>
#include<conio.h>
void main()
{
int x, y;
printf("Input the value of x and y");
scanf("%d %d",&x,&y);
if (x !=y)
{
printf("x is not equal to y");
}
else if (x > y)
{
printf("x is greater than y");
}
else if (y > x)
{
printf("y is greater than x");
}
else
{
10
getch();
}
Output:
x is not equal to y
--------------------------------------------------------------------------------------
Q:3 Explain the importance of Switch case statement. In which situation use
a switch case statement is desirable. Also give its limitation.
Ans:
Switch Case Statement:
The switch case statement is used when we have multiple options and we need to
perform a different task for each option.
Syntax:
case constant:
C Statement 2;
11
default:
C Statement;
void main()
{
int i=2;
switch (i)
{
case 1:
printf("Case1 ");
case 2:
printf("Case2 ");
case 3:
printf("Case3 ");
case 4:
printf("Case4 ");
default:
printf("Default ");
}
getch();
}
Output:
Break statements are useful when you want your program-flow to come out of the
switch body. Whenever a break statement is encountered in the switch body, the
control comes out of the switch case statement.
12
I’m taking the same above code that we have seen above but this time we are
using break.
#include <stdio.h>
#include<conio.h>
void main()
{
int i=2;
switch (i)
{
case 1:
printf("Case1 ");
break;
case 2:
printf("Case2 ");
break;
case 3:
printf("Case3 ");
break;
case 4:
printf("Case4 ");
break;
default:
printf("Default ");
}
getch();
}
Output:
Case 2
-----------------------------------------------------------------------------------------
13
Ans:
For Loop:
Initialization: This part is performed only once at for loop start. We can
initialize a loop variable here.
Test condition: It is the most important part of the loop. Loop will
continue to run if this condition is valid (true). If the condition becomes
invalid (false) then the loop will terminate.
Output:
1 2 3
-------------------------------------------------------------------------------------
While loop is used for executing a block of statements repeatedly until a given
condition returns false. The while loop test the termination condition at the top.
In the while loop, first the condition is checked and then the statements in while
loop are executed else control comes out of the loop.
#include <stdio.h>
int main()
{
int a=1;
while (a <= 4)
{
printf("%d ", a);
a++;
}
return 0;
}
Output:
1234
------------------------------
Do While:
do-while loop, tests the condition at the bottom after making each pass through
the loop body. The body is always executed at least once.
A do while loop executes the statements inside the body of do-while before
checking the condition. So you can say that if a condition is false at the first place
then the do while would run once.
do
{
Statements
} while(condition test);
16
} while (j<=2);
return 0;
}
Output:
---------------------------------------------------------------------------------------
Q:6 a) Write a Program to find Factorial of a given number using for loop.
b) Write a program to print the fibonacci series.
#include<stdio.h>
#include<conio.h>
17
void main()
{
int n,i;
int f=1;
printf("Enter a Number");
scanf("%d",&n);
getch();
}
Output:
If the entered number is 3, then the output will be:
The factorial is 6
---------------------------------------------------------
#include <stdio.h>
#include<conio.h>
void main()
{
18
int n,i;
int final=0,temp=1,prev=0;
Printf(“enter the number for Fibonacci series”);
Scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
printf(“%d”,final);
prev=temp;
temp=final;
final=prev+temp;
}
getch();
}
Output:
if value of n=8, then output will be
0 1 1 2 3 5 8 13
------------------------------------------------------------------------------------------------
Q:7 With is the purpose of break and continue statement. With in which
control statement, both can be included? Explain with the help of example.
Ans:
Break and Continue Statement:
1) Break statement:
19
Break statement is used to come out of the loop instantly. When a break statement
is encountered inside a loop, the control directly comes out of loop and the loop
gets terminated. It is used with if statement, whenever used inside loop.
Break statement can also be used in switch case control structure. Whenever it is
encountered in switch-case block, the control comes out of the switch-case
The break statement can be used inside loop Control Structures like (For,
While and do while loop) and can also be used in Switch case Control structure.
Syntax of break:
Break;
Example of (break) :
#include <stdio.h>
#include<conio.h>
void main()
{
int i=2;
switch (i)
{
case 1:
printf("Case1 ");
break;
case 2:
printf("Case2 ");
break;
case 3:
printf("Case3 ");
break;
case 4:
printf("Case4 ");
break;
default:
20
printf("Default ");
}
getch();
}
Output:
Case 2
----------------------------
The continue statement can only be used inside loop Control Structures.
Syntax:
continue;
}
return 0;
}
Output:
12 45
--------------------------------------------------------------------------------------
Ans:
goto statement:
Label:
A label has the same form as a variable name, and is followed by a colon. It can
be attached to any statement in the same function as the goto. The scope of a label
is the entire function.
goto label_name;
..
..
label_name: C-statements
22
void main()
{
int sum=0;
if(i= =5)
{
goto addition;
}
}
addition:
printf("%d", sum);
getch();
}
Output: 15
1
C Programming
Unit-3
Short Questions:
--------------------------------------------------------------------------------------------
2
void main()
{
int c; // c is the local variables of main function
c=a+b;
Printf(“%d”, c);
getch();
}
Output: 30
-------------------------------------------------------------------------------------------
3
For functions the declaration needs to be done before the first call of
the function. Function declaration is also known as function prototype.By
default the return type of a function is integer (int) data type. You can also
specify any data type of function according to your need.
Function Definition:
A function definition specifies what a function does. A function
definition provides the actual body of the function. A function
definition specifies the name of the function, the types and number of
parameters it expects to receive, and its return type. A function definition also
includes a function body with the declarations of its local variables, and the
statements that determine what the function does.
Function Calling:
A function call is an expression that passes control and arguments (if any) to
a function. The point at which the function is being invoked or called is known
as the calling function.
The function which is calling that function is called as the calling function and
the function which is being executed due to the function call is known as the
called function.
-----------------------------------------------------------------------------------------
4
Long Question:
Ans:
(a) getch():
This function presents in conio.h header file . getch() reads a single byte
character from input. It is a way to get a user inputted character. It can be used
to hold program execution, which is to wait until the user enters a character.
During the program execution, a single character gets or read through
the getch(). The given value is not displayed on the screen and the compiler
does not wait for another character to be typed. And then, the given character is
printed through the printf function.
Syntax:
getch();
Example of getch():
#include<stdio.h>
#include<conio.h>
Void main()
{
printf(“Hello World”);
5
getch();
}
(b) getche():
getche() function is a function in C programming language which waits for
any character input from keyboard and it will also echo the input character on to
the output screen.
During the program execution, a single character gets or read through
the getche(). The given value is displayed on the screen and the compiler does
not wait for another character to be typed. Then, afterward, the character is
printed through the printf function.
Syntax:
getche();
Example of getche():
#include<stdio.h>
#include<conio.h>
void main()
{
printf(“Hello World”);
getche();
}
6
Run this program and press a character. Then again when we view the output
screen sing Turbo C compiler. We will find the character printed on the screen
because of getche() function.
---------------------------
(c) puts() :
Puts function is used to write a line to the output screen. It works like printf
function. But the difference is printf() function is used to print both strings
and variables to the screen while the puts() function only permits you to print a
string only to your screen.
Syntax:
Puts();
Example of puts() :
#include<stdio.h>
#include<conio.h>
void main()
{
getch();
}
---------------------
(d) gets() :
This function is declared in the header file stdio.h. It reads a line from stdin and
stores it into the location pointed by string. This function stops when either the
7
Syntax:
gets(argument list);
where argument list can contain one or more arguments And here argument are
the stored location of string.
Example of gets() :
#include<stdio.h>
#include<conio.h>
void main()
{
Char name[20];
getch();
}
Output:
Suppose if user has provided the input string as “Nitish” then through gets()
function nitish will be stored in the array name[] and when we use puts(name).
this means that it will print the string value which is stored in the array name[].
---------------------------------------------------------------------------------------------
8
Need of functions in C:
Syntax of a function:
return_type function_name (argument list)
{
Set of statements – Block of code
}
9
Where return type can be of any data type such as int, float etc. And
argument list contains variables names along with their data types.
Example:
Program of Addition of 2 numbers using Function:
#include<stdio.h>
#include<conio.h>
Void main()
{
int a, b, s;
printf(“enter the value of a and b”);
scanf(“%d %d”, &a,&b);
s = sum(a,b);
printf(“The sum of given numbers is %d”, s);
getch();
}
int c;
c=a+b;
return (c);
---------------------------------------------------------------------------------------------
10
When we pass the actual parameters while calling a function then this is known
as function call by value. In this case the values of actual parameters are copied
to the formal parameters. Thus operations performed on the formal parameters
don’t reflect in the actual parameters.
#include <stdio.h>
#include<conio.h>
int swap(int , int); //prototype
Void main( )
{
int num1 = 10, num2 = 20 ;
printf("Before swapping: %d, %d", num1, num2);
getch();
temp = a ;
a=b;
b = temp ;
}
12
Output:
When we call a function by passing the addresses of actual parameters then this
way of calling the function is known as call by reference. In call by reference,
the operation performed on formal parameters, affects the value of actual
parameters because all the operations performed on the value stored in the
address of actual parameters.
Here we are swapping the numbers using call by reference. As you can see the
values of the variables have been changed after calling the num() function
because the swap happened on the addresses of the variables num1 and num2.
#include<stdio.h>
#include<conio.h>
int swap(int , int); // prototype
void main( )
{
int num1 = 10, num2 = 20 ;
printf("Before swapping: %d %d",num1,num2);
getch();
}
13
Output:
------------------------------------------------------------------------------------
Use of Recursion:
Reduce unnecessary calling of function.
To reduce the code size
14
#include<stdio.h>
#include<conio.h>
Void main()
{
int n, f;
printf(“enter the number”);
scanf(“%d”, &n);
f = fact(n);
printf(“The factorial of given number is %d”, f);
getch();
}
int fact(int n)
{
if(n==0)
{
return (1);
}
else
return (n*fact(n-1));
15
Output: suppose if user has provided the input as 3. Then the output will be 6
because the factorial of 3 is 6.
---------------------------------------------------------------------------------------------
strlen ( argument );
The function takes a single argument which represents the array in which string
is stored.
where argument1 point to the object where string1 is stored and agriument2
points to the object where string 2 is stored.
and thois function is used to copy one string to another. It returns a pointer to
the destination.
--------------------------------------------------------------------------------------
Q: 6 a) How can you find the length of a string. Explain with example.
b) How can you compare the two string. Explain with example.
c) How can you copy a string. Explain with example.
Void main()
{
int l;
Char m[20];
Printf(“enter the string”);
gets(m);
l = strlen(m);
printf(“The length of the given string is %d”,l);
getch();
}
Output:
18
Suppose if user has given the string as “Jay” then it will print the output as 3
because there are 3 characters in the string. So, the length of the string is 3.
------------------
void main()
{
int c;
Char m[20], n[20];
Printf(“enter the string 1”);
gets(m);
c = strcmp(m,n);
printf(“The length of the given string is %d”,c);
getch();
}
Output:
It will provide the difference of the ascii values of the 2 strings.
--------------------------
void main()
{
19
strcpy(m,n);
printf(“The string is %s”,m);
getch();
}
----------------------------------------------------------------------------------
1
C Programming
Unit-4
Short Question:
--------------------------------------------------------------------------------------------
Q:2 What do you mean by a string. How can you initialise Array string.
Explain with example.
Ans: String: A string in C is an array of characters. '\0' represents the end of the
string. It is also referred as String terminator & Null Character.
Method 1:
char address[]={'T', 'E', 'X', 'A', 'S', '\0'};
In the above declaration NULL character (\0) will automatically be inserted at the
end of the string.
Example of String:
2
#include <stdio.h>
#include <string.h>
int main()
{
char nickname[20]; /* String Declaration*/
puts(nickname);
return 0;
}
Output:
Enter your Nick Name: Sumit
Sumit
------------------------------------------------------------------------------------------
Ans: Structure:
Example of Structure in C:
We can create a structure that has members for name, roll_no and age and then
we can create the variables of this structure for each student.
#include <stdio.h>
#include<conio.h>
void main()
{
struct Student
{
char name[10];
int roll_no;
int age;
}s;
getch()
}
Output:
--------------------------------------------------------------------------------------
4
Long Question
Ans: Storage Class: Storage class is the class according to which the storage
space of some variable is decided. There are basically two kinds of location in a
computer where such a value may be kept:
Memory
CPU Registers
It is the variable’s storage class which decides in which of these two locations the
value is stored.
To fully define a variable, one needs to mention not only its type but also its
storage class. If we don’t specify the storage class of a variable in its declaration,
then, C has got default storage classes.
Storage : Memory
main()
{
auto int i, j;
Output:
1211
221
Where 1211 and 221 are garbage values of I, j. when you run this program, you
may get different values, since garbage values could be any value.
--------------------
The values stored in a CPU Register can always be accessed faster than the one
stored in memory. Therefore, if a variable is used at many places in a program, it
is better to declare its storage class as register.
6
main()
{
register int I;
For(i=1;i<=10;i++)
{
Printf(“%d”, i);
}
----------------
The features of a variable defined to have a static storage class are as under
:
Storage : Memory
main()
{
increment();
increment();
increment();
}
increment()
{
Static int i=1;
Printf(“%d”, i);
i=i + 1;
}
Output:
1
2
3
-------------
Storage : Memory
Scope : Global
extern int i;
main()
{
Printf(“%d”, i);
increment();
increment();
decrement();
decrement();
}
increment()
{
i=i+1;
printf(“On incrementing %d”, i);
}
decrement()
{
i=i-1;
printf(“On decrementing %d”, i);
}
Output:
i=0
On incrementing 1
On incrementing 2
On Decrementing 3
On Decrementing 4
------------------------------------------------------------------------------------------
9
Ans:
Obviously the second solution is better, it is convenient to store same data types
in one single variable and later access them using array
Similarly an array can be of any data type such as double, float, short etc.
Types of Array:
One Dimensional Array
Two Dimensional Array
10
#include <stdio.h>
int main()
{
int x, avg;
int sum =0;
avg = sum/4;
printf("Average of entered number is: %d", avg);
return 0;
}
Output:
Enter number 1
10
Enter number 2
10
Enter number 3
20
Enter number 4
40
Average of entered number is: 20
11
Here we are using iteration for the array from 0 to 3 because the size of the
array is 4. Inside the loop we are displaying a message to the user to enter the
values. All the input values are stored in the corresponding array elements using
scanf function.
for (x=0; x<4;x++)
{
printf("Enter number %d \n", (x+1));
scanf("%d", &num[x]);
}
Suppose, if we want to display the elements of the array then we can use
the for loop in C like this.
for (x=0; x<4;x++)
{
printf("num[%d]\n", num[x]);
}
int main()
{
int i, j;
for(j=0;j<2;j++)
{
printf("%d ", array[i][j]);
}
}
return 0;
}
Output:
------------------------------------------------------------------------------------------
Q:3 What do you mean by Pointer. How you can pass pointer to a function.
Explain with example?
Ans:
Pointers in C Programming:
For example: we live in a house and our house has an address, which helps
other people to find our house. The same way the value of the variable is stored
in a memory address, with the help of pointer in the C program, we can find that
value when it is needed.
#include <stdio.h>
int main()
{
int *p;
int a = 10;
p= &var;
return 0;
}
Output:
Just like any other argument, pointers can also be passed to a function as an
argument. Lets take an example to understand how this is done.
#include<stdio.h>
#include<conio.h>
int swap(int , int); // prototype
void main( )
{
int num1 = 10, num2 = 20 ;
printf("Before swapping: %d %d",num1,num2);
getch();
}
Q:4 What is the use of Flowchart. What are the different symbols used in
Flowchart. Explain with example.
The Flowchart symbols along with their purpose are given below:
Types of Flowchart:
System Flowchart
Modular Program Flowchart
b) Modular Program Flowchart: This flowchart defines the logical steps for
the input, output and processing of information of a specific program. In this the
independent modules are written for different procedure. In this the relationship
and the order in which processes are to be performed are included.
Example of Flowchart:
We are showing flowchart to find the biggest of 3 numbers.
18
19
----------------------------------------------------------------------------------