0% found this document useful (0 votes)
5 views

FC Unit 2 notes

C is a high-level programming language developed in 1972 by Dennis Ritchie and Brian Kernighan, known for its general and specific purpose applications. It features a structured programming approach, a rich set of library functions, and is portable across different operating systems. C's syntax is relatively simple, making it easy to read and maintain, and it serves as the foundation for many other programming languages.

Uploaded by

a01020541
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

FC Unit 2 notes

C is a high-level programming language developed in 1972 by Dennis Ritchie and Brian Kernighan, known for its general and specific purpose applications. It features a structured programming approach, a rich set of library functions, and is portable across different operating systems. C's syntax is relatively simple, making it easy to read and maintain, and it serves as the foundation for many other programming languages.

Uploaded by

a01020541
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

C - Programming

Unit 2 C - Programming
What is C…?
C is a general purpose and specific purpose high level programming language developed by
Dennis Ritchie and Brian Kernighan in 1972 at “AT & T” Bell Laboratories of USA.
Introduction
C is a high level programming language developed by Dennis Ritchie and Brian Kernighan in
the year 1972 at Bell Telephone Laboratory, USA (now it is AT & T Bell Lab). It is both
general and specific purpose programming language. Now C has become one of the most
commonly used programming language for developing system software and application
software’s.
C is relatively a small programming language. You need not to remember many C keywords
before you start to write programs in C to solve problems. That’s why „C‟ language is called
the lowest high-level programming language. Many other high-level languages have been
developed based on C. Perl is a popular programming language in web designing across the
Interne. Perl actually borrows a lot of features from C. If you understand C, learning Perl is a
snap. Another example is the C++ language, which is simply an extended version of C.
History of C
By 1960 many computer languages came into existence, almost each for a specific purpose.
For example, COBOL was being used for commercial applications, FORTAN for
engineering and scientific applications and so on. At this stage people started thinking that
instead of learning and using so many languages, each for a different purpose, why not use
only one language, which can perform all possible applications.
Therefore, a new language called Combined Programming Language (CPL) was developed
at Cambridge University. CPL turned out to be so big, having so many features, that it was
hard to learn and difficult to implement. In 1967, Basic Combined Programming Language
(BCPL) was developed by Martin Richards at Cambridge University aimed to solve this
problem by CPL down to its basic good features. But unfortunately it turned out to be too
less powerful and too specific. In 1970 a language called B was written by Ken Thompson
at Bell Lab, as a further simplification of CPL and took first character of BCPL for
naming his new language.
In 1972, Dennis Ritchie developed a new language called C by inheriting some of the
features of B and BCPL, added some of his own. He selected the name C for his new
language because C comes after B in the alphabetical order, which indicates advancement to
B. Some sources also said that Dennis Ritchie selected the 2nd character of BCPL for
naming his language. C runs under various operating systems including MS-DOS, UNIX,
Windows etc. In 1978, Dennis Ritchie and Brian Kernighan jointly published a detailed
description of the C language document. It is known as K & RC. C was exclusively used
within Bell Laboratory before it was released to public and commercial applications until
1978.

Page
C - Programming

Year Language Developed by Remarks


Hard to learn, difficult to implement
1963 CPL Cambridge University
Martin Richards at Cambridge Could deal with only specific
1967 BCPL University problem
Ken Thompson at AT& T‟s Bell Could deal with only specific
1970 B labs problems
Inherits some features
Dennis Ritchie at AT & T‟s Bell
1972 C of B and BCPL with its own
labs
features
Features or Characteristics of C
1. C is a general purpose and as well as specific purpose high level programming
language.
2. C is a structured programming language. The programmers can easily divide a
problem into a number of modules or functions. These functions makes programmer
to understanding, debugging and testing of the program easier.
3. The C compiler has assembly level and high level language features. Therefore it is
well suited for developing both systemsoftware and as well as application software.
4. C has rich set of library functions. These are the functions that provide readymade
functionality to the users. So these make user easy to write any type of complex
programs. C also supports graphic programming.
5. C has rich set of operators and data types. So the programs written in C are more
efficient.
6. C has very less number of keywords or reserved words. It has only 32 keywords. It
is very less compared to other languages.
7. C is a case sensitive language. The words which are written in upper case and lower
both are significant in C.
8. C has no rigid format. Any number of statements can be typed in a single line.
9. Portability: The programs written using C in one system can be run in another
system with little or no modifications.
10. The errors in the program can be easily detected by the C compiler. The error is
displayed with line number and the error message.
11. C program has the ability to extend itself by adding our own functions to its
library.
12. C allows reusability of modules. This can be achieved by splitting the C program
into number of functions instead of repeating the same logic statements.

