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

2. lecture Basics of C (2)

The document outlines various programming concepts, including the declaration of constants, escape sequences, data types, operators, and compiler types. It explains the syntax for defining constants, the purpose of escape sequences, and the different categories of data types and operators in C. Additionally, it covers operator precedence, null statements, the comma operator, and command line arguments.

Uploaded by

atharv playz
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)
6 views

2. lecture Basics of C (2)

The document outlines various programming concepts, including the declaration of constants, escape sequences, data types, operators, and compiler types. It explains the syntax for defining constants, the purpose of escape sequences, and the different categories of data types and operators in C. Additionally, it covers operator precedence, null statements, the comma operator, and command line arguments.

Uploaded by

atharv playz
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/ 33

Declaration of Constants

Value of an identifier defined as constant, cannot be altered


during program execution.
Generally, the constant is associated with an identifier and
that name is used instead of actual number.
General Syntax
const typename identifier =const;
e.g. const float pi=3.14159;
Another way to define constants is with the #define
preprocessor which has the advantage that it does not use
ant storage (but who count bytes these days?).
#define Identifier Value
#define PI 3.1415
Escape Sequences
backslash symbol (\) is considered an "escape character": it causes an
escape from the normal interpretation of string, so that the character
is recognized as one having a special meaning.
Escape Sequence Purpose
\n New Line (Line feed)
\b Backspace
\t Horizontal Tab
\r Carriage Return
\f Form feed
\' Single quote
\" Double quote
\\ Backslash
\a Alert
\? Question mark
Data Types
1. Primary Data Types
int
char
float
double
2. Derived Data Types
structures,
unions,
enumerations
3. User-defined Data Types
Primary Data 27

Types
int
E.g. int a; a =5;
float
E.g. float miles; miles=5.6;
double
E.g. double big; big =312E7;

char
E.g. char letter; letter =‘y';
Modifying the Basic Data Types
The lists of modifiers are:
signed
unsigned
short
long
signed, unsigned, short, long can be used with int.
signed, unsigned can be used with char.
long can be used with double.
The amount of storage allocated is not fixed. ANSI has
the following rules
short int <=int <=long int
float <=double <=long double
User-Defined Data Types
Syntax:
typedef existing_data_type new_name_for_existing_data_type;

Note:- We’ll study example of typedef & Derived Data Type in


module 2.
Operators
1. Unary operators
-, ++,--, sizeof, (type)
e.g. -743, -a, -root, a++, a--, ++a, --a
2. Binary operators
Arithmetic,
Assignment,
Relational,
Logical
3. Tertiary operators
(?:)
sizeof operator
Typecast Operator
Typecast operator is also unary operator. It is used to change the data
type of a variable.
Syntax: (type) expression
e.g. (float) x/2;
main(){
int var1; float var2;
var2= (float) var1;
}
Unary operators have higher precedence. Also, the associativity of the
unary operator is right to left.
Binary Operators
Arithmetic Operators
+ for addition
- for subtraction
* for multiplication
/ for division
% for remainder operation, modulo operator
Arithmetic Expressions
main(){
int a=10,b=3; int c,d,e,f;
c=a+b;
d=a-b;
e=a*b;
f=a%b;
printf("Value of c=%d\n",c);
printf("Value of d=%d\n",d);
printf("Value of e=%d\n",e);
printf("Value of f=%d\n",f); getch();
}
Binary Operators
Assignment Operators
= for assignment
+= add with destination and assign
-= deduct from destination and assign
*= multiply with destination and assign
/= divide the destination and assign
%= assign the remainder after dividing the destination
exp1+=exp2 is equivalent to exp1=exp1+exp2
exp1-=exp2 is equivalent to exp1=exp1-exp2
exp1*=exp2 is equivalent to exp1=exp1*exp2
exp1/=exp2 is equivalent to exp1=exp1/exp2
exp1%=exp2 is equivalent to exp1=exp1%exp2
Binary Operators
Relational Operators
< less than
> greater than
<= less than or equals to
>= greater than or equals to

Equality operators are closely associated with this group


== equal to
!= not equal to
Binary Operators
Logical Operators
&& logical and (AND)
|| logical or (OR)

Expression Interpretation Value


(j>=6)&&(c= ='w') true 1
(k<11)&&(j>100) false 0
(j>=6)||(c= =19) true 1
(c!=p)||(j+k)<10 true 1
Tertiary Operator
(Test expression) ? true_value : false-value;
E.g. (j<0)?0:100
Bitwise Operators 40
a = 15 = 0000 1111
Bitwise Logical b = 7 = 0000 0111
Bitwise Operator

Operators
a = 15 = 0000 1111
Bitwise Shift
Operators b = 7 = 0000 0111
AND = 0000 0111
One’s Complement
Operator a = 15 = 0000 1111
b = 7 = 0000 0111
OR = 0000 1111
Bitwise Logical
a = 15 = 0000 1111
Operators b = 7 = 0000 0111
Bitwise AND (&) XOR = 0000 1000
Bitwise OR (|)
Bitwise NOT (^)
Bitwise Operators 41
Bitwise Shift Operators
Left Shift (<<)
Right Shift (>>)

a 0000 1111
Left Shift 0111 1000
by 3

Right 0000 0001


Shift by 3
Bitwise Operators
One’s Complement Operator

a = 12 = 0000 1100
~a = 1111 0011
Bitwise Operators
Two’s Complement
a = -13 = 1111 0011

~a = 0000 1100
+1

13 = 0000 1101
Modify Operators in C
Pre-[increment | decrement]

Substitution

Evaluation

Assignment

Post-[increment | decrement]
PSE.AP 45
Task
Write output of following program:
Priority Operators
1st *, /, %
Hierarchy of Operations 2nd
3 rd
+, -
=
Precedence - which of the operator is to be executed first, BODMAS,
bracket is operated first.
Associativity is the preference of the operators (of same precedence level),
when there is tie between them. e.g. suppose we have
z=a+b*c
Then, operation occurs as z=a+(b*c) as *higher precedence than +
But when the case is like
Z=a*b+c/d, operation occurs as
Z=(a*b)+(c/d) i.e. multiplication is done first. Then (c/d) is evaluated. Then only
the two values are added. It happens so because *and / have higher
precedence than +. However, as *and / have same precedence level, *is
evaluated first, as the associativity of the arithmetic operators is from left to right,
hence whichever operator occurs first, that is evaluated.
Arithmetic Expression:
Z=a+2*b/c*d+23+a*a; a =2, b =3, c =4, d =5 then z =?
Operator Precedence & Associativity
Operator Category Operators Associativity
Arithmetic Multiply, Divide, & *, /, % L->R
Remainder

Arithmetic Add and Subtract +, - L->R


Relational Operators <, >, <=, >= L->R
Equality Operators ==, != L->R
Logical AND && L->R
Logical OR || L->R
Conditional Operator ?: R->L
Assignment Operator =, +=, -=, *=, /=, %= R->L
Unary Operators -, ++, --, sizeof, (type) R->L
ASCII Character Set
1.Incremental Compiler:
1. Recompiles only the parts of the code that have changed instead of recompiling the entire source code.
2. Improves efficiency during development.
2.Cross Compiler:
1. Generates executable code for a platform different from the one on which the compiler is running.
2. Commonly used in embedded systems development.
3.Load & Go Compiler:
1. Produces machine code and immediately executes it without saving it to a file.
2. Useful for quick testing and debugging.
1.Threaded Code Compiler:
1. Generates code in the form of sequences of addresses of precompiled routines
(threaded code).
2. Improves performance by reusing predefined routines.
2.Stage Compiler:
1. Breaks the compilation process into multiple stages, with each stage focusing on a
specific part of the process (e.g., lexical analysis, syntax analysis).
2. Enhances modularity and debugging.
3.Just-in-Time (JIT) Compiler:
1. Compiles code at runtime, just before execution, rather than ahead of time.
2. Common in environments like Java Virtual Machine (JVM) and .NET CLR for
performance optimization.
4.Parallelizing Compiler:
1. Analyzes the source code to detect independent tasks and automatically parallelizes
them for execution on multi-core processors or distributed systems.
2. Increases performance by utilizing hardware parallelism.
According to its Pass Structure
1.One Pass Compiler:
1. Completes the entire compilation process in a single pass over the
source code.
2. Simple and efficient but may have limitations in handling complex
optimizations or forward references.
2.Multi Pass Compiler:
1. Processes the source code in multiple passes, each handling a specific
aspect of compilation.
2. Supports complex optimizations and features like resolving forward
references and dependencies.
Null Statement
A null statement in programming is a statement that does nothing. In C, it is
represented by a single semicolon (;) and is often used intentionally where a
statement is syntactically required but no action is desired.

if (condition) {
; // Do nothing
}

for (int i = 0; i < n; arr[i++] = 0); // Initialize array elements to 0

if (error)
; // TODO: Add error handling here
Comma Operator
The comma operator in C allows multiple expressions to be evaluated in a single
statement, with the value of the entire expression being the value of the last
expression. It is represented by the comma symbol (,).

•The expressions are evaluated from left to right.


•The result of the entire comma-separated expression is the value of the last expression.
Command line arguments

You might also like