100% found this document useful (1 vote)
23 views74 pages

C Programming Language Overview

The document provides an overview of the C programming language, detailing its history, features, and usage. It explains the structure of C programs, the environment setup, data types, variables, and operators, along with examples of code. C is highlighted as a versatile and widely-used language for system programming and application development.
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
100% found this document useful (1 vote)
23 views74 pages

C Programming Language Overview

The document provides an overview of the C programming language, detailing its history, features, and usage. It explains the structure of C programs, the environment setup, data types, variables, and operators, along with examples of code. C is highlighted as a versatile and widely-used language for system programming and application development.
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

C Language Overview

• C programming language is a general-purpose, high-level language that


was originally developed by Dennis M. Ritchie to develop the UNIX
operating system at Bell Labs.

• C was originally first implemented on the DEC PDP-11 computer in 1972.

• In 1978, Brian Kernighan and Dennis Ritchie produced the first publicly
available description of C, now known as the K&R standard.

• The UNIX operating system, the C compiler, and essentially all UNIX
applications programs have been written in C.

• The C has now become a widely used professional language for various
reasons.
Continue…
 Easy to learn
 Structured language
 It produces efficient programs.
 It can handle low-level activities.
 It can be compiled on a variety of computer platforms.

Facts about C
• C was invented to write an operating system called UNIX.

• C is a successor of B language, which was introduced around 1970.

• The language was formalized in 1988 by the American National Standard


Institute. (ANSI).

• The UNIX OS was totally written in C by 1973.


Continue..

• Today, C is the most widely used and popular


System Programming Language.

• Most of the state-of-the-art software's have been


implemented using C.

• Today's most popular Linux OS and RBDMS MySQL


have been written in C.
Why to use C?
• C was initially used for system development work, in
particular the programs that make up the operating system.
• C was adopted as a system development language because
it produces code that runs nearly as fast as code written in
assembly language.
• Some examples of the use of C might be:
• Operating Systems, Language Compilers, Assemblers, Text
Editors, Print Spoolers, Network Drivers, Modern Programs,
Databases, Language Interpreters, Utilities
C Programs

• A C program can vary from 3 lines to millions of


lines and it should be written into one or more text
files with extension ".c";

• for example, hello.c.

• You can use "vi", "vim" or any other text editor to


write your C program into a file.
C Environment Setup
Text Editor
• This will be used to type your program. Examples of few editors include
Windows Notepad, OS Edit command, Brief, Epsilon, EMACS, and vim or vi.

• Name and version of text editor can vary on different operating systems.

• For example, Notepad will be used on Windows, and

• vim or vi can be used on windows as well as Linux or UNIX.

• The files you create with your editor are called source files and contain
program source code.

• The source files for C programs are typically named with the extension “.c”.
The C Compiler
• The source code written in source file is the human
readable source for your program.

• It needs to be "compiled", to turn into machine language so


that your CPU can actually execute the program as per
instructions given.

• This C programming language compiler will be used to


compile your source code into final executable program.
C Program Structure
• C Hello World Example:
• A C program basically consists of the following parts:
Preprocessor Commands
Functions
Variables
Statements & Expressions
Comments
• The first line of the program #include <stdio.h> is a

preprocessor command, which tells a C compiler to


Example 1: include stdio.h file before going to actual

compilation.

• The next line int main() is the main function where


#include <stdio.h>
program execution begins.
int main()
{ • The next line /*...*/ will be ignored by the compiler

/* my first program in C */ and it has been put to add additional comments in

printf("Hello, World! \n"); the program. So such lines are called comments in

return 0; the program.


}
• The next line printf(...) is another function available

in C which causes the message "Hello, World!" to

be displayed on the screen.

• The next line return 0; terminates main() function

and returns the value 0.


Compile & Execute C Program
Open a text editor and add the above-mentioned code.

Save the file as hello.c

Open a command prompt and go to the directory where you


saved the file.

Type gcc hello.c and press enter to compile your code.

If there are no errors in your code, the command prompt will take
you to the next line and would generate [Link] executable file.

Now, type [Link] to execute your program.

You will be able to see "Hello World" printed on the screen


C Keywords and Identifiers
1. Character set

• A character set is a set of alphabets, letters and some special characters


that are valid in C language.

2. Alphabets

Uppercase: A B C ................................... X Y Z

Lowercase: a b c ...................................... x y z

