0% found this document useful (0 votes)
4 views34 pages

Constants

The document provides an overview of programming in C, focusing on constants, data types, operators, and input/output statements. It covers numeric and character constants, various types of operators (arithmetic, relational, logical, assignment, increment/decrement, conditional, bitwise, and special operators), and the syntax for using them. Additionally, it discusses input/output methods and functions available in C for data handling.

Uploaded by

ellammalvikram
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views34 pages

Constants

The document provides an overview of programming in C, focusing on constants, data types, operators, and input/output statements. It covers numeric and character constants, various types of operators (arithmetic, relational, logical, assignment, increment/decrement, conditional, bitwise, and special operators), and the syntax for using them. Additionally, it discusses input/output methods and functions available in C for data handling.

Uploaded by

ellammalvikram
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

Programming in C

Unit 1
Constants
• The item whose value cannot be changed
during the execution of the programs
– Numeric constant
• Integer constants
• Real constants
– Character constants
• Single character constants
• String constants
Numeric constants
• A. integer constants
– 3 types
• Decimal numbers – 0 to 9
• Octal numbers – 0 to 7
• Hexa decimal numbers – 0 to 9, A,B,C,D,E,F
B. Real constants (with decimal point)
example
distance = 126.5
height = 5.6
speed = 3e11
Character constants
• A. single character constant
– Example: ‘s’, ‘v’, ‘c’, e’, ‘t’
• B. String constant
– Example: ”Hi”, “IyrCSE”
• C. declaring a variable as constant
– Syntax: const datatype variable = constant
– Example: const int dob = 3977;
• D. declaring a variable as volatile
– Syntax: volatile datatype variable = constant
– Example: volatile int year = 2007
• Volatile – variables that are changed at any time by other external
programs or same program
Typedef and Enumeration constants
The typedef is a keyword that is used to provide existing
data types with a new name. The C typedef keyword is used
to redefine the name of already existing data types.
• When names of datatypes become difficult to use in
programs, typedef is used with user-defined datatypes,
which behave similarly to defining an alias for commands.
• Following is an example to define a term BYTE for one-byte
numbers −
• Syntax:
– typedef unsigned char BYTE;
• After this type definition, the identifier BYTE can be used as
an abbreviation for the type unsigned char, for example..
• BYTE b1, b2;
Enumeration constants
• The enum in C is also known as the enumerated
type.
• It is a user-defined data type that consists of integer
values, and it provides meaningful names to these
values.
• The use of enum in C makes the program easy to
understand and maintain.
• The enum is defined by using the enum keyword.
• Syntax:
– enum identifier {value, value2, value3, ……., valuen};
• Example:
– enum day {mon, tue, wed, ….., sun}
Delimiters
• Will not specify any operation
• Has some syntactic meaning
–#
–,
–:
–;
– ()
– {}
– []
Operators in C
• Is a symbol that specifies an operation to be
performed on the operands
• Types
a) Arithmetic operators
b) Relational operators
c) Logical operators
d) Assignment operators
e) Increment and decrement operators
f) Conditional operators (Ternary operators)
g) Bitwise operators
h) Special operators
Arithmetic operators
• + addition
• - subtraction
• * multiplication
• / division
• % modulo division
– Division operator on various data types
• Int/int 2/5 = 0
• Real/int 5.0/2 = 2.5
• Int/real 5/2.0 = 2.5
• Real/real 5.0/2.0 = 2.5
• Arithmetic operators are further classified as
1. Unary arithmetic – only one operand (+x, -y)
2. Binary arithmetic – two operands (a+b, a-b, a*b)
3. Integer arithmetic – both operands are integer
a = 5, b= =4
a+b = 9
4. Floating arithmetic – float type
a = 6.5, b = 3.5
a+b = 10.0

Program
Relational operators
• < - less than
• <= - less than or equal to
• > - greater than
• >= - greater than or equal to
• == - equal to
• != - not equal to
Syntax
AE1 relational operator AE2
example:
a=1, b=2, c=3
a<b True 1
(a+b)>=c True 1
(b+c)>(a+5)
c!=3
b==2
Logical operators
• && - logical AND
• || - logical OR
• ! – logical NOT
Example:
‘i’ is an integer variable whose value is 7
‘f’ is a float variable whose value is 5.5
‘c’ is a character variable that represents a character
‘w’
Expression Interpretation value

(i>=6)&&(c==‘w’) True 1

(f<11)&&(i>100)

(c!=p)||(i<=100)
Assignment operators
• Used to assign a value or an expression or a value of a variable
to another variable.
• Syntax
• Variable = expression (or) a value
• Example: x = 10, x = a+b, x = y
• Compound assignment
• Assign a value to a variable in order to assign a new value
to a variable after performing a specified operation
Operator Example Meaning
+= X += Y X=X+Y
-=
*=
/=
%=
Increment and Decrement operators
• ++ - increment operator – adds one to the variable
• -- decrement operator – subtracts one from the
variable
• Called Unary operator, because it acts upon only one
variable
Operator Meaning
• X = a++ ++x Pre increment
– Content of a before = 5 --x Pre decrement
– Value of expression = 5 X++ Post increment

– Content of a after = 6 X-- Post decrement

• X = ++a
– Content of a before = 5
– Content of a after = 6
– Value of expression = 6
Increment Operators
• We use increment operators in C to increment the
given value of a variable by 1.
• For instance,
• int a = 1, b = 1;
• ++b; // valid
• ++3; // invalid – increment operator is operating on
constant value
• ++(a+b); // invalid – increment operator is operating on
expression
Prefix Increment Operators
• The prefix increment operator increments the current value of
the available variable immediately, and only then this value is
used in an expression.
• In simpler words, the value of a variable first gets
incremented and then used inside any expression in the case
of an increment operation.
– b = ++a;
• In this case, the current value of a will be incremented first by
1. Then the new value will be assigned to b for the expression
in which it is used.
• Thus, Syntax for Prefix Increment Operator:
– ++variable;
Example
#include<stdio.h>
#include<conio.h>
void main()
{
int p,q;
q=10;
p=++q;
printf(“p: %d”,p);
printf(“q: %d”,q);
getch();
}
The output here would be:
p: 11
q: 11
Postfix Increment Operators
• The postfix increment operator allows the usage
of the current value of a variable in an expression
and then increments its value by 1.
• In simpler words, the value of a variable is first
used inside the provided expression, and then its
value gets incremented by 1.
• For example:
– b = a++;
• In this case, the current value of a is first assigned
to b, and after that, a is incremented.
• Thus, Syntax for Postfix Increment Operator:
– variable++;
Example
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
b=10;
a=b++;
printf(“a: %d”,a);
printf(“b: %d”,b);
getch();
}
The output here would be:
a: 10
b: 11
Decrement Operators
• We use decrement operators in C to
decrement the given value of a variable by 1.
• For instance,
– int a = 2, b = 1;
– –b; // valid
– –5; // invalid – decrement operator is operating on
constant value
– –(a-b); // invalid – decrement operator is
operating on expression
Prefix Decrement Operators
• The prefix decrement operator decrements the current value
of the available variable immediately, and only then this value
is used in an expression.
• In simpler words, the value of a variable first gets
decremented and then used inside any expression in the case
of an decrement operation.
– b = --a;
• In this case, the current value of a will be decremented first by
1. Then the new value will be assigned to b for the expression
in which it is used.
• Thus, Syntax for Prefix decrement Operator:
– --variable;
Example
#include<stdio.h>
#include<conio.h>
void main()
{
int p,q;
q=10;
p=--q;
printf(“p: %d”,p);
printf(“q: %d”,q);
getch();
}
The output here would be:
p: 9
q: 9
Postfix Decrement Operators
• The postfix decrement operator allows the usage
of the current value of a variable in an expression
and then decrements its value by 1.
• In simpler words, the value of a variable is first
used inside the provided expression, and then its
value gets decremented by 1.
• For example:
– b = a--;
• In this case, the current value of a is first assigned
to b, and after that, a is decremented.
• Thus, Syntax for Postfix decrement Operator:
– Variable--;
Example
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
b=10;
a=b--;
printf(“a: %d”,a);
printf(“b: %d”,b);
getch();
}
The output here would be:
a: 10
b: 9
Conditional operator or Ternary operator
• when we use an operator on three variables or operands, it is
known as a Ternary Operator.
• We can represent it using ? : . Thus, it is also known as a
conditional operator.
• In C, the conditional operator evaluates the condition and
implicitly converts the result to a Boolean value.
– If the condition is true, expression1 is executed; otherwise,
expression2 is executed.
– The result of the operator is the result of either expression1 or
expression2.
– Only one of the two expressions is evaluated, while the other is
ignored.

syntax:
• variable = condition ? value1 : value2
Example

#include <stdio.h>
int main()
{
int age; // variable declaration
printf("Enter your age");
scanf("%d",&age); // taking user input for age variable
(age>=18)? (printf("eligible for voting")) : (printf("not elig
ible for voting")); // conditional operator
return 0;
}
Bitwise operators
• Bitwise operators are used to manipulate the
data at bit level.
• It operates on integers only and may not be
applied to float or real.
Operator Meaning
& Bitwise AND
| Bitwise OR
^ Bitwise XOR
<< Shift left
>> Shift right
~ One’s complement
a b a&b a|b a^b ~a
0 0 0 0 0 1
0 1 0 1 1 1
1 0 0 1 1 0
1 1 1 1 0 0
The special operator
• Comma operator(,)
– Used to separate variables, constants or expressions
etc..
• The sizeof() operator
– Unary operator that returns the length of the
specified variable
• Pointer operators
– & - specifies the address of the variable
– * - specifies the value of the variable
• Member selection operators
– . and - used to access the elements from a structure
Assignment topics
submission date: on or before 27.03.2024
• EXPRESSIONS
• PRECEDENCE AND ASSOCIATIVITY OF
OPERATORS
INPUT / OUTPUT STATEMENTS
• The three essential features of computer
program are input, process, output
• Two methods for providing data to the
program
– Assigning the data to the variables in a program
– By using the input/output statements
• Two types of input/output statements are
available
– Unformatted input/output statements
– Formatted input/output statements
Input and output functions

Unformatted Formatted
input/output statements input/output statements
Input Output Input Output

getc() putc() scanf() printf()


getchar() putchar() fscanf() fprintf()
gets() puts()
getche()
getch()

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