You are on page 1of 22

Unit II: C Fundamentals

Introduction to c language and also express highly similar syntax to


history of c language C, and they tend to combine the
recognizable expression and statement
C is a procedural programming syntax of C with underlying type
language. It was initially developed systems, data models, and semantics
by Dennis Ritchie in the year 1972 at that can be radically different.
AT&T’s Bell Laboratories in USA. It
was mainly developed as a system Why it is named as C? There is no
programming language to write an such logic behind the naming of C
operating system. The main features Language. It was developed to cover
of C language include low-level all the inabilities of B language
access to memory, a simple set of (simplified version of BCPL-Basic
keywords, these features make C Combined Programming Language).
language suitable for system So, it was just named C as it is next to
programming like an operating B in the English alphabets. C is the
system or compiler development. only programming language that
Many later languages have borrowed exists for such a long period and still
syntax/features directly or indirectly it is widely used.
from C language. Like syntax of Java, Features of C Programming
PHP, JavaScript, and many other Language:
languages are mainly based on C
language. Many later languages have
 Simple
borrowed directly or indirectly from
 Machine Independent or Portable
C, including C++, C#, Unix's C shell,
D, Go, Java, JavaScript (including  Mid-level programming language
transpilers), Limbo, LPC, Objective-  structured programming language
C, Perl, PHP, Python, Rust, Swift,  Rich Library
Verilog and SystemVerilog (hardware  Memory Management
description languages. These  Fast Speed
languages have drawn many of their
 Pointers
control structures and other basic
 Recursion
features from C. Most of them
(Python being a dramatic exception)  Extensible
Prof. Farzana Azeem Sheikh
Farzana.nadaf@azamcampus.org
B.C.A. (Science) Department, AISC Page 1
Unit II: C Fundamentals

we can free the allocated memory at


any time by calling
Simple: C is a simple language in the
the free() function.
sense that it provides a structured
approach (to break the problem into  Speed:
parts) The compilation and execution time
of C language is fast since there are
Machine Independent or Portable:
lesser inbuilt functions and hence the
Unlike assembly language, c
lesser overhead.
programs can be executed on
different machines. Therefore, C is a Pointer:
machine independent language. C provides the feature of pointers. We
can directly interact with the memory
Mid-level programming language:
It is used to develop system by using the pointers. We can use
applications such as kernel, driver, pointers for memory, structures,
etc. It also supports the features of a functions, array, etc.
high-level language. That is why it is
Recursion:
known as mid-level language.
In C, we can call the function within
Structured programming language: the function. It provides code
C is a structured programming reusability for every function.
language in the sense that we can Recursion enables us to use the
break the program into parts using approach of backtracking.
functions. So, it is easy to
Extensible:
understand and modify. Functions
also provide code reusability. C language is extensible because
it can easily adopt new features.
 Rich Library:
C provides a lot of inbuilt
Advantages of C Programming
functions that make the development
Language
fast.

Memory Management:
It supports the feature of dynamic
memory allocation. In C language,
Prof. Farzana Azeem Sheikh
Farzana.nadaf@azamcampus.org
B.C.A. (Science) Department, AISC Page 2
Unit II: C Fundamentals

1. Building block for many other 2. Used in writing Embedded


programming languages software
2. Powerful and efficient language 3. It is also used in
3. Portable language developing verification software,
4. Built-in functions test code, etc. for various
5. Structured programming language applications and hardware
6. Dynamic memory allocation products.
7. Programming is done for the 4. For Creating Compilers of
hardware devices. different Languages which can
take input from other language
Disadvantages of C and convert it into lower level
Programming language machine dependent language.
5. C is used to implement
1. There is no strict type checking
different Operating System
(for example: we can pass an
Operations.
integer value for floating data
6. UNIX kernel is completely
type.)
developed in C Language.
2. Run-time checking Library Files
3. C is a very vast language, but it
Structure of ‘C’ Program
Global declaration
does not support the concept of
OOPs (Inheritance, Polymorphism,
Encapsulation, Abstraction, Data
Hiding). C simply follows the Main()
procedural programming approach. {
4. As the program extends it is very
local declarations;
difficult to fix the bugs.
5. Now days, it does not use to Statements;
develop complex type of software. }

Application Areas of ‘C’


1. C language is used for
creating computer applications 
User define function
Function()
Prof. Farzana Azeem Sheikh
Farzana.nadaf@azamcampus.org {
B.C.A. (Science) Department, AISC Page 3 }
Unit II: C Fundamentals

1. Library files are nothing but Program Development Life


header files which contains all Cycle:
built-in functions. Global variable
and function declaration is done
here.
2. Second part consist of main ()
function. Every c program must
contain only one main function.
Execution of every c program
starts from main () function.
3. Function is collection of
statements. Every statement in c
must ends with semicolon (;).
These statements can contain Every file that contains a C program
declaration of variables and must be saved with ‘.c’ extension.
declaration of other user define This is necessary for the compiler to
functions also you can use built-in understand that this is a C program
functions within the main file. Suppose a program file is named,
function. first.c. The file first.c is called the
A Simple C Program: source file which keeps the code of
the program. Now, when we compile
the file, the C compiler looks for
errors. If the C compiler reports no
error, then it stores the file as a .obj
file of the same name, called the
object file. So, here it will create the
first.obj. This .obj file is not
executable. The process is continued
by the Linker which finally gives a
.exe file which is executable.
Linker: First of all, let us know that
library functions are not a part of any

Prof. Farzana Azeem Sheikh


Farzana.nadaf@azamcampus.org
B.C.A. (Science) Department, AISC Page 4
Unit II: C Fundamentals

C program but of the C software. Keywords


Thus, the compiler doesn’t know the Keywords are predefined, reserved
operation of any function, whether it words in C and each of which is
be printf or scanf. The definitions of associated with specific features.
these functions are stored in their These words help us to use the
respective library which the compiler functionality of C language. They
should be able to link. This is what have special meaning to the
the Linker does. So, when we write compilers.
#include, it includes stdio.h library There are total 32 keywords in C.
which gives access to Standard Input auto double int struct
and Output. The linker links the
object files to the library functions break else long switch
and the program becomes a .exe file. case enum register typedef
Here, first.exe will be created which
is in an executable format. char extern return union
Loader: Whenever we give the continue for signed void
command to execute a particular
program, the loader comes into work. do if static while
The loader will load the .exe file in default goto sizeof volatile
RAM and inform the CPU with the
starting point of the address where const float short unsigned
this program is loaded.
At the end we get the output of the Identifiers
program. These are user-defined names which
consist of alphabets, number,
C Language Fundamentals: underscore ‘_’. Identifier’s name
Tokens are the smallest elements of a should not be same or same as
program, which are meaningful to the keywords. Keywords are not used as
compiler. identifiers.
The following are the types of tokens: Rules for naming C identifiers −
Keywords, Identifiers, Constant, 1. It must begin with alphabets or
Strings, Operators, etc. underscore.

Prof. Farzana Azeem Sheikh


Farzana.nadaf@azamcampus.org
B.C.A. (Science) Department, AISC Page 5
Unit II: C Fundamentals

2. Only alphabets, numbers, We can also provide values while


underscore can be used, no other declaring the variables as given
special characters, punctuations below:
are allowed. int a=10,b=20;
3. It must not contain white-space. float f=20.8;  
4. It should not be a keyword. char c='A';  
5. It should be up to 31 characters Types of Variables:
long. 1. Local Variables
2. Global Variables
Variables: 3. Formal Parameters(in chap 5)
A variable is a name of the memory Local Variables:
location. It is used to store data. Its 6. The scope of local variables will be
value can be changed, and it can be within the function only.
reused many times. 7. These variables are declared within
It is a way to represent memory the function and can’t be accessed
location through symbol so that it can outside the function.
be easily identified. 8. We can have same variable name in
Variable Declaration: 2 different blocks, but not in same
data type variable_name;   block
for example: Global Variable:
int a;   9. The scope of global variables will
float b;   be throughout the program. These
char c;   variables can be accessed from
Here, a, b, c are variables. The int, anywhere in the program.
float, char are the data types (Data 10. This variable is defined outside
types are used to identify which the main function. So that, this
type of data.) variable is visible to main function
and all other sub functions.
11. Global variables are helpful
when many functions in your
Variable Initialization: program use same data. (default
value=0).

Prof. Farzana Azeem Sheikh


Farzana.nadaf@azamcampus.org
B.C.A. (Science) Department, AISC Page 6
Unit II: C Fundamentals

Constants: Single Character Constant


I. Constants in C are the fixed values It simply contains a single character
that are used in a program, and its enclosed within ' and ' (a pair of single
value remains the same during the quote). It is to be noted that the
entire execution of the program. character '8' is not the same as 8.
II. Constants are also called literals. Character constants have a specific set
III. Constants can be any of the data of integer values known as ASCII
types. values (American Standard Code for
Syntax: Information Interchange).
const data_type constant_name; Example:
Types of Constants: 'X', '5', ';'

1. Numeric Constants String Constant:


a. Integer Constants These are a sequence of characters
b. Real Constants enclosed in double quotes, and they
2. Character Constants may include letters, digits, special
a. Single Character Constants characters, and blank spaces. It is
b. String Constants again to be noted that "G" and 'G' are
c. Backslash Character Constants different - because "G" represents a
string as it is enclosed within a pair of
Integer Constant double quotes whereas 'G' represents
It's referring to a sequence of digits. a single character.
Integers are of three types viz: Example:
"Hello!", "2015", "2+1"
1. Decimal Integer
2. Octal Integer Backslash Character Constants:
C supports some character constants
3. Hexadecimal Integer
having a backslash in front of it. The
Real Constant lists of backslash characters have a
The numbers containing fractional specific meaning which is known to
parts like 99.25 are called real or the compiler. They are also termed as
floating points constant. "Escape Sequence".

Prof. Farzana Azeem Sheikh


Farzana.nadaf@azamcampus.org
B.C.A. (Science) Department, AISC Page 7
Unit II: C Fundamentals

Escape Sequences Symbolic Constant:

Escape A symbolic constant is a name given


Sequence Character to some numeric constant, or a
s
character constant or string constant,
\b Backspace or any other constants.
\n Newline Symbolic constant names are also
\t Horizontal tab
known as constant identifiers. Pre-
processor directive.
\v Vertical tab
#define is used for defining symbolic
\\ Backslash constants.
Single quotation Syntax :
\'
mark

Double quotation #define symbolic_constant_name


\"
mark value_of_the_constant
\? Question mark Symbolic Constants Examples:
\0 Null character
#define PI 3.14
#define GOLDENRATIO 1.6
#define MAX 500

Here PI, GOLDENRATIO, SIZE are


symbolic constants.

Note: symbolic constants names are


written in uppercase by convention,
but it is not required.

Prof. Farzana Azeem Sheikh


Farzana.nadaf@azamcampus.org
B.C.A. (Science) Department, AISC Page 8
Unit II: C Fundamentals

Data Types: type.


Data Types are used to:
1. Identify the type of a variable float, Used to denote a floating
double point type.
when it declared.
2. Identify the type of the return int *, Used to denote a pointer
value of a function. float *, type.
3. Identify the type of a parameter char *
expected by a function.
Data types in C Language are
Some data types with their sizes
categorized as
and value range
1. Primary (Built-in) Data Types:
void, int, char, double and float. Type Storage Value range
size
2. Derived Data Types:
Array, References, and Pointers. char 1 byte -128 to 127 or 0 to 255
3. User Defined Data Types: unsigne 1 byte 0 to 255
Structure, Union, d char
and Enumeration. signed 1 byte -128 to 127
char
Primary Data Types
Every C compiler supports five 2 or 4
-32,768 to 32,767 or
int -2,147,483,648 to
primary data types: bytes
2,147,483,647
void As the name suggests, it
holds no value and is unsigne 2 or 4 0 to 65,535 or 0 to
generally used for specifying d int bytes 4,294,967,295
the type of function or what short 2 bytes -32,768 to 32,767
it returns. If the function has
a void type, it means that the unsigne 2 bytes 0 to 65,535
d short
function will not return any
value. long 8 bytes -9223372036854775808
to
int Used to denote an integer 9223372036854775807
type. unsigne 8 bytes 0 to
d long 18446744073709551615
char Used to denote a character

Prof. Farzana Azeem Sheikh


Farzana.nadaf@azamcampus.org
B.C.A. (Science) Department, AISC Page 9
Unit II: C Fundamentals

Derived Data Types: Union These allow storing various


Derived data types are derivative of data types in the same memory
primitive data types. C supports location. Programmers can
three derived data types: define a union with different
Description
members, but only a single
member can contain a value at
Arrays Arrays are sequences of data a given time.
items having homogeneous Enum Enumeration is a special data
values. They have adjacent type that consists of integral
memory locations to store constants, and each of them is
values. assigned with a specific name.
"enum" keyword is used to
Reference Function pointers allow
define the enumerated data
s referencing functions with a
type.
particular signature.
For example:/*An example
Pointers These are powerful C program to demonstrate
features which are used to working of enum in C*/
access the memory and deal #include<stdio.h>
with their addresses. enum week{Mon, Tue, Wed,
Thur, Fri, Sat, Sun};
 int main()
{
User Defined Data Types
    enum week day;
C allows user to define data types.     day = Wed;
The data types which are defined by     printf("%d",day);
the user or programmer himself are     return 0; }
called as User defined data type. Typedef typedef is a keyword used in C
Data Description language to assign alternative
Types names to existing data types.
Its mostly used with user
Structure It is a package of variables of
defined data types, when
different types under a single names of the data types
name. This is done to handle become slightly complicated
data efficiently. "struct" to use in programs.
keyword is used to define a Syntax:
structure.
Prof. Farzana Azeem Sheikh
Farzana.nadaf@azamcampus.org
B.C.A. (Science) Department, AISC Page 10
Unit II: C Fundamentals

{
typedef <existing_name>     char ch = 'A';
<alias_name>     printf("%c\n", ch);
example:     return 0;
}
typedef int* IntPtr; Output:
IntPtr x, y, z; A
Type Casting:

Format Specifier: Typecasting is converting one data


Format specifiers in C are used for type into another one. It is also called
input and output purposes. Using as data conversion or type conversion.
format specifier the compiler can It is one of the important concepts
introduced in 'C' programming.
understand that what type of data is in
input and output operation. 'C' programming provides two types
Format of type casting operations:
Specifier Type
i. Implicit type casting
%c Character ii. Explicit type casting
%d Signed integer Implicit type casting means
conversion of data types without
%f Float values
losing its original meaning. This type
%l or %ld or Long of typecasting is essential when you
%li want to change data
types without changing the
%Lf Long double significance of the values stored
%o Octal representation inside the variable. Implicit type
conversion happens automatically
%s String when a value is copied to its
compatible data type. During
%u Unsigned int conversion, strict rules for type
%x or %X Hexadecimal conversion are applied. If the
operands are of two different data
Example: types, then an operand having lower
#include <stdio.h> data type is automatically converted
int main()
Prof. Farzana Azeem Sheikh
Farzana.nadaf@azamcampus.org
B.C.A. (Science) Department, AISC Page 11
Unit II: C Fundamentals

into a higher data type. This type of are some scenarios in which we may
type conversion can be seen in the have to force type conversion.
following example. Suppose we have a variable div that
stores the division of two operands
#include<stdio.h> which are declared as an int data type.
int main(){ int result, var1=10, var2=3;
short a=10; //initializing result=var1/var2;
variable of short data type In this case, after the division
int b; //declaring int variable performed on variables var1 and var2
b=a; //implicit type casting the result stored in the variable
printf("%d\n",a); "result" will be in an integer format.
printf("%d\n",b); Whenever this happens, the value
} stored in the variable "result" loses its
meaning because it does not consider
Output the fraction part which is normally
obtained in the division of two
10 numbers.
10 To force the type conversion in such
situations, we use explicit type
#include <stdio.h> casting.
main() { It requires a type casting operator.
int number = 1; The general syntax for type casting
char character = 'k'; /*ASCII value operations is as follows:
is 107 */ type-name) expression
int sum;
sum = number + character; #include<stdio.h>
printf("Value of sum : %d\n", sum ); int main()
} {
float a = 1.2;
Output: //int b = a; //Compiler will throw an
Value of sum : 108 error for this
int b = (int)a + 1;
printf("Value of a is %f\n", a);
printf("Value of b is %d\n",b);
Explicit type casting return 0;}
In implicit type conversion, the data
type is converted automatically. There Output:

Prof. Farzana Azeem Sheikh


Farzana.nadaf@azamcampus.org
B.C.A. (Science) Department, AISC Page 12
Unit II: C Fundamentals

Value of a is 1.200000 An operator is a symbol that tells the


Value of b is 2 compiler to perform specific
mathematical or logical functions. C
Expression language is rich in built-in operators
1. In programming, an expression and provides the following types of
is any legal combination of operators −
symbols that represents a value.  Arithmetic Operators
2. C Programming provides its
 Relational Operators
own rules of Expression, whether
 Logical Operators
it is legal expression or illegal
 Bitwise Operators
expression. For example, in the C
language x+5 is a legal expression.  Assignment Operators
3. Every expression consists of at  Misc Operators
least one operand and can have Arithmetic Operators
one or more operators.
The following table shows all the
4. Operands are values and arithmetic operators supported by the
Operators are symbols that C language. Assume
represent particular actions. variable A holds 10 and
variable B holds 20 then −
Types of Expression:
Operato Description Example
Type Explanation Example r
Expression in which + Adds two operands. A+B=
Infix Operator is in between a + b 30
Operands
− Subtracts second A−B=
Expression in which
operand from the -10
Prefix Operator is written + a b
before Operands first.

Expression in which * Multiplies both A*B=


