UNIT-1 Part 1 C Programming

Download as pdf or txt
Download as pdf or txt
You are on page 1of 120

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

C is often called a middle-level language


because it combines the best elements of low-
level or machine language with high-level
languages.
C is Structured Programming
Language
C is a structured programming language, which
means that it allows you to develop programs
using well-defined control and provides
modularity (breaking the task into multiple
sub tasks that are simple enough to
understand and to reuse).
Features of C Language
➢Portability
• C Programs are portable i.e they can be run on
any Compiler with Little or no Modification
➢Powerful
• Provides Wide verity of ‘Data Types‘
• Provides Wide verity of ‘Functions’
• Provides useful Control & Loop Control
Statements
➢Bit Manipulation
C Programs can be manipulated using bits. We
can perform different operations at bit level.
We can manage memory representation at bit
level
➢Modular Programming
Modular programming is a software design
technique that increases the extent to which
software is composed of separate parts, called
modules
C Program Consist of Different Modules that
are integrated together to form complete
program
➢Efficient Use of Pointers
Pointers has direct access to memory.
C Supports efficient use of pointer.
➢Memory Management
Various memory management in-built
functions are available in C language which
helps to save memory and hence improve
efficiency of the program. e.g. malloc(),alloc()
and calloc().
➢Easy Debugging
The syntax errors can be easily detected by
the C compiler. The error is displayed with line
number of the code and the error message.
➢Rich set of Library Functions
C has a rich set of library functions. These are
the functions that provide readymade
functionality to the users. It also supports
graphic programming.
Applications of C Language
• C programming language can be used to design the system
software like operating system.
• To develop application software like database and spread
sheets.
• For Develop Graphical related application like computer and
mobile games.
• To evaluate any kind of mathematical equation use c language.
• C programming language can be used to design the compilers.
• UNIX Kernal is completely developed in C Language.
• For Creating Compilers of different Languages which can take
input from other language and convert it into lower level
machine dependent language.
• C programming language can be used to design Network
Devices.
The following is a partial list of areas where C
language is used:
Ø Embedded Systems
Ø Systems Programming
Ø Artificial Intelligence
Ø Industrial Automation
Ø Computer Graphics
Ø Space Research
Ø Image Processing
Ø Game Programming
Benefits of C language
• As a middle-level language, C combines the features of
both high-level and low-level languages. It can be used for
low-level programming, such as scripting for drivers and
kernels and it also supports functions of high-level
programming languages, such as scripting for software
applications etc.
• C is a structured programming language which allows a
complex program to be broken into simpler programs called
functions. It also allows free movement of data across
these functions.
• Various features of C including direct access to machine
level hardware APIs, the presence of C compilers,
deterministic resource use and dynamic memory allocation
make C language an optimum choice for scripting
applications and drivers of embedded systems.
• C language is case-sensitive which means lowercase and
uppercase letters are treated differently.
Benefits of C language
• C is highly portable and is used for scripting system
applications which form a major part of Windows,
UNIX, and Linux operating system.
• C is a general-purpose programming language and can
efficiently work on enterprise applications, games,
graphics, and applications requiring calculations, etc.
• C language has a rich library which provides a number
of built-in functions. It also offers dynamic memory
allocation.
• C implements algorithms and data structures swiftly,
facilitating faster computations in programs. This has
enabled the use of C in applications requiring higher
degrees of calculations like MATLAB and Mathematica.
A sample program
#include <stdio.h>

void main()
{
printf("Hello World! ");
}
Flow of c program execution
Data Types

• All C compilers support a variety of data types.


• This enables the programmer to select the
appropriate data types as per the need of the
application.
• The type of a variable determines how much
space it occupies in storage.
Classification of Data Types
C Data types can be classified as follows:

• Basic Data Types


• Derived Data Types
• Void Data Type
• Basic data types: integer (int), character
(char), floating point (float), double floating
point (double).
• Derived data types: pointers, arrays, structure,
union and typedef.
• Void data type: no value
Integer
• The integer type has a value without any
decimal.

All C compilers offers different types:


• short integer and long integer.
• Signed integer and Unsigned integer
• When variable is declared without short or
long keyword, the default is short signed int.
Cont…
Short integer Long integer
• Occupies 2 bytes • Occupies 4 bytes

• Range: -32768 to 32767 • Range: -2147483648 to


