C PRO ANS
C PRO ANS
9. The -----------operator can be used to determine the length of array and structures.
sizeof
10. The operator “++’’ adds the value ------------ to the operand.
1
II.
1.
Algorithms are step-by-step procedures designed to solve specific problems and
perform tasks efficiently in the realm of computer science and mathematics.
A flowchart is a type of diagram that represents a workflow or process.
2.
Top-down design means to repeatedly divide a problem into (smaller) sub-problems,
until they are directly solvable. A top-down design process can be represented by a
structure chart.
3.
5.
They are used for inputting the data of the arithmetic unit (ALU), outputting the calculation
result, transferring the operation of the data stored in memory, etc.
6.
A Structure is a data structure that can contain variables of different data types. An
Array is a data structure that can only contain variables of the same data type.
7.
8.
Type variableName = value; Where type is one of C types (such as int ), and
variableName is the name of the variable (such as x or myName). The equal sign is used
to assign a value to the variable.
9.
printf function is used to print character stream of data on stdout
console.
Syntax:
printf(const char* str, ...);
fprintf is used to print the string content in file but not on the stdout
console.
Syntax: fprintf(FILE *fptr, const char
*str, ...);
10.
Function fopen() is used both to open or create a file, and fclose() is used to close an
already opened file. File 'read' and 'write' operations can be performed after opening a file.
The fputc() and fputs() functions are used to write characters and strings respectively in a file.
III.
1.
A function that calls itself is known as a recursive function. And, this technique
is known as recursion.
The recursion continues until some condition is met to prevent it.
To prevent infinite recursion, if...else statement (or similar approach) can be
used where one branch makes the recursive call, and other doesn't.
#include <stdio.h>
int factorial(int n);
int main() {
int num;
printf("Enter a non-negative integer: ");
scanf("%d", &num);
if (num < 0) {
printf("Error: Factorial of a negative number is undefined.\n");
} else {
int result = factorial(num);
printf("Factorial of %d = %d\n", num, result);
}
return 0;
}
In if- else-if ladder statement, if a condition is true then the statements defined in the if
block will be executed, otherwise if some other condition is true then the statements
defined in the else-if block will be executed, at the last if none of the condition is true then
the statements defined in the else block .
3. Basic Types:
int: Used to store integers (whole numbers), such as -5, 0, 42.
char: Used to store single characters, such as 'a', '5', or '$'.
float: Used to store single-precision floating-point numbers, such as 3.14,
-0.001, 7.0.
double: Used to store double-precision floating-point numbers, with more
precision than float. For example, 3.14159.
4.
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
sortString(str);
return 0;
}
5.
This function is used to find the length of a string, i.e., the number of characters in the string
excluding the null terminator '\0'.
This function is used to copy the contents of one string into another. It copies the string along
with the null terminator '\0'.
Syntax:
This function is used to compare two strings lexicographically. It returns an integer less than,
equal to, or greater than zero if the first string is found, respectively, to be less than, to match,
or be greater than the second string.
Syntax:
IV.
1)
a) for Loop
for loop in C programming is a repetition control structure that
allows programmers to write a loop that will be executed a specific number of
times. for loop enables programmers to perform n number of steps together
in a single line.
Syntax:
for (initialize expression; test expression; update
expression)
{
//
// body of for loop
//
}
b) do-while Loop
The do-while loop is similar to a while loop but the only difference lies in the
do-while loop test condition which is tested at the end of the body. In the do-
while loop, the loop body will execute at least once irrespective of the test
condition.
Syntax:
initialization_expression;
do
{
// body of do-while loop
update_expression;
} while (test_expression);
c) While Loop
While loop does not depend upon the number of iterations. In for loop the
number of iterations was previously known to us but in the While loop, the
execution is terminated on the basis of the test condition. If the test condition
will become false then it will break from the while loop else body will be
executed.
Syntax:
initialization_expression;
while (test_expression)
{
// body of the while loop
update_expression;
}
4)
Program
#include <stdio.h>
#include <string.h>
int main() {
char input[MAX_SIZE];
// Counting words
words++;
words++;
// Counting characters
characters = strlen(input);
printf("Number of words: %d\n", words);
return 0;
5) Program
#include <stdio.h>
int isPrime(int num) {
if (num <= 1) {
return 0; // 0 and 1 are not prime numbers
}
return 0;
}