Postfi operands. 200
Operator is written after a b +
x
Operands
/ Divides numerator by B/A=2
Operators in C de-numerator.

% Modulus Operator B%A=

Prof. Farzana Azeem Sheikh


Farzana.nadaf@azamcampus.org
B.C.A. (Science) Department, AISC Page 13
Unit II: C Fundamentals

and remainder of after 0 becomes true.


an integer division.
< Checks if the value (A < B) is
++ Increment operator A++ = of left operand is true.
increases the integer 11 less than the value
value by one. of right operand. If
yes, then the
-- Decrement operator A-- = 9
condition becomes
decreases the integer
true.
value by one.
>= Checks if the value (A >= B)
Relational Operators of left operand is is not
greater than or equal true.
The following table shows all the
to the value of right
relational operators supported by C.
operand. If yes, then
Assume variable A holds 10 and
the condition
variable B holds 20 then −
becomes true.
Operato Description Example <= Checks if the value (A <= B)
r of left operand is is true.
== Checks if the values (A == B) less than or equal to
of two operands are is not the value of right
equal or not. If yes, true. operand. If yes, then
then the condition the condition
becomes true. becomes true.

!= Checks if the values (A != B) Logical Operators


of two operands are is true.
equal or not. If the Following table shows all the logical
values are not equal, operators supported by C language.
then the condition Assume variable A holds 1 and
becomes true. variable B holds 0, then −
Operator Description Example
> Checks if the value (A > B) is
of left operand is not true.
greater than the && Called Logical AND (A &&
value of right operator. If both the B) is
operand. If yes, then operands are non- false.
the condition zero, then the
condition becomes

