CProgramming Tutorial

  • C Programming Tutorial
  • C - Overview
  • C - Features
  • C - History
  • C - Environment Setup
  • C - Program Structure
  • C - Hello World
  • C - Compilation Process
  • C - Comments
  • C - Keywords
  • C - Identifiers
  • C - User Input
  • C - Basic Syntax
  • C - Data Types
  • C - Variables
  • C - Integer Promotions
  • C - Type Conversion
  • C - Constants
  • C - Literals
  • C - Escape sequences
  • C - Format Specifiers
  • C - Storage Classes
  • C - Operators
  • C - Decision Making
  • C - if statement
  • C - if...else statement
  • C - nested if statements
  • C - switch statement
  • C - nested switch statements
  • C - While loop
  • C - For loop
  • C - Do...while loop
  • C - Nested loop
  • C - Infinite loop
  • C - Break Statement
  • C - Continue Statement
  • C - goto Statement
  • C - Functions
  • C - Main Functions
  • C - Return Statement
  • C - Recursion
  • C - Scope Rules
  • C - Properties of Array
  • C - Multi-Dimensional Arrays
  • C - Passing Arrays to Function
  • C - Return Array from Function
  • C - Variable Length Arrays
  • C - Pointers
  • C - Pointer Arithmetics
  • C - Passing Pointers to Functions
  • C - Strings
  • C - Array of Strings
  • C - Structures
  • C - Structures and Functions
  • C - Arrays of Structures
  • C - Pointers to Structures
  • C - Self-Referential Structures
  • C - Nested Structures
  • C - Bit Fields
  • C - Typedef
  • C - Input & Output
  • C - File I/O
  • C - Preprocessors
  • C - Header Files
  • C - Type Casting
  • C - Error Handling
  • C - Variable Arguments
  • C - Memory Management
  • C - Command Line Arguments
  • C Programming Resources
  • C - Questions & Answers
  • C - Quick Guide
  • C - Useful Resources
  • C - Discussion
  • Selected Reading
  • UPSC IAS Exams Notes
  • Developer's Best Practices
  • Questions and Answers
  • Effective Resume Writing
  • HR Interview Questions
  • Computer Glossary

Assignment Operators in C

In C, the assignment operator stores a certain value in an already declared variable. A variable in C can be assigned the value in the form of a literal, another variable or an expression. The value to be assigned forms the right hand operand, whereas the variable to be assigned should be the operand to the left of = symbol, which is defined as a simple assignment operator in C. In addition, C has several augmented assignment operators.

The following table lists the assignment operators supported by the C language −

Simple assignment operator (=)

The = operator is the most frequently used operator in C. As per ANSI C standard, all the variables must be declared in the beginning. Variable declaration after the first processing statement is not allowed. You can declare a variable to be assigned a value later in the code, or you can initialize it at the time of declaration.

You can use a literal, another variable or an expression in the assignment statement.

Once a variable of a certain type is declared, it cannot be assigned a value of any other type. In such a case the C compiler reports a type mismatch error.

In C, the expressions that refer to a memory location are called "lvalue" expressions. A lvalue may appear as either the left-hand or right-hand side of an assignment.

On the other hand, the term rvalue refers to a data value that is stored at some address in memory. A rvalue is an expression that cannot have a value assigned to it which means an rvalue may appear on the right-hand side but not on the left-hand side of an assignment.

Variables are lvalues and so they may appear on the left-hand side of an assignment. Numeric literals are rvalues and so they may not be assigned and cannot appear on the left-hand side. Take a look at the following valid and invalid statements −

Augmented assignment operators

In addition to the = operator, C allows you to combine arithmetic and bitwise operators with the = symbol to form augmented or compound assignment operator. The augmented operators offer a convenient shortcut for combining arithmetic or bitwise operation with assignment.

For example, the expression a+=b has the same effect of performing a+b first and then assigning the result back to the variable a.

Similarly, the expression a<<=b has the same effect of performing a<<b first and then assigning the result back to the variable a.

Here is a C program that demonstrates the use of assignment operators in C:

When you compile and execute the above program, it produces the following result −

Next: Execution Control Expressions , Previous: Arithmetic , Up: Top   [ Contents ][ Index ]

7 Assignment Expressions

As a general concept in programming, an assignment is a construct that stores a new value into a place where values can be stored—for instance, in a variable. Such places are called lvalues (see Lvalues ) because they are locations that hold a value.

An assignment in C is an expression because it has a value; we call it an assignment expression . A simple assignment looks like

We say it assigns the value of the expression value-to-store to the location lvalue , or that it stores value-to-store there. You can think of the “l” in “lvalue” as standing for “left,” since that’s what you put on the left side of the assignment operator.

