0% found this document useful (0 votes)
20 views28 pages

Short Questions 2M

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

Short Questions 2M

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

Short Answer Questions-2M

UNIT - 1 :
1. Explain Characteristics of algorithm.
Ans.
Algorithm: it is step by step process of solving a problem.
Characteristics of algorithm:
Finiteness: algorithm must always terminate after finite number of steps.
Definiteness: each and every step should be clearly understood.
Effectiveness :each instruction should be performed in finite amount of time.
Input and Output :algorithm how zero or more input and one or more outputs.
Generality: same input to generate different outputs.

2. Explain compiler and compilation in C.


Ans.
Compiler: a compiler is a program that translate the source code written in high level
language into the machine code. so that the computer processor can execute directly.
Compilation: process of converting source code into machine code is called compilation.

3. Explain Linker and Loader in C.


Ans.
Linker: the machine code relating to the source code you have written is combined with
some other code to derive the complete program in an execution file. this is done by a
program called by linker
Loader: loader is a program that loads machine codes of program into the system memory.
in computing a loader is the part of an operating system that is responsible for loading
programs.

4. Explain sizeof operator with example.


Ans.
The size of the operator in programming returns the size of a variable , datetypes or
expression in the bytes
SYNTAX: sizeof(expression)
Eg:
sizeof int =4bytes
sizeof float=4bytes
sizeof char=1byte

5. Write a C program to convert float to int.


Ans.
//to convert float into int//
#include <stdio.h>
#include<conio.h>
int main()
{
float y;
int x;
clrscr();
printf("Enter a floating value: ");
scanf(" %f", &y);
x = y;
printf("the value of '%f' in integer is: %d\n", y, x);
getch();
return 0;
}

6. Explain about shift operators with example.


Ans.
There are two ship operators are there they are :
(i)bitwise right shift (ii)bitwise left shift
Bit wise right shift: bits are shifted towards right hand side.
Eg:a>>1 means one bit moved or shifted towards right hand side
Bit wise left shift:bits are shifted towards left hand side.
Eg:a<<1 means one bit moved or shifted towards left hand side.

7. Write a C program to convert char to int.


Ans.
//to convert char into int//
#include <stdio.h>
#include<conio.h>
int main()
{
char ch;
int x;
clrscr();
printf("Enter a character: ");
scanf(" %c", &ch);
x = ch;
printf("the value of '%c' in integer is: %d\n", ch, x);
getch();
return 0;
}

8. What is the difference between 3/5 and 3/5.0


Ans.
Here
'3/5' =0 (integer division)
'3/5.0'=0.6(floating -point division)
The difference:
-integer division ('/') cuts off decimals.
-floating-point division ('/' with decimals) keeps decimals.
Examples:
- 3 cookies shared amount 5 people.
- integer division : 0 cookies each.
- floating-point division 0.6 cookies each.
9. Evaluate A&B, A|B, A^B when A=8, B=4.
Ans.
Bit wise AND :If the both inputs are one result will be one .Remains zero.
A=8=0 1 0 0
B=4=0 0 1 0
-------------------
A&B=0 0 0 0 =0
Bit wise OR:If the both inputs are zero then the result will be zero. Remains one.
A=8=0 1 0 0
B=4=0 0 1 0
-------------------
A|B=1 1 0 0=12
Bit wise XOR : If the both the inputs are same then the result will be zero. Remains one.
A=8=0 1 0 0
B=4=0 0 1 0
-------------------
A^B=1 1 0 0=12

10. Explain identifier naming rules.


Ans.
Rules for meaning and identifier in C:
(i). An identifier name is any combination of 1 to 31 alphabet ,digital or underscores.
(ii). No blanks or special symbols other than underscore can be used in identifier name.
(iii). Keywords are not allowed to be used as identifiers.

11. Evaluate A<<2 and A>>2 where A is 3.