Note: C accepts both lowercase and uppercase alphabets as variables and


functions.

3. Digits

0123456789
Special Characters
C Keywords
• Keywords are predefined, reserved words used in programming
that have special meanings to the compiler. Keywords are part of
the syntax and they cannot be used as an identifier.

• For example: int money;

• Here, int is a keyword that indicates money is a variable of type


int (integer).

• As C is a case sensitive language, all keywords must be written in


lowercase. Here is a list of all keywords allowed in ANSI C.
Continue.

• Link: [Link]
keywords-c-language
C Identifiers
• Identifier refers to name given to entities such as variables, functions,
structures etc.

• Identifiers must be unique. They are created to give a unique name to


an entity to identify it during the execution of the program.

• For example:

int money;

double accountBalance;

• Here, money and accountBalance are identifiers.

Note: Also remember, identifier names must be different from keywords.


You cannot use int as an identifier because int is a keyword.
Rules for naming identifiers
• A valid identifier can have letters (both uppercase and lowercase
letters), digits and underscores.

• The first letter of an identifier should be either a letter or an


underscore.

• You cannot use keywords like int, while etc. as identifiers.

• There is no rule on how long an identifier can be. However, you may run
into problems in some compilers if the identifier is longer than 31
characters.

• You can choose any name as an identifier if you follow the above rule,
however, give meaningful names to identifiers that make sense.
Data Types
• Data types are declarations for variables.

• This determines the type and size of data associated


with variables.

• For example,

int myVar;

Here, myVar is a variable of int (integer) type. The size of


int is 4 bytes.
Continue..
Continue..
int
Integers are whole numbers that can have both zero, positive and negative
values but no decimal values.

For example, 0, -5, 10

We can use int for declaring an integer variable.

• Here, id is a variable of type integer.

• You can declare multiple variables at once in C programming.

• For Example: int id, age;

• The size of int is usually 4 bytes (32 bits).

• And, it can take 232 distinct states from -2147483648 to 2147483647.


float and double
• float and double are used to hold real numbers.

• For Example:
float salary;
double price;

• In C, floating-point numbers can also be represented in


exponential.

• For example,

float normalizationFactor = 22.442e2;


What's the difference between float and
double?

The size of float (single precision float data type) is 4

bytes. And the size of double (double precision float

data type) is 8 bytes.


char

• Keyword char is used for declaring character type


variables.

For example,

char test = 'h';

• The size of the character variable is 1 byte.


void
• void is an incomplete type. It means "nothing" or "no type".
You can think of void as absent.

• For example, if a function is not returning anything, its


return type should be void.

• Note that, you cannot create variables of void type.


short and long
• If you need to use a large number, you can use a type specifier long.

• Here's how:
long a;
long long b;
long double c;

• Here variables a and b can store integer values. And, c can store a
floating-point number.

• If you are sure, only a small integer ([−32,767, +32,767] range) will be
used, you can use short.
Continue..
short d;

 You can always check the size of a variable using the sizeof() operator.
Example:
#include <stdio.h> size of short = 2 bytes
int main() {
size of long = 8 bytes
short a;
long b;
size of long long = 8 bytes
long long c; size of long double= 16 bytes
long double d;
printf("size of short = %d bytes\n", sizeof(a));
printf("size of long = %d bytes\n", sizeof(b));
printf("size of long long = %d bytes\n", sizeof(c));
printf("size of long double= %d bytes\n", sizeof(d));
return 0;
}
Output
size of short = 2 bytes
size of long = 8 bytes
size of long long = 8 bytes
size of long double= 16 bytes
signed and unsigned
• In C, signed and unsigned are type modifiers. You can alter the data storage of a
data type by using them:

• signed - allows for storage of both positive and negative numbers

• unsigned - allows for storage of only positive numbers

For Example:

// valid codes

unsigned int x = 35;

int y = -35; // signed int

int z = 36; // signed int

// invalid code: unsigned int cannot hold negative integers

unsigned int num = -35;


Continue..

• Here, the variables x and num can hold only zero and positive
values because we have used the unsigned modifier.

• Considering the size of int is 4 bytes, variable y can hold values


from -231 to 231-1, whereas variable x can hold values from 0 to
232-1.

• Derived Data Types