However, that’s not the only way to use an lvalue, and not all lvalues can be assigned to. To use the lvalue in the left side of an assignment, it has to be modifiable . In C, that means it was not declared with the type qualifier const (see const ).

The value of the assignment expression is that of lvalue after the new value is stored in it. This means you can use an assignment inside other expressions. Assignment operators are right-associative so that

is equivalent to

This is the only useful way for them to associate; the other way,

would be invalid since an assignment expression such as x = y is not valid as an lvalue.

Warning: Write parentheses around an assignment if you nest it inside another expression, unless that is a conditional expression, or comma-separated series, or another assignment.

C Programming Tutorial

  • Assignment Operator in C

Last updated on July 27, 2020

We have already used the assignment operator ( = ) several times before. Let's discuss it here in detail. The assignment operator ( = ) is used to assign a value to the variable. Its general format is as follows:

The operand on the left side of the assignment operator must be a variable and operand on the right-hand side must be a constant, variable or expression. Here are some examples:

The precedence of the assignment operator is lower than all the operators we have discussed so far and it associates from right to left.

We can also assign the same value to multiple variables at once.

here x , y and z are initialized to 100 .

Since the associativity of the assignment operator ( = ) is from right to left. The above expression is equivalent to the following:

Note that expressions like:

are called assignment expression. If we put a semicolon( ; ) at the end of the expression like this:

then the assignment expression becomes assignment statement.

Compound Assignment Operator #

Assignment operations that use the old value of a variable to compute its new value are called Compound Assignment.

Consider the following two statements:

Here the second statement adds 5 to the existing value of x . This value is then assigned back to x . Now, the new value of x is 105 .

To handle such operations more succinctly, C provides a special operator called Compound Assignment operator.

The general format of compound assignment operator is as follows:

where op can be any of the arithmetic operators ( + , - , * , / , % ). The above statement is functionally equivalent to the following:

Note : In addition to arithmetic operators, op can also be >> (right shift), << (left shift), | (Bitwise OR), & (Bitwise AND), ^ (Bitwise XOR). We haven't discussed these operators yet.

After evaluating the expression, the op operator is then applied to the result of the expression and the current value of the variable (on the RHS). The result of this operation is then assigned back to the variable (on the LHS). Let's take some examples: The statement:

is equivalent to x = x + 5; or x = x + (5); .

Similarly, the statement:

is equivalent to x = x * 2; or x = x * (2); .

Since, expression on the right side of op operator is evaluated first, the statement:

is equivalent to x = x * (y + 1) .

The precedence of compound assignment operators are same and they associate from right to left (see the precedence table ).

The following table lists some Compound assignment operators:

The following program demonstrates Compound assignment operators in action:

Expected Output:

Load Comments

  • Intro to C Programming
  • Installing Code Blocks
  • Creating and Running The First C Program
  • Basic Elements of a C Program
  • Keywords and Identifiers
  • Data Types in C
  • Constants in C
  • Variables in C
  • Input and Output in C
  • Formatted Input and Output in C
  • Arithmetic Operators in C
  • Operator Precedence and Associativity in C
  • Increment and Decrement Operators in C
  • Relational Operators in C
  • Logical Operators in C
  • Conditional Operator, Comma operator and sizeof() operator in C
  • Implicit Type Conversion in C
  • Explicit Type Conversion in C
  • if-else statements in C
  • The while loop in C
  • The do while loop in C
  • The for loop in C
  • The Infinite Loop in C
  • The break and continue statement in C
  • The Switch statement in C
  • Function basics in C
  • The return statement in C
  • Actual and Formal arguments in C
  • Local, Global and Static variables in C
  • Recursive Function in C
  • One dimensional Array in C
  • One Dimensional Array and Function in C
  • Two Dimensional Array in C
  • Pointer Basics in C
  • Pointer Arithmetic in C
  • Pointers and 1-D arrays
  • Pointers and 2-D arrays
  • Call by Value and Call by Reference in C
  • Returning more than one value from function in C
  • Returning a Pointer from a Function in C
  • Passing 1-D Array to a Function in C
  • Passing 2-D Array to a Function in C
  • Array of Pointers in C
  • Void Pointers in C
  • The malloc() Function in C
  • The calloc() Function in C
  • The realloc() Function in C
  • String Basics in C
  • The strlen() Function in C
  • The strcmp() Function in C
  • The strcpy() Function in C
  • The strcat() Function in C
  • Character Array and Character Pointer in C
  • Array of Strings in C
  • Array of Pointers to Strings in C
  • The sprintf() Function in C
  • The sscanf() Function in C
  • Structure Basics in C
  • Array of Structures in C
  • Array as Member of Structure in C
  • Nested Structures in C
  • Pointer to a Structure in C
  • Pointers as Structure Member in C
  • Structures and Functions in C
  • Union Basics in C
  • typedef statement in C
  • Basics of File Handling in C
  • fputc() Function in C
  • fgetc() Function in C
  • fputs() Function in C
  • fgets() Function in C
  • fprintf() Function in C
  • fscanf() Function in C
  • fwrite() Function in C
  • fread() Function in C