Ans.
Bit wise right shift: bits are shifted towards right and side.
Given A=3=0 0 1 1
Then A>>2=0 0 0 0=0
Bit wise left shift : bits are shifted towards left hand side.
Given A=3=0 0 1 1
Then A<<2=1 1 0 0=12

12. Explain any 5 assignment operators in detail with examples.


Ans.
Assignment operators in C are:
(i)Equal (=) operator:
SYNTAX: variable=value;
(ii).(+=) operator:
SYNTAX: variable + = value;
(iii).(- =) operator:
SYNTAX: variable - = value ;
(iv).(* = ) operator:
SYNTAX : variable * = value ;
(v).(/=) Operator:
SYNTAX : variable /=value ;
PROGRAM:
//To perform assignment operators//
#include<studio.h>
void main()
{
int a=5;
Printf("a=%d\t",a);
Printf("a=%d\t",a+=5);
Printf("a=%d\t",a-=5);
Printf("a=%d\t",a*=5);
Printf("a=%d\t",a/=5);
}
Out put:
a=5 a=10 a=0 a=25 a=1

UNIT - 2 :

1. Explain Control statements.


Ans.
Control statement: control statements enable us to specify the flow of program control. the
order in which the instructions in a program must be executed they make it possible to make
decision to perform task repeately or to jump from one section of code to construction of go
to another.
control statement or classified into two types. they are :
1.selection statements
2.iteration statements.

2. Explain break statement in C with example.


Ans.
Break :it is the jumping statement keyword it is is used to exit from the loop.
SYNTAX: break;
Eg :
//To print 1 to 5 number//
#include<studio.h>
int main()
{
for(i=0 ; i<=10 ; i++)
{
printf("%d\n" ,i)
if(i==5)
{
break;
}
}
return 0;
}
3. Explain continue statement in C with example.
Ans.
Continue : it is the jumping statement keyword .when these keyword is used then the
statement is skipped.
SYNTAX: continue ;
Eg :
//To print 1 to 5 number//
#include<studio.h>
int main()
{
for(i=0 ; i<=10 ; i++)
{
printf("%d\n" ,i)
if(i==5)
{
continue ;
}
}
return 0;
}

4. Write a C program to read a number to find the number is even or odd.


Ans.
// to know given number is even or odd //
#include<studio.h>
#include<conio.h>
int main( )
{
int n ;
clrscr( ) ;
printf("Enter the number:");
scanf("%d" , &n);
if( n%2==0)
{
printf("%d is a even number ",n);
}
else
{
printf("%d is a odd number " , n);
}
getch( ) ;
return 0 ;
}

5. Construct an infinite loop using while.


Ans.
//to print infinite natural numbers//
#include<stdio.h>
#include<conio.h>
int main()
{
int i=1;
clrscr();
while(i>0)
{
printf("%d\n",i);
i++;
}
getch();
return 0;
}

6. Draw do while loop execution flow chart.


Ans. Flow chart

7. Explain difference between if-else and switch statement.


Ans.
if-else :it is used to check the check the condition.If the condition is true then if block is
executed.or else is executed.
SYNTAX : if(condition)
{
//Code
}
else
{
//Code
}
Eg :
//to check eligibility for vote
#include <stdio.h>
int main()
{
int x;
printf("Enter the age of a person:");
scanf("%d",&x);
if(x>18)
{
printf ("Eligible for vote");
}
else
{
printf ("Not eligible for vote ");
}
return 0;
}
switch : it is a selection statements keyword.It is used to used to select one particular
statement from block of statements.
SYNTAX : switch(operation/choice)
{
case 1 :
//Statements
break ;
case n :
//Statements
break ;
default:
//Code
break;
}
Eg:
#include <stdio.h>
int main()
{
int x=18;
switch(x)
{
case 18 :
printf ("Eligible for the vote ");
break ;
default :
printf("Not eligible for vote ");
break;
}
}

8. Write a C program to find the largest of three numbers.


Ans.
//to find larger of three numbers//
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b,c,big;
printf("enter a value:\n");
scanf("%d",&a);
printf("enter b value:\n");
scanf("%d",&b);
printf("enter c value:\n");
scanf("%d",&c);
if(a>b&&a>c)
printf(“%d is big”,a);
else if(b>c)
printf(“%d is big “,b);
else
printf(“%d is big “,c);
getch();
return 0;
}

9. Write a C program to print Fibonacci series up to given number.


Ans.
//to print Fibonacci series upto the given number//
#include <stdio.h>
#include<conio.h>
int main()
{
int i, n, first= 0, second= 1;
int nextTerm = first+ second;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: %d, %d, ", first, second);
for (i = 3; i <= n; ++i)
{
printf("%d, ", nextTerm);
first= second;
second= nextTerm;
nextTerm = first+ second;
}
getch();
return 0;
}

10. What is the difference between while loop and do…while loop? .
Ans.
While loop :
• It is a entry control loop
• It is used to check the condition. if the condition is true then while block is executed
continuously until the condition is false
Syntax : while(condition)
{
//Code
}
Eg program :
//to print the hello world message two times /
#include<stdio.h>
int main()
{
int i=1;
while(i<=2)
{
printf("Hello World\n");
i++;
}
return 0;
}

Do-While loop :
•It is exit control loop
•First, do the statement at least one time afterwards and check the condition. If the condition
is true then the loop is executed repeatedly until the condition is false.
SYNTAX: do
{
// Code
}
while(condition);
Eg program:
//To print hello world message//
#include<stdio.h>
int main()
{
int i=1;
do
{
printf("Hello World\n");
i++;
}
while(i<=2);
return 0;
}

11. Write a C program to find sum of numbers up to the given number.


Ans.
//To find the sum of all numbers upto the given number//
#include<stdio.h>
#include<conio.h>
int main()
{
int i, n ,sum=0;
printf("Enter the number:");
scanf("%d",&n);
printf("sum of numbers upto the given number are :\n");
for(i=1 ; i<=n ; i++ )
{
sum=sum+i;
}
printf("%d" ,sum);
getch( );
return 0 ;
}

12. Write a C program to find all even numbers up to the given numbers.
Ans.
//To find the sum of all numbers upto the given number//
#include<stdio.h>
#include<conio.h>
int main()
{
int i, n ;
printf("Enter the number:");
scanf("%d",&n);
printf("Even numbers upto the given number are :\n");
for(i=1 ; i<=n ; i++ )
{
if(i%2==0)
{
printf("%d" ,i );
}
}
return 0 ;
}

UNIT - 3 :

1. List various String oriented I/O statements in C with an example each.


Ans.
Here are various string-oriented I/O statements in C, along with examples:

1. gets() - Read a line of input from the standard input.


Eg :
#include <stdio.h>
int main()
{
char str[50];
printf("Enter a line of text: ");
gets(str);
printf("You entered: %s\n", str);
return 0;
}
2. puts() - Write a line of output to the standard output
Eg :
#include <stdio.h>
int main()
{
char str[] = "Hello, World!";
puts(str);
return 0;
}

3. scanf() with %s format specifier - Read a string from the standard input.
Eg :
#include <stdio.h>
int main()
{
char str[50];
printf("Enter a string: ");
scanf("%s", str);
printf("You entered: %s\n", str);
return 0;
}

4. printf() with %s format specifier - Write a string to the standard output.


Eg :
#include <stdio.h>
int main()
{
char str[] = "Hello, World!";
printf("%s\n", str);
return 0;
}

5. fgets() - Read a line of input from a file.


Eg :
#include <stdio.h>
int main()
{
FILE *fp;
char str[50];
fp = fopen("example.txt", "r");
if(fp == NULL)
{
printf("Could not open file\n");
return 1;
}
fgets(str, sizeof(str), fp);
printf("Read from file: %s\n", str);
fclose(fp);
return 0;
}

6. fputs() - Write a line of output to a file.