2147483647
• Program runs faster
• Program runs slower
• Format specifier: %d or %i
• Format specifier: %ld
Cont...
Signed Integer Unsigned Integer
• Occupies 2 bytes • Occupies 2 bytes

• Range: -32768 to 32767 • Range: 0 to 65535

• Format specifier: %d or %i • Format specifier: %u

• Eg. long int b=2 • Eg. unsigned long b=4562


Char
• A character denotes any alphabet, digit or
special symbol used to represent information.
Like:

• 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

• Range: -128 to 127 • Range: 0 to 255

• Format specifier: %c • Format specifier: %c

• Eg. char ch=‘B’ • Eg. Unsigned char=‘b’


Float and Double
• A floating-point literal has an integer part, a
decimal point, a fractional part, and an exponent
part. You can represent floating point literals
either in decimal form or exponential form.

• While representing decimal form, you must


include the decimal point, the exponent, or both;
and while representing exponential form, you
must include the integer part, the fractional part,
or both. The signed exponent is introduced by e
or E.
Cont…
float double
• Occupies 4 bytes • Occupies 8 bytes

• Range: 3.4e-38 to 3.4e+38 • Range: 1.7 e-308 to +308

• Format specifier: %f • Format string: %lf

• Eg. Float y • Eg. Double y


Format Specifier
• %d integer variable
• %c character variable
• %f float variable
• % lf double variable
• %s string variable
• %o octal value
• %x hexadecimal value
Summary
Data Type Size Range Format
String
char 1 -128 to 127 %c
unsigned char 1 0 to 255 %c
short or int 2 -32768 to 32767 % i or % d
unsigned int 2 0 to 65535 %u
long 4 -2147483648 to 2147483647 % ld
unsigned long 4 0 to 4294967295 % lu
float 4 3.4 e-38 to 3.4 e+38 %f
double 8 1.7 e-308 to 1.7 e+308 % lf
long double 10 3.4 e-4932 to 1.1 e +4932 % lf
Difference between Fundamental Data Types and
Derived Data Types
FUNDAMENTAL DATA TYPES DERIVED DATA TYPES
Fundamental data type is also called
Derived data type is the aggregation of
primitive data type. These are the basic data
fundamental data type.
types.
character, integer, float, and void are Pointers, arrays, structures and unions are
fundamental data types. derived data types.

Character is used for characters. It can be


Pointers are used for storing address of
classified as
variables.
char, Signed char, Unsigned char.

Integer is used for integers( not having


decimal digits). It can be classified as signed
Array is used to contain similar type of data.
and unsigned. Further classified as int, short
int and long int.

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");

The individual tokens are −

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);
}

Here, roll_no and marks are variables.


Variable Declaration Vs Variable
Definition
S.no Variable declaration Variable definition

Declaration tells the compiler


Definition allocates memory for the
1 about data type and size of the
variable.
variable.
Variable can be declared many It can happen only one time for a
2
times in a program. variable in a program.
The assignment of properties and Assignments of storage space to a
3
identification to a variable. variable.
Rules for declaring Variable
• Variable name must begin with letter or
underscore.
• They can be constructed with digits, letters.
• It can be a combination of lower case and
upper case letters.
• No special symbols are allowed other than
underscore.
• The variable should not be a keyword.
Examples:
• int a;
• int roll_no;
• int su+m;
• int t@tal;
• int p%age;
• int a123;
• int 123a;
• int AbCd;
Examples:
• int a; Valid
• int roll_no; Valid
• int su+m; Invalid
• int t@tal; Invalid
• int p%age; Invalid
• int a123; Valid
• int 123a; Invalid
• int AbCd; Valid
Scope of Variable
• A scope in any programming is a region of the
program where a defined variable can have its
existence and beyond that variable it cannot
be accessed.
• There are three places where variables can be
declared in C programming language −
• Inside a function or a block which is called
local variables.
• Outside of all functions which is called global
variables.
Local Variables

• Variables that are declared inside a function or


block are called local variables.
• They can be used only by statements that are
inside that function or block of code.
• Local variables are not known to functions
outside their own.
Example:
#include <stdio.h>
int main ()
{
int a,b;
int c;
a = 10;
b = 20;
c = a + b;
printf ("value of a = %d, b = %d and c = %d\n", a, b, c);

}
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>

/* global variable declaration */


int g;