Recent Posts

  • Machine Learning Experts You Should Be Following Online
  • 4 Ways to Prepare for the AP Computer Science A Exam
  • Finance Assignment Online Help for the Busy and Tired Students: Get Help from Experts
  • Top 9 Machine Learning Algorithms for Data Scientists
  • Data Science Learning Path or Steps to become a data scientist Final
  • Enable Edit Button in Shutter In Linux Mint 19 and Ubuntu 18.04
  • Python 3 time module
  • Pygments Tutorial
  • How to use Virtualenv?
  • Installing MySQL (Windows, Linux and Mac)
  • What is if __name__ == '__main__' in Python ?
  • Installing GoAccess (A Real-time web log analyzer)
  • Installing Isso

Home » Learn C Programming from Scratch » C Assignment Operators

C Assignment Operators

Summary : in this tutorial, you’ll learn about the C assignment operators and how to use them effectively.

Introduction to the C assignment operators

An assignment operator assigns the vale of the right-hand operand to the left-hand operand. The following example uses the assignment operator (=) to assign 1 to the counter variable:

After the assignmment, the counter variable holds the number 1.

The following example adds 1 to the counter and assign the result to the counter:

The = assignment operator is called a simple assignment operator. It assigns the value of the left operand to the right operand.

Besides the simple assignment operator, C supports compound assignment operators. A compound assignment operator performs the operation specified by the additional operator and then assigns the result to the left operand.

The following example uses a compound-assignment operator (+=):

The expression:

is equivalent to the following expression:

The following table illustrates the compound-assignment operators in C:

  • A simple assignment operator assigns the value of the left operand to the right operand.
  • A compound assignment operator performs the operation specified by the additional operator and then assigns the result to the left operand.

If Statement in C – How to use If-Else Statements in the C Programming Language

Dionysia Lemonaki

In the C programming language, you have the ability to control the flow of a program.

In particular, the program is able to make decisions on what it should do next. And those decisions are based on the state of certain pre-defined conditions you set.

The program will decide what the next steps should be based on whether the conditions are met or not.

The act of doing one thing if a particular condition is met and a different thing if that particular condition is not met is called control flow .

For example, you may want to perform an action under only a specific condition. And you may want to perform another action under an entirely different condition. Or, you may want to perform another, completely different action when that specific condition you set is not met.

To be able to do all of the above, and control the flow of a program, you will need to use an if statement.

In this article, you will learn all about the if statement – its syntax and examples of how to use it so you can understand how it works.

You will also learn about the if else statement – that is the else statement that is added to the if statement for additional program flexibility.

In addition, you will learn about the else if statement for when you want to add more choices to your conditions.

Here is what we will cover:

  • How to create an if statement in C
  • What is an example of an if statement?
  • What is an example of an if else statement?
  • What is an example of an else if statement?

What Is An if Statement In C?

An if statement is also known as a conditional statement and is used for decision-making. It acts as a fork in the road or a branch.

A conditional statement takes a specific action based on the result of a check or comparison that takes place.

So, all in all, the if statement makes a decision based on a condition.

The condition is a Boolean expression. A Boolean expression can only be one of two values – true or false.

If the given condition evaluates to true only then is the code inside the if block executed.

If the given condition evaluates to false , the code inside the if block is ignored and skipped.

How To Create An if statement In C – A Syntax Breakdown For Beginners

The general syntax for an if statement in C is the following:

Let's break it down:

  • You start an if statement using the if keyword.
  • Inside parentheses, you include a condition that needs checking and evaluating, which is always a Boolean expression. This condition will only evaluate as either true or false .
  • The if block is denoted by a set of curly braces, {} .
  • Inside the if block, there are lines of code – make sure the code is indented so it is easier to read.

What Is An Example Of An if Statement?

Next, let’s see a practical example of an if statement.

I will create a variable named age that will hold an integer value.

I will then prompt the user to enter their age and store the answer in the variable age .

Then, I will create a condition that checks whether the value contained in the variable age is less than 18.

If so, I want a message printed to the console letting the user know that to proceed, the user should be at least 18 years of age.

I compile the code using gcc conditionals.c , where gcc is the name of the C compiler and conditionals.c is the name of the file containing the C source code.

Then, to run the code I type ./a.out .

When asked for my age I enter 16 and get the following output:

The condition ( age < 18 ) evaluates to true so the code in the if block executes.

Then, I re-compile and re-run the program.

This time, when asked for my age, I enter 28 and get the following output:

Well... There is no output.

This is because the condition evaluates to false and therefore the body of the if block is skipped.

I have also not specified what should happen in the case that the user's age is greater than 18.

I could write another if statement that will print a message to the console if the user's age is greater than 18 so the code is a bit clearer:

I compile and run the code, and when prompted for my age I enter again 28:

This code works. That said, there is a better way to write it and you will see how to do that in the following section.

What Is An if else Statement in C?

Multiple if statements on their own are not helpful – especially as the programs grow larger and larger.

So, for that reason, an if statement is accompanied by an else statement.

The if else statement essentially means that " if this condition is true do the following thing, else do this thing instead".

If the condition inside the parentheses evaluates to true , the code inside the if block will execute. However, if that condition evaluates to false , the code inside the else block will execute.

The else keyword is the solution for when the if condition is false and the code inside the if block doesn't run. It provides an alternative.

The general syntax looks something like the following:

What Is An Example Of An if else Statement?

Now, let's revisit the example with the two separate if statements from earlier on:

Let's re-write it using an if else statement instead:

If the condition is true the code in the if block runs:

If the condition is false the code in the if block is skipped and the code in the else block runs instead:

What Is An else if Statement?

But what happens when you want to have more than one condition to choose from?

If you wish to chose between more than one option and want to have a greater variety in actions, then you can introduce an else if statement.

An else if statement essentially means that "If this condition is true, do the following. If it isn't, do this instead. However, if none of the above is true and all else fails, finally do this."

What Is An Example Of An else if Statement?

Let's see how an else if statement works.

Say you have the following example:

If the first if statement is true, the rest of the block will not run:

If the first if statement is false, then the program moves on to the next condition.

If that is true the code inside the else if block executes and the rest of the block doesn't run:

If both of the previous conditions are all false, then the last resort is the else block which is the one to execute:

And there you have it – you now know the basics of if , if else , and else if statements in C!

I hope you found this article helpful.

To learn more about the C programming language, check out the following free resources:

  • C Programming Tutorial for Beginners
  • What is The C Programming Language? A Tutorial for Beginners
  • The C Beginner's Handbook: Learn C Programming Language basics in just a few hours

Thank you so much for reading and happy coding :)

Read more posts .

If this article was helpful, share it .

Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started

Codeforwin

Assignment and shorthand assignment operator in C

Quick links.

  • Shorthand assignment

Assignment operator is used to assign value to a variable (memory location). There is a single assignment operator = in C. It evaluates expression on right side of = symbol and assigns evaluated value to left side the variable.

For example consider the below assignment table.

The RHS of assignment operator must be a constant, expression or variable. Whereas LHS must be a variable (valid memory location).

Shorthand assignment operator

C supports a short variant of assignment operator called compound assignment or shorthand assignment. Shorthand assignment operator combines one of the arithmetic or bitwise operators with assignment operator.

For example, consider following C statements.

The above expression a = a + 2 is equivalent to a += 2 .

Similarly, there are many shorthand assignment operators. Below is a list of shorthand assignment operators in C.

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

C Assignment Operators

  • 6 contributors

An assignment operation assigns the value of the right-hand operand to the storage location named by the left-hand operand. Therefore, the left-hand operand of an assignment operation must be a modifiable l-value. After the assignment, an assignment expression has the value of the left operand but isn't an l-value.

assignment-expression :   conditional-expression   unary-expression assignment-operator assignment-expression

assignment-operator : one of   = *= /= %= += -= <<= >>= &= ^= |=

The assignment operators in C can both transform and assign values in a single operation. C provides the following assignment operators:

In assignment, the type of the right-hand value is converted to the type of the left-hand value, and the value is stored in the left operand after the assignment has taken place. The left operand must not be an array, a function, or a constant. The specific conversion path, which depends on the two types, is outlined in detail in Type Conversions .

  • Assignment Operators

Was this page helpful?

Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see: https://aka.ms/ContentUserFeedback .

Submit and view feedback for

Additional resources

cppreference.com

If statement.

Conditionally executes another statement.

Used where code needs to be executed based on a run-time or compile-time (since C++17) condition , or whether the if statement is evaluated in a manifestly constant-evaluated context (since C++23) .

[ edit ] Syntax

  • expression which is contextually convertible to bool
  • declaration of a single non-array variable with a brace-or-equals initializer .
  • is evaluated in a manifestly constant-evaluated context , if ! is not preceding consteval
  • is not evaluated in a manifestly constant-evaluated context, if ! is preceding consteval
  • is not evaluated in a manifestly constant-evaluated context , if ! is not preceding consteval
  • is evaluated in a manifestly constant-evaluated context, if ! is preceding consteval

