Important Questin N CCC
Important Questin N CCC
Important Questin N CCC
Ans. Array:
<pre>
The 2-Dimensional Array has used a matrix from (row and Coolum).
<DataTypes>ArrayName[Row_size][Coloum_size];
Declare the 2D array by specifying the data type, array name, number of rows, and
number of columns. For example, to declare a 2D array of integers with 3 rows and 4
columns:
int myArray[3][4];
Initialize the elements of the array with desired values. You can either initialize the array
during declaration or assign values to individual elements later.
int myArray[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
};
myArray[0][0] = 1;
myArray[0][1] = 2;
myArray[0][2] = 3;
myArray[0][3] = 4;
myArray[1][0] = 5;
myArray[1][1] = 6;
myArray[1][2] = 7;
myArray[1][3] = 8;
myArray[2][0] = 9;
myArray[2][1] = 10;
myArray[2][2] = 11;
myArray[2][3] = 12;
Ex –
Ans. Pointer: –
Define Pointer?
The Declare of the pointer with using Asteric (*) Sign. This asteric sign also known as
Indirection Operator.
Syntax
int *ptr;
Initialization of Pointer
Dynamic Memory Allocation: – DMA stands for Dynamic Memory Allocation. it means
we can allocate the memory after the execution of the program. and the compiler
decided how much memory to allocate at the compiler time not run time.
There are ways in which we can allocate memories dynamically in a heap. We use these
four standard library functions often;
1. malloc()
2. calloc()
3. free()
4. realloc()
1. malloc():
malloc stands for memory allocation. This inbuilt function requests memory from the
heap and returns a pointer to the memory. The pointer is of the void type and we can
typecast it to any other data type of our choice.
All the values at the allocation time are initialized to garbage values. The function
expects the memory space along with the size we want in bytes at the time it is used.
Syntax:
Example:
int *ptr;
2. calloc():
calloc stands for contiguous memory allocation. Similar to malloc, this function also
requests memory from the heap and returns a pointer to the memory. Differences lie in
the way we have to call it.
First, we have to send as parameters the number of blocks needed along with their size
in bytes. Second, in calloc(), the values at the allocation time are initialized to 0 instead
of garbage value unlike what happens in malloc().
Syntax:
Example:
int *ptr;
3. realloc():
realloc stands for reallocation of memory. It is used in cases where the dynamic memory
allocated previously is insufficient and there is a need of increasing the already allocated
memory to store more data.
We also pass the previously declared memory address, and the new size of the memory
in bytes while calling the function.
Syntax:
Example:
ptr = (int *)realloc(ptr,10* sizeof(int));
4. free():
We just have to pass the pointer as a parameter inside the function and the address
being pointed gets freed.
Syntax:
free(ptr);
Ans. String: –
A string is an array of characters. Data of the same type are stored in an array, for
example, integers can be stored in an integer array, and similarly, a group of characters
can be stored in a character array. This character array is also known as a string. A string
is a one-dimensional array of characters that is terminated by a null (‘\0’).
We can use C’s string handling library functions to handle strings. The string.h library is
used to perform string operations. It provides several functions for manipulating strings.
1. strcat():
This function is used to concatenate the source string to the end of the target string.
This function expects two parameters, first, the base address of the source string and
then the base address of the target string. For example, “Hello” and “World” on
concatenation would result in a string “HelloWorld”.
#include <string.h>
int main()
strcat(s, t);
Output:
String = HelloWorld
2. strlen():
#include <stdio.h>
#include <string.h>
int main()
Output:
Length = 5
3. strcpy():
This function is used to copy the contents of one string into the other. This function
expects two parameters, first, the base address of the source string and then the base
address of the target string.
#include <stdio.h>
#include <string.h>
int main()
char t[50];
strcpy(t, s);
Output:
4. strcmp():
The strcmp() function is used to compare two strings to find out whether they are the
same or different. It takes two strings as two of its parameter. It will compare two
strings, character by character until there is a mismatch or the iterator reaches the end
of one of the strings.
If both of the strings are identical, strcmp( ) returns a value of zero. If they are not
identical, it will return a value less than zero, considering the ASCII value of the
mismatched character in the first string is less than the mismatched character in the
second string. Else, it will return a value greater than 0.
#include <stdio.h>
#include <string.h>
int main()
Output:
Comparison result = -1
5. strrev():
This function is used to return the reverse of the string.
#include <stdio.h>
#include <string.h>
int main()
Output:
4) What are structure and union? How to deferent unions by structure. What is the
difference between them?
Ans.
Structure: –
Structures are usually used when we wish to store data of different data types together.
For example, if we want to store information about a book, there could be a number of
parameters defining a book.
Books have a title, an author name, the number of pages, and a price. All of the book
attributes belong to different data types. The titles and author names must be strings,
but the prices and number of pages must be numerical.
One way to store the data is to construct individual arrays, and another method is to
use a structure variable. It is to keep in mind that structure elements are always stored in
contiguous memory locations.
Union: –
Just like Structures, the union is a user-defined data type. All the members in unions
share the same memory location. The union is a data type that allows different data
belonging to different data types to be stored in the same memory locations. One of the
advantages of using a union is that it provides an efficient way of reusing the memory
location, as only one of its members can be accessed at a time. A union is used in the
same way we declare and use a structure. The difference lies just in the way memory is
allocated to their members.
here are some differences between union and structure are given below:
Basic for
Structure Union
comparison
Space
Consume more space than union. Consume less space than structure.
Consuming
Accessing Individual member can be accessed Only one first member of a union can
members at a time. be initialized.
Several members of a structure can Only the first member of a union can
Initializations
initialize at once. be initialized.
union tag-name
struct tag-name
{
{
data-type member-1;
data-type member-1;
data-type member-2;
Syntax data-type member-2;
- - - - - - - - - - - -
- - - - - - - - - - - -
Data-type member-n;
Data-type member-n;
}
}
Ans. Maco: – In c, the macro is used to define any constant value or any variable with its
value in the entire program that will be replaced by the macro name. Where macro is
contains the set of code that will be called when the macro name used in the program.
Ex –
void main()
int C;
C = A*B;
printf(“C= %d”,C);
Output:
C= 45
Ans. File:- A file can be defined as a collection of related records that give a
complete set of information about a certain item or entity. A file can be stored
manually in a file cabinet or electronically in computer storage devices.
Modes of Files
We can use one of the following modes with the helpf of the fopen() function.
Mode Description
“r” Open for reading. If the file does not exist, fopen() return null.
“rb” Open for writing. If the file does not exist, fopen() return null.
“w” Open for writing. If the file exists, the content will be over write.
“wb” Open for writing in binary. If the file exists, the content will be over write.
“a” Open for appended. If the file does not exist, it will be return created.
(Short Questions)
Ans.
2). The String always ended with Null Character 2). The Character array ended with the last
‘\0’. variable of the array.
3). Slow access compare to a character array. 3). Fast access compare to string.
Ans. The masking bit field is the method of saving the waste memory. it saves memory
by using set the bit.
Ex –
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
struct {
uint32_t year:23;
uint32_t day:5;
uint32_t month:4;
} typedef Bitfield;
int main() {
return EXIT_SUCCESS;
Output
date: 13/12/2020
Null Pointer: – Null pointer by assigning the null value at the time of pointer
declaration. This method is useful when you do not assign any address to the pointer. A
null pointer always contains value 0.
Void Pointer: – The void pointer is a generic pointer that isn’t associated with any data
type. A void pointer can be type casted to any type, so it is instrumental in assigning the
different types of variables to the void pointer.
Wild Pointer: – If a pointer isn’t initialized to anything, it’s called a wild pointer.
Dereferencing a wild pointer has undefined behavior that may crash the program or
give a garbage value.
#include<stdio.h>
#include<conio.h>
void main()
char str1[10];
char str2[10];
char str[20];
int i,j,k,l;
scanf("%s", str1);
scanf("%s", str2);
for(i=0;i<10;i++){
if(str1[i]=='\0')
break;
for(j=0;j<10;j++){
if(str1[j]=='\0')
break;
int n = 0;
str[l] = str2[n++];
getch();
Output:
sdak
24
Value copied actual parameter to formal Have the both parameter in a same memory
parameter. address.
Change the formal parameter value the value Change the value the can reflect actual
cannot be reflect into actual parameter. parameter because both value having in a same
Because it copied value. memory address.
(Very Short)
Ans. Bitwise Operator: – bitwise operator are used for manipulating the data at bit
level, also called as (bit level programming) bitwise operator. Bit level programing
consist at 0 and 1.
Operator Description
| Bitwise OR