Prof. Farzana Azeem Sheikh


Farzana.nadaf@azamcampus.org
B.C.A. (Science) Department, AISC Page 14
Unit II: C Fundamentals

true. Assume A = 60 and B = 13 in binary


format, they will be as follows −
|| Called Logical OR (A || B) is A = 0011 1100
Operator. If any of true.
B = 0000 1101
the two operands is
non-zero, then the -----------------
condition becomes
A&B = 0000 1100
true.
A|B = 0011 1101
! Called Logical NOT !(A && A^B = 0011 0001
Operator. It is used B) is true.
~A = 1100 0011
to reverse the logical
state of its operand. The following table lists the bitwise
If a condition is true, operators supported by C. Assume
then Logical NOT variable 'A' holds 60 and variable 'B'
operator will make it holds 13, then −
false.
Operator Description Example

& Binary AND (A & B) =


Bitwise Operators Operator copies a 12, i.e.,
Bitwise operator works on bits and bit to the result if it 0000 1100
exists in both
perform bit-by-bit operation. The
operands.
truth tables for &, |, and ^ is as
follows − | Binary OR (A | B) =
Operator copies a 61, i.e.,
p q p&q p|q p^q bit if it exists in 0011 1101
either operand.

0 0 0 0 0 ^ Binary XOR (A ^ B) =
Operator copies the 49, i.e.,
bit if it is set in one 0011 0001
0 1 0 1 1 operand but not
both.

