22CSE14
22CSE14
Relational Operators
• Used to compare the values of two expressions.
• If the result is true, return 1. If the result is false, returns 0.
Example:
int a=4, b=78, c; Output:
c=a==b;printf(“%d”,c); 01
c=4!=78;printf(“%d”,c);
Logical Operators
Used to combine two expressions. The result will be either true or false.
Logical NOT !
It reverses the logical value of a variable or a constant or an expression.
Logical AND &&
• Works on 2 operands.
• Result is true if values of both expressions are true.
• Result is false, if any one expression value is false.
Logical OR ||
• Works on 2 operands.
• Result is false if values of both expressions are false.
• Result is true, if any one expression value is true.
Example:
3|Page GAT STUDENTS LIBARY
10!=10 || 5<4 && 8
(10!=10) || (5<4) && 8
false || false && true
false || (false && true)
false || false = false
3) Define token. List the tokens used in C Language and explain
Keywords and Strings with examples.
ANS: In a C program, the smallest individual units are known as C
tokens. Types of tokens are keywords, identifiers, constants, strings,
special symbols and operators.
Keywords:
Every C word is classified as either a keyword or an identifier. All
keywords have fixed meaning and these meaning cannot be changed.
Keyword serve as basic building blocks for program statement. All
keywords must be written in lower case.
Strings:
Strings in C are always represented as an array of character ‘\0’ at the end
of the string. This null character denotes the end of the strings. Strings in
C are enclosed in double quote,while charters are enclosed within single
character. The size of a string is a number of characters that the string
contains.
int
• An int is a keyword which is used to define integers.
• Using int keyword, the programmer can inform the compiler that the
data associated with this keyword should be treated as integer.
• → int requires 2 bytes of storage space
• C has three classes of integer storage, namely short int, int, and long int
float
• A float is a keyword which is used to define floating point numbers.
• float provides an accuracy of 6 digits after the decimal point.
• It consumes 4 bytes of storage space.
• Floating point numbers are defined in C by the keyword float.
double
• A double is a keyword used to define long floating-point numbers.
• It gives an accuracy of 16 digits after the decimal point
• 8 bytes of storage space
Example:-
int a,b;
printf(“enter the values of a and b”);
scanf (“%d%d”,&a,&b);
• The field specification for reading an integer number is:
% wd
• The percentage sign (%) indicates that a conversion specification
follows.
• w is an integer number that specifies the field width of the number to
be read and
d, known as data type character, indicates that the number to be read is
in integer mode.
6|Page GAT STUDENTS LIBARY
• Consider the following example:
scanf (“%2d %5d”, &num1, &num2);
• Data line: 50 31426
• The value 50 is assigned to num1 and 31426 to num2.
• Suppose the input data is as follows: 31426 50
• The variable num1 will be assigned 31 (because of %2d) and num2
will be assigned 426 (unread part of 31426).
• The value 50 that is unread will be assigned to the fi rst variable in the
next scanf call.
Formatted output:
printf()
• The printf (print formatted) standard library function is used to print
the values of expressions on standard output (i.e, display) in a specifies
format
• Syntax:
printf(“ControlString”, arg1,arg2,…,argn);
• Control string consists of:
• Character that will be printed on the screen as they
• Format specifications that define the output format
for display of each item
• Escape sequence characters
Example:-
printf(“good morning\n”)’;
printf(“%d%d%d”,num1,num2,sum);
• The format specification for printing an integer number is:
%wd
• where w specifies the minimum fi eld width for the output. However,
if a number is greater than the specified fi eld width, it will be printed
7|Page GAT STUDENTS LIBARY
in full, overriding the minimum specification. d specifies that the
value to be printed is an integer. The number is written right-justified
in the given field width.
6) List the rules for naming a variable in C.
ANS: Rules for naming the variables
• The first character in the variable name should be a letter or an
underscore.
• The first character can be followed by any number of letters or
digits(0to9) or underscores.
• No extra symbols are allowed other than letter, digits and underscore.
• C Keywords must not be used.
• The maximum size of the characters supported for naming the internal
variable is 31.
Example 1:
• m = 5;
• y = --m; (prefix)
• In this case the value m will be decremented to 4 first and then it will be
assigned to y. Therefore at the end of the execution, the value of y and m
will be 4.
Example 2:
• m = 5;
• y = m--; (postfix)
• In this case the values of m will gets assigned first to y and then it will
gets decremented. Therefore at the end of the execution, the value of y will
be 5 and m will be 4.
Relational Operators:
• The operators that are used to find the relationship between
two operands are called relational operators
Conditional Operators
• Conditional operator which is also known as ternary operator,
operates on three operands. Conditional operators return one value if
condition is true and returns another value if condition is false.
• The syntax is shown below:
(exp1)? exp2: exp3;
• where exp1 is an expression evaluated to true or false;
• If exp1 is evaluated to true, exp2 is executed;
• If exp1 is evaluated to false, exp3 is executed.
• Example
a=10 ;
b=15;
x=(a>b) ? a : b;
Example program
#include<stdio.h>
main()
{
int i=1;
while(i<=5)
{
printf(“Welcome to C language\n”);
i=i+1;
}
}
Output:
Welcome to C language Welcome to C language
13 | P a g e GAT STUDENTS LIBARY
Welcome to C language
Welcome to C language
Welcome to C language
2) Explain the working of for loop with example.
• The for loop is another entry-controlled loop that provides a more
concise loop control structure. The general form of the for loop is
For(initialization; test-condition; increment)
{
body of the loop
}
• Initialization of the control variables is done first, using assignment
statements such as i = 1 and count = 0.
• The value of the control variable is tested using the test-condition. f the
condition is true, the body of the loop is executed; otherwise the loop is
terminated and the execution continues with the statement that
immediately follows the loop.
• When the body of the loop is executed, the control is transferred back
to the for statement after evaluating the last statement in the loop. Now,
the control variable is incremented using an assignment statement such
as i = i+1
INITIALIZATION:
One dimensional arrays can be initialized in 2
ways
1)At compile time
2)At run time
1. Compile Time initialization
– Giving values to array at the time of array declaration itself is
called compile time initialization. Values are separated by commas.
• Syntax:
datatype arrayname[size]={v1,v2…vn};
• Example:
int a[3]={10,20,30};
float b[2]={1.1,2.2};
• Character arrays may be initialized in a similar
19 | P a g e GAT STUDENTS LIBARY
manner. Thus, the statement
char name[ ] = {‘J’,‘o’, ‘h’, ‘n’, ‘\0’};
• Alternatively, we can assign the string literal
directly as under:
char name [ ] = “John”
➢ The various ways of initializing arrays are as
follows:
1. Initializing all elements of array(Complete array initialization)
2. Partial array initialization
3. Initialization without size
4. Initialize individual elements
1. Initializing all elements of array:
➢ Arrays can be initialized at the time of declaration
when their initial values are known in advance.
➢ In this type of array initialization, initialize all the
elements of specified memory size.
➢ Example: int a[5]={10,20,30,40,50};
INITIALIZING 2D ARRAYS
• Two dimensional arrays can be initialized in 2 ways
1)At compile time
2)At run time
1. Compile Time initialization
i)Basic initialization: Mentioning the array size and all the values at the
time of declaration.
Example: int a[2][3]={6,4,2,1,8,26};
ii)Initialization without size: While initializing a 2-D array it is
necessary to mention the second (column) dimension, whereas the first
dimension (row) is optional.
Example: int a[][3]={6,4,2,1,8,26};
22 | P a g e GAT STUDENTS LIBARY
iii)Partial initialization: can initialize the array elements partially
as in the below example.
int a[2][3]={{1,2},{5}}
• Initializing row wise
Example:- int a[3][3]= { {10,20,30},
{40,50,60},
{70,80,90} };
• Combining row elements and initializing
int a[3][3]= { 10,20,30,40,50,60,70,80,90 };
2. At run time:
• (reading 2D array elements)
Two for loops are needed to initialize two dimensional array at runtime.
for(i=0;i <m;i++)
{
for(j=0;j<n;j++)
{
scanf(“%d”,&a[i][j]);
}
}
EXAMPLE PROGRAM:
Write a program to read and display matrix
#include<stdio.h>
void main()
{
int a[10][10],m,n,i,j;
printf("Enter number of rows and columns\n");
1)strlen() function
• This function counts the number of characters present in a string
Syntax:
length=strlen(string);
Example program using strlen()
#include <stdio.h>
#include<string.h>
void main()
{
char str[100];
int length;
printf("Enter string\n");
scanf("%s",str); //or use gets(str)
length=strlen(str);
printf("Length of the string is %d\n",length);
}
27 | P a g e GAT STUDENTS LIBARY
2)strcpy() function
• This function copies the contents of source string to destination string.
• Syntax: strcpy(dest, source);
Or
strcpy(string1, string2);
– and assigns the contents of string2 to string1.
• strcpy(city, “DELHI”);
– will assign the string “DELHI” to the string variable city.
Similarly, the statement
– strcpy(city1, city2);
– will assign the contents of the string variable city2 to
the string variable city1
3)strcat() function:
• strcat function is used to join two strings together.
• Syntax: strcat(string1,string2);
• Ex:-
Function Call :
• A function can be called by simply using the function name followed
by a list of actual parameters (or arguments) if any enclosed in
parentheses.
• Syntax: function_name(parameters);
• Example: add(a,b);
Write a function to find sum of two numbers.
#include<stdio.h>
int add(int a, int b ); -----------------→//function declaration
void main()
{
int a,b,c;
printf(“\n Enter the values of a and b\n”);
}
• Three Rules to pass an array to a function:
1. The function must be called by passing only the name of the array.
2. In the function definition the formal parameter must be an array type,
the size of the array does not need to be specified.
35 | P a g e GAT STUDENTS LIBARY
3. The function prototype must show that the argument in an array.
4. What is recursion? Write a C program to calculate factorial of a
number using recursion..
• Recursion refers to the process where a function calls itself repeatedly
until a certain exit condition is met.
• A function which calls itself repeatedly until a certain exit condition
is met is called recursive function.
#include<stdio.h>
int fact(int n);
void main( )
{
int k , a;
printf(“enter the value of number \n” );
scanf(“%d”, &k);
a= fact(k);
printf(“the factorial of a %d is %d”, k, a);
}
int fact(int n)
{
if(n==0)
return 1;
else
return( n * fact(n-1));
}
5. What is a pointer? Explain how the pointer variable is declared
and initialized.
• A pointer is a variable whose value is the address of another variable.
• Pointers contain memory addresses as their values.
Pointer Declaration:
Pointer variables should be declared before they are used.
Syntax: datatype *pointer_name;
Eg: int *p; /*pointer to an integer */
36 | P a g e GAT STUDENTS LIBARY
float *f; /*pointer to float*/
Initialization of Pointer :
• It is the process of assigning an address to the pointer variable.
• Syntax: pointer_name =&variable_name;
Eg: int a=10;
int *p ; //pointer declaration
p= &a; //Initialization of pointer variable
Categories of user defined functions:
1. Function with parameters and with return values.
2. Functions with parameters and without return values
3. Function without parameter and with return values.
4. Functions without parameters and without return values. (Void and
parameter less Functions).
6. List and explain the categories of user defined function
Categories of user defined functions
1. Function with parameters and with return values.
2. Functions with parameters and without return values
3. Function without parameter and with return values.
4. Functions without parameters and without return values. (Void and
parameter less Functions).
1. Function with parameters and with return values.
• Parameters are passed from calling function to the called function.
Based on the received parameter values called function performs
required action and returns a value back to the calling function.
Example:
//C program to find the cube of a given number
#include<stdio.h>
int cube(int k);
void main()
{
int n,
printf(“Enter a Number\n”);
scanf(“%d”,&n);
a=cube(n);
37 | P a g e GAT STUDENTS LIBARY
printf(“cube of %d is %d\n”,n,a);
}
int cube(int k)
{
int c;
c=k*k*k;
return c;
}
Output:
Enter a number
3
Cube of 3 is 27
2. Function with parameters and without return values
• Parameters are passed from calling function to called function and
called function does not return a value. It just performs the specified
action.
Example:
//C program to find the cube of a given number
#include<stdio.h>
void cube(int k);
void main()
{
int n;
printf(“Enter a Number\n”);
scanf(“%d”,&n);
cube(n);
}
void cube(int k)
{
int c;
c=k*k*k;
printf(“cube of %d is %d\n”,k,c);
}
3. Function without parameters and with return values
Eg:
int *ptr;
If ptr=1000,
ptr++ = ptr + (1*size of int)
= 1000+(1*2)=1002
float *ptr;
If ptr=1000,
ptr++ = ptr + (1*size of float)
= 1000+(1*4)=1004
#include<stdio.h>
main()
42 | P a g e GAT STUDENTS LIBARY
{
int a,*p;
p=&a;
printf(“value of pointer p before decrementation %u “, p);
p--;
printf(“value of pointer p after decrementation %u “, p);
}
Output:
value of pointer p before decrementation: 3472147
value of pointer p after decrementation : 3472145
10. Illustrate the use of indirection operator ‘*’ in accessing a value
pointed by a pointer with an example program
Module 5
1. Define a structure. Explain the syntax of structure declaration in
C with an example.
Structure is a collection of elements of same or different data types
under a single name.
Structure Definition:
Syntax:
struct tagName
{
datatype member1;
datatype member2;
------------
------------
datatype membern;
};
Example :
struct employee
43 | P a g e GAT STUDENTS LIBARY
{
char name[20];
float salary;
int empno;
};
2. Differentiate between Array and Structures.
Arrays Strings
1. An array is a collection of 1. Structure is a collection of
elements of the same data type elements of different data type.
2. An array is a derived data type 2. A structure is a user defined
3. Array uses index/subscript for data type
accessing elements of the array. 3. Structures uses (.) operator for
4. . An array is a pointer to the accessing the member of a
first element of it. structure.
5. Eg: int a[10] 4. Structure is not a pointer
5. Eg: struct employee
{
char name[20];
int empno;
float salary;
};
3. Explain with an example the different ways of declaring structure
variables.
Declaring Structure Variables:
• A structure variable declaration is similar to thedeclaration of
variables of any other data types. It includes the following elements:
1. The keyword struct.
2. The structure tag name.
3. List of variable names separated by commas.
4. A terminating semicolon.
Syntax: struct tagName variable1,variable2;
Eg: struct employee emp1,emp2;
Another way of structure variable declaration