Unit - 4
Unit - 4
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:
#include <stdio.h>
#include <string.h>
int main()
{
char nickname[20]; /* String Declaration*/
puts(nickname);
return 0;
}
Output:
---------------------------------------------------------------------------------
---------
Ans: Structure:
3
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()
}
4
Output:
---------------------------------------------------------------------------------
-----
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.
What will be the initial value of the variable, if the initial value is not
assigned
What is the scope of the variable
What is the life of the variable
Storage : Memory
Life : Till the control remains within the block in which it is defined.
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.
Life : Till the control remains within the block in which it is defined.
main()
{
register int I;
For(i=1;i<=10;i++)
{
7
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;
}
8
Output:
1
2
3
-------------
Storage : Memory
Scope : Global
extern int i;
main()
{
Printf(“%d”, i);
increment();
increment();
decrement();
decrement();
}
9
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
--------------------------------------------------------------------------------
----------
Ans:
Arrays: An array is a collection of same data types elements. For example an int
array holds the elements of int types while a float array holds the elements of float
types.
10
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
#include <stdio.h>
int main()
{
int x, avg;
int sum =0;
11
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
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++)
12
{
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]);
}
This program demonstrates how to store the elements entered by user in a 2d array
and how to display the elements of a two dimensional array.
#include<stdio.h>
int main()
{
int i, j;
{
for(j=0;j<2;j++)
{
printf("Enter value for i and j);
scanf("%d", & array[i][j]);
}
}
for(j=0;j<2;j++)
{
printf("%d ", array[i][j]);
}
}
return 0;
}
Output:
---------------------------------------------------------------------------------
---------
14
Q:3 What do you mean by Pointer. How you can pass pointer to a function.
Explain with example?
Ans:
Pointers in C Programming:
A pointer is a variable that stores the address of another variable. A variables hold
values of a certain type, but pointer holds the address of a variable.
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();
}
Output:
Q:4 What is the use of Flowchart. What are the different symbols used in
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:
---------------------------------------------------------------------------------
-