C Programming DEC 2021 Ktunotes - in
C Programming DEC 2021 Ktunotes - in
C Programming DEC 2021 Ktunotes - in
Low-level
languages are used to write the system software. System Software maintains the system resources and gives the
path for application software to run. An important thing is that without system software, the system can not run. It is a
general-purpose software.
Application Software is the type of software that runs as per user request. It runs on the platform which is provided
by system software. High-level languages are used to write the application software. It’s a specific purpose software.
The main difference between System Software and Application Software is that without system software, the system
can not run on the other hand without application software, the Low-level maintains system always runs.
3. The “=” is an assignment operator is used to assign the value on the right to the variable on the left.
The ‘==’ operator checks whether the two given operands are equal or not. If so, it returns true. Otherwise it
returns false.
static variable is used for a common value which is shared by all the methods and its scope is till the lifetime of
whole program. In the C programming language, static is used with global variables and functions to set their scope
to the containing file.
5. #include <stdio.h>
void main()
{
char string[50];
int i, length = 0;
6. An array is a collection of elements of the same type placed in contiguous memory locations that can be
individually referenced by using an index to a unique identifier
If you will not define size of an array inside [ ] brackets, and you are also not initializing the array while declaring.
Compiler will show an error “size of array is unknown or zero” and program will not compile.
int a[5];
a[0] = 10;
a[1] = 20;
a[2] = 30;
a[3] = 40;
a[4] = 50;
This is the most useful approach to initialize one-dimensional array in c programming. Most of the time, array
elements are initialized with this method as per the requirement of the program.
7.
Example:
void main()
{
int sum = sumDigits();
printf("%d\n", sum);
return;
}
int sumDigits()
{
int sum = 0;
int digit;
for(digit = 0; digit <= 9; ++digit)
{
sum += digit;
}
return sum;
}
9.
10. fseek() is used to move file pointer associated with a given file to a specific position.
Syntax:
returns:
zero if successful, or else it returns a non-zero value
position defines the point with respect to which the file pointer needs to be moved. It has three values:
SEEK_END : It denotes end of the file.
SEEK_SET : It denotes starting of the file.
SEEK_CUR : It denotes file pointer’s current position.
11.
(a)
(b)
#include <stdio.h>
int main()
{
int a = 5;
int b = 10;
int t;
t = a;
a = b;
b = t;
printf("\n a = %d", a);
printf("\n b = %d", b);
return 0;
}
int a = 10;
int b = 5;
int result;
result = a + b;
result = a - b;
result = a * b;
result = a / b;
result = a % b;
Relational Operators
Any relation between the two operands is validated by using relational operators. Relational operators return
Boolean values. If the relation between two operands is successfully validated then it will return “true” and if the
validation fails then “false” will be returned.
Relational operators are mainly used in decision making or for defining conditions for loops.
Greater than operator: (denoted by “>”): Validates greater than the relation between operands.
Less than operator: (denoted by “<“): Validates less than the relation between operands.
Equals To operator: (denoted by “==”): Validates the equality of two operands.
Greater than or equals to (denoted by “>=”): Validates greater than or equals to the relation between the two
operands.
Less than or equals to (denoted by “<=”): Validates less than or equals to the relations between the two
operands.
Not equal: (denoted by “!=”): Validates not an equal relationship between the two operands.
int a = 10;
int b = 5;
bool validate;
validate = a > b;
Console.WriteLine(validate);
validate = a < b;
Console.WriteLine(validate);
validate = a == b;
Assignment Operators
Assignment operators are used for assigning value to a variable. These are generally used before an arithmetic
operator.
(i) Equals to (“=”): It is one of the simplest assignment operators. It assigns the value of one operand to another.
i.e. the value of the right side operand to the left side operand.
Example: a = b
(ii) Add Equal to the Assignment Operator: As the name suggests it is a combination of plus “+” and equal to “=”.
It is written as “+=” and it adds the operand at the right side to the left operand and stores the final value in the left
operand.
(iii) Subtract Equal Assignment Operator: Similar to the add equals, it subtracts the value of the right operand
from the left operand and then assigns the value to the left operand.
(iv) Division Equal to the Assignment Operator: It divides the value of the right operand with the left operand and
then stores the result in the left operand.
(v) Multiply Equal to the Assignment Operator: It multiplies the value of the right operand with the left operand
and then stores the result in the left operand.
(vi) Modulus Equals to the Assignment Operator: It finds the modulus of the left and right operand and stores the
value in the left operand.
Logical Operators
AND operator returns true when both the values are true. If any of the value is false then it will return false.
For example, A && B will return true if both A and B are true, if any or both of them are false then it will return
false.
OR operator returns true if any of the condition/operands is true. It will return false when both of the operands are
false.
For example, A || B returns true if the value of either of A or B is true. It will return false if both A and B have false
values.
NOT operator is used to reverse the logical conclusion of any condition. If the condition is true then it will return
false and if the condition is false then it will return true.
Example, !(A||B) returns false if “A||B” returns true and will return true if “A||B” returns false.
(b) #include<stdio.h>#include<stdlib.h>
int main(int a, char *b[])
{
int number, i, temp, sum = 0, factorial = 1;
number = atoi(b[1]);
temp = number;
while(number != 0)
{
int rem = number%10;
for(i=2; i<=rem; i++)
{
factorial = factorial * i;
}
sum = sum + factorial;
number = number/10;
factorial = 1;
}
14.
(a) #include<stdio.h>
#include<stdlib.h>
int main(){
int a[10],n,i;
system ("cls");
printf("Enter the number to convert: ");
scanf("%d",&n);
for(i=0;n>0;i++)
{
a[i]=n%2;
n=n/2;
}
printf("\nBinary of Given Number is=");
for(i=i-1;i>=0;i--)
{
printf("%d",a[i]);
}
return 0;
}
(b) Formatted I/O functions are used to take various inputs from the user and display multiple outputs to the user.
These types of I/O functions can help to display the output to the user in different formats using the format specifiers.
These I/O supports all data types like int, float, char, and many more.
The user has to pass two arguments that are described below:
i) src
ii) dest
Here at the place of “src” string is specified, while at the place of ‘dest’ the destination string in which we have to
append the source string is specified.
Example
#include<string.h>
int main()
strcat(dest, src);
puts(dest);
return 0;
2) Function strlen()
One more function of string header file that can be directly used for the strings is strlen(). You can use the
function strlen(), the string function in C, when you have to find out the length of any string. The strlen() string
functions in c basically calculate the length of a given string. However, one can also write a program manually to find
out the length of any string, but the use of this direct function can save your time and the example is given below:
#include<stdio.h>
int length;
length=strlen(s);
return 0;
3) Function strcpy()
If you have to copy the content of one string to another string, then this function is being used. Even the null
characters are copied in the process. Syntax of the function is strcpy(dest,source). The function can copy the
content of one string to another. One example of the function is given below:
#include<string.h>
int main()
strcpy(dest, src);
return 0;
4.Function strcmp()
str1
str2
On comparing the return value be determined basis the strings setup as shown below.
The function returns a definite value that may be either 0, >0, or <0. In this function, the two values passed are
treated as case sensitive means ‘A’ and ‘a’ are treated as different letters. The values returned by the function are
used as:
Example:
#include<stdio.h>
#include<string.h>
int main()
char str1[]=”copy”;
char str2[]=”Trophy”;
int I,j,k;
i=strcmp(str1, “copy”);
j=strcmp(str1, str2);
k-strcmp(str1, “f”);
printf(“\n %d %d %d”,I,j,k);
return 0;
return 0;
16.
(a) #include <stdio.h>
#include <limits.h>
int main()
{
int arr[50], i, Size;
int first, second;
return 0;
}
(b) #include<stdio.h>
int main()
{
char string[40];
int length=0, flag=1,i;
printf("Enter string:\n");
gets(string);
for(i=0;string[i]!='\0';i++)
{
length++;
}
for(i=0;i< length/2;i++)
{
if( string[i] != string[length-1-i] )
{
flag=0;
break;
}
}
if(flag==1)
{
printf("PALINDROME");
}
else
{
printf("NOT PALINDROME");
}
return 0;
}
The scope of an auto variable is limited with the particular block only. Once the control goes out of the block, the
access is destroyed. This means only the block in which the auto variable is declared can access it.
A keyword auto is used to define an auto storage class. By default, an auto variable contains a garbage value.
Keyword extern is used to declaring a global variable or function in another file to provide the reference of
variable or function which have been already defined in the original file.
The variables defined using an extern keyword are called as global variables. These variables are accessible
throughout the program. Notice that the extern variable cannot be initialized it has already been defined in the
original file.
Static local variable is a local variable that retains and stores its value between function calls or block and
remains visible only to the function or block in which it is defined.
Static global variables are global variables visible only to the file in which it is declared.
Example: static int count = 10;
Keep in mind that static variable has a default initial value zero and is initialized only once in its lifetime.
The variables declared using register storage class has no default value. These variables are often declared at
the beginning of a program.
int main() {
int i;
printf("Enter information of students:\n");
// storing information
for (i = 0; i < 5; ++i) {
s[i].roll = i + 1;
printf("\nFor roll number%d,\n", s[i].roll);
printf("Enter first name: ");
scanf("%s", s[i].firstName);
printf("Enter marks: ");
scanf("%f", &s[i].marks);
}
printf("Displaying Information:\n\n");
// displaying information
for (i = 0; i < 5; ++i) {
printf("\nRoll number: %d\n", i + 1);
printf("First name: ");
puts(s[i].firstName);
printf("Marks: %.1f", s[i].marks);
printf("\n");
}
return 0;
}
18.
(a) Recursion is a process by which a function calls itself directly or indirectly. The corresponding function is
called as recursive function. Using recursive algorithms, certain complex problems can be solved quite easily.
#include<stdio.h>
int main()
{
int n, i = 0, c;
scanf("%d",&n);
printf("Fibonacci series\n");
return 0;
}
int Fibonacci(int n)
{
if ( n == 0 )
return 0;
else if ( n == 1 )
return 1;
else
return ( Fibonacci(n-1) + Fibonacci(n-2) );
}
sort_numbers_ascending(number, count);
}
19.
(a) In C, you can make use of some functions which are used for file handling purposes. Below is the list of
functions:-
20.
(a) Key Differences Between Array and Pointer
1.An array stores the variables of similar data types and the data types of the variables must match the type of
array.
2.Conversely, the pointer variable stores the address of a variable, of a type similar to a type of pointer variable
type.
3.We can generate an array of pointers i.e. array whose variables are the pointer variables.
4.On the other hand, we can create a pointer that points to an array.
6.As against, a pointer variable can store the address of the only variable.