int main () {

/* local variable declaration */


int a, b;

/* actual initialization */
a = 10;
b = 20;
g = a + b;

printf ("value of a = %d, b = %d and g = %d\n", a, b, g);

return 0;
}
Identifier

• A symbolic name used to refer to a variable,


constant, function, structure etc. Identifiers are
user defined names.

• int age;
• const pi;
• float marks;

• Here age, pi and marks are identifiers.


Declaring & initializing C variable

• Variables should be declared in the C program


before use.
• Memory space is not allocated for a variable
while declaration.

• It happens only on variable definition.

• Variable initialization means assigning a value to


the variable.
Example:
1. void main()
2. {
3. int a,b;
4. a=10,b=20;
5. }

• 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.

Eg. int a; int a;


Or float a;
int a()
{
}
Dynamic Initialization
The initialization of variable at run time is called dynamic initialization.
Dynamic refers to the process during execution.
Eg.

Void main()
{
int a=10;
int b;

b=a*10;
}
Here b is initialized dynamically.
Semicolons

In a C program, the semicolon is a statement


terminator. That is, each individual statement
must be ended with a semicolon. It indicates
the end of one logical entity.
Eg.
printf(“Hello”);
Comments

Comments are like helping text in your C program


and they are ignored by the compiler
There are two types of comments:
// single line
/* double line */

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

Integer Real Character String


constant constant constant constant
• Integer Constant: these constants are
represented with whole numbers.
Eg. 10,20, -50

• Real Constant/Floating Constant: these are


represented in exponential or fraction form.
Eg. 2.5, 5.521, 2.4561 X e+3
• Character Constant: Character constants are enclosed
between single quotes.
• For example, 'a' and '%' are both character constants.
• C defines both multibyte characters, which consist of
one or more bytes, and wide characters (which are
usually 16 bits long).
• Multibyte and wide characters are used primarily to
represent languages that have large character sets.
• To specify a multibyte character, enclose the characters
within single quotes, for example, 'xy'.
• To specify a wide character constant, precede the
character with an L.

• String Constant: It contains sequence of characters


enclosed in double quotes.
Eg. “Hello”, “365”, “a”
Constant Example
#include <stdio.h>
void main()
{
const int height = 100; /*int constant*/
const float number = 3.14; /*Real constant*/
const char letter = 'A'; /*char constant*/
const char backslash_char = '\?'; /*special char cnst*/
printf("value of height :%d \n", height );
printf("value of number : %f \n", number );
printf("value of letter : %c \n", letter );
printf("value of backslash_char : %c \n", backslash_char);
}
Lvalue
• lvalue − Expressions that refer to a memory location are called "lvalue" expressions.
• An lvalue may appear as either the left-hand or right-hand side of an assignment.
• Expressions referring to modifiable locations are called “modifiable l-values“.
• A modifiable l-value cannot have an array type, an incomplete type, or a type with
the const attribute.
• For structures and unions to be modifiable lvalues, they must not have any members
with the const attribute. The name of the identifier denotes a storage location, while
the value of the variable is the value stored at that location.
• An identifier is a modifiable lvalue if it refers to a memory location and if its type is
arithmetic, structure, union, or pointer.
• For example, if ptr is a pointer to a storage region, then *ptr is a modifiable l-value
that designates the storage region to which ptr points.
• In C, the concept was renamed as “locator value”, and referred to expressions that
locate (designate) objects.
The l-value is one of the following:
• The name of the variable of any type i.e, an identifier of integral, floating, pointer,
structure, or union type.
• A subscript ([ ]) expression that does not evaluate to an array.
• A unary-indirection (*) expression that does not refer to an array
• An l-value expression in parentheses.
• A const object (a nonmodifiable l-value).
• The result of indirection through a pointer, provided that it isn’t a function pointer.
• The result of member access through pointer(-> or .)
• int a; // a is an expression referring to an
// 'int' object as l-value
a = 1;
int b = a; // Ok, as l-value can appear on right
// Switch the operand around '='
operator
9 = a;
• // Compilation error:
• // as assignment is trying to change the value of
assignment operator
Rvalue
• rvalue − The term rvalue refers to a data value
that is stored at some address in memory.
• rvalue appear on the right-hand side but not
on the left-hand side of an assignment.
Error

Error is a mistake or bug, which can be


associated with programmer in program,
common programming errors in C can be
classified into 3 types:

• 1. compilation errors.(syntax errors)