Eg:
#include <stdio.h>
int main()
{
FILE *fp;
char str[] = "Hello, World!";
fp = fopen("example.txt", "w");
if (fp == NULL)
{
printf("Could not open file\n");
return 1;
}
fputs(str, fp);
fclose(fp);
return 0;
}

2. What is Array? Explain different types of arrays in c with an example each.


Ans.
An array is a collection of elements of the same data type stored in a contiguous memory
location.
Types of Arrays in C :
1. One-Dimensional Array (1D Array): A 1D array is a list of elements of the same data
type.
Eg :int scores[5] = {10, 20, 30, 40, 50};

2. Multi-Dimensional Array (2D Array, 3D Array, etc.): A 2D array is a table of elements,


where each row represents a 1D array.
Eg(2D Array):
int matrix[2][2] = {{1, 2}, {4,6}};

3. Explain extracting “world” from “hello world”?


Ans.
#include <stdio.h>
#include<string.h>
int main()
{
char str[] = "hello world";
int len = strlen(str);
char *ptr = str;
for (int i = 0; i < len; i++)
{
if (*ptr == 'w')
{
printf("Extracted substring: %s\n", ptr);
break;
}
ptr++;
}
return 0;
}
In this , we manually iterate through the string to find the substring "world".

4. Explain declaration of arrays with example?


Ans.
Declaration of 1D array :
SYNTAX : datatype arrayname[size] ;
Eg: int a[5]; //integers array //
float x[3] ; // float array //
Declaration of 2D array :
SYNTAX : datatype arrayname[size1][size2] ;
Here datatype , arrayname, size1, size2 are user defined .
Eg: int a[3][3] ; // integers array //

5. Explain accessing elements of array with example?


