Introduction to C (2)
Introduction to C (2)
Programming Language C
BIL 110E
E-mail: [email protected]
Text Books:
C-How-to-Program-Deitel-6Ed, H.M. Deitel and P.J. Deitel, Prentice-Hall
Inc., 2010
Web Pages:
https://www.programiz.com/c-programming#tutorial
https://www.sitesbay.com/cprogramming/index
2
Introduction
to C Programming
OBJECTIVES
In this chapter you will learn:
▪ To write simple computer programs in C.
▪ To use simple input and output statements.
▪ The fundamental data types.
▪ Computer memory concepts.
▪ To use arithmetic operators.
▪ The precedence of arithmetic operators.
▪ To write simple decision-making statements.
2.1 Introduction
2.2 A Simple C Program: Printing a Line of Text
2.3 Another Simple C Program: Adding Two Integers
2.4 Memory Concepts
2.5 Arithmetic in C
2.6 Decision Making: Equality and Relational Operators
2.1 Introduction
▪ C programming language
– Structured and disciplined approach to program design
▪ Structured programming
Code output
Welcome to C!
▪ return 0;
– A way to exit a function
– return 0, in this case, means that the program terminated
normally. When you return 0, you tell the caller (OS in case of
main) that the status of your program was successful.
Welcome to C!
Welcome to C!
Welcome
to
C!
* *
* *
* *
Code is:
printf ( "* *\n* *\n* *" );
Q1
* *
* *
* *
Code is:
printf ( "*\t*\n*\t*\n*\t*" );
Declaring Variables
int x; // Declare x to be an
// integer variable;
float radius; // Declare radius to
// be a double variable;
char a; // Declare a to be a
// character variable;
Assignment Statements
x = 1; // Assign 1 to x;
same definition
▪ int integer1;
▪ int integer2;
▪ int sum;
– Definitions appear before executable statements
- If an executable statement references an undeclared variable it
will produce a syntax (compiler) error
– Similar to scanf
- %d means decimal integer will be printed
- sum specifies what integer will be printed
– Calculations can be performed inside printf
statements
printf("Sum is %d\n", integer1 +
integer2);
spaces
int integer1;
interger1 = 45;
After executing the above statement
Fig. 2.6 | Memory location showing the name and value of a variable.
interger1 = 45;
interger2 = 72;
Operator precedence
– Some arithmetic operators act before others (i.e.,
multiplication before addition)
Equality operators
= == x == y x is equal to y
!= x != y x is not equal to y
Relational operators
Result is:
3==3 results 1
3==4 results 0
3<4 results 1
3!=3 results 0
3>4 results 0
a%5+b*a/2-c+(a+b)
a) 5 b) 4 c) 20 d) 11 e) 13
43
No space
if control statement
▪ if control statement
– Simple version in this section, more detail later
if (condition) {
body statements
}
Operator precedence
– Some arithmetic operators act before others
Operators Associativity
() left to right
* / % left to right
+ - left to right
< <= > >= left to right
== != left to right
= right to left
Fig. 2.14 | Precedence and associativity of the operators discussed so far.
a) 5 10 10 b) 13 13 10 c) 10 10 10 d) 5 4 10 e) 13 10 10
52
Keywords
– Special words reserved for C
– Cannot be used as identifiers or variable names
Keywords
auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while