[ edit ] Explanation

If the condition yields true after conversion to bool , statement-true is executed.

If the else part of the if statement is present and condition yields false after conversion to bool , statement-false is executed.

In the second form of if statement (the one including else), if statement-true is also an if statement then that inner if statement must contain an else part as well (in other words, in nested if-statements, the else is associated with the closest if that doesn't have an else).

except that names declared by the init-statement (if init-statement is a declaration) and names declared by condition (if condition is a declaration) are in the same scope, which is also the scope of both statement s.

both compound-statement and statement (if any) must be compound statements.

If statement is not a compound statement, it will still be treated as a part of the consteval if statement (and thus results in a compilation error):

If a consteval if statement is evaluated in a manifestly constant-evaluated context , compound-statement is executed. Otherwise, statement is executed if it is present.

A case or default label appearing within a consteval if statement shall be associated with a switch statement within the same if statement. A label declared in a substatement of a consteval if statement shall only be referred to by a statement in the same substatement.

If the statement begins with if !consteval , the compound-statement and statement (if any) must both be compound statements. Such statements are not considered consteval if statements, but are equivalent to consteval if statements:

  • if ! consteval { /*stmt*/ } is equivalent to if consteval { } else { /*stmt*/ } .
  • if ! consteval { /*stmt-1*/ } else { /*stmt-2*/ } is equivalent to if consteval { /*stmt-2*/ } else { /*stmt-1*/ } .

compound-statement in a consteval if statement (or statement in the negative form) is in an immediate function context , in which a call to an immediate function needs not to be a constant expression.

[ edit ] Notes

If statement-true or statement-false is not a compound statement, it is treated as if it were:

is the same as

The scope of the name introduced by condition , if it is a declaration, is the combined scope of both statements' bodies:

If statement-true is entered by goto or longjmp , condition is not evaluated and statement-false is not executed.

[ edit ] Keywords

if , else , constexpr , consteval

[ edit ] Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

[ edit ] See also

  • Todo no example
  • Recent changes
  • Offline version
  • What links here
  • Related changes
  • Upload file
  • Special pages
  • Printable version
  • Permanent link
  • Page information
  • In other languages
  • This page was last modified on 14 October 2023, at 21:37.
  • This page has been accessed 700,425 times.
  • Privacy policy
  • About cppreference.com
  • Disclaimers

Powered by MediaWiki

  • Election 2024
  • Entertainment
  • Newsletters
  • Photography
  • Personal Finance
  • AP Investigations
  • AP Buyline Personal Finance
  • AP Buyline Shopping
  • Press Releases
  • Israel-Hamas War
  • Russia-Ukraine War
  • Global elections
  • Asia Pacific
  • Latin America
  • Middle East
  • Election Results
  • Delegate Tracker
  • AP & Elections
  • Auto Racing
  • 2024 Paris Olympic Games
  • Movie reviews
  • Book reviews
  • Personal finance
  • Financial Markets
  • Business Highlights
  • Financial wellness
  • Artificial Intelligence
  • Social Media

Baltimore leaders accuse ship’s owner and manager of negligence in Key Bridge collapse

People work on a standing section of the collapsed Francis Scott Key Bridge, Monday, April 15, 2024, in Baltimore. The FBI confirmed that agents were aboard the Dali conducting court-authorized law enforcement activity. (AP Photo/Julia Nikhinson)

People work on a standing section of the collapsed Francis Scott Key Bridge, Monday, April 15, 2024, in Baltimore. The FBI confirmed that agents were aboard the Dali conducting court-authorized law enforcement activity. (AP Photo/Julia Nikhinson)

Salvage work continues on the collapsed Francis Scott Key Bridge, Monday, April 15, 2024, in Baltimore. The FBI confirmed that agents were aboard the Dali conducting court-authorized law enforcement activity. (AP Photo/Julia Nikhinson)

The collapsed Francis Scott Key Bridge lays on top of the container ship Dali, Monday, April 15, 2024, in Baltimore. The FBI confirmed that agents were aboard the Dali conducting court-authorized law enforcement activity. (AP Photo/Julia Nikhinson)

People are seen aboard the container ship Dali, Monday, April 15, 2024, in Baltimore. The FBI confirmed that agents were aboard the Dali conducting court-authorized law enforcement activity. (AP Photo/Julia Nikhinson)

  • Copy Link copied

BALTIMORE (AP) — The owner and manager of the massive container ship that took down the Francis Scott Key Bridge last month should be held fully liable for the deadly collapse, according to court papers filed Monday on behalf of Baltimore’s mayor and city council.

The two companies filed a petition soon after the March 26 collapse asking a court to cap their liability under a pre-Civil War provision of an 1851 maritime law — a routine but important procedure for such cases. A federal court in Maryland will ultimately decide who’s responsible and how much they owe in what could become one of the most expensive maritime disasters in history.

Singapore-based Grace Ocean Private Ltd. owns the Dali, the vessel that veered off course and slammed into the bridge. Synergy Marine Pte Ltd., also based in Singapore, is the ship’s manager.

In their filing Monday, attorneys for the city accused them of negligence, arguing the companies should have realized the Dali was unfit for its voyage and manned the ship with a competent crew, among other issues.

The collapsed Francis Scott Key Bridge lay on top of the container ship Dali, Monday, April 15, 2024, in Baltimore. The FBI confirmed that agents were aboard the Dali conducting court-authorized law enforcement activity. (AP Photo/Julia Nikhinson)

A spokesperson for the companies said Monday that it would be inappropriate to comment on the pending litigation.

The ship was headed to Sri Lanka when it lost power shortly after leaving Baltimore and struck one of the bridge’s support columns, collapsing the span and sending six members of a roadwork crew plunging to their deaths.

“For more than four decades, cargo ships made thousands of trips every year under the Key Bridge without incident,” the city’s complaint reads. “There was nothing about March 26, 2024 that should have changed that.”

FBI agents boarded the stalled ship last week amid a criminal investigation. A separate federal probe by the National Transportation Safety Board will include an inquiry into whether the ship experienced power issues before starting its voyage, officials have said. That investigation will focus generally on the Dali’s electrical system.

In their earlier petition, Grace Ocean and Synergy sought to cap their liability at roughly $43.6 million. The petition estimates that the vessel itself is valued at up to $90 million and was owed over $1.1 million in income from freight. The estimate also deducts two major expenses: at least $28 million in repair costs and at least $19.5 million in salvage costs.

Grace Ocean also recently initiated a process requiring owners of the cargo on board to cover some of the salvage costs. The company made a “general average” declaration , which allows a third-party adjuster to determine what each stakeholder should contribute.

Baltimore leaders argue the ship’s owner and manager should be held responsible for their role in the disaster, which has halted most maritime traffic through the Port of Baltimore and disrupted an important east coast trucking route. The economic impacts could be devastating for the Baltimore region, the filing says.

“Petitioners’ negligence caused them to destroy the Key Bridge, and singlehandedly shut down the Port of Baltimore, a source of jobs, municipal revenue, and no small amount of pride for the City of Baltimore and its residents,” the attorneys wrote.

Lawyers representing victims of the collapse and their families also have pledged to hold the companies accountable and oppose their request for limited liability.

In the meantime, salvage crews are working to remove thousands of tons of collapsed steel and concrete from the Patapsco River. They’ve opened three temporary channels to allow some vessels to pass through the area, but the port’s main shipping channel is expected to remain closed for several more weeks.

c assignment operator in if statement

  • C++ Data Types
  • C++ Input/Output
  • C++ Pointers
  • C++ Interview Questions
  • C++ Programs
  • C++ Cheatsheet
  • C++ Projects
  • C++ Exception Handling
  • C++ Memory Management

Assignment Operators In C++

  • Move Assignment Operator in C++ 11
  • JavaScript Assignment Operators
  • Assignment Operators in Programming
  • Is assignment operator inherited?
  • Solidity - Assignment Operators
  • Augmented Assignment Operators in Python
  • bitset operator[] in C++ STL
  • C++ Assignment Operator Overloading
  • Self assignment check in assignment operator
  • Copy Constructor vs Assignment Operator in C++
  • Operators in C++
  • C++ Arithmetic Operators
  • Bitwise Operators in C++
  • Casting Operators in C++
  • Assignment Operators in C
  • Assignment Operators in Python
  • Compound assignment operators in Java
  • Arithmetic Operators in C
  • Operators in C
  • Vector in C++ STL
  • Initialize a vector in C++ (7 different ways)
  • Map in C++ Standard Template Library (STL)
  • std::sort() in C++ STL
  • Inheritance in C++
  • The C++ Standard Template Library (STL)
  • Object Oriented Programming in C++
  • C++ Classes and Objects
  • Virtual Function in C++
  • Set in C++ Standard Template Library (STL)

In C++, the assignment operator forms the backbone of many algorithms and computational processes by performing a simple operation like assigning a value to a variable. It is denoted by equal sign ( = ) and provides one of the most basic operations in any programming language that is used to assign some value to the variables in C++ or in other words, it is used to store some kind of information.

The right-hand side value will be assigned to the variable on the left-hand side. The variable and the value should be of the same data type.

The value can be a literal or another variable of the same data type.

Compound Assignment Operators

In C++, the assignment operator can be combined into a single operator with some other operators to perform a combination of two operations in one single statement. These operators are called Compound Assignment Operators. There are 10 compound assignment operators in C++:

  • Addition Assignment Operator ( += )
  • Subtraction Assignment Operator ( -= )
  • Multiplication Assignment Operator ( *= )
  • Division Assignment Operator ( /= )
  • Modulus Assignment Operator ( %= )
  • Bitwise AND Assignment Operator ( &= )
  • Bitwise OR Assignment Operator ( |= )
  • Bitwise XOR Assignment Operator ( ^= )
  • Left Shift Assignment Operator ( <<= )
  • Right Shift Assignment Operator ( >>= )

Lets see each of them in detail.

1. Addition Assignment Operator (+=)

In C++, the addition assignment operator (+=) combines the addition operation with the variable assignment allowing you to increment the value of variable by a specified expression in a concise and efficient way.

This above expression is equivalent to the expression:

2. Subtraction Assignment Operator (-=)

The subtraction assignment operator (-=) in C++ enables you to update the value of the variable by subtracting another value from it. This operator is especially useful when you need to perform subtraction and store the result back in the same variable.

3. Multiplication Assignment Operator (*=)

In C++, the multiplication assignment operator (*=) is used to update the value of the variable by multiplying it with another value.

4. Division Assignment Operator (/=)

The division assignment operator divides the variable on the left by the value on the right and assigns the result to the variable on the left.

5. Modulus Assignment Operator (%=)

The modulus assignment operator calculates the remainder when the variable on the left is divided by the value or variable on the right and assigns the result to the variable on the left.

6. Bitwise AND Assignment Operator (&=)

This operator performs a bitwise AND between the variable on the left and the value on the right and assigns the result to the variable on the left.

7. Bitwise OR Assignment Operator (|=)

The bitwise OR assignment operator performs a bitwise OR between the variable on the left and the value or variable on the right and assigns the result to the variable on the left.

8. Bitwise XOR Assignment Operator (^=)

The bitwise XOR assignment operator performs a bitwise XOR between the variable on the left and the value or variable on the right and assigns the result to the variable on the left.

9. Left Shift Assignment Operator (<<=)

The left shift assignment operator shifts the bits of the variable on the left to left by the number of positions specified on the right and assigns the result to the variable on the left.

10. Right Shift Assignment Operator (>>=)

The right shift assignment operator shifts the bits of the variable on the left to the right by a number of positions specified on the right and assigns the result to the variable on the left.

Also, it is important to note that all of the above operators can be overloaded for custom operations with user-defined data types to perform the operations we want.

Please Login to comment...

Similar reads.

  • Geeks Premier League 2023
  • Geeks Premier League

advertisewithusBannerImg

Improve your Coding Skills with Practice

 alt=

What kind of Experience do you want to share?

IMAGES

  1. Assignment Operators in C

    c assignment operator in if statement

  2. Assignment Operators in C

    c assignment operator in if statement

  3. Assignment Operators in C Example

    c assignment operator in if statement

  4. C Ternary Operator (With Examples)

    c assignment operator in if statement

  5. Assignment Operators in C++

    c assignment operator in if statement

  6. Operators In C Logicmojo

    c assignment operator in if statement

VIDEO

  1. PPS42: Conditional Branching in C

  2. Assignment Operator in C Programming

  3. Assignment Operator in C Programming

  4. NPTEL Problem Solving through Programming in C ASSIGNMENT 6 ANSWERS 2024

  5. JS Coding Assignment-2

  6. Lecture 5 (Part1): Decision Making and Branching

COMMENTS

  1. C assignments in an 'if' statement

    Basically C evaluates expressions. In. s = data[q] The value of data[q] is the the value of expression here and the condition is evaluated based on that. The assignment. s <- data[q] is just a side-effect.

  2. Assignment Operators in C

    1. "=": This is the simplest assignment operator. This operator is used to assign the value on the right to the variable on the left. Example: a = 10; b = 20; ch = 'y'; 2. "+=": This operator is combination of '+' and '=' operators. This operator first adds the current value of the variable on left to the value on the right and ...

  3. Assignment Operators in C

    Assigns values from right side operands to left side operand. C = A + B will assign the value of A + B to C. +=. Add AND assignment operator. It adds the right operand to the left operand and assign the result to the left operand. C += A is equivalent to C = C + A. -=. Subtract AND assignment operator.

  4. Assignment Expressions (GNU C Language Manual)

    An assignment in C is an expression because it has a value; we call it an assignment expression. A simple assignment looks like. lvalue = value-to-store. We say it assigns the value of the expression value-to-store to the location lvalue, or that it stores value-to-store there. You can think of the "l" in "lvalue" as standing for ...

  5. If...Else Statement in C Explained

    Conditional code flow is the ability to change the way a piece of code behaves based on certain conditions. In such situations you can use if statements.. The if statement is also known as a decision making statement, as it makes a decision on the basis of a given condition or expression. The block of code inside the if statement is executed is the condition evaluates to true.

  6. Assignment Operator in C

    The assignment operator ( = ) is used to assign a value to the variable. Its general format is as follows: variable = right_side. The operand on the left side of the assignment operator must be a variable and operand on the right-hand side must be a constant, variable or expression. Here are some examples:

  7. Assignment operators

    for assignments to class type objects, the right operand could be an initializer list only when the assignment is defined by a user-defined assignment operator. removed user-defined assignment constraint. CWG 1538. C++11. E1 ={E2} was equivalent to E1 = T(E2) ( T is the type of E1 ), this introduced a C-style cast. it is equivalent to E1 = T{E2}

  8. Assignment Operators in Programming

    Assignment operators are used in programming to assign values to variables. We use an assignment operator to store and update data within a program. They enable programmers to store data in variables and manipulate that data. The most common assignment operator is the equals sign (=), which assigns the value on the right side of the operator to ...

  9. C Assignment Operators

    Code language:C++(cpp) The = assignment operator is called a simple assignment operator. It assigns the value of the left operand to the right operand. Besides the simple assignment operator, C supports compound assignment operators. A compound assignment operator performs the operation specified by the additional operator and then assigns the ...

  10. Assignment operators

    Assignment performs implicit conversion from the value of rhs to the type of lhs and then replaces the value in the object designated by lhs with the converted value of rhs . Assignment also returns the same value as what was stored in lhs (so that expressions such as a = b = c are possible). The value category of the assignment operator is non ...

  11. If Statement in C

    The if else statement essentially means that " if this condition is true do the following thing, else do this thing instead". If the condition inside the parentheses evaluates to true, the code inside the if block will execute. However, if that condition evaluates to false, the code inside the else block will execute.

  12. Assignment and shorthand assignment operator in C

    C supports a short variant of assignment operator called compound assignment or shorthand assignment. Shorthand assignment operator combines one of the arithmetic or bitwise operators with assignment operator. For example, consider following C statements. The above expression a = a + 2 is equivalent to a += 2.

  13. C

    The working of the if statement in C is as follows: STEP 1: When the program control comes to the if statement, the test expression is evaluated. STEP 2A: If the condition is true, the statements inside the if block are executed. STEP 2B: If the expression is false, the statements inside the if body are not executed. STEP 3: Program control moves out of the if block and the code after the if ...

  14. Can we put assignment operator in if condition?

    zeeshan. 13 Answers. Answer. + 8. Yes you can put assignment operator (=) inside if conditional statement (C/C++) and its boolean type will be always evaluated to true since it will generate side effect to variables inside in it. And if you use equality relational operator (==) then its boolean type will be evaluated to true or false depending ...

  15. C Assignment Operators

    The assignment operators in C can both transform and assign values in a single operation. C provides the following assignment operators: | =. In assignment, the type of the right-hand value is converted to the type of the left-hand value, and the value is stored in the left operand after the assignment has taken place.

  16. if statement

    Explanation. If the condition yields true after conversion to bool, statement-true is executed.. If the else part of the if statement is present and condition yields false after conversion to bool, statement-false is executed.. In the second form of if statement (the one including else), if statement-true is also an if statement then that inner if statement must contain an else part as well ...

  17. c

    Inside the if statement there is a valid assignment. The result of such operation is the assigned value (5) which is valid inside an if statement and evaluates to true. Actually, any number other than 0 will be interpreted as true. Maybe you already know, but you are not making a comparison there. The comparison operation is ==.

  18. c#

    The problem (with the syntax) is not with the assignment, as the assignment operator in C# is a valid expression. Rather, it is with the desired declaration as declarations are statements. If I must write code like that I will sometimes (depending upon the larger context) write the code like this:

  19. Baltimore Key Bridge collapse: City leaders accuse ship owner, manager

    BALTIMORE (AP) — The owner and manager of the massive container ship that took down the Francis Scott Key Bridge last month should be held fully liable for the deadly collapse, according to court papers filed Monday on behalf of Baltimore's mayor and city council.

  20. Assignment Operators In C++

    In C++, the assignment operator can be combined into a single operator with some other operators to perform a combination of two operations in one single statement. These operators are called Compound Assignment Operators. There are 10 compound assignment operators in C++: Addition Assignment Operator ( += ) Subtraction Assignment Operator ( -= )