Ans.
Accessing elements into 1D array :
SYNTAX :
datatype arrayname([size] = {elements};
Eg: int a[3]={1,2,3}

Accessing elements into 2D array :


SYNTAX :
datatype arrayname[size1][size2] = { {row 1},{row2}, ………,{row n} };
Eg: int a[2][2] = { {1,2} , {3, 4} };

6. Explain storing values in arrays?


Ans.
Storing values into 1D array:
SYNTAX :
for( initialisation ; condition ; increament )
{
scanf( "format specifier ", arrayname );
}

Eg:
for( i=0; i<n;i++)
{
Scanf(" %d'", &a[i] );
}
Storing values into 2D array :
SYNTAX :
for (initialisation1;condition1;increament)
{
for(initialization2; condition2; increament2)
{
scanf( "format specifier ", arrayname );
}
}

Eg:
for( i=0 ; i< 3; i++)
{
for( j=0 ; j<3; j++)
{
scanf("%d" , a[i][j]);
}
}

7. Develop a C program to find the length of the given string without using predefined
functions?
Ans.
//to find the length of string//
#include<stdio.h>
#include<string.h>
int main()
{
char str[128];
int i,count=0;
printf("Enter a string:");
scant("%s'str);
for(i=0 ;str[i]!=0;i++)
{
count++;
}
printf(“length of the string =%d",count);
return 0;
}

8. Explain declaring a string with example?


Ans.
declaration of a string in C:
Declaring a String : A string in C is an array of characters, terminated by a null character
(`\0`).
Method 1: Array of Characters
Eg : char name[6] = {'J', 'o', 'h', 'n', '\0'};
Method 2: String Literal
Eg : char name[] = "John";
Method 3: Pointer to Character
Eg : char *name = "John";

Note: In Method 3, the string "John" is stored in read-only memory, so you cannot modify it.

Eg :
//To demonstrates declaring a string:
#include <stdio.h>
int main()
{
char name[] = "John";
printf("Hello, %s!\n", name);
return 0;
}

9. Explain reversing a string with string predefined function?


Ans.
//to find the reverse of string//
#include <stdio.h>
#include <string.h>
int main()
{
char str[20];
int len, i;
printf("Enter the string: ");
scant("%s", str);
len = strlen(str);
printf("Reverse string of the given string is: \n");
for(i = len - 1; i >= 0; i--)
{
printf("%c", str[i]);
}
return 0;
}

10. Explain concatenating two string with string predefined function?


Ans .
//To concatenate two strings//
#include<stdio.h>
#include<string.h>
int main()
{
char str1[20],str2[20];
printf("Enter the first string:");
scant("%s",str1);
printf("Enter the second string:");
scanf("%s",str2);
strcat(str1,str2);
printf("%s",str1);
return 0;
}

11. Explain difference between scanf() and gets() with example?


Ans.
difference between scanf() and gets()
scanf():
- Reads input from the standard input (usually the keyboard)
- Can read different data types, such as integers, floats, and strings
-`scanf()` reads different data types and stops at whitespace characters.
Syntax : scanf("format specifier ",&variable_name);
Eg :
#include <stdio.h>
int main()
{
int age;
printf("Enter your age: ");
scanf("%d", &age);
printf("Age: %d\n", age);
return 0;
}

gets()
- Reads a line of input from the standard input (usually the keyboard)
- Reads until it encounters a newline character
- gets()` reads a line of input until the newline character and includes whitespace characters.
Syntax : gets(string_name);
Eg :
#include <stdio.h>
int main()
{
char name[20];
printf("Enter your name: ");
gets(name);
printf("Name: %s\n", name);
return 0;
}

12. Explain null character and importance of null character in strings?


Ans.
Null Character : A null character (`\0`) is a special character that marks the end of a string.
Importance of Null Character :
The null character is important because it:
1. Tells the computer where the string ends.
2. Prevents the computer from reading beyond the end of the string.
3. Helps with string operations like copying, comparing, and searching.
Eg: char name[] = {'J', 'o', 'h', 'n', '\0'};
The null character (`\0`) marks the end of the string "John".

UNIT - 4 :

1. Write a program to convert floating point numbers into integers using pointers.
Ans.
//to convert float to int using pointers //
#include<stdio.h>
#include<conio.h>
Int main()
{
Int x, *p;
float y, *q;
printf( "Enter the floating point numbers :");
Scant ("%f",&y);
q=&y;
p=&x;
x= y;
Printf("floating number %f in integers is %d", *q,*p);
getch();
return O;
}

2. What is Dangling Pointer. Explain with an example.


Ans.
Dangling pointer:If any pointer is pointing the memory address of any variable but after
some time the variable is detected from the memory location while pointer Is still pointing
such memory location.such pointer is known|as "dangling pointer".and this problem is known
as "dangling pointer problem”.
Eg:
Program:-
#include<stdio.h>
#include<stdlib.h>
Int main()
{
Int *ptr=(int*)malloc(size of (int));
Ptr =10;
Printf("value before freeing:%d\n",*ptr);
free(ptr);
Printf("value after freeing:%d/n",* ptr);
return O;
}

3. What is Null Pointer. Explain with an example.


Ans.
A NULL pointer ts a pointer that does not
have any assigned to it.the NULL pointer Is typically represented by the keyword NULL or
constant O.
Eg Program:-
#include<stdio.h>
Int main()
{
Int *ptr=NULL;
If(ptr==NULL)
{
Printf("pointer = NULL:");
}
else
{
Printf("pointer = not NULL");
}
return O;
}

4. What is Generic Pointer. Explain with an example.


ANS.
Generic Pointer : A generic pointer is also
known as void pointer , it is a type of pointer that can point to any data ,without knowing the
specific data type it points to .it is a pointer that has no associated data type with it. In C
language ,a generic
pointer is declared the keyword void*.
Eg:
#include<stdio.h>
Int main()
{
int x=10;
float y=20.5;
Char z='A’;
Ptr=&x;
printf("integers value =%d\n", *(int*) ptr);
Ptr=&y;
orintf(“float valuer =%f\n",*(float*)
ptr);
Ptr=&z;
printf("character value =%c", *(char*)ptr);
return 0;
}

5. What is double Pointer. Explain with an example.


Ans.
points another pointer variable.
Declaration of double pointer:
Syntax : datatype **variable_ name;
Eg: int **p;
Program:
#include<stdio.h>
Int main()
{
int X=10, *p,°"q;
P=&x;
q=&p;
Printf("q=%d",**q);
return 0;
}

6. What is self-reference Pointer. Explain with an example.


Ans.
Self -Reference pointer:A self-reference
pointer Is a pointer that points to the same
structure or object that it is a part of .in
other words it is an pointer that reference it
self.
Eg:
PROGRAM :
#include<stdio.h>
struct janu
{
int data ;
Struct janu *self;
i
Int main()
{
janu.data=10;
janu.self=skjanu;
printf("Data =%d\n" , janu.self->data);
return 0;
}

7. Differentiate between dot operator (.) and arrow operator (->).


Ans.
Dot operator(.):
1.It is also known as member operator.
2.It is used to accessing the members of a
structure.
Syntax :
Structure_variable_name.membername
Eg: stud2.name;

Arrow operator(->):
1.it is also called as structure pointer operator.
2.It is used to accessing members of pointer to structure
syntax:

8. Differentiate between malloc() and calloc().


Ans.
malloc( ) :These is used for allocating block of memory at runtime .This function reserves a
block of memory of given size.if it fails to allocate enough space , it return as a NULL
pointer .
SYNTAX :
Pointername=(castname*)malloc(n*sizeof(datatype));

calloc() : It is used to reserve multiple blocks of memory each of same size and then sets all
bytes to zero .calloc stands for continue memory allocation .it is primarily used to allocate
memory for arrays .
SYNTAX :
Pointername=(castname*)calloc(n,sizeof(data type));
Eg: p=(int*)calloc(10*sizeof(int));

9. Explain purpose of using realloc().


Ans.
realloc(): sometimes we may feel that
memory allocated using malloc (or) calloc is in sufficient Is in sufficient (or) excess. If we
want to change the size of the
allocated memory; can use realloc() to add (or) delete memory at the end of/ from the end of
block of initial declared memory.
SYNTAX: p=realloc(p,newsize);

10. How is pointer variable different from an ordinary variable?


Ans.
Ordinary variable: In C programming
language, a variable is a user-defined or a
user-readable custom name assigned to a
memory location.
Syntax for declaration
Syntax: datatype variablename;
Eg: int x;
Accessing elements of varaible
Eg:x=10
Example program:
#include<stdio.h>
Int main()
{
Int x=10;
Printf("%d",x);
return O;
}

11. Write a program using pointers to swap two numbers.


Ans.
//To swap two numbers //
#include<stdio.h>
void swap(int *x, int *y);
void main()
Int a,b;
printf("Enter a,b values :");
scant("%d%d ",&a,&b);
printf("Before swapping a =%d ,b=%d\n",a,b);
}
void swap(int *x , int *y)
{
Int temp ;
Temp=*x;
*x=*y;
*y=temp;
}

12. Develop a C program to access elements of an array using pointer.


Ans.
//To access the elements of an array using
pointers //
#include<stdio.h>
int main()
{
int a[10] ,i, *p;
printt("“Enter array elements :");
for(i=O; i<10 ; i++)
{
scant("%d",&a[i]);
}
printf("“Entered elements are :");
for(i=0; i<10 ; i++)
{
printf("%d", *(i+1));
}
getch();
return 0;
}
UNIT - 5 :

1. Write about ‘fprintf()’ and ‘fscanf()’ functions in C programming.


Ans.
fprintf(): fprintf() is a function that writes formatted output to a file. It is similar to `printf()`, but
instead of writing to the standard output (usually the screen), it writes to a file.
Syntax: fprintf(file_pointer, format_string, argument1, argument2, ...);
Example:
FILE *file = fopen("example.txt", "w");
fprintf(file, "Hello, %s!\n", "World");
fclose(file);
This will write the string "Hello, World!" to the file "example.txt".

fscanf(): fscanf() is a function that reads formatted input from a file. It is similar to `scanf()`,
but instead of reading from the standard input (usually the keyboard), it reads from a file.
Syntax: fscanf(file_pointer, format_string, argument1, argument2, ...);
Example:
FILE *file = fopen("example.txt", "r");
int x;
char name[20];
fscanf(file, "%d %s", &x, name);
printf("x = %d, name = %s\n", x, name);
fclose(file);
This will read an integer and a string from the file "example.txt" and print them to the
console.

2. Compare and contrast Text and Binary stream.


Ans.
differentiate between text and binary streams:

Text Streams:

1. Character-based: Text streams process data one character at a time.


2. ASCII or Unicode: Text streams typically use ASCII or Unicode character encodings.
3. Formatted data: Text streams often contain formatted data, such as lines of text,
separated by newline characters.
4. Human-readable: Text streams are designed to be human-readable, making them suitable
for editing and viewing.

Examples of text streams include:

- Text files (e.g., .txt, .csv)


- Console input/output
- Web pages (HTML, CSS, JavaScript)
Binary Streams:

1. Byte-based: Binary streams process data one byte at a time.


2. Raw, unformatted data: Binary streams contain raw, unformatted data, which can
represent images, audio, or other types of data.
3. Machine-readable: Binary streams are designed to be machine-readable, making them
suitable for processing by computers.
4. Not human-readable: Binary streams are not human-readable, as they contain raw,
unformatted data.

Examples of binary streams include:

- Image files (e.g., .jpg, .png)


- Audio files (e.g., .mp3, .wav)
- Executable files (e.g., .exe, .dll)
- Database files
In summary, text streams are designed for human-readable, formatted data, while
binary streams are designed for machine-readable, raw, unformatted data.

3. Explain types of arguments.


Ans.
Types of Arguments:
1. Formal Arguments:
Variables declared in a function definition.
2. Actual Arguments :
Values passed to a function when it's called.
3. Positional Arguments :
Arguments passed in a specific order.
4. Keyword Arguments :
Arguments passed using the variable name.
5. Default Arguments :
Arguments with a default value if not provided.
6. Variable-Length Arguments :
Functions that accept a varying number of arguments.
7. Pass-by-Value Arguments :
Arguments passed by copying the value.
8. Pass-by-Reference Arguments :
Arguments passed by referencing the original value.

4. How to pass arrays as parameters


Ans.
Passing an array as a parameter to a function is a common practice in programming. Here's
how to do it:

Passing a one-dimensional Array:


Syntax:
void function_name(data_type array_name[]) {
// function body
}
int main()
{
data_type array_name[size]; function_name(array_name);
return 0;
}

Passing a Multidimensional Array


Syntax:
void function_name(data_type array_name[][column_size])
{
// function body
}
int main()
{
data_type array_name[row_size[column_size];
function_name(array_name);
return 0;
}

Passing an Array with a Specific Size


Syntax:
void function_name(data_type array_name[specific_size])
{
// function body
}
int main()
{
data_type array_name[specific_size];
function_name(array_name);
return 0;
}

5. How to pass pointers as parameters and what were the advantages of it.
Ans.
Passing a pointer as a parameter to a function is a common practice in programming,
especially in languages like C. Here's how to do it:

Passing a Pointer as a Parameter


Syntax:
void function_name(data_type *pointer_name) {
// function body
}

int main() {
data_type variable_name;
function_name(&variable_name);
return 0;
}

Advantages of Passing Pointers as Parameters


1. Pass by Reference
2. Efficient Memory Use
3. Flexibility.
4. Improved Performance:

Eg Program:
#include<stdio h>
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
int main()
{
int x =5, y = 10;
printf("Before swap: x = %d, y = %d\n", x, y);
swap(&x, &y);
printf("After swap: x = %d, y = %d\n", x, y); return 0;
}

6. What are the types of storage classes


Ans.
There are four types of storage classes are there they are :

7. Syntax to create a file


Ans.
Here is the syntax to create a file:
SYNTAX:
FILE *fp;
fp = fopen("filename.txt", "w");
if (fp == NULL)
{
printf("Could not open file");
return 1;
}
fclose(filename);

8. What are different modes of file opening.


Ans.
In file handling, a file can be opened in different modes, which determine the operations that
can be performed on the file. Here are the different modes of opening a file:

Text Modes
1. r(Read): Opens a file for reading.
2. *w* (Write): Opens a file for writing.
3. *a* (Append): Opens a file for appending.
4. *r+* (Read and Write): Opens a file for both reading and writing.
5. *w+(Read and write): open a file for both reading and writing.
6. *a+* (Read and Append): Opens a file for both reading and appending.

Binary Modes
1. *rb* (Read Binary): Opens a file for reading in binary mode.
2. *wb* (Write Binary): Opens a file for writing in binary mode..
3. *ab* (Append Binary): Opens a file for appending in binary mode.
4. *r+b* (Read and Write Binary): Opens a file for both reading and writing in binary mode.
The file pointer is positioned at the beginning of the file.
5. *w+b* (Read and Write Binary): Opens a file for both reading and writing in binary mode.

9. Explain different functions in file IO


Ans.
Here are different functions used in File Input/Output (IO) operations:

Input Functions:
fopen(): Opens a file in a specified mode (e.g., "r" for reading, "w" for writing).
Eg: FILE *fp = fopen("example.txt", "r");

fscanf(): Reads formatted data from a file.


Eg: fscanf(fp, "%s %d", name, &age);

fgets(): Reads a line of text from a file.


Eg: fgets(line, sizeof(line), fp);

Output Functions
fopen(): Opens a file in a specified mode (e.g., "w" for writing, "a" for appending).
Eg: `FILE *fp = fopen("example.txt", "w")

fprintf(): Writes formatted data to a file.


Eg: fprintf(fp, "%s %d", name, age);

fputs(): Writes a string to a file.


Eg: fputs("Hello, World!", fp);

Other Functions
fclose(): Closes a file.
Eg: fclose(fp);
10. Explain return type and return value with syntax
Ans.
Return Type: The return type is the data type of the value that a function returns.
Syntax:
return-type function-name()
{
// function body
return return-value;
}
Eg:
int addNumbers()
{
int sum = 2 + 3;
return sum; // returns an integer value
}
In this example, `int` is the return type.

Return Value: The return value is the actual value that a function returns.
Syntax: return return-value;
Example :
int addNumbers()
{
int sum = 2 + 3;
return sum; // returns the value 5
}
In this example, '5' is the return value.

11. What is a functions and what are advantages of it


Ans.
Functions: A function is a group of statements that together perform a task. Every C
program has at least one function, which is main(), and all the most trivial programs can
define additional functions.
Advantages of functions:
-C functions are used to avoid rewriting same code again and again in a program.
-The main usage of functions is dividing a big task into small pieces to improve understand
ability of very large c programs.
-We can call functions any number of times in a program and from any place in a program.

12. What will be passed if an array is passed as parameter to the function & how to
manipulate array
in the function.
Ans.
When an array is passed as a parameter to a function, the following things happen:

1. Address of the first element is passed to a function, the address of the first element of the
array is passed, not the entire array.
2. Array decays to a pointer: In the function parameter list, the array name decays to a
pointer to the first element of the array.

To manipulate the array in the function:


1. Use pointer arithmetic: You can use pointer arithmetic to access and modify the elements
of the array.
2. Use array indexing: You can also use array indexing to access and modify the elements of
the array.

Eg:
#include <stdio.h>
void manipulate_array(int arr[], int size)
{
for (int i = 0; i < size; i++)
{
*(arr + i) = i * 2;
}
for (int i = 0; i < size; i++)
{
arr[i] = i * 3;
}
}
int main()
{
int arr[5];
int size = sizeof(arr) / sizeof(arr[0]);
manipulate_array(arr, size);
for (int i = 0; i < size; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}

You might also like