• 2. linker errors.
• 3. logical errors.
Compilation(syntax) errors
These are raised when we compile the program
and can be located and corrected easily. Most
common compilation or syntax errors are:

• 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:

• Unable to link cos.obj


• Undefined symbol
Logical ( run time) errors:
These are raised because of the due to logical
inefficiency. We can know them when program
gets executed. It is very difficult to locate
them, most common logical 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

This will be used to type your program like


Windows Notepad. The files you create with
your editor are called the source files and they
contain the program source codes. The source
files for C programs are typically named with
the extension ".c".
Execution of preprocessor

The first stage of compilation is called


preprocessing. In this stage, lines starting with
a # character are interpreted by the
preprocessor as preprocessor commands. The
preprocessor program executes first
automatically before the compilation of the
program.
Compilation and Linking

The source program contains statements that


are to be translated into object codes. These
object codes are suitable for execution by the
computer. If there is no error in the program,
compilation proceeds and translated program
stored with the .obj extension.
Cont…
Linking puts together all other program files and
functions that are required by the program.
Eg. If a program is using pow() function, then
the object code for this function should be
brought from math.h library.
Therefore, to produce an executable program,
the existing pieces have to be rearranged and
the missing ones filled in. This process is called
linking.
Executing the program

After compilation the executable code will be


loaded in the computer’s main memory is
executed. The loader performs this function.
This executes your program, printing any
results to the screen. At this stage there may
be run-time errors.
If so, you must return to edit your program
source, and recompile it, and run it again.
Operators
An operator is a symbol that tells the compiler to
perform specific mathematical or logical
functions.
C language is rich in built-in operators and provides
the following types of operators −

• Arithmetic Operators
• Relational Operators
• Logical Operators
• Bitwise Operators
• Assignment Operators
• Misc Operators
Arithmetic Operators

Arithmetic operators are used to perform


numeric calculations like addition, subtraction,
multiplication, division etc.
List of arithmetic operators
Types
• Unary operators: (+ - ++ --)
The operators which work upon single operand
are called unary operator.

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

Bitwise operator works on bits and perform bit-


by-bit operation. These operators can operate
only on integer operands such as int, char,
short, long.
List of bit wise operators
WAP to use bitwise operator between two integers

#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.

Eg. int x=5;


Miscellaneous Operators
Apart from above mentioned operators there
are other operators also:

• 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>

enum week{Mon, Tue, Wed, Thur, Fri, Sat, Sun};

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

• Type casting is a way to convert a variable


from one data type to another data type. For
example, if you want to store a 'long' value
into a simple integer then you can type cast
'long' to 'int'. You can convert the values from
one type to another explicitly using the cast
operator as follows −
• (type_name) expression
Cont…
• #include <stdio.h>
• main()
• {
• int sum = 17, count = 5; double mean;
• mean = (double) sum / count;
• printf("Value of mean : %f\n", mean );
• }
Types
• Type casting is of two types:

• Implicit type casting


• Explicit type casting
Implicit conversion
• Implicit conversions do not required any
operator for converted .
• They are automatically performed when a
value is copied to a compatible type in
program .
• Here, the value of a has been promoted from
int to double and we have not had to specify
any type-casting operator.
• This is known as a standard conversion.
Example:
void main()
{
int i,j;
float f;
double d;
i=d*f+f*j;
}
Explicit conversion

• In c language , Many conversions, specially


those that imply a different interpretation of
the value, require an explicit conversion.
• We have already seen two notations for
explicit type conversion.
They are not automatically performed when a
value is copied to a compatible type in
program.
Example:
void main()
{
int m1=70,m2=70,m3=100,total;
float per;
total=m1+m2+m3;
per=(float)total/300*100;
printf("%f",per);
}

output:- 80.000000
Backslash Character Constants in C:

• There are some characters which have special


meaning in C language.
• They should be preceded by backslash symbol
to make use of special function of them.
• Given below is the list of special characters
and their purpose.
Backslash_character Meaning

\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

\N Octal constant (N is an octal constant)

\XN Hexadecimal constant (N – hex.dcml cnst)


Advantages of C
• Procedure Oriented Language
• Lots of Libraries
• Speed of Compilation
• Easy to Learn (Syntax is near to English
Language)
• Portable
Disadvantages of C
• Object Oriented Programming Features
(OOPS)
• Run Time Type Checking is Not Available
• Namespace Feature
• Constructor and Destructor is not available

You might also like