Merits or Advantages of C
C is the most popular programming language, it has the following advantages:
1. Readability: C programs are very easy to read and understand. Because the program
syntax is easy and which contains English like statements.
2. Maintainability: C programs are easy to maintain. Because the program is divided into a

Page
C - Programming

number of modules and functions. That makes the programmer to easy to debug, test and
maintain.
3. Portability: C programs written in one computer can be ported to other computer easily
with little or no modifications.
4. Reusability: Programs written in C can be reused. We can save our C programs into a
library file and invoke them in our next programs simply by including the library file.
5. Assembly Code: We can write Assembly code in C. But, its impossible to write C code in
Assembly language.

Basic Structure of a C Program


The Structure of C program is defined by set of rules called protocols to be followed by the

Documentation
Preprocessor Statements
Global Declarations
main()
{
declaration;
statements;
}
user defined functions
programmer while writing C program. All C programs will be having the following
structure.

Documentation
In this section we can give comments about the program, creation or modified date,
author name etc. The statements which are followed by “//” (single line comment) and
the statements which are enclosed between “/*” and “*/” (multi line comment) will be
ignored by the C compiler during compilation and execution.
Preprocessor Statements
The Preprocessor statements begin with # symbol and are also called preprocessor
directives. These statements tell the C compiler to include header files and symbolic
constants into a C program.
Some of the preprocessor statements are:
#include<stdio.h> : for the standard input/output functions. #define NULL 0 : for
defining symbolic constant, NULL=0.
Global Declarations
The variables and functions declared before the main() function are called Global
Variables, and their declarations are called Global Declarations. These variables or
functions can be used and accessed in main() function and other user defined functions.
Main()
Any C program will not execute without the main(). Every C program should contain
only one main(). Execution of C program starts from main(). This main() calls other

Page
C - Programming

built-in functions and user defined functions. And it should be written in lowercase
letters and should not be terminated by a semicolon.
Braces ({,})
Every C program contains a pair of braces ({,}). The left brace indicates the beginning
of main() function and the right indicates the end of the main() function. The braces can
also be used to indicate the beginning and end of user-defined functions and compound
statements.

Declarations
In this section of C program all the variables, arrays, functions etc., used in the C program
are declared with data types and can also be initialized.
Statements
Statements are the instructions to the computer to perform specific operations. These
statements are written according to the syntax of C to solve a particular program. The
statements may be input-output statements, arithmetic statements, control statements,
looping statements and other statements. They also include comments. Comments are
explanatory notes on some instructions.
User-defined functions
These are subprograms. A subprogram is a function. These are written by the user, hence the
name user-defined functions. A Function is a block of statements that gets executed upon
calling to perform a specific task. A program may have any number of functions which
contain local declarations and statements. Functions may be written before or after the main()
function.
A simple C Program Example:

#include<stdio.h>
main()
{
Printf(“Welcome to C programming”);
}
The above program prints only Welcome to C Programming in the output screen. The first
line tells the compiler to include standard input/output header file for accepting and printing
the data. The actual execution of program starts from main(). The program body contains
only one printf() statement. The printf() is included in stdio.h, whenever the main() function
calls printf(), then the content in the printf() will prints on the computer screen.

Characters Set
The character set is the fundamental raw material of any language and they are used to
represent information. Like natural languages, every computer programming language
also has its own set of characters to form lexical elements. The characters used in C are:
 Alphabets [A-Z] [a-z]
 Digits 0-9
 Special Characters

