Cs3353 Unit I
Cs3353 Unit I
UNIT I
C PROGRAMMING FUNDAMENTALS
What is C Programming Language?
C is a general-purpose programming language that is
extremely popular, simple, and flexible to use.
It is a structured programming language that is machine-
independent and extensively used to write various
applications, Operating Systems like Windows, and
many other complex programs like Oracle database, Git,
Python interpreter, and more.
It is said that ‘C’ is a god’s programming language. One
can say, C is a base for the programming. If you know
‘C,’ you can easily grasp the knowledge of the other
programming languages that uses the concept of ‘C’
C Basic Commands
C Basic commands Explanation
This command includes standard input output header
#include <stdio.h> file(stdio.h) from the C library before compiling a C
program
It is the main function from where C program execution
int main()
begins.
{ Indicates the beginning of the main function.
Whatever written inside this command “/* */” inside a C
/*_some_comments_*/ program, it will not be considered for compilation and
execution.
printf(“Hello_World! “); This command prints the output on the screen.
This command is used for any character input from
getch();
keyboard.
This command is used to terminate a C program (main
return 0;
function) and it returns 0.
} It is used to indicate the end of the main function.
How C Programming Language Works?
Enumeration Data Type They are again arithmetic types and they
are used to define variables that can only
assign certain discrete integer values
throughout the program.
Output:
Value of a is : 10
Value of A is :20
The above output shows that the values of both the variables, 'a' and
'A' are different. Therefore, we conclude that the identifiers are case
sensitive.
VARIABLES
A variable is a name of the memory location. It is
used to store data. Its value can be changed, and it can
be reused many times.
int a;
float b;
char c;
Here, a, b, c are variables. The int, float, char are the
data types.
Rules for defining variables
* multiplication
/ division
c = a+b;
printf("a+b = %d \n",c);
c = a-b;
printf("a-b = %d \n",c);
c = a*b;
printf("a*b = %d \n",c);
c = a/b;
printf("a/b = %d \n",c);
c = a%b;
printf("Remainder when a divided by b = %d \n",c);
return 0;
}
Increment and Decrement
Operators
Increment and Decrement Operators are useful
operators generally used to minimize the
calculation, i.e. ++x and x++ means x=x+1 or -
x and x−−means x=x-1. But there is a slight
difference between ++ or −− written before or
after the operand. Applying the pre-increment
first add one to the operand and then the result
is assigned to the variable on the left whereas
post-increment first assigns the value to the
variable
Operatoron the Description
left and then increment the
operand. ++ Increment
−− Decrement
// Working of increment and decrement operators
#include <stdio.h>
int main()
{
int a = 10, b = 100;
float c = 10.5, d = 100.5;
return 0;
}
Output:
++a=11
--b=99
++c=11.50
--d=99.50
Relational operators
Relational operators are used to comparing two
quantities or values
Operator Description
== Is equal to
!= Is not equal to
> Greater than
< Less than
return 0;
}
Logical operators
C provides three logical operators when we test
more than one condition to make decisions. These
are: && (meaning logical AND), || (meaning logical
OR) and ! (meaning logical NOT)
Operator Description
And operator. It performs logical conjunction
of two expressions. (if both expressions
&& evaluate to True, result is True. If either
expression evaluates to False, the result is
False)
Or operator. It performs a logical disjunction
on two expressions. (if either or both
||
expressions evaluate to True, the result is
True)
Not operator. It performs logical negation on
!
// Working of logical operators
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10, result;
result = (a == b) && (c > b);
printf("(a == b) && (c > b) is %d \n", result);
result = (a == b) && (c < b);
printf("(a == b) && (c < b) is %d \n", result);
result = (a == b) || (c < b);
printf("(a == b) || (c < b) is %d \n", result);
result = (a != b) || (c < b);
printf("(a != b) || (c < b) is %d \n", result);
result = !(a != b);
printf("!(a != b) is %d \n", result);
result = !(a == b);
printf("!(a == b) is %d \n", result);
return 0;
}
Bitwise Operators
C provides a special operator for bit operation
between two variables.
Operators Meaning of operators
| Bitwise OR
^ Bitwise exclusive OR
~ Bitwise complement
= a=b a=b
+= a += b a = a+b
-= a -= b a = a-b
*= a *= b a = a*b
/= a /= b a = a/b
%= a %= b a = a%b
// Working of assignment operators
#include <stdio.h>
int main()
{
int a = 5, c;
c = a; // c is 5
printf("c = %d\n", c);
c += a; // c is 10
printf("c = %d\n", c);
c -= a; // c is 5
printf("c = %d\n", c);
c *= a; // c is 25
printf("c = %d\n", c);
c /= a; // c is 5
printf("c = %d\n", c);
c %= a; // c = 0
printf("c = %d\n", c);
return 0;
}
Special Operators
C programming language also offers a few other
important operators including sizeof, comma,
pointer(*), and conditional operator (?:).
Operator Example What it does
* *a A pointer.
a? b:
?: Alternative of if-else.
statement
#include<stdio.h>
int main()
{
int number = 10, *pointer;
int another_number=13;
pointer=&number;
printf("int is: %d bytes\n", sizeof(int));
printf("Example of pointer and reference operator!\n");
printf("Memory address: %d\n",pointer);
printf("Example of condition operator!\n");
(another_number>14)? (printf("It is greater than number 14!")) : (printf("It
is less than number 14!"));
// if you put the value 15 in another_number variable then it will output>>
It is greater than number 14!
return 0;
}
Output:
int is: 4 bytes
Example of pointer and reference operator!
Memory address: 1701039932
Example of condition operator!
It is less than number 14!
Operator Precedence in C
In between operators, some have higher precedence and some
have lower precedence. For example, Division has a higher
precedence than subtraction operator.
Category Operator Associativity
Postfix () [] -> . ++ – – Left to right
+ – ! ~ ++ – – (type)* &
Unary Right to left
sizeof
Multiplicative */% Left to right
Additive +– Left to right
Shift << >> Left to right
Relational < <= > >= Left to right
Equality == != Left to right
Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right
Logical AND && Left to right
Logical OR || Left to right
Conditional ?: Right to left
= += -= *= /= %=>>=
Assignment Right to left
<<= &= ^= |=
EXPRESSION AND
C Expressions:
STATEMENTS
An expression is a combination
of constants and variables interconnected by one or more operators. An
expression consists of one or more operands and one or more operators.
Operands are values and operators are symbols that represent
particular actions.
Examples of Expressions:
num1 + num2 // variables num1 and num2 are operands and + is
the operator used.
x = y // the assignment operator (=) is used to assign the value
stored in y to x.
a = b + c // the value of the expression (b + c) is assigned to a.
x <= y // the lesser-than-equalTo (<=) the relational operator
compares the values of x and y and returns either 1 (true) or 0
(false).
a++ // the value of variable a is incremented by 1 i.e, this
expression is equivalent to a = a + 1.
TYPES OF EXPRESSION
Arithmetic Expressions
Addition (+), Subtraction(-), Multiplication(*), Division(/),
Modulus(%), Increment(++) and Decrement(–) operators are
said to “Arithmetic expressions”. This operator works in
between operands. like A+B, A-B, A–, A++ etc.
#include <stdio.h>
int main()
{
int a,b,result;
printf("Enter 2 numbers for Arithmetic operation \n");
scanf("%d\n%d",&a,&b);
result = a+b;
printf("Addition of %d and %d is = %d \n",a,b,result);
result = a-b;
printf("Subtraction of %d and %d is = %d \n",a,b,result);
result = a*b;
printf("Multiplication of %d and %d is = %d \n",a,b,result);
result = a/b;
printf("Division of %d and %d is = %d \n",a,b,result);
result = a%b;
printf("Modulus(Remainder) when %d divided by %d = %d \n",a,b,result);
int c=a;
result = a++;
printf("Post Increment of %d is = %d \n",c,result);
result = ++a;
printf("Pre Increment of %d is = %d \n",c,result);
result=a--;
printf("Post decrement of %d is = %d \n",c,result);
result=--a;
printf("Pre decrement of %d is = %d \n",c,result);
printf("==========================================");
Relational expression
== (equal to), != (not equal to), > (greater than), < (less than),
>= (greater than or equal to), <= (less than or equal to) operators are
said to “Relational expressions”.This operators works in between
operands. Used for comparing purpose. Like A==B, A!=B, A>B, A<B etc.
#include <stdio.h>
Example:
int main()
{
int x=4;
if(x%2==0)
{
printf("The number x is even");
}
else
printf("The number x is not even");
return 0;
}
Logical Expressions
&&(Logical and), ||(Logical or) and !(Logical not) operators are said
to “Logical expressions”. Used to perform a logical operation. This
operator works in between operands. Like A&&B, A||B,A!B etc.
Example:
#include <stdio.h>
int main()
{
int x = 4;
int y = 10;
if ( (x <10) && (y>5))
{
printf("Condition is true");
}
else
printf("Condition is false");
return 0;
}
Conditional Expressions
?(Question mark) and :(colon) are said to “Conditional
expressions”. Used to perform a conditional check. It has 3 expressions
first expression is condition. If it is true then execute expression2 and if
it is false then execute expression3. Like (A>B)?”A is Big”:”B is Big”.
Example:
#include<stdio.h>
#include<string.h>
int main()
{
int age = 25;
char status;
status = (age>22) ? 'M': 'U';
if(status == 'M')
printf("Married");
else
printf("Unmarried");
return 0;
C Statements
A statement is an instruction given to the computer
to perform an action.
There are three different types of statements in C:
Expression Statements
Compound Statements
Control Statements
An expression statement or simple
statement consists of an expression followed by a
semicolon (;).
Examples of expression statements:
a = 100; b = 20; c = a / b;
COMPOUND STATEMENTS
while (condition)
{
statements;
}
Print numbers from 1 to 5
#include <stdio.h>
int main()
{
int i = 1;
while (i <= 5)
{
printf("%d\n", i);
++i;
}
return 0;
}
Output:
1
2
3
4
5
Do-While
The do..while loop is similar to the while loop with one
important difference.
The body of do...while loop is executed at least once. Only
then, the test expression is evaluated.
The syntax:
do
{
// Statement block
}
while (Condition);
Example program:
#include <stdio.h>
int main ()
{
/* local variable definition */
int a = 10;
/* do loop execution */
Do
{
printf("value of a: %d\n", a);
a = a + 1;
}
while( a < 20 );
return 0;
}
FUNCTIONS
A function is a block of code that performs a
specific task.
There are two types of functions in C programming:
2 Changes made inside the function is limited Changes made inside the function
to the function only. The values of the actual validate outside of the function also.
parameters do not change by changing the The values of the actual parameters do
formal parameters. change by changing the formal
parameters.
3 Actual and formal arguments are created at Actual and formal arguments are
the different memory location created at the same memory location
C Library Functions
SN Header file Description
1 stdio.h This is a standard input/output header file. It contains all the library
functions regarding standard input/output.
2 conio.h This is a console input/output header file.
3 string.h It contains all string related library functions like gets(), puts(),etc.
4 stdlib.h This header file contains all the general library functions like
malloc(), calloc(), exit(), etc.
5 math.h This header file contains all the math operations related functions
like sqrt(), pow(), etc.
6 time.h This header file contains all the time-related functions.
7 ctype.h This header file contains all character handling functions.
8 stdarg.h Variable argument functions are defined in this header file.
9 signal.h All the signal handling functions are defined in this header file.
10 setjmp.h This file contains all the jump functions.
11 locale.h This file contains locale functions.
12 errno.h This file contains error handling functions.
13 assert.h This file contains diagnostics functions.
Benefits of functions
a)To improve the readability of code.
b) Improves the reusability of the code, same function
can be used in any program rather than writing the same
code from scratch.
c) Debugging of the code would be easier if you use
functions, as errors are easy to be traced.
d) Reduces the size of the code, duplicate set of
statements are replaced by function calls.
Recursive Function
Recursion is the process of repeating items in a self-
similar way. In programming languages, if a program
allows you to call a function inside the same function,
then it is called a recursive call of the function.
Pseudocode for writing any recursive function
void recursion()
{
recursion();
/* function calls itself */
}
int main()
{
recursion();
Example of recursion in C
#include <stdio.h> int main()
unsigned long long int {
factorial(unsigned int i) int i = 12;
{ printf("Factorial of %d is
if(i <= 1) %d\n", i, factorial(i));
{ return 0;
return 1; }
} Output:
return i * factorial(i - 1); Factorial of 12 is
} 479001600
Benefits of recursive
function
Used to create clearer and simply versions of several
algorithms
Memory occupied when the recursive function is called
is very less compared to an ordinary function
Recursion is a very useful way of creating and
accessing certain dynamic data structures such as
linked lists, stacks, queues etc…
ARRAYS
Arrays a kind of data structure that can store a
fixed-size sequential collection of elements of the
same type.
An array is used to store a collection of data, but it
is often more useful to think of an array as a
collection of variables of the same type.
A specific element in an array is accessed by an
index.
All arrays consist of contiguous memory locations.
The lowest address corresponds to the first
element and the highest address to the last
element.
Declaring Arrays
To declare an array in C, a programmer specifies
the type of the elements and the number of
elements required by an array as follows
type arrayname [ arraysize];
This is called a single-dimensional array. The
arraySize must be an integer constant greater
than zero and type can be any valid C data type.
For example, to declare a 10-element array called
balance of type double, use this statement
double balance[10];
Initializing Arrays
You can initialize an array in C either one by one or
using a single statement as follows
double balance[5]={1000.0,2.0,3.4,7.0,50.0};
The number of values between braces { } cannot
be larger than the number of elements that we
declare for the array between square brackets [ ].
If you omit the size of the array, an array just big
enough to hold the initialization is created.
Therefore,
double balance[]={1000.0,2.0,3.4,7.0,50.0};
Accessing Array Elements
int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},
Two-dimensional array
#include<stdio.h>
arr[0][0] = 1
int main(){
int i=0,j=0;
arr[0][1] = 2
int arr[4][3]={{1,2,3},{2,3,4}, arr[0][2] = 3
{3,4,5},{4,5,6}}; arr[1][0] = 2
//traversing 2D array arr[1][1] = 3
for(i=0;i<4;i++)
{
arr[1][2] = 4
for(j=0;j<3;j++) arr[2][0] = 3
{ arr[2][1] = 4
printf("arr[%d] [%d] = %d \ arr[2][2] = 5
n",i,j,arr[i][j]);
arr[3][0] = 4
}//end of j
}//end of i arr[3][1] = 5
return 0; arr[3][2] = 6
}
Multi-dimensional Arrays in C
C programming language allows multidimensional
arrays. Here is the general form of a
multidimensional array declaration
type name[size1][size2]...[sizeN];
The following declaration creates a three dimensional
integer array
int threedim[5][10][4];