1 1 1 1 0 ~ Binary One's (~A ) =


Complement ~(60), i.e,.
Operator is unary -0111101
1 0 0 1 1
and has the effect
of 'flipping' bits.

Prof. Farzana Azeem Sheikh


Farzana.nadaf@azamcampus.org
B.C.A. (Science) Department, AISC Page 15
Unit II: C Fundamentals

<< Binary Left Shift operand.


Operator. The left
operands value is A << 2 =
moved left by the 240 i.e., -= Subtract AND
number of bits 1111 0000 assignment
specified by the operator. It
C -= A is
right operand. subtracts the right
equivalent
operand from the
>> Binary Right Shift to C = C -
Operator. The left left operand and
A
operands value is A >> 2 = assigns the result
moved right by the 15 i.e., to the left
number of bits 0000 1111 operand.
specified by the
right operand.
*= Multiply AND
Assignment Operators assignment
operator. It
C *= A is
The following table lists the multiplies the
equivalent
assignment operators supported by right operand with
to C = C *
the C language − the left operand
A
and assigns the
Operato Description Example result to the left
r operand.

= Simple /= Divide AND


assignment C=A+B assignment
operator. Assigns will assign operator. It
values from right the value of divides the left C /= A is
side operands to A + B to C operand with the equivalent
left side operand right operand and to C = C / A
assigns the result
+= Add AND C += A is to the left
assignment equivalent operand.
operator. It adds to C = C +
the right operand A %= Modulus AND C %= A is
to the left operand assignment equivalent
and assign the operator. It takes to C = C %
result to the left modulus using

Prof. Farzana Azeem Sheikh


Farzana.nadaf@azamcampus.org
B.C.A. (Science) Department, AISC Page 16
Unit II: C Fundamentals

two operands and r


assigns the result
A
to the left sizeof() sizeof(a),
operand. Returns the
where a is
size of a
integer, will
variable.
<<= Left shift AND C <<= 2 is return 4.
assignment same as C =
operator. C << 2 & Returns the &a; returns the
address of a actual address
>>= Right shift AND C >>= 2 is variable. of the variable.
assignment same as C =
operator. C >> 2 * Pointer to a *a;
variable.
&= Bitwise AND C &= 2 is
assignment same as C = ?: If Condition is
operator. C&2 true ? then
value X :
^= Bitwise exclusive C ^= 2 is otherwise value
OR and same as C = Conditional Y
assignment C^2 Expression.
operator. Example:

return (a > b) ?
|= Bitwise inclusive C |= 2 is a : b;
OR and same as C =
assignment C|2
operator. Increment and decrement
Operator:
Other Operators: sizeof & ternary C programming has two operators
increment ++ and decrement --
Besides the operators discussed
Increment ++ increases the value by 1
above, there are a few other
important operators whereas decrement -- decreases the
including sizeof and ? : supported by value by 1. These two operators are
the C Language. unary operators, meaning they only
operate on a single operand.
Operato Description Example

Prof. Farzana Azeem Sheikh


Farzana.nadaf@azamcampus.org
B.C.A. (Science) Department, AISC Page 17
Unit II: C Fundamentals

Example 2: Increment and Decrement The -- operator works in a similar way


Operators like the ++ operator except it
decreases the value by 1.
// Working of increment and #include<stdio.h>
decrement operators
#include <stdio.h> int main()
int main() {
{ int x = 12, y = 1;
int a = 10, b = 100;
float c = 10.5, d = 100.5; printf("Initial value of x = %d\n",
x); // print the initial value of x
printf("++a = %d \n", ++a); printf("Initial value of y = %d\n\n",
printf("--b = %d \n", --b); y); // print the initial value of y
printf("++c = %f \n", ++c);
printf("--d = %f \n", --d); y = x++; // use the current value of
x then increment it by 1
return 0;
} printf("After incrementing by 1: x
= %d\n", x);
Output printf("y = %d\n\n", y);
++a = 11
--b = 99 y = x--; // use the current value of x
++c = 11.500000 then decrement it by 1

--d = 99.500000 printf("After decrementing by 1: x


Here, the operators ++ and -- are used = %d\n", x);
printf("y = %d\n\n", y);
as prefixes. These two operators can
also be used as postfixes like a+ // Signal to operating system
+ and a--. everything works fine
if you use the ++ operator as postfix return 0;
like: var++. The original value }
of var is returned first then, var is Expected Output:
incremented by 1.
Initial value of x = 12
Initial value of y = 1

Prof. Farzana Azeem Sheikh


Farzana.nadaf@azamcampus.org
B.C.A. (Science) Department, AISC Page 18
Unit II: C Fundamentals

After incrementing by 1: x = & sizeof


13
y = 12 Multiplicat */% Left to right
ive
After decrementing by 1: x =
12 Additive +- Left to right
y = 13 Shift << >> Left to right
Operators Precedence in C Relational < <= > Left to right
Operator precedence determines the >=
grouping of terms in an expression Equality == != Left to right
and decides how an expression is
evaluated. Certain operators have Bitwise & Left to right
higher precedence than others; for AND
example, the multiplication operator
has a higher precedence than the Bitwise ^ Left to right
addition operator. XOR

For example, x = 7 + 3 * 2; here, x is Bitwise | Left to right


assigned 13, not 20 because operator OR
* has a higher precedence than +, so
Logical && Left to right
it first gets multiplied with 3*2 and
AND
then adds into 7.
Here, operators with the highest Logical || Left to right
precedence appear at the top of the OR
table, those with the lowest appear at Conditiona ?: Right to left
the bottom. Within an expression, l
higher precedence operators will be
evaluated first. Assignme = += -= Right to left
nt *= /=
Category Operator Associativit %=>>=
y <<= &=
^= |=
Postfix () [] -> . + Left to right
+-- Comma , Left to right
Unary + - ! ~ ++ Right to left
- - (type)* Trace the Output Questions

Prof. Farzana Azeem Sheikh


Farzana.nadaf@azamcampus.org
B.C.A. (Science) Department, AISC Page 19
Unit II: C Fundamentals

++a+,=
#include <stdio.h> A++=,+
int main() main()
{ {
int num1 = 5; int a = 1, b = 3;
int num2 = 3; b= a++ + a++ + a++ + a++ + a++;
int num3 = 2; printf("a = %d \n b = %d", a, b);
num1 = num2++;
num2 = --num3; }
printf("%d %d %d", num1, num2, a = 6, b = 15
num3); a =1 , b = 3
return 0; a = 1 , b =15
} a=2,b=4

231 #include <stdio.h>


311
main()
327
{
321
int a = 9, b = 10;
a = b++;
#include <stdio.h>
b = a++;
int main()
b = ++b;
{
int x = 4, y, z; printf("%d %d", a, b);
y = --x;
z = x--; }
printf("%d, %d, %d\n", x, y, z); a) 9, 9
return 0; b) 10, 10
} c) 9, 10
4, 3, 3 d) 10, 9
3, 3, 3
2, 3, 3
4, 4, 3

#include <stdio.h> #include <stdio.h>

Prof. Farzana Azeem Sheikh


Farzana.nadaf@azamcampus.org
B.C.A. (Science) Department, AISC Page 20
Unit II: C Fundamentals

main()
{ #include <stdio.h>
int a, b; int main()
b = 12; {
a = ++b + ++b; int i = 1;
printf("%d %d", a, b); int j = i++ + i;
printf("%d\n", j);
} }
a) 24, 12 a) 0
b) 23, 12 b) 1
c) 23, 10 c) 2
d) 24, 10 d) Compile time error

#include <stdio.h> #include <stdio.h>


int main() int main()
{ {
int a = 2, b = 1, c; int x=1, y ;
c = a++ + b; y = ( x ==1 ? 2 : 0 ) ;
printf("%d, %d", a, b); printf("x value is %d\n", x);
} printf("y value is %d", y);
a) a = 1, b = 1 }
b) a = 2, b = 1
c) a = 1, b = 2  1,2
d) a = 2, b = 2
#include<stdio.h>
#include <stdio.h> void main()
int main() {
{ int i=-1,j=-1,k=0,l=2,m;
int a = 10, b = 10;
if (a = 5) m = i++ && j++ && k++ || l++;
b--;
printf("%d, %d", a, b--); printf("%d %d %d %d %d",i,j,k,l,m);
} }
a) a = 10, b = 9
b) a = 10, b = 8 00131
c) a = 5, b = 9 11020
d) a = 5, b = 8 22131

Prof. Farzana Azeem Sheikh


Farzana.nadaf@azamcampus.org
B.C.A. (Science) Department, AISC Page 21
Unit II: C Fundamentals

None of these
-1 -1 0 2 1

Prof. Farzana Azeem Sheikh


Farzana.nadaf@azamcampus.org
B.C.A. (Science) Department, AISC Page 22

You might also like