Page
C - Programming

The following are the special characters that are used in C.


Symbol Meaning Symbol Meaning
# Hash ( Left parenthesis
! Exclamation mark ) Right parenthesis
~ Tilde { Left brace
% Percentage } Right brace
^ Caret [ Left bracket
& Ampersand ] Right bracket
* Asterisk / Slash
_ Underscore \ Backslash
- Minus sign „ Apostrophe
+ Plus sign “ Quotation mark
= Equal sign ; Semicolon
| Pipeline character : Colon
? Question mark < Less than
, Comma > Greater than
. Period

C tokens
The basic and the smallest individual units of a C program are called C tokens. In a
passage of text, individual words and punctuation marks are called tokens.There are 6
types of tokens in C.
1. Key words
2. Identifiers
3. Constants
4. Strings
5. Operators
6. Special Symbols

Key words
All Keywords (Reserved words) have fixed meanings and these meanings cannot be changed
in any circumstances. All C keywords must be written in lowercase letters. Because, in C
bothuppercase and lower case letters are significant. There are 32 keywords in C.

auto double int struct


break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while

Page
C - Programming

Identifiers
Identifier refers to the names of variables, arrays and functions. These are user-defined
names and consist of sequence of letters and digits.
Rules for forming identifier or declaring a variable:
 The first character must be an alphabet or an underscore.
 All succeeding characters may be either letters or digits.
 Uppercase and lowercase identifiers are different in C.
 No special characters are allowed except the underscore “_”.
 No two successive underscores are allowed.
 Keywords should not be used as identifiers.
Constants
A constant refers to the fixed value that does not change during the execution of a
program. They are also called as literals. Constants can be declared by using the
keyword const.
Constants can be classified

Integer Constant

Integer constants are the whole numbers without a decimal point. They may have prefixed „-„
sign.

Ex: 10, 0, -5, 50 etc.

Floating-Point Constant

Floating-point constants are the numbers with preceded or followed by the decimal point.
They may have prefixed „-„ sign. Ex: 2.5, 30.2456, -6.27, 1.2536 etc.

Character Constant

Character constants are the characters of type i.e., alphabets, digits or special symbols, which
are enclosed within a pair of single quotes.
Ex: „a‟, „x‟, „?‟, „/‟, „ ‟ etc.
String Constant
String constants are the sequence of characters enclosed within a pair of double
quotes.
Ex: “Hi”, “Welcome”, “Program” etc.

Page
C - Programming

Data Types
Data type defines the type of data that a variable can store.
The data types in are categorized into three types:
1. Built-in data types.
2. Derived data types.
3. User-defined data types.

Built-in or Primitive data types are


1. Integers
2. Real or Floating-point number
3. Double precision number
4. Character
. int : The keyword int is used for declaring the integer type variables and which can store
integer number. An Integer number is a whole number with a particular range of values
without a decimal point.
Ex: 27, 1, 31, 42 etc.
Range: -32,765 to +32,768

Size: 2 bytes

2. Float : Variables of floating types can hold real numbers. The keyword float is used for
declaring floating type variables and which can store floating point number. Any float
number is a sequence of digits preceded and followed by decimal point.
Ex: 0.234, 14.263546 etc.
Size: 4 bytes

3. Double : Double is equivalent to float, but, the number of digits after the decimal point is
double than the float. The keyword double is used to indicate double precession floating
point number. Double is used whenever more accuracy is required.The float can have a
maximum of 6 digits after the decimal point. But double can stores 16 digits after the
decimal point.
Ex: 234.000000000000000.
Size: 8 bytes

4. char : The keyword char is used for declaring the character type variables. The char data
type allows a variable to store only one character. A character is any alphabet, digit or any
special character that is written within a single quote.
Ex: „x‟, „*‟ etc.
Size: 1 byte
Data Types Keyword Size (in bytes)
Integer int 2
Real (Floating-Point) float 4
Double Precision double 8
Character char 1
Non-Specific void -

Page
C - Programming

Data Type Modifiers