• Data types that are derived from fundamental data types are
derived types. For example: arrays, pointers, function types,
structures, etc.
Difference Between Keywords and
Datatype
• Keywords are the reserved words in a programming
language. They cannot be used as a variable name
inside of your program.
• Datatype are keyword which define what type of data a
particular variable can hold.
• Ex. for keywords : for, int ,double, float , switch, while
• Ex. for datatype : int, double, float, char, long etc.
Continue..
• Keyword is a syntactical concept. A keyword is a sequence
of characters which has a special meaning for the compiler.

• A data type is way of classifying data.

example:

• ‘if’ is keyword that represents a control structure

• ‘int’ is also a keyword and it represents the data type


integer.
Variables
• In programming, a variable is a container (storage area) to
hold data.
• To indicate the storage area, each variable should be given a
unique name (identifier). Variable names are just the
symbolic representation of a memory location.
• For example: int playerScore = 95;
• Here, playerScore is a variable of int type. Here, the variable
is assigned an integer value 95.
• The value of a variable can be changed, hence the name
variable.
• For example:
char ch = 'a';
// some code
ch = 'l';
Rules for naming a variable
• A variable name can only have letters (both uppercase and
lowercase letters), digits and underscore.
• The first letter of a variable should be either a letter or an
underscore.
• There is no rule on how long a variable name (identifier) can be.
However, you may run into problems in some compilers if the
variable name is longer than 31 characters.
• Note: You should always try to give meaningful names to
variables. For example: firstName is a better variable name than
fn.
• C is a strongly typed language. This means that the variable type
cannot be changed once it is declared.
• For example:
int number = 5; // integer variable
number = 5.5; // error
double number; // error
Literals
• Literals are data used for representing fixed values. They can be used directly in
the code.

For example: 1, 2.5, 'c' etc.

 Here, 1, 2.5 and 'c' are literals.

 Why? You cannot assign different values to these terms.

1. Integers

• An integer is a numeric literal(associated with numbers) without any fractional


or exponential part. There are three types of integer literals in C programming:

 decimal (base 10)

 octal (base 8)

 hexadecimal (base 16)


Continue..
• For example:
Decimal: 0, -9, 22 etc.
Octal: 021, 077, 033 etc.
Hexadecimal: 0x7f, 0x2a, 0x521 etc.
Note: In C programming, octal starts with a 0, and hexadecimal
starts with a 0x.
2. Floating-point Literals
A floating-point literal is a numeric literal that has either a fractional
form or an exponent form.
For example:
-2.0
0.0000234
-0.22E-5
Continue..
3. Characters
A character literal is created by enclosing a single character
inside single quotation marks.
For example: 'a', 'm', 'F', '2', '}' etc.
4. Escape Sequences
Sometimes, it is necessary to use characters that cannot be
typed or has special meaning in C programming. For
example: newline(enter), tab, question mark etc.
In order to use these characters, escape sequences are
used.
Continue..
Continue.

5. String Literals
A string literal is a sequence of characters enclosed in double-quote marks.
For example:

"good" //string constant


"" //null string constant
" " //string constant of six white space
"x" //string constant having a single character.
"Earth is round\n" //prints string with a newline
Constants
• If you want to define a variable whose value cannot
be changed, you can use the const keyword.
• This will create a constant.
• For example,
const double PI = 3.14;
Notice, we have added keyword const.
Here, PI is a symbolic constant; its value cannot be
changed.
const double PI = 3.14;
PI = 2.9; //Error
C Programming Operators
• An operator is a symbol that operates on a value or a
variable.
For example: + is an operator to perform addition.
• C has a wide range of operators to perform various
operations.
C Arithmetic Operators
• An arithmetic operator performs mathematical operations
such as addition, subtraction, multiplication, division etc. on
numerical values (constants and variables).
Continue..
Example 1:
// Working of arithmetic operators Output:
#include <stdio.h>
int main() a+b = 13
{ a-b = 5
int a = 9,b = 4, c; a*b = 36
a/b = 2
c = a+b;
Remainder when a
printf("a+b = %d \n",c); divided by b=1
c = a-b;
printf("a-b = %d \n",c);
c = a*b;
printf("a*b = %d \n",c);
c = a/b;
printf("a/b = %d \n",c);
c = a%b;
printf("Remainder when a divided by b = %d \n",c);
return 0;
}
C Increment and Decrement Operators

• C programming has two operators increment ++


