UNIT-1 Part 1 C Programming
UNIT-1 Part 1 C Programming
UNIT-1 Part 1 C Programming
Introduction
• The C programming language was developed
at Bell Laboratories in 1972 by Dennis Ritchie.
• C programming language features were
derived from an earlier language called “B”
(Basic Combined Programming Language –
BCPL)
• C language was invented for implementing
UNIX operating system
Cont..
• In 1978, Dennis Ritchie and Brian Kernighan
published the first edition “The C Programming
Language” and commonly known as K&R C
• In 1983, the American National Standards
Institute (ANSI) established a committee to
provide a modern, comprehensive definition of C.
The resulting definition, the ANSI standard, or
“ANSI C”, was completed late 1988.
C is Middle Level Language
void main()
{
printf("Hello World! ");
}
Flow of c program execution
Data Types
• Alphabets: A → Z Or a → z
• Digits: 0,1,2…..9
• Special symbols: ~ ‘ @ & % _ { etc…
Cont…
Signed character Unsigned character
• Occupies 1 byte in memory • Occupies 1 byte in memory
float is used for decimal numbers. These are structure is used to group items of possibly
classified as float, double and long double. different types into a single type.
void is used where there is no return value It is like structure but all members in union
required. share the same memory location
2016
What are the basic data types and their
associated range? (2.5 marks)
2015
What do you mean by the term data type?
Explain any four data types with examples.(2.5
marks)
Swapping of two numbers using temporary variable
#include<stdio.h>
int main()
{
int a, b, temp;
printf("Enter two numbers a and b ");
scanf("%d %d", &a, &b);
temp = a;
a = b;
b = temp;
printf("\n After swapping \na = %d\nb = %d\n", a, b);
return 0;
}
Write a program to swap two numbers without using third
variable.(2.5 marks)
#include<stdio.h>
int main()
{
int a, b;
printf("Enter Two Numbers:\n");
scanf("%d%d",&a,&b);
printf("Before swap a=%d b=%d",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("\nAfter swap a=%d b=%d",a,b);
return 0;
}
Token
The smallest unit in a program/statement is called
as Token. A C program consists of various tokens.
A token can be classified as:
• Keyword
• Variable
• Constant
• Operators
• Special Characters
• Strings
Example:
The following C statement consists of five tokens −
printf("Hello, World! \n");
printf
(
"Hello, World! \n"
)
;
Keyword
• The words which are reserved by C compiler
for some specific purpose are called
Keywords.
• Each keyword has its own significance and is
used for some predefined purpose. These
reserved words may not be used as constants
or variables or any other identifier names.
• There are 32 keywords in total.
List of keywords
Variable
• C variable is a named location in a memory
where a program can manipulate the data.
• The data is stored in the memory, and at the
time of execution it is fetched for carrying out
different operations on it.
• The variable value keeps on changing at
different times during program execution.
• C variable might be belonging to any of the
data type like int, float, char etc. Variables are
case sensitive.
Example:
void main()
{
int roll_no=10;
float marks=75.5;
printf(“%d%f”,roll_no,marks);
}
}
Global Variable
Global variables are defined outside a function,
usually on top of the program.
Global variables hold their values throughout the
lifetime of your program and they can be
accessed inside any of the functions defined for
the program.
Example:
#include <stdio.h>
int main () {
/* actual initialization */
a = 10;
b = 20;
g = a + b;
return 0;
}
Identifier
• int age;
• const pi;
• float marks;
• Here,
Line 3 is declaration.
Line 4 is initialization.
Identifier Vs Variable
Basis Identifier Variable
Use Identifier is used to name a variable, Variable is used to name a memory location,
function, class, structure, union etc. which holds a value.
Purpose Created to give a unique name to an entity. Allots unique name to a particular memory
location.
Range All identifiers are not variables. All variables are identifier.
Void main()
{
int a=10;
int b;
b=a*10;
}
Here b is initialized dynamically.
Semicolons
Example:
/* my first program in C */
You cannot have comments within comments
White space
• Whitespace is the term used in C to describe
blanks, tabs, newline characters and
comments.
• Whitespace separates one part of a statement
from another and enables the compiler to
identify where one element in a statement,
such as int, ends and the next element begins.
Example:
int age;
there must be at least one whitespace character
(usually a space) between int and age for the
compiler to be able to distinguish them. On
the other hand, in the following statement −
fruit = apples + oranges; // get the total fruit
no whitespace characters are necessary
between fruit and =, or between = and apples,
Constant
• Constants refer to fixed values that the program
may not alter.
• Constants can be of any of the basic data types.
• The way each constant is represented depends
upon its type.
• Constants are also called literals.
• Eg. const int width = 5;
Types
Constant
Numeric Character
Constant Constant
• Undefined variable
• Missing semicolon
• Function call missing
• Function should have prototype
Linker errors
The program must be linked to the ‘C’ library. If
it fails in such case these errors are raised .
most common errors are:
• Divide by zero
• Null value
• Garbage result in printing.
Execution of C program
Compiling a C program is a multi-stage process.
At an overview level, the process can be split
into four separate stages:
• Creation,
• Preprocessing,
• Compilation and linking,
• Executing the program.
Creation of program
• Arithmetic Operators
• Relational Operators
• Logical Operators
• Bitwise Operators
• Assignment Operators
• Misc Operators
Arithmetic Operators
Binary operators: (+ - * / %)
• The operators which work upon more than
one operand are called binary operator.
Increment(++)
• The ‘++’ operator is used to increment the
value of an integer. When placed before the
variable name (also called pre-increment
operator), its value is incremented instantly.
For example, ++x.
• And when it is placed after the variable name
(also called post-increment operator), its value
is preserved temporarily until the execution of
this statement and it gets updated before the
execution of the next statement. For
example, x++.
Pre/Post increment
#include<stdio.h>
#include<conio.h>
int main() {
int i = 0, j = 0;
j = i++ + ++i;
printf("%d\n", i);
printf("%d\n", j);
}
decrement(--)
• The ‘ – – ‘ operator is used to decrement the value of an
integer. When placed before the variable name (also called
pre-decrement operator), its value is decremented
instantly. For example, – – x.
• And when it is placed after the variable name (also called
post-decrement operator), its value is preserved
temporarily until the execution of this statement and it
gets updated before the execution of the next statement.
For example, x – –.
Increment/ decrement program
#include<stdio.h>
#include<conio.h>
int main()
{
int x = 12, y = 1;
clrscr();
printf("Initial value of x = %d\n", x); // print the initial value of x
printf("Initial value of y = %d\n\n", y); // print the initial value of y
y = x++; // use the current value of x then increment it by 1
printf("After incrementing by 1: x = %d\n", x);
printf("y = %d\n\n", y);
y = x--; // use the current value of x then decrement it by 1
printf("After decrementing by 1: x = %d\n", x);
printf("y = %d\n\n", y);
return 0;
}
Modulo Operator
• The modulo operator, denoted by %, is an arithmetic
operator.
• The modulo division operator produces
the remainder of an integer division.
• Syntax: If x and y are integers, then the expression: x
% y. produces the remainder when x is divided by y.
Return Value:
• If y completely divides x, the result of the expression is
0.
• If x is not completely divisible by y, then the result will
be the remainder in the range [1, x-1].
• If x is 0, then division by zero is a compile-time error.
• The % operator cannot be applied to floating-point
numbers i.e float or double.
Example of Modulo Operator
#include <stdio.h>
int main(void)
{
int x, y, result;
x = 3;
y = 4;
result = x % y;
printf("%d", result);
result = y % x;
printf("\n%d", result);
x = 4;
y = 2;
result = x % y;
printf("\n%d", result);
return 0;
}
Unary minus (-) Operator
• This operator makes the value negative.
• It makes positive value to negative and
negative value to positive.
#include <stdio.h>
int main()
{
int x=10;
int y=-20;
printf("value of -x: %d\n",-x);
printf("value of -y: %d\n",-y);
return 0;
}
Relational Operator
Relational operators are used to provide the
relationship between two expressions.
If the relation is true then it returns a value 1
otherwise 0 for false.
List of Relational operators
Relational operator
#include <stdio.h>
int main()
{
int a = 5, b = 6;
printf("\n%d", a == b);
printf("\n%d", a != b);
printf("\n%d", a > b);
printf("\n%d", a < b);
printf("\n%d", a >= b);
printf("\n%d", a <= b);
return 0;
}
Logical Operators
The operators which are used to test relation
between two or more variables or
expressions. It provides logical true 1, if the
condition is true and 0 otherwise.
List 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
#include<stdio.h>
int main()
{
int num1,num2,output;
clrscr();
printf("\n enter the value of num1=");
scanf("\n %d",&num1);
printf("\n enter the value of num2=");
scanf("\n %d",&num2);
output= (num1 & num2);
printf("\n output=%d",output);
getch();
}
Program for bit wise operator shift left
#include<stdio.h>
#include<conio.h>
int main()
{
int num1,num2;
printf("\n enter the value of num1=");
scanf("\n %d",&num1);
num2=num1<<2;
printf("\n %d",num2);
getch();
}
Bitwise example
#include <stdio.h>
int main() {
int a = 20; /* 20 = 010100 */
int b = 21; /* 21 = 010101 */
int c = 0;
c = a & b; /* 20 = 010100 */
printf("AND - Value of c is %d\n", c );
c = a | b; /* 21 = 010101 */
printf("OR - Value of c is %d\n", c );
c = a ^ b; /* 1 = 0001 */
printf("Exclusive-OR - Value of c is %d\n", c );
getch(); }
Assignment Operator
The assignment operator is used to assign value to a
variable.
• Comma operator
• Conditional operator
• sizeof operator
Comma operator
• The comma operator is used to separate two
or more expressions. It has lowest priority.
Eg.
• a=10,b=20,c=30;
• (a=10,b=20,c=a+b)
Conditional/Ternary Operator ( ? : )
The conditional operator contains condition
followed by two statements or values. If the
condition is true, the first statement is
executed, otherwise the second statement is
executed.
Syntax:
Condition ? Expression 1 : Expression 2
Eg. printf(2==3?4:5);
Ternary Operator Example
#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, c, large;
clrscr();
printf("Enter any three number: ");
scanf("%d%d%d",&a,&b,&c);
large=a>b ? (a>c?a:c) : (b>c?b:c);
printf("Largest Number is: %d",large);
getch();
}
sizeof operator
This operator is used to find out the size
occupied by a variable.
Eg.
int a;
Printf(“%d”,sizeof(a));
Ans. 2
Priority of operators
2019
Discuss the various operators in C. (5 marks)
2017
What are different type of operators in c?
Briefly explain. (6 marks)
2016
Define operators. What are the different types
of operators (2.5 marks)
2017
Explain bitwise operators available in C. (2.5
marks
2016
what are bitwise operators?( 2.5 marks)
Enumeration (or enum) in C
• Enumeration (or enum) is a user defined data
type in C.
• It is mainly used to assign names to integral
constants, the names make a program easy to
read and maintain.
#include<stdio.h>
int main()
{
enum week day;
day = Wed;
printf("%d",day);
return 0;
}
2015
• What is Enumerated Data type? Explain with
suitable example.(2.5 marks)
Type Casting
output:- 80.000000
Backslash Character Constants in C:
\b Backspace
\f Form feed
\n New line
\r Carriage return
\t Horizontal tab
\” Double quote
\’ Single quote
\\ Backslash
\v Vertical tab
\a Alert or bell
\? Question mark