The basic data types can be modified using a series of type modifiers to fit the requirement
of a particular program moreclosely. These modifiers are also called Qualifiers.
The Different types of Data Type Modifiers are:
1. signed
2. unsigned
3. short
4. long
1. signed
This can be applied to integer and character type variables. The default integer declaration assumes a
signed number. If it is specified as signed, then it can hold the numbersin the range of -128 to 127.

2. unsigned
This can be used for both int and char. It can also be used with the combination of long or short.
The unsigned char can hold numbers in the range of 0 to 255.
3. long
This is used for int data type. When applied to int, it doubles the length of the data type. If an int
is of 2 bytes, then long int is of 4 bytes. Similarly, this can be used with double. When long is
applied to double, it roughly doubles the precision.

4. short
This is used for int data type. When applied to int, it makes the size of an int half.

Type Modifier Size (bytes) Range of values


int 2 -32768 to +32767
signed int 2 -32768 to +32767
unsigned int 2 0 to 65535
short int 2 -32768 to +32767
long int 4 -2147483648 to
+2147483647
float 4 -3.4E+48 to +3.4E+48
double 8 -1.7E+308 to +1.7E308
char 1 -128 to +127
signed char 1 -128 to +127
unsigned char 1 0 to 255
unsigned short 2 0 to 65535
int
unsigned long 4 0 to 4294967295
int
long double 10 -3.4E-4932 to +1.1E+4932

Variable
The variables are the names used to refer some location in the computer‟s memory that
holds a value. The value of variables changes during the execution of a program. Variables
are also called as Identifiers.
The name of a variable can be composed of letters, digits and the underscore. It must begin

Page
C - Programming

with a letter. Upper and lowercase letters are distinct because C is a case-sensitive.
Ex: sum, area_tri, n1, name, basic_salary etc.
Rules for Declaring a Variable.
 The first character must be an alphabet or an underscore.
 All succeeding characters may be either letters or digits.
 Uppercase and lowercase identifiers are different in C.
 No special characters are allowed except the underscore “_”.
 No two successive underscores are allowed.
 Keywords should not be used as identifiers.
Declaration of Variables
The variables which are used in the program must be declared in the declaration part before
they are used. After declaring the variables then only the compiler will reserve the memory
for these declared variables to store the values.

Syntax:
Data_Type Varlist Semicolon

Ex: int n1, n2;

float area;char ch;


float a[15], area_tri;char
name[20];

Assigning Values to a Variable


The process of storing some values to variables is called assignment of values. The assignment
operator „=‟ is used to assign a value to a variable.
Syntax:
Data_type Variable_name = Value ;
There are two methods of assigning values to variable.
1. The initial values can be assigned to variables within the declaration section. Assigning
variables within the declaration is called initialization.
Ex: int a=10;

float sum=0.0;
int n1=10, n2=20; char ch=‟x‟;

2. The initial values are assigned to the variables in theexecutable part of a program.
Ex: x=20; PI=3.142;

name=”Harish”; ch=‟A‟;

Assigning or Initializing multiple values to a single Variable


The process of giving initial values to variables is known as initialization. We can initialize more
than one variable in a single statement. This type of initialization would need more number of
assignment operators. Hence the name multiple assignment statements.

Page
C - Programming

Ex: int x=y=z=10;

float a=b=c=d=5.75;

Declaring variables as constant

C defines a new data type qualifier called the const to provide constant-ness to the values
of the variables during the execution of a program. Once a variable is declared as a const
variable, you cannot change its value. Any attempt to modify its value will result in a error
message.
Ex: const float PI=3.142;
const int MAXSIZE=10;
const char name 20 =”Peter”;

Symbolic Constants
A symbolic constant is a name that substitutes a numeric constant or character constant or a
string constant. Symbolic constants are defined using preprocessor statement #define.
Ex: #define PI 3.142

#define NAME “Dinesh”#define FOUND 1


Here we substituted PI for a numeric constant 3.142. During compile-time, each occurrence
of PI is replaced by its value 3.142.
Generally, the symbolic constant is written in uppercase letters. It is not a strict rule but a
good programming practice. Symbolic constants must be defined at the beginning of a
program. That is, before the main() function.

