SlideShare una empresa de Scribd logo
1 de 31
C Programming Language
Mo’meN Ali
Operators, Expressions &
statements
/* Without loop */
#include <stdio.h>
#define ADJUST 7.64
#define SCALE 0.325
int main(void)
{
double shoe, foot;
shoe = 9.0;
foot = SCALE * shoe + ADJUST;
printf("Shoe size (men's) foot lengthn");
printf("%10.1f %15.2f inchesn", shoe, foot);
return 0;
}
printf("Also, your first name has %d letters,n", letters);
printf("and we have %d bytes to store it in.n", size);
return 0;
}
/* With loop*/
#include <stdio.h>
#define ADJUST 7.64
#define SCALE 0.325
int main(void)
{
double shoe, foot;
printf("Shoe size (men's) foot lengthn");
shoe = 3.0;
while (shoe < 18.5) /* starting the while loop */
{ /* start of block */
foot = SCALE*shoe + ADJUST;
printf("%10.1f %15.2f inchesn", shoe, foot);
shoe = shoe + 1.0;
} /* end of block */
printf("If the shoe fits, wear it.n");
return 0;
}
Output
Shoe size (men's) foot length
3.0 8.62 inches
4.0 8.94 inches
... ...
17.0 13.16 inches
18.0 13.49 inches
If the shoe fits, wear it.
C’s Terminology: Data Objects, Lvalues,
Rvalues, and Operands
• Data object is a general term for a region of data storage that can be
used to hold values.
• C uses the term lvalue to mean a name or expression that identifies a
particular data object. The name of a variable, for instance, is an lvalue Not
all objects can have their values changed.
• C uses the term modifiable lvalue to identify objects whose value can
be changed.
• Rvalues can be constants, variables, or any other expression that yields a
value.
Unary and binary operators
• Unary operators appears before their operand and associate from
right to left.
• Binary operators is an operator that operates on two operands and
manipulates them to return a result.
/* wheat.c -- exponential growth example*/
#include <stdio.h>
#define SQUARES 64 /* squares on a checkerboard */
#define CROP 1E15 /* US wheat crop in grains */
int main(void)
{
double current, total;
int count = 1;
printf("square grains total ");
printf("fraction of n");
printf(" added grains ");
printf("US totaln");
total = current = 1.0; /* start with one grain */
printf("%4d %13.2e %12.2e %12.2en", count, current,
total, total/CROP);
while (count < SQUARES)
{
count = count + 1;
current = 2.0 * current; /* double grains on next square */
total = total + current; /* update total */
printf("%4d %13.2e %12.2e %12.2en", count, current, total, total/CROP);
}
printf("That's all.n");
return 0;
}
Output
square grains total fraction of
added grains US total
1 1.00e+00 1.00e+00 1.00e-15
2 2.00e+00 3.00e+00 3.00e-15
3 4.00e+00 7.00e+00 7.00e-15
4 8.00e+00 1.50e+01 1.50e-14
5 1.60e+01 3.10e+01 3.10e-14
6 3.20e+01 6.30e+01 6.30e-14
7 6.40e+01 1.27e+02 1.27e-13
8 1.28e+02 2.55e+02 2.55e-13
9 2.56e+02 5.11e+02 5.11e-13
10 5.12e+02 1.02e+03 1.02e-12
Operator Precedence
• In C Each operator is assigned a precedence level. As in ordinary arithmetic,
multiplication and division have a higher precedence than addition and
subtraction, so they are performed first.
• What if two operators have the same precedence? If they share an operand, they
are executed according to the order in which they occur in the statement. For
most operators, the order is from left to right. (The = operator was an exception
to this.) Therefore, in the statement.
- butter = 25.0 + 60.0 * n / SCALE;
60.0 * n The first * or / in the expression (assuming n is 6 so that 60.0
* n is 360.0)
360.0 / SCALE Then the second * or / in the expression
25.0 + 180 Finally (because SCALE is 2.0), the first + or - in the
expression, to yield 205.0
#include <stdio.h>
int main(void)
{
int top, score;
top = score = -(2 + 5) * 6 + (4 + 3 * (2 + 3));
printf("top = %d n", top);
return 0;
}
Output
What value will this program print? Figure it out, and then run the
program
Precedence and the Order of Evaluation
The strlen() function gives the length of a string in characters. Note that you can not use
sizeof() to get the string length.
Modulus Operator: %
The modulus operator is used in integer arithmetic. It gives the remainder that results
when the integer to its left is divided by the integer to its right. For example, 13 % 5
(read as "13 modulo 5") has the value 3.
#include <stdio.h>
#define SEC_PER_MIN 60 // seconds in a minute
int main(void)
{
int sec, min, left;
printf("Convert seconds to minutes and seconds!n");
printf("Enter the number of seconds (<=0 to quit):n");
scanf("%d", &sec); // read number of seconds
while (sec > 0)
{
min = sec / SEC_PER_MIN; // truncated number of minutes
left = sec % SEC_PER_MIN; // number of seconds left over
printf("%d seconds is %d minutes, %d seconds.n", sec, min, left);
printf("Enter next value (<=0 to quit):n");
scanf("%d", &sec);
}
return 0;
}
Output
Convert seconds to minutes and seconds!
Enter the number of seconds (<=0 to quit):
154
154 seconds is 2 minutes, 34 seconds.
Enter next value (<=0 to quit):
567
567 seconds is 9 minutes, 27 seconds.
Enter next value (<=0 to quit):
0
Increment and Decrement Operators:
++ and --
The increment operator performs a simple task; it increments (increases)
the value of its operand by 1. This operator comes in two varieties. The
first variety has the ++ come before the affected variable; this is the prefix
mode. The second variety has the ++ after the affected variable; this is
the postfix mode. The two modes differ with regard to the precise time that
the incrementing takes place.
#include <stdio.h>
int main(void)
{
int ultra = 0, super = 0;
while (super < 5)
{
super++;
++ultra;
printf("super = %d, ultra = %d n", super, ultra);
}
return 0;
}
Output
super = 1, ultra = 1
super = 2, ultra = 2
super = 3, ultra = 3
super = 4, ultra = 4
super = 5, ultra = 5
#include <stdio.h>
#define MAX 100
int main(void)
{
int count = MAX + 1;
while (--count > 0)
{
printf("%d bottles of spring water on the wall, "
"%d bottles of spring water!n", count, count);
printf("Take one down and pass it around,n");
printf("%d bottles of spring water!nn", count - 1);
}
return 0;
}
Output
100 bottles of spring water on the wall, 100 bottles of spring water!
Take one down and pass it around,
99 bottles of spring water!
99 bottles of spring water on the wall, 99 bottles of spring water!
Take one down and pass it around,
98 bottles of spring water!
.
.
.
1 bottles of spring water on the wall, 1 bottles of spring water!
Take one down and pass it around,
0 bottles of spring water!
Expressions
An expression consists of a combination of operators and operands. The simplest
expression is a lone operand, and you can build in complexity from there.
Statements
Statements are the primary building blocks of a program. A program is a
series of statements with some necessary punctuation. A statement is a
complete instruction to the computer. In C, statements are indicated by
a semicolon at the end.
Type Conversions
Statements and expressions should normally use variables and constants of
just one type. If, however, you mix types, C doesn't stop dead in its tracks the
way, say, Pascal does. Instead, it uses a set of rules to make type conversions
automatically.
• When appearing in an expression, char and short, both signed and unsigned,
are automatically converted to int or, if necessary, to unsigned int.
• In any operation involving two types, both values are converted to the higher
ranking of the two types.
• In an assignment statement, the final result of the calculations is converted
to the type of the variable being assigned a value.
• When passed as function arguments, char and short are converted to int, and
float is converted to double. This automatic promotion can be overridden by
function prototyping.
#include <stdio.h>
int main(void)
{
char ch;
int i;
float fl;
fl = i = ch = 'C'; /* line 9 */
printf("ch = %c, i = %d, fl = %2.2fn", ch, i, fl); /* line 10 */
ch = ch + 1; /* line 11 */
i = fl + 2 * ch; /* line 12 */
fl = 2.0 * ch + i; /* line 13 */
printf("ch = %c, i = %d, fl = %2.2fn", ch, i, fl); /* line 14 */
ch = 5212205.17; /* line 15 */
printf("Now ch = %cn", ch);
return 0;
}
Output
ch = C, i = 67, fl = 67.00
ch = D, i = 203, fl = 339.00
Now ch = -
• Lines 9 and 10— The character 'C' is stored as a 1-byte ASCII value in ch. The integer
variable i receives the integer conversion of 'C', which is 67 stored as 4 bytes. Finally,
fl receives the floating conversion of 67, which is 67.00.
• Lines 11 and 14— The character variable 'C' is converted to the integer 67, which is
then added to the 1. The resulting 4-byte integer 68 is truncated to 1 byte and stored in
ch. When printed using the %c specifier, 68 is interpreted as the ASCII code for 'D'.
• Lines 12 and 14— The value of ch is converted to a 4-byte integer (68) for the
multiplication by 2. The resulting integer (136) is converted to floating point in order to
be added to fl. The result (203.00f) is converted to int and stored in i.
• Lines 13 and 14— The value of ch ('D', or 68) is converted to floating point for
multiplication by 2.0. The value of i (203) is converted to floating point for the addition,
and the result (339.00) is stored in fl.
• Lines 15 and 16— Here the example tries a case of demotion, setting ch equal to a
rather large number. After truncation takes place, ch winds up with the ASCII code for
the hyphen character.
The Cast Operator
cast is a manual type conversion and it consists of preceding the quantity
with the name of the desired type in parentheses. The parentheses and
type name together constitute a cast operator. This is the general form of
a cast operator:
(type)
The actual type desired, such as long, is substituted for the word type.
mice = 1.6 + 1.7;
mice = (int) 1.6 + (int) 1.7;
The first example uses automatic conversion. First, 1.6 and 1.7 are
added to yield 3.3. This number is then converted through truncation to
the integer 3 to match the int variable. In the second example, 1.6 is
converted to an integer (1) before addition, as is 1.7, so that mice is
assigned the value 1+1, or 2.

Más contenido relacionado

La actualidad más candente

C Programming Language Part 6
C Programming Language Part 6C Programming Language Part 6
C Programming Language Part 6Rumman Ansari
 
Expressions using operator in c
Expressions using operator in cExpressions using operator in c
Expressions using operator in cSaranya saran
 
46630497 fun-pointer-1
46630497 fun-pointer-146630497 fun-pointer-1
46630497 fun-pointer-1AmIt Prasad
 
C tech questions
C tech questionsC tech questions
C tech questionsvijay00791
 
An imperative study of c
An imperative study of cAn imperative study of c
An imperative study of cTushar B Kute
 
Python Unit 3 - Control Flow and Functions
Python Unit 3 - Control Flow and FunctionsPython Unit 3 - Control Flow and Functions
Python Unit 3 - Control Flow and FunctionsDhivyaSubramaniyam
 
C Programming Language Part 9
C Programming Language Part 9C Programming Language Part 9
C Programming Language Part 9Rumman Ansari
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 2 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 2 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 2 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 2 of 5 by...ssuserd6b1fd
 
C programming(part 3)
C programming(part 3)C programming(part 3)
C programming(part 3)SURBHI SAROHA
 
C Programming Language Part 8
C Programming Language Part 8C Programming Language Part 8
C Programming Language Part 8Rumman Ansari
 
Core programming in c
Core programming in cCore programming in c
Core programming in cRahul Pandit
 
C Programming Language Part 11
C Programming Language Part 11C Programming Language Part 11
C Programming Language Part 11Rumman Ansari
 
C programming(Part 1)
C programming(Part 1)C programming(Part 1)
C programming(Part 1)SURBHI SAROHA
 

La actualidad más candente (20)

Buffer OverFlow
Buffer OverFlowBuffer OverFlow
Buffer OverFlow
 
C Programming Language Part 6
C Programming Language Part 6C Programming Language Part 6
C Programming Language Part 6
 
Expressions using operator in c
Expressions using operator in cExpressions using operator in c
Expressions using operator in c
 
46630497 fun-pointer-1
46630497 fun-pointer-146630497 fun-pointer-1
46630497 fun-pointer-1
 
What is c
What is cWhat is c
What is c
 
C tech questions
C tech questionsC tech questions
C tech questions
 
An imperative study of c
An imperative study of cAn imperative study of c
An imperative study of c
 
Python Unit 3 - Control Flow and Functions
Python Unit 3 - Control Flow and FunctionsPython Unit 3 - Control Flow and Functions
Python Unit 3 - Control Flow and Functions
 
C Programming Language Part 9
C Programming Language Part 9C Programming Language Part 9
C Programming Language Part 9
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 2 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 2 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 2 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 2 of 5 by...
 
Python unit 3 and Unit 4
Python unit 3 and Unit 4Python unit 3 and Unit 4
Python unit 3 and Unit 4
 
C programming(part 3)
C programming(part 3)C programming(part 3)
C programming(part 3)
 
Functions
FunctionsFunctions
Functions
 
Arrays
ArraysArrays
Arrays
 
Lecture 6- Intorduction to C Programming
Lecture 6- Intorduction to C ProgrammingLecture 6- Intorduction to C Programming
Lecture 6- Intorduction to C Programming
 
Unit 3
Unit 3 Unit 3
Unit 3
 
C Programming Language Part 8
C Programming Language Part 8C Programming Language Part 8
C Programming Language Part 8
 
Core programming in c
Core programming in cCore programming in c
Core programming in c
 
C Programming Language Part 11
C Programming Language Part 11C Programming Language Part 11
C Programming Language Part 11
 
C programming(Part 1)
C programming(Part 1)C programming(Part 1)
C programming(Part 1)
 

Similar a 4 operators, expressions &amp; statements

Similar a 4 operators, expressions &amp; statements (20)

2 data and c
2 data and c2 data and c
2 data and c
 
Programming ppt files (final)
Programming ppt files (final)Programming ppt files (final)
Programming ppt files (final)
 
9.C Programming
9.C Programming9.C Programming
9.C Programming
 
Tu1
Tu1Tu1
Tu1
 
Program flowchart
Program flowchartProgram flowchart
Program flowchart
 
C Programming by Süleyman Kondakci
C Programming by Süleyman KondakciC Programming by Süleyman Kondakci
C Programming by Süleyman Kondakci
 
Presentation1
Presentation1Presentation1
Presentation1
 
5 c control statements looping
5  c control statements looping5  c control statements looping
5 c control statements looping
 
12 lec 12 loop
12 lec 12 loop 12 lec 12 loop
12 lec 12 loop
 
3. chapter ii
3. chapter ii3. chapter ii
3. chapter ii
 
Concepts of C [Module 2]
Concepts of C [Module 2]Concepts of C [Module 2]
Concepts of C [Module 2]
 
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
 
C faq pdf
C faq pdfC faq pdf
C faq pdf
 
C programming
C programmingC programming
C programming
 
Practical File of C Language
Practical File of C LanguagePractical File of C Language
Practical File of C Language
 
Cpl
CplCpl
Cpl
 
Functions
FunctionsFunctions
Functions
 
深入淺出C語言
深入淺出C語言深入淺出C語言
深入淺出C語言
 
C file
C fileC file
C file
 
Code optimization
Code optimization Code optimization
Code optimization
 

Último

%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...masabamasaba
 
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in sowetomasabamasaba
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnAmarnathKambale
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrandmasabamasaba
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareJim McKeeth
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationJuha-Pekka Tolvanen
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...masabamasaba
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024VictoriaMetrics
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2
 
tonesoftg
tonesoftgtonesoftg
tonesoftglanshi9
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park masabamasaba
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrainmasabamasaba
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...masabamasaba
 

Último (20)

%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaS
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 

4 operators, expressions &amp; statements

  • 3. /* Without loop */ #include <stdio.h> #define ADJUST 7.64 #define SCALE 0.325 int main(void) { double shoe, foot; shoe = 9.0; foot = SCALE * shoe + ADJUST; printf("Shoe size (men's) foot lengthn"); printf("%10.1f %15.2f inchesn", shoe, foot); return 0; }
  • 4. printf("Also, your first name has %d letters,n", letters); printf("and we have %d bytes to store it in.n", size); return 0; }
  • 5. /* With loop*/ #include <stdio.h> #define ADJUST 7.64 #define SCALE 0.325 int main(void) { double shoe, foot; printf("Shoe size (men's) foot lengthn"); shoe = 3.0; while (shoe < 18.5) /* starting the while loop */ { /* start of block */ foot = SCALE*shoe + ADJUST; printf("%10.1f %15.2f inchesn", shoe, foot); shoe = shoe + 1.0; } /* end of block */ printf("If the shoe fits, wear it.n"); return 0; }
  • 6. Output Shoe size (men's) foot length 3.0 8.62 inches 4.0 8.94 inches ... ... 17.0 13.16 inches 18.0 13.49 inches If the shoe fits, wear it.
  • 7. C’s Terminology: Data Objects, Lvalues, Rvalues, and Operands • Data object is a general term for a region of data storage that can be used to hold values. • C uses the term lvalue to mean a name or expression that identifies a particular data object. The name of a variable, for instance, is an lvalue Not all objects can have their values changed. • C uses the term modifiable lvalue to identify objects whose value can be changed. • Rvalues can be constants, variables, or any other expression that yields a value.
  • 8. Unary and binary operators • Unary operators appears before their operand and associate from right to left. • Binary operators is an operator that operates on two operands and manipulates them to return a result.
  • 9. /* wheat.c -- exponential growth example*/ #include <stdio.h> #define SQUARES 64 /* squares on a checkerboard */ #define CROP 1E15 /* US wheat crop in grains */ int main(void) { double current, total; int count = 1; printf("square grains total "); printf("fraction of n"); printf(" added grains "); printf("US totaln");
  • 10. total = current = 1.0; /* start with one grain */ printf("%4d %13.2e %12.2e %12.2en", count, current, total, total/CROP); while (count < SQUARES) { count = count + 1; current = 2.0 * current; /* double grains on next square */ total = total + current; /* update total */ printf("%4d %13.2e %12.2e %12.2en", count, current, total, total/CROP); } printf("That's all.n"); return 0; }
  • 11. Output square grains total fraction of added grains US total 1 1.00e+00 1.00e+00 1.00e-15 2 2.00e+00 3.00e+00 3.00e-15 3 4.00e+00 7.00e+00 7.00e-15 4 8.00e+00 1.50e+01 1.50e-14 5 1.60e+01 3.10e+01 3.10e-14 6 3.20e+01 6.30e+01 6.30e-14 7 6.40e+01 1.27e+02 1.27e-13 8 1.28e+02 2.55e+02 2.55e-13 9 2.56e+02 5.11e+02 5.11e-13 10 5.12e+02 1.02e+03 1.02e-12
  • 12. Operator Precedence • In C Each operator is assigned a precedence level. As in ordinary arithmetic, multiplication and division have a higher precedence than addition and subtraction, so they are performed first. • What if two operators have the same precedence? If they share an operand, they are executed according to the order in which they occur in the statement. For most operators, the order is from left to right. (The = operator was an exception to this.) Therefore, in the statement. - butter = 25.0 + 60.0 * n / SCALE; 60.0 * n The first * or / in the expression (assuming n is 6 so that 60.0 * n is 360.0) 360.0 / SCALE Then the second * or / in the expression 25.0 + 180 Finally (because SCALE is 2.0), the first + or - in the expression, to yield 205.0
  • 13. #include <stdio.h> int main(void) { int top, score; top = score = -(2 + 5) * 6 + (4 + 3 * (2 + 3)); printf("top = %d n", top); return 0; }
  • 14. Output What value will this program print? Figure it out, and then run the program
  • 15. Precedence and the Order of Evaluation The strlen() function gives the length of a string in characters. Note that you can not use sizeof() to get the string length.
  • 16. Modulus Operator: % The modulus operator is used in integer arithmetic. It gives the remainder that results when the integer to its left is divided by the integer to its right. For example, 13 % 5 (read as "13 modulo 5") has the value 3.
  • 17. #include <stdio.h> #define SEC_PER_MIN 60 // seconds in a minute int main(void) { int sec, min, left; printf("Convert seconds to minutes and seconds!n"); printf("Enter the number of seconds (<=0 to quit):n"); scanf("%d", &sec); // read number of seconds while (sec > 0) { min = sec / SEC_PER_MIN; // truncated number of minutes left = sec % SEC_PER_MIN; // number of seconds left over printf("%d seconds is %d minutes, %d seconds.n", sec, min, left); printf("Enter next value (<=0 to quit):n"); scanf("%d", &sec); } return 0; }
  • 18. Output Convert seconds to minutes and seconds! Enter the number of seconds (<=0 to quit): 154 154 seconds is 2 minutes, 34 seconds. Enter next value (<=0 to quit): 567 567 seconds is 9 minutes, 27 seconds. Enter next value (<=0 to quit): 0
  • 19. Increment and Decrement Operators: ++ and -- The increment operator performs a simple task; it increments (increases) the value of its operand by 1. This operator comes in two varieties. The first variety has the ++ come before the affected variable; this is the prefix mode. The second variety has the ++ after the affected variable; this is the postfix mode. The two modes differ with regard to the precise time that the incrementing takes place.
  • 20. #include <stdio.h> int main(void) { int ultra = 0, super = 0; while (super < 5) { super++; ++ultra; printf("super = %d, ultra = %d n", super, ultra); } return 0; }
  • 21. Output super = 1, ultra = 1 super = 2, ultra = 2 super = 3, ultra = 3 super = 4, ultra = 4 super = 5, ultra = 5
  • 22. #include <stdio.h> #define MAX 100 int main(void) { int count = MAX + 1; while (--count > 0) { printf("%d bottles of spring water on the wall, " "%d bottles of spring water!n", count, count); printf("Take one down and pass it around,n"); printf("%d bottles of spring water!nn", count - 1); } return 0; }
  • 23. Output 100 bottles of spring water on the wall, 100 bottles of spring water! Take one down and pass it around, 99 bottles of spring water! 99 bottles of spring water on the wall, 99 bottles of spring water! Take one down and pass it around, 98 bottles of spring water! . . . 1 bottles of spring water on the wall, 1 bottles of spring water! Take one down and pass it around, 0 bottles of spring water!
  • 24. Expressions An expression consists of a combination of operators and operands. The simplest expression is a lone operand, and you can build in complexity from there.
  • 25. Statements Statements are the primary building blocks of a program. A program is a series of statements with some necessary punctuation. A statement is a complete instruction to the computer. In C, statements are indicated by a semicolon at the end.
  • 26. Type Conversions Statements and expressions should normally use variables and constants of just one type. If, however, you mix types, C doesn't stop dead in its tracks the way, say, Pascal does. Instead, it uses a set of rules to make type conversions automatically. • When appearing in an expression, char and short, both signed and unsigned, are automatically converted to int or, if necessary, to unsigned int. • In any operation involving two types, both values are converted to the higher ranking of the two types. • In an assignment statement, the final result of the calculations is converted to the type of the variable being assigned a value. • When passed as function arguments, char and short are converted to int, and float is converted to double. This automatic promotion can be overridden by function prototyping.
  • 27. #include <stdio.h> int main(void) { char ch; int i; float fl; fl = i = ch = 'C'; /* line 9 */ printf("ch = %c, i = %d, fl = %2.2fn", ch, i, fl); /* line 10 */ ch = ch + 1; /* line 11 */ i = fl + 2 * ch; /* line 12 */ fl = 2.0 * ch + i; /* line 13 */ printf("ch = %c, i = %d, fl = %2.2fn", ch, i, fl); /* line 14 */ ch = 5212205.17; /* line 15 */ printf("Now ch = %cn", ch); return 0; }
  • 28. Output ch = C, i = 67, fl = 67.00 ch = D, i = 203, fl = 339.00 Now ch = -
  • 29. • Lines 9 and 10— The character 'C' is stored as a 1-byte ASCII value in ch. The integer variable i receives the integer conversion of 'C', which is 67 stored as 4 bytes. Finally, fl receives the floating conversion of 67, which is 67.00. • Lines 11 and 14— The character variable 'C' is converted to the integer 67, which is then added to the 1. The resulting 4-byte integer 68 is truncated to 1 byte and stored in ch. When printed using the %c specifier, 68 is interpreted as the ASCII code for 'D'. • Lines 12 and 14— The value of ch is converted to a 4-byte integer (68) for the multiplication by 2. The resulting integer (136) is converted to floating point in order to be added to fl. The result (203.00f) is converted to int and stored in i. • Lines 13 and 14— The value of ch ('D', or 68) is converted to floating point for multiplication by 2.0. The value of i (203) is converted to floating point for the addition, and the result (339.00) is stored in fl. • Lines 15 and 16— Here the example tries a case of demotion, setting ch equal to a rather large number. After truncation takes place, ch winds up with the ASCII code for the hyphen character.
  • 30. The Cast Operator cast is a manual type conversion and it consists of preceding the quantity with the name of the desired type in parentheses. The parentheses and type name together constitute a cast operator. This is the general form of a cast operator: (type) The actual type desired, such as long, is substituted for the word type.
  • 31. mice = 1.6 + 1.7; mice = (int) 1.6 + (int) 1.7; The first example uses automatic conversion. First, 1.6 and 1.7 are added to yield 3.3. This number is then converted through truncation to the integer 3 to match the int variable. In the second example, 1.6 is converted to an integer (1) before addition, as is 1.7, so that mice is assigned the value 1+1, or 2.