and decrement -- to change the value of an
operand (constant or variable) by 1.
• Increment ++ increases the value by 1 whereas
decrement -- decreases the value by 1. These two
operators are unary operators, meaning they only
operate on a single operand.
Example 2: Increment and Decrement
Operators
// Working of increment and decrement operators

#include <stdio.h>
int main()
{
int a = 10, b = 100;
float c = 10.5, d = 100.5;
printf("++a = %d \n", ++a); Output
printf("--b = %d \n", --b); ++a = 11
printf("++c = %f \n", ++c); --b = 99
printf("--d = %f \n", --d); ++c = 11.500000
--d = 99.500000
return 0;
}
C Assignment Operators
• An assignment operator is used for assigning a
value to a variable.
• The most common assignment operator is =
Example 3: Assignment Operators
// Working of assignment operators
#include <stdio.h>
int main()
{
int a = 5, c;
c = a; // c is 5
printf("c = %d\n", c);
c += a; // c is 10
printf("c = %d\n", c);
c -= a; // c is 5
printf("c = %d\n", c); Output
c *= a; // c is 25
c=5
printf("c = %d\n", c);
c = 10
c /= a; // c is 5 c=5
printf("c = %d\n", c); c = 25
c %= a; // c = 0 c=5
printf("c = %d\n", c); c=0
return 0;
}
C Relational Operators
• A relational operator checks the relationship between two
operands.
• If the relation is true, it returns 1; if the relation is false, it
returns value 0.
• Relational operators are used in decision making and loops.
Example 4: Relational Operators
// Working of relational operators
#include <stdio.h>
int main()
{ Output:
int a = 5, b = 5, c = 10;
printf("%d == %d is %d \n", a, b, a == b); 5 == 5 is 1
printf("%d == %d is %d \n", a, c, a == c); 5 == 10 is 0
5 > 5 is 0
printf("%d > %d is %d \n", a, b, a > b);
5 > 10 is 0
printf("%d > %d is %d \n", a, c, a > c); 5 < 5 is 0
printf("%d < %d is %d \n", a, b, a < b); 5 < 10 is 1
printf("%d < %d is %d \n", a, c, a < c); 5 != 5 is 0
printf("%d != %d is %d \n", a, b, a != b); 5 != 10 is 1
printf("%d != %d is %d \n", a, c, a != c); 5 >= 5 is 1
5 >= 10 is 0
printf("%d >= %d is %d \n", a, b, a >= b);
5 <= 5 is 1
printf("%d >= %d is %d \n", a, c, a >= c); 5 <= 10 is 1
printf("%d <= %d is %d \n", a, b, a <= b);
printf("%d <= %d is %d \n", a, c, a <= c);
return 0;
}
C Logical Operators
• An expression containing logical operator returns
either 0 or 1 depending upon whether expression
results true or false.
• Logical operators are commonly used in decision
making in C programming.
Example 5: 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); Output
printf("(a == b) && (c < b) is %d \n", result);
(a == b) && (c > b) is 1
result = (a == b) || (c < b); (a == b) && (c < b) is 0
printf("(a == b) || (c < b) is %d \n", result); (a == b) || (c < b) is 1
result = (a != b) || (c < b); (a != b) || (c < b) is 0
!(a != b) is 1
printf("(a != b) || (c < b) is %d \n", result);
!(a == b) is 0
result = !(a != b);
printf("!(a != b) is %d \n", result);
result = !(a == b);
printf("!(a == b) is %d \n", result);
return 0;
}
Continue..
 (a == b) && (c > 5) evaluates to 1 because both operands (a == b) and (c
> b) is 1 (true).

 (a == b) && (c < b) evaluates to 0 because operand (c < b) is 0 (false).

 (a == b) || (c < b) evaluates to 1 because (a = b) is 1 (true).

 (a != b) || (c < b) evaluates to 0 because both operand (a != b) and (c <


b) are 0 (false).

 !(a != b) evaluates to 1 because operand (a != b) is 0 (false). Hence, !(a


!= b) is 1 (true).

 !(a == b) evaluates to 0 because (a == b) is 1 (true). Hence, !(a == b) is 0


(false).
C Bitwise Operators
• During computation, mathematical operations like:
addition, subtraction, multiplication, division, etc.
are converted to bit-level which makes processing
faster and saves power.
• Bitwise operators are used in C programming to
perform bit-level operations.
Comma Operator & sizeof Operator

• Comma operators are used to link related


expressions together.

For example: int a, c = 5, d;

• The sizeof is a unary operator that returns the size


of data (constants, variables, array, structure, etc).
Example 6: sizeof Operator

#include <stdio.h> Output:

int main() Size of int = 4 bytes


{ Size of float = 4 bytes
Size of double = 8 bytes
int a; Size of char = 1 byte
float b;
double c;
char d;
printf("Size of int=%lu bytes\n",sizeof(a));
printf("Size of float=%lu bytes\n",sizeof(b));
printf("Size of double=%lu bytes\n",sizeof(c));
printf("Size of char=%lu byte\n",sizeof(d));
return 0;
}
C Conditional Operator
• The conditional operator is also known as a ternary
operator.
• The conditional statements are the decision-making
statements which depends upon the output of the
expression.
• It is represented by two symbols, i.e., '?' and ':'.
• As conditional operator works on three operands, so it is
also known as the ternary operator.
• The behavior of the conditional operator is similar to the 'if-
else' statement as 'if-else' statement is also a decision-
making statement.
Example:
#include <stdio.h>
Output 1:
int main() {

int age; Enter your age: 12


You cannot vote
// take input from users

printf("Enter your age: ");

scanf("%d", &age);

// ternary operator to find if a person can vote or not

(age >= 18) ? printf("You can vote") : printf("You cannot vote");

return 0;

} If else
Conditional Operator Vs. if...else Statement in C
• In some of the cases, we can replace the if...else statement with a ternary
operator. This will make our code cleaner and shorter.
C Expressions
• An expression is a formula in which operands are linked
to each other by the use of operators to compute a
value.

• An operand can be a function reference, a variable, an


array element or a constant.

Example: a-b;

• In the above expression, minus character (-) is an


operator, and a, and b are the two operands.
Types of expressions

• Each type of expression takes certain types of operands and


uses a specific set of operators. Evaluation of a particular
expression produces a specific value.
For example: x = 9/2 + a-b;
The entire above line is a statement, not an expression. The
portion after the equal is an expression.
Arithmetic Expressions
• An arithmetic expression is an expression that consists of
operands and arithmetic operators.
• An arithmetic expression computes a value of type int, float
or double.
• When an expression contains only integral operands, then it
is known as pure integer expression when it contains only
real operands, it is known as pure real expression, and when
it contains both integral and real operands, it is known as
mixed mode expression.
• Evaluation of Arithmetic Expressions
The expressions are evaluated by performing one operation
at a time.
The precedence and associativity of operators decide the
order of the evaluation of individual operations.
When individual operations are performed, the
following cases can be happened:

1. When both the operands are of type integer, then arithmetic will be performed, and the result of the
operation would be an integer value.

For example, 3/2 will yield 1 not 1.5 as the fractional part is ignored.

2. When both the operands are of type float, then arithmetic will be performed, and the result of the
operation would be a real value.

For example, 2.0/2.0 will yield 1.0, not 1.

3. If one operand is of type integer and another operand is of type real, then the mixed arithmetic will
be performed.

• In this case, the first operand is converted into a real operand, and then arithmetic is performed to
produce the real value.

For example, 6/2.0 will yield 3.0 as the first value of 6 is converted into 6.0 and then arithmetic is
performed to produce 3.0.
Example: 6*2/ (2+1 * 2/3 + 6) + 8 * (8/4)

Evaluation of expression Description of each operation


6*2/( 2+1 * 2/3 +6) +8 * (8/4) An expression is given.

6*2/(2+2/3 + 6) + 8 * (8/4) 2 is multiplied by 1, giving value 2.

6*2/(2+0+6) + 8 * (8/4) 2 is divided by 3, giving value 0.

6*2/ 8+ 8 * (8/4) 2 is added to 6, giving value 8.

6*2/8 + 8 * 2 8 is divided by 4, giving value 2.

12/8 +8 * 2 6 is multiplied by 2, giving value 12.

1+8*2 12 is divided by 8, giving value 1.

1 + 16 8 is multiplied by 2, giving value 16.

17 1 is added to 16, giving value 17.


Relational Expressions
• A relational expression is an expression used to compare
two operands.

• It is a condition which is used to decide whether the action


should be taken or not.

• In relational expressions, a numeric value cannot be


compared with the string value.

