0% found this document useful (0 votes)
4 views20 pages

Unit - 4

This document covers various fundamental concepts of C programming, including array initialization, string handling, structures vs unions, storage classes, and pointers. It provides examples and explanations of one-dimensional and two-dimensional arrays, as well as the use of pointers in functions. Additionally, it discusses flowcharts and their symbols for representing data flow and processes.

Uploaded by

shubhverma941
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views20 pages

Unit - 4

This document covers various fundamental concepts of C programming, including array initialization, string handling, structures vs unions, storage classes, and pointers. It provides examples and explanations of one-dimensional and two-dimensional arrays, as well as the use of pointers in functions. Additionally, it discusses flowcharts and their symbols for representing data flow and processes.

Uploaded by

shubhverma941
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

1

C Programming
Unit-4

Short Question:

Q:1 How one dimensional Array is initialised?

Ans: Array: An array is a collection of same data types elements.

Way to initialize an array:


We have just declared the array with 5 elements stored in it and we can also
initialize the array during declaration like this:

int arr[5] = {1, 2, 3, 4 ,5};

---------------------------------------------------------------------------------
-----------

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.

String Declaration and Initialisation:

Method 1:
char address[]={'T', 'E', 'X', 'A', 'S', '\0'};

Method 2: The above string can also be defined as –


char address[]="TEXAS";
2

In the above declaration NULL character (\0) will automatically be inserted at the
end of the string.

Example of String:

Read & Write Strings in C using gets() and puts() functions:

#include <stdio.h>
#include <string.h>
int main()
{
char nickname[20]; /* String Declaration*/

puts("Enter your Nick name:");


gets(nickname);

puts(nickname);
return 0;
}

Output:

Enter your Nick Name: Sumit


Sumit

---------------------------------------------------------------------------------
---------

Q:3 What is the difference between Structure and Union.

Ans: Structure:
3

Structure is a group of variables of different data types represented by a single


name. All the data elements of the structure are stored in different memory
location. So, all the data members can be accessed at the same time. Structure is
declared by using the keyword struct.

Union: It is a group of variables of different data types represented by a single


name. But all the data elements of the Union are stored in the same memory
location. So, all the data members cannot be accessed at the same time. Only one
member can be accessed at a time. Union is declared by using the keyword Union.

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;

Printf(“Enter the data elements”);


Scanf(“%s %d %d”, &s.name, &s.roll_no, &s.age);

Printf(“Student name is: %s, s.name);

Printf(“Student roll_no is: %d, s.roll_no);

Printf(“Student age is: %d, s.age);

getch()
}
4

Output:

Enter the data element: Amit 1 30


Student Name is: Amit
Student roll_no is: 1
Student Age is: 20

---------------------------------------------------------------------------------
-----

Long Question

Q:1 What are the different storage classes used in C Programming ?

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.

A variable Storage class tells us:

 Where the variable would be stored


5

 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

There are 4 Storage classes in C:


 Automatic Storage class
 Register storage class
 Static Storage class
 External storage class

a) Automatic Storage Class:

The features of a variable defined to have an automatic storage class are as


under :

Storage : Memory

Default initial value : A garbage value

Scope : Local to the block in which the variable is defined

Life : Till the control remains within the block in which it is defined.

Example of Automatic Storage Class:

main()
{
auto int i, j;

Printf(“%d %d”, i,j);


}
6

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.

--------------------

b) Register Storage Class:

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.

The features of a variable defined to have a Register storage class are as


under:

Storage : CPU Register

Default initial value : A garbage value

Scope : Local to the block in which the variable is defined

Life : Till the control remains within the block in which it is defined.

Example of Register Storage Class:

main()
{
register int I;

For(i=1;i<=10;i++)
{
7

Printf(“%d”, i);
}

----------------

c) Static Storage Class:

The features of a variable defined to have a static storage class are as under :

Storage : Memory

Default initial value : Zero

Scope : Local to the block in which the variable is defined

Life : Value of the variable exist between different function calls

Example of Static Storage Class:

main()
{
increment();
increment();
increment();
}

increment()
{
Static int i=1;

Printf(“%d”, i);
i=i + 1;
}
8

Output:
1
2
3

-------------

d) External Storage Class:

The features of a variable defined to have an External storage class are as


under :

Storage : Memory

Default initial value : Zero

Scope : Global

Life : As long as the Program execution does not come to an end

Example of External Storage Class:

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

--------------------------------------------------------------------------------
----------

Q:2 What do you mean by an Array? Explain with example.

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

Need of Array in C Programming


Consider a scenario where you need to find out the average of 100 integer numbers
entered by user. In C, you have two ways to do this:
1) Define 100 variables with int data type and then perform 100 scanf() operations
to store the entered values in the variables and then at last calculate the average of
them.
2) Second way is to have a single integer array to store all the values, loop the array
to store all the entered values in an array and later calculate the average.

Obviously the second solution is better, it is convenient to store same data types in
one single variable and later access them using array

How to declare Array in C:

int num[35]; /* An integer array of 35 elements */

char ch[10]; /* An array of characters for 10 elements */

Similarly an array can be of any data type such as double, float, short etc.

Types of Array:
 One Dimensional Array
 Two Dimensional Array

Example of One Dimensional Array In C programming to find out the


average of 4 integers:

#include <stdio.h>
int main()
{
int x, avg;
int sum =0;
11

int num[4]; /* Array- declaration */

for (x=0; x<4;x++)


{
printf("Enter number”);
scanf("%d", & num[x]);
}

for (x=0; x<4;x++)


{
sum = sum+num[x];
}

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

Lets discuss the important parts of the above program:

Input data into the array:

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]);
}

Reading out data from an array:

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]);
}

Two dimensional (2D) arrays in C programming with example:

An array of arrays is known as 2D array. The two dimensional (2D) array in C


programming is also known as matrix. A matrix can be represented as a table of
rows and columns.

Example of Two dimensional(2D) Array:

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 array[2][3]; /* 2D array declaration*/

int i, j;

for(i=0; i<2; i++)


13

{
for(j=0;j<2;j++)
{
printf("Enter value for i and j);
scanf("%d", & array[i][j]);
}
}

//Displaying array elements

printf("Two Dimensional array elements:\n");


for(i=0; i<2; i++)
{
Printf(“\n”);

for(j=0;j<2;j++)
{
printf("%d ", array[i][j]);

}
}
return 0;
}

Output:

Enter value for array[0][0]:1


Enter value for array [0][1]:2
Enter value for array [1][0]:3
Enter value for array [1][1]:4

Two Dimensional array elements:


12
34

---------------------------------------------------------------------------------
---------
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, an integer variable holds an integer value, however an integer


pointer holds the address of a integer 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.

How to declare a pointer?

int *p1 /*Pointer to an integer variable*/


double *p2 /*Pointer to a variable of data type double*/
char *p3 /*Pointer to a character variable*/
float *p4 /*pointer to a float variable*/
15

By using * operator we can access the value of a variable through a pointer.

Example of Pointer demonstrating the use of & and *

#include <stdio.h>
int main()
{

int *p;

int a = 10;

p= &var;

printf("Value of variable a is: %d", a);


printf("\nValue of variable a is: %d", *p);
printf("\nAddress of variable a is: %p", &a);
printf("\nAddress of variable a is: %p", p);

return 0;
}

Output:

Value of variable var is: 10


Value of variable var is: 10
Address of variable var is: 0x7fff
Address of variable var is: 0x7fff

Passing pointer to a function:


16

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.

Example: Passing Pointer to a Function in C Programming

In this example, we are passing a pointer to a function. When we pass a pointer as


an argument instead of a variable then the address of the variable is passed instead
of the value. So any change made by the function using the pointer is permanently
made at the address of passed variable. This technique is known as call by
reference in C.

Example : Swapping two numbers using Pointers:

#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);

swap( &num1, &num2 ); // call by Reference

printf("After swapping: %d %d",num1,num2);

getch();
}

int swap( int *a, int *b )


{
int temp ;
temp = *a ;
*a = *b ;
*b = temp ;
}
17

Output:

Before swapping: 10, 20

After swapping: 20, 10


---------------------------------------------------------------------------------
---

Q:4 What is the use of Flowchart. What are the different symbols used in

Flowchart. Explain with example.

Ans: Flowchart: Flowchart is a technique which we use for graphical


representation of data in which we show the flow of data.
In flowchart, we use some symbols of different shapes for showing necessary
operations. Each symbol has some specific meaning and function in flowchart.

The Flowchart symbols along with their purpose are given below:

This symbol is used for showing Start or end.


18

This symbol is used for Input or Output data.

This symbol is used for processing data.

This symbol is used for Decision Logic.

This symbol is used for showing data flow.

Types of Flowchart:
 System Flowchart
 Modular Program Flowchart

a) System Flowchart: A system is a group of inter related components tied


together to a plan to achieve predefined goal. The elements and features of a system
are graphically shown and its structure and relationship are also represented by
Flowchart symbols.
19

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.


20

---------------------------------------------------------------------------------
-

You might also like