Backslash Constants
The Backslash Constants are also called as Escape Sequences. These are unprintable
characters which can perform special functions in the output statements. Backslash
constants are preceded by backslash (\) symbol and followed by any of characters from the
below table.

The Backslash Constants used in C are as follows.


Backslash Constant Meaning
\a Alert (alerts a bell)
\b Backspace
\f Form feed
\n New line
\r Carriage return
\t Horizontal tab
\v Vertical tab
\? Question mark
\‟ Single quote

Page
C - Programming

\” Double quote
\\ Backslash
0 Null (End of String)
Input/Output Functions
C provides many built-in functions to read data into computer and write data on screen,
printer or in any file. These functions are called standard Input Output library functions. To
perform input/output operations in C program we must include a header file called stdio.h.
There are two types of input/output statements.
1. Formatted Input/Output statement.
2. Unformatted Input/Output statement.
Input Statement Output Statement
Formatted scanf() print()
Unformatted getch() purch()
gets() puts()

1.1 Formatted Input statement


The scanf() function is used to read the values from the keyboard to the variables in C
program. We can read numeric, character and string type of data using scanf() function. To
use scanf() function in our programs, we must include the stdio.h header file.
Syntax: scanf(“control-string”, var_list);
Eg: scanf(“%d%d%d”,&a,&b,&c);

scanf(“%d%f”,&a,&b); scanf(“%f%d%c%s”,&avg,&age,&initial,&name);
scanf(“3d”,&n);
In the above scanf() function, the variable n will takes only 3 digits input. If you give more
than 3 digits, it discards last digits after the first 3 digits. For ex, if you give input as 45215
means, that will takes only first 3 digits i.e., 452.
scanf(“5f”,&x);
In the above scanf() function, the variable x will takes only 4 digits input and one decimal
point. Consider the input is 75.1245 then it will store only 75.12 and last two digits are
discarded.

scanf(“%ld,%hd”,&a,&b);
scanf(“%lf”,&b);
Here, the %ld for long int, %hd for short int and %lf for double.

1.2 Formatted Output statement


The printf() function is used in C program to print the data on monitor or on printer.
To use printf() function in our programs, we must include the stdio.h header file.

Syntax:printf(“control-string”, var_list);
Eg: printf(“Programming is an art”);printf(“%d”,a);
printf (“%d%f”,a,b); printf(“sum=%d”,sum);
printf(“Name=%s”,name); printf(“Average=%s”,avg);

Page
C - Programming

printf(“6. f”,&x);
In the above printf() function, the variable x has the content 4583.424 means, then it prints only
4583.4 only. The 6.1f indicates that it prints total 6 digits including the decimal point and there
will be only one digit after the decimal point.

2.1 Unformatted Input statements


Unformatted Input statements read only character type data from the keyboard. The
getch() function is used for reading a single character and gets() function is used for reading
string.

2.1.1 getch() function


The getch() function accepts only single character fromthe keyboard. There is no
parameter within the parenthesis.
Syntax:char_var=getch();

Ex:ch=getch();

2.1.2 gets() function


The gets() function accepts a line of string including spaces from the keyboard. It
stops reading characters from the keyboard only when the enter key is pressed.
Syntax: gets(str_var);

Ex: gets(name);

2.2 Unformatted Output statements


Unformatted Output statements prints only character type data on the monitor. The
putch() function is used for printing single character and puts() function is used for printing
string.

2.2.1 putch() function


The putch() function prints any single character on the
screen.
Syntax:putch(ch_var);

Ex:putch(a)

puts() function
The puts() function prints a sequence of characters orstring on the screen.
Syntax: puts(str_var);

Ex: puts(name);

Page
C - Programming

C Operators and Expressions


C supports many kinds of operators; operators can be applied on variables or constants.
The variables and constants are called operands. Operators are the symbols, they indicates
the type of operation to be performed on operands. Operators may operate on a single
operand or two or more operands.
C has rich set of operators. They are used to perform basic arithmetic operations,
comparisons, manipulation of numbers, bits and so on.
C operators are broadly classified into three main categories.

