Operators

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 17

Variables, Operators

Date: 20.09.2023
Variables and arrays
In C programming, variables, and arrays are fundamental concepts used to store and manage data in a program.

<datatype><variable _name>;

Variable: A variable in C is a named memory location used to store a value of a specific data type. It's a way to label and
refer to a particular chunk of memory, making it easier to work with data in your program.
Arrays: Arrays in C are used to store multiple values of the same data type under a single variable name. Each value in an
array is called an element, and the elements are accessed using an index.

int numbers[5];]; // Declaration of an integer array named 'numbers' with 5 elements


int numbers[5]= {5,10,15,20,25} ; // Declaration and initialization of an integer array named 'numbers’
numbers[0]= 5, numbers[1]= 10, numbers[2]= 15, numbers[3]= 20, numbers[4]= 25

numbers[0]=50; // Assigning a value to the first element of the array


Int value=numbers[2]; // Accessing the third element of the array and assigning it to a variable
Character array
• A character array or Strings in C is a sequence of characters stored in contiguous memory locations. It is used
to represent strings. Strings in C are null-terminated, meaning they end with a null character ('\0') to indicate
the end of the string.

• char myString[20]; // Declaration of a character array to hold a string with a maximum of 19 characters

• char greeting[] = "Hello, world!"; // Declaration and initialization of a character array with a string

• char myString[] = "This is a string"; // Declaration and initialization of a character array

• char anotherString[15] = "Hello"; // Declaration and initialization with a specific size


Expression in c
• An expression represents a single data item, such as a number or a character. The expression may consist of a
single entity, such as a constant, a variable, an array element or a reference to a function. It may also consist
of some combination of such entities, interconnected by one or more operators. The use of expressions
involving operators is particularly common in C, as in most other programming languages
• A symbolic constant is a name that substitutes for a sequence of characters. The characters may represent a
numeric constant, a character constant or a string constant. Thus, a symbolic constant allows a name to appear
in place of a numeric constant, a character constant or a string. When a program is compiled, each occurrence of
a symbolic constant is replaced by its corresponding character sequence.
Symbolic constants are usually defined at the beginning of a program. The symbolic constants may then appear
later in the program in place of the numeric constants, character constants, etc. that the symbolicconstants
represent.
Operators in C

 Arithmetic operators Logical operators

 + (addition)  && (logical AND)


Relational operators
 - (subtraction)  || (logical OR)
 == (equal to)
 * (multiplication)  ! (logical NOT)
 != (not equal to)
 / (division)
 < (less than)
 % (modulus, remainder) Assignment operator
 > (greater than)
 <= (less than or equal to)  = (assignment)
Increment and decrement operators
  >= (greater than or equal  += (addition and assignment)
++ (increment by 1)
 -- (decrement by 1) to) == (equal to)  -= (subtraction and assignment)
 != (not equal to)  *= (multiplication and
 Bitwise operators  < (less than) assignment)
 & (bitwise AND)  > (greater than)  /= (division and assignment)
 | (bitwise OR)
 <= (less than or equal to)  %= (modulus and assignment)
 ^ (bitwise XOR)
 ~ (bitwise NOT)  >= (greater than or equal
 << (left shift) to)
 >> (right shift)
1. Conditional operator
? : (conditional or ternary operator)
2. Comma Operator:
, (comma operator)
3. Sizeof Operator:
sizeof (returns the size of a variable or data type)

#include <stdio.h>
int main() {
int num = 10;
char result = (num % 2 == 0) ? "even" : "odd";
printf("The number %d is %s.\n", num, result);
return 0;
}
Logical and Bitwise operator
Logical operators are used to make decisions based on conditions (true or false), while bitwise
operators are used for bit-level operations on integers.
Relational operators
Assignment operator & Increment,Decrement
operator
Increment and decrement operator example
Summary
Operator precedence in c
Operator precedence determines the grouping of terms in an expression. This affects how an expression is
evaluated. Certain operators have higher precedence than others;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy