C-Programming:-
In this tutorial, you will learn:-
Top 10 Questions are:
Q1. What is an array? Explain it's types with suitable example.
Ans: Array is a collection of homogeneous data items that share a common name. Array contains a similar kind of value on the same name it allocates contiguous or sequential memory.
There are two types of an array are:-
*Single dimension array:-
Single dimension array also known as a 1D array that contains either row or column of a given size in memory.
Syntax:- datatype array name [size];
int arr [20] ;
For example:-
//write a program to accept value in an array and print.
#include<stdio.h>
int mian()
{
int a[5] i , j ;
printf("enter 5 values");
for(i=0 ; i<5 ; i++)
{
sacnf("%d",&a[i]);
}
printf('values are \ n");
for(j=0 ; j<5 ; j++)
{
printf('%d",a[j]);
}
return 0 ;
}
OUTPUT
enter 5 values 1
2
3
4
5
values are 1
2
3
4
5
* Multidimension array (2D array) :-
Multidension are like 2D array that contain double dimension having row and column.
Syntax for declaration of 2D array.
Data type array name [row][column]
for example :-
Write a program to printf this pattern using array:-
1 0 1
0 1 0
1 0 1
#include<stdio.h>
int main()
{
int a[3][3] , i , j ;
printf("enter the values ");
for (i=0 ; i<3 ; i++)
{
for( j=0 ; j<3 ; j++)
{
if(i==j)
{
printf("a[i][j]=1");
}
else
{
printf("a[i][j]=0");
}
}
}
printf("values are \n ");
for(i=0 ; i<3 ; i++)
for(j=0 ; j<3 ; j++)
{
printf("%d",a[i][j]);
}
printf("\n");
}
return 0 ;
}
OUTPUT
1 0 1
0 1 0
1 0 1
Q2. Define string with all it's library function.
Ans: A group of characters is known as a string. C doesn't support any string as a datatype. We can create a string using character array.
Syntax:- Datatype array name [size] ;
for e.g: char arr[] = "codeswithabhi" ;
There are some library function of string in C Programing are:-
1. strlen () : It is used to find the length of the string .
2. strrev () : It is used to reverse the given string.
3. strcpy () : It is use to copy one string to another string.
4. strcat () : It is used for concatenation(this function combines both strings in one string.)
5. strlwr() : This function is used to convert upper case string to lower case.
6. strcmp() : It is used for comparing whether both string are identical or not.
7. strupr() : This function is used to converts given string into upper case.
Q3. What is a pointer and explain it features with suitable example.
Ans: Pointer is a varible That stores the address of another variable. The pointer is represented by ( * ) sign.
declaration , initializing and accessing of pointer.
pointer declaration name a pointer variable and specifies the type of object to which the variable points.
Avariable declared as a pointer holds a memory address.
Syntax: Datatype * pointer name
#include<stdio.h>
int main()
{
int x=10 ;
int *p ;
p=&x ;
printf("value of p is =%d",*p);
return 0 ;
}
OUTPUT:
value of p is =10
Features of pointer:-
* Pointer saves memory space.
* Execution time with pointer is faster cause data is manipulated with the address is direct access to memory location.
* Pointer reduces length and complexity of program.
* Pointer is efficient in handling data & associated with an array.
* 2D, 3D, and many more dimensional array representation is easy in pointer.
Q4. define static memory allocation.
Static memory allocation:-
The static memory is allocated during compilation time, when a variable
is declared, based on their types, compiler allocate space
in memory, these variables can be indirectly accessed with the help of
pointer variable, This way assigning the pointer variable is called
static memory allocation.
let see one example to more clear this topic:-
// Write a program to find the sum of two numbers using pointer...
#include<stdio.h>
int main ( )
{
int a, b, c ;
int *a , * b ;
printf("enter value of a & b \n");
scanf("%d %d", &a , &b );
*a=&a;
*b=&b;
c = *a + *b ;
printf("\n sum of a and b is = %d", c);
return( 0 ) ;
}
OUTPUT
enter the value of a & b 10
20
sum of a and b is = 30
Q5 Explain structure with the help of one example.
Structure:-
It is a collection of different data type variable groups together under a single name.
Syntax :-
struct structure name
{
data type mem 1 ;
data type mem 2 ;
data type mem 3 ;
, , , , , , , , , , , , ,
, , , , , , , , , , , , ,
data type mem n;
Declaration of structure:-
struct book
{
int pages ;
char name [ 100 ] ;
float price ;
} b ;
#include<stdio.h>
struct book
{
int pages ;
char name [ 100 ] ;
float price ;
};
int main ( )
{
struct book b ;
printf("enter pages , book name , price of book");
scanf("%d%s%f ", &b.pages , b.name , &b.price);
printf("\n details of books are ");
printf(" pages of book are = %d", b.pages);
printf("writer name = %s", b.name);
printf(" price of book = %d",b.price);
return ( 0 );
}
OUTPUT
enter pages , writer name , price of books 1600
codeswithabhi
234.23
details of book are
pages of books = 1600
write name = codeswithabhi
price of books = 234.23
Q6. What is Nested structure in C programming?
Nested structure :-
A structure that contain another structure as a member of a variable is known as nested structure .
For example:-
#include<stdio.h>
int main ()
{
struct address
{
char city [30];
char colony [30];
int pin ;
};
struct employ
{
char name [30];
struct address a;
};
struct employ e = { "abhi" , "kanpur" , "abc" , "208011" } ;
printf("%s\n%s", e.a.colony , e.a.city);
printf("%d\n %s", e.a.pi , e.name );
return( 0 );
}
OUTPUT
abhi
kanpur
abc
208011
Q7. Difference between Arry and structure.
Difference between array and structure:-
Array |
Structure |
|
1 |
An array behave like a built in data type all we have to do this to declare an array variable and use it.
|
First we have to design and declare a data type structure before the variable are declare and use.
|
2 |
An array is the collection of similar data type.
|
A structure have a collection of different data type.
|
3 |
An array is a derived data type,.
|
A structure is a user defined data type.
|
Q8. Define Union with all types of declarations.
Union:-
* Union is user-defined data type just like structure.
* Each member of the structure is assigned its own unique storage area whereas union all the members share common storage area.
* All members share common areas so only one member can be active at a time.
* Union are used when all the members are not assigned a value at the same time.
syntax:-
Union tag name
{
datatype member 1;
datatype member 2 ;
};
There are 3 types of declaration of union in C are :-
1.
union sample
{
int a;
float b;
char c;
};
union sample s;
2.
union sample
[
int a ;
float b;
char c;
}s;
3.
union
{
int a ;
float b;
char c;
}s;
Initialization and accessing of union :-
Union sample
{
int a;
float b;
char c;
};
int main ( )
{
union sample s ={ 10 , 10.5 , 'a' };
union sample s ={ 10 , 10.5 , 'a' };
printf('%d \ n ", s.a);
printf('%f \ n", s.b);
printf('%c \ n ", s.c);
return( 0 );
}
OUTPUT
10
10.5
a
Q9.Difference between structure and union.
Ans:
Union | Structure | |
1. |
All members share the same storage area.
|
Each member is assigned its own unique storage area.
|
2. |
The maximum memory required by the member is allocated. |
The total memory required by all members is allocated.
|
3. |
Only one member is active at a time
|
All member is active at a time.
|
4. |
Only first member can be initialized
|
All members can be initialized.
|
5. |
Require less memory.
|
Require more memory.
|
Q10. Write a short note on the following are:-
a. Bit field:-
A bit field is a data structure that allows the programer to allocate memory to structure and union in bit in order to utilize computer memory in an efficient manner.
* It used to reduce memory consumption
* Easy to emplement
* Provide flexibility to the code.
For example:-
#include<stdio.h>
struct data
{
int d: 5;
int m : 4 ;
};
int main()
{
struct data d;
printf("size %d \ n ", size of (d));
return 0 ;
}
b. Macros:-
Macro area piece of code in a program which is given sam name
whenever this name is encountered by the compiler th replaces the name with an actual piece of code.
For example:-
#include<stdio.h>
#include<conio.h>
#define line 5
void main()
{
int i ;
for(i=1 ; i<line ; i++)
{
printf("%d",i);
}
getch();
}
OUTPUT:-
1
2
3
4
5
c. Compiler control
Compiler control directives are a type of directive which helps to compile a specific portion of the program or to skip compilation of some specific part of the program based on some condition.
Syntax:-
indef macro name
s1 ;
s2 ;
s3 ;
,,
,,
,,
sn ;
#endif
d. Typedef :
The typedef keyword allows the programmer to create new names for types such as
int or, more commonly in C++, templated types--it literally stands for "type
definition". Typedefs can be used both to provide more clarity to your code
and to make it easier to make changes to the underlying data types that you use.
syntax:-
1
| typedef unsigned int size_t ; |
From here on out, you would be able to use size_t instead of unsigned int.
Note that in C, typedefs can also be used to remove some of the burden
associated with declaring structs. In C, struct variables must be declared
by a combination of the keyword struct and the name of the struct:
1
| struct my_struct_type my_struct_variable; |
typedef
struct
my_struct_type my_short_type_t;
1
|
e. Bitwise operator :
In C programming the following 6 operator are bitwise operator are:-
1. &( Bitwise AND ):-
The &(bitwise AND ) in C takes two number as operands and does AND
on every bit of two number . The result of AND is 1 only of both bits
are 1 .
2. | ( Bitwise OR ) :-
The | (bitwise OR ) in C takes two numbers as operands and does OR every
bit of two numbers the result of OR is 1 if any of the two bits is 1 .
3. ^ ( Bitwise XOR ):-
The ^ ( Bitwise XOR ) in c takes two number as operands and does XOR on
every bit of two numbers . The result of XOR is 1 if the two bits are
different.
4. << ( Left shift ) :-
The << ( Left shift ) in C takes two numbers , left shifts the
bits of the first operands , the second operands decides the numbers of
places to shift.
5. >> ( Right shift ) :-
The >> ( Right shift ) in C takes two numbers , right shift the
bits of the first operands , the seconds operands decides the number of
place to shift .
6. ~ ( Bitwise NOT ):-
The ~ ( Bitwise NOT ) in C takes one number and inverts all bits of it.
0 comments:
Post a Comment
If you have any doubt, please let me know