Unary Operators
An operator that acts upon only one operand is known asunary operator. The C unary
operators are:
1. Unary minus
2. Logical NOT operator
3. Bitwise complementation

Unary minus
Any positive operand associated with unary minus operator gets its value changed to
negative.
Ex: Let, a=3, b=4

c=a+(-b)
=3+(-4)
= -1

Binary Operators
The operators operates on two operands are known as binary operators. The binary operators are
classified as follows:

1. Arithmetic Operators
Arithmetic operators are used to perform the arithmetic calculations like addition,
subtraction, multiplication, division and modulus in C programs.

Page
C - Programming

Ex: a+b, a-b, x*y, x/y

Operations Operator Precedence Associativity


Addition + 2 L to R
Subtraction - 2 L to R
Multiplication * 1 L to R
Division / 1 L to R
Modulus % 1 L to R
2. Relational Operators
Relational operators are used to find the relation existing in two operands or variables i.e. to
compare two operands. They result in either a true or false value. The value of true is 1 and false
is 0.
Ex: x>y; A<b; Salary>=10,000; Age!=10 etc.
Operator Meaning Precedence Associativity
< Less than 1 L to R
> Greater than 1 L to R
<= Less than or equal to 1 L to R
>= Greater than or equal to 1 L to R
== Equal to 2 L to R
!= Not equal to 2 L to R

3.Logical Operators
Logical operators are used to perform logical operations (taking decision and comparing)
on two or more conditions. The result of this operator is either true or false.
Operator Meaning Precedence Associativity
&& Logical AND 2 L to R
|| Logical OR 3 L to R
! Logical NOT 1 L to R
The && operator is called the Logical AND operator. It is equivalent to a multiplication
operation. The result of Logical AND is true when both the operands are true; otherwise
the result is false.
Ex: if(avg>=60 && avg<=85)

Operand1 Operand2 Operand1 && Operand2


False False False
False True False
True False False
True True True
The || operator is called the Logical OR operator. It is equivalent to an addition operation.
The result of logical OR is false when both the operands are false; otherwise the result is
true.
Ex: if(m1<35 || m2<85 || m3<35)

Page
C - Programming

Operand1 Operand2 Operand1 || Operand2


False False False
False True True
True False True
True True True
The ! operator is called the Logical NOT operator. This operator is used to obtain the
logical compliment of the operand. The result of logical NOT is true when the operand is
false and result is false when the operand is true.

Ex: !a

Operand !Operand
True False
False True

.Bitwise Operators
The Bitwise Operators are used to manipulate one or more bits from internal operands like char,
int, short, long.
Operator Symbol Name Meaning
& Ampersand Bitwise AND
| Pipeline Bitwise OR
^ Caret Exclusive OR (X-OR)
~ Tilde 1‟s complement
<< Double Less than Left shift of bits
>> Double greater than Right shift of bits

The Bitwise AND (&) operator takes 2 bit patterns and perform AND operation on them. The
result of bitwise AND is 1 when both the bits are 1, otherwise it is 0.
The Bitwise OR ( | ) operator takes 2 bit patterns and perform OR operations on them. The result
of bitwise OR is 1 if any one bit is 1. If both the bits are 0, then the result is 0.
The Bitwise XOR (^) operator takes 2 bit patterns and perform XOR operation on them. The
result of bitwise X-OR is 1, if the bits are different (1 and 0) otherwise it is 0 (when both are 0 or
both are 1).
The Bitwise Complement (~) operator is unary operator. It takes only one operand that is used to
convert each bit 1 to 0 and 0 to 1.
The Left Shift (<<) operator shifts the bits towards left for the given number of times. The Right
Shift (>>) operator shifts the bits towards right for the given number of times.

Let a=4 4=100


b=3 3=11
a=00000100 b=00000010
a&b = 00000000
a||b = 00000111
a^b = 00000111
~a = 11111011

Page
C - Programming

a<< = 00001000
a>> = 00100001

5.Increment and Decrement Operators


The increment operator is used to increase the value of aninteger variable by 1. This is
represented by „++‟ symbol.
a. ++var indicates pre increment. The value of var must be incremented before it is used.
b. var++ indicates post increment. Use the value of var firstand then increment it.
The decrement operator is used to reduce the value of aninteger quantity by 1. This is
represented by „--„ symbol.
a. --var indicates pre decrement. Decrement the value of var
first and then use it.
b. var-- indicates post decrement. Use the value of var firstand then decrement it.

6.Ternary Operator
The ternary operator is also called as conditional operator in C. It takes three expressions.
The symbol „?‟ is used as a ternary operator. Expression1 is a condition. If the result of
Expression1 is true then Expression2 is executed. If the result of Expression1 is false then
Expression3 is executed.
Syntax:
<expression1> ? <expression2> : <expression3> ;

Ex: // Program to find biggest of two numbers.


#include<stdio.h>void main()
{
printf(“Enter a and b values”);
scanf("%d%d",&a,&b);
if(a>b)
printf("\n A is Biggest"): printf("\n B is Biggest"); getch();
}
7.Special Operators

a. Comma Operator
The comma operator is used to separate variable names. The comma operator is also
associated with for statement. It is also used to link two or more related expressions together.
Ex: sum=(a=10, b=20, a+b);
b. sizeof() Operator
This operator returns the size (number of bytes) of an operand. The operand may be a
constant, a variable or a data type. It should be written in lower case letter.
Syntax:
sizeof(operand);
Ex: x=sizeof(int);

Shorthand Assignment Operator


C provides a compact way of writing assignment statements in an expression. This is basically
associated with all arithmetic operators and bitwise operators.

Page
C - Programming

Syntax:
Var Operator = Exp ;

The shorthand operators are as follows:

Operator Assignments Shorthand assignment


+ (plus) a=a+b a+=b
- (minus) a=a-b a-=b
* (asterisk) a=a*b a*=b
/ (slash) a=a/b a/=b
% (percentage) a=a%b a%=b
& (bitwise AND) a=a&b a&=b
| (bitwise OR) a=a|b A|=b
^ (XOR) a=a^b A^=b
<< (left shift) a=a<<b A<<=b
>> (right shift) a=a>>b a>>=b

There is no space between the operator and the “=” sign.


The operator “=” is known as shorthand assignment operator.

Expressions
An Expression is a statement that has a value. Expression in C is a combination of
operators, operands and parenthesis. An expression involves arithmetic operators is called
arithmetic expression. It takes one or more operands and collects them by arithmetic
operands.
Ex: x = a+b*(-c)

Evaluate the following Arithmetic Expression:-


2*[(i/3)+4*(j*5)]Where i=6,
j=2
=2* (6/3)+4*(2*5)
=2*[2+4*(j*5)]
=2* 2+4*10
=2* 2+40
=2*42
=84
Type Conversions
Converting a value of one data type to another data type is known as data type conversion or
type casting. In some situations, we need to change the data type of the variables. Suppose we
declared the variables as integer, and the desired output will be the float. In such situations,
we need to use thetype conversion or type casting.
For Ex, assume that we need to divide 2 integer numbers. i.e., 10/4. We know that the answer is
2.5. But also we will get the answer as 4 only. Because, the integer divides integer, the answer is
also be the integer only. To get float answer we need to change any of one value to float i.e.,
10/4.0, then we will get the answer as float value 2.5.

Page
C - Programming

Syntax:
(datatype)variable;
Ex: int x=25, y=4; float z;z=x/float(y);
Library Functions (Mathematical Functions)
There are many standard library functions in C which perform specific operations. The C library
also contains many mathematical functions which are defined in the header file math.h. Some of
the most commonly used mathematicalfunctions are given below.

Function Action
abs(n) Return the absolute value of n.
pow(x,y) Return x to the power y
sqrt(n) Return the square root of n.
floor(n) Return a value rounded down to the next lower integer.
round(n) Return the nearest integer value of the float argument.
log(x) Return the natural logarithm of x.
log 10(x) Return the logarithm (base 10) of x.
cos/sin(x) Return the cosine/sine value of x.

Page

You might also like