• The result of the relational expression can be either zero or


non-zero value. Here, the zero value is equivalent to a false
and non-zero value is equivalent to true.
Continue..
Relational Expression Description

This condition is used to check whether the


x is an even number or not. The relational
x%2 = = 0
expression results in value 1 if x is an even
number otherwise results in value 0.

It is used to check whether a is not equal to


a!=b b. This relational expression results in 1 if a
is not equal to b otherwise 0.

It is used to check whether the expression


a+b = = x+y
"a+b" is equal to the expression "x+y".

It is used to check whether the value of a is


a>=9
greater than or equal to 9.
Example:
#include <stdio.h>
int main()
{
int x=4;
if(x%2==0)
{
printf("The number x is even");
}
else
printf("The number x is not even");
return 0;
}
Logical Expressions
• A logical expression is an expression that computes
either a zero or non-zero value.
• It is a complex test condition to take a decision.
Logical Expressions Description

It is a test condition to check whether the x is greater


( x > 4 ) && ( x < 6 ) than 4 and x is less than 6. The result of the condition
is true only when both the conditions are true.

It is a test condition used to check whether x is greater


than 10 or y is less than 11. The result of the test
x > 10 || y <11
condition is true if either of the conditions holds true
value.

It is a test condition used to check whether x is not


! ( x > 10 ) && ( y = = 2 ) greater than 10 and y is equal to 2. The result of the
condition is true if both the conditions are true.
Simple program of "&&" operator
#include <stdio.h>
int main()
{
int x = 4;
int y = 10;
if ( (x <10) && (y>5))
{
printf("Condition is true");
}
else
printf("Condition is false");
return 0;
}
Simple Program of "||" operator
#include <stdio.h>
int main()
{
int x = 4;
int y = 9;
if ( (x <6) || (y>10))
{
printf("Condition is true");
}
else
printf("Condition is false");
return 0;
}
Conditional Expressions
• A conditional expression is an expression that returns 1 if the
condition is true otherwise 0.

• A conditional operator is also known as a ternary operator.

• The Syntax of Conditional operator

• Suppose exp1, exp2 and exp3 are three expressions.


Ex. exp1 ? exp2 : exp3
• The above expression is a conditional expression which is
evaluated on the basis of the value of the exp1 expression. If the
condition of the expression exp1 holds true, then the final
conditional expression is represented by exp2 otherwise
represented by exp3.
Program
#include<stdio.h>
#include<string.h>
int main()
{
int age = 25;
char status;
status = (age>22) ? 'M': 'U';
if(status == 'M')
printf("Married");
else
printf("Unmarried");
return 0;
}
Operators Precedence in C
• Operator precedence determines

 the grouping of terms in an expression

 and decides how an expression is evaluated.

 Certain operators have higher precedence than others;

• The multiplication operator has a higher precedence than the addition


operator.

• For example,

x = 7 + 3 * 2;

• here, x is assigned 13, not 20 because operator * has a higher precedence


than +, so it first gets multiplied with 3*2 and then adds into 7.
Continue..
Category Operator Associativity

Postfix () [] -> . ++ - - Left to right

Unary + - ! ~ ++ - - (type)* & sizeof Right to left

Multiplicative */% Left to right

Additive +- Left to right

Shift << >> Left to right

Relational < <= > >= Left to right

Equality == != Left to right

Bitwise AND & Left to right

Bitwise XOR ^ Left to right


Continue..

Bitwise OR | Left to right

Logical AND && Left to right

Logical OR || Left to right

Conditional ?: Right to left

= += -= *= /= %=>>= <<=
Assignment Right to left
&= ^= |=

Comma , Left to right


Example to understand operator precedence in C
#include <stdio.h>
main() {
int a = 20;
int b = 10;
int c = 15;
int d = 5;
int e;
e = (a + b) * c / d; // ( 30 * 15 ) / 5
printf("Value of (a + b) * c / d is : %d\n", e );
e = ((a + b) * c) / d; // (30 * 15 ) / 5
printf("Value of ((a + b) * c) / d is : %d\n" , e );
e = (a + b) * (c / d); // (30) * (15/5)
printf("Value of (a + b) * (c / d) is : %d\n", e );
e = a + (b * c) / d; // 20 + (150/5)
printf("Value of a + (b * c) / d is : %d\n" , e );
return 0;
}

You might also like