Eee0115 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

EEE0115

Chapter 2: Introduction to C
Programming

C How to Program
1 Deitel & Deitel
2

• First C Program: Printing a Line of Text


• Second C Program: Adding Two Integers
• Memory Concepts
• Arithmetic in C
• Decision Making: Equality and Relational Operators
3

First C Program: Printing a Line of Text

1 /* Fig. 2.1: fig02_01.c


2 A first program in C */
3 #include <stdio.h>
4
5 /* function main begins program execution */
6 int main( void )

Yükleniyor…
7 {
8 printf( "Welcome to C!\n" );
9
10 return 0; /* indicate that program ended successfully */
11
12 } /* end function main */

Welcome to C!
4

First C Program: Printing a Line of Text


• The textual information given between /* and */ is called
as comments.
• Comments are not executable statements.
• They are used to inform users of the program.
• #include <stdio.h>
• #include is a preprocessor directive and used to load a
specific file.
• In this example, the file is stdio.h and it is used for
standard input/output operations.
5

First C Program: Printing a Line of Text


• Each C program must have main() function.
• int main() – int shows that main function returns integer
value.
• Like in all functions, { and } braces are used to specify
function body in main function.

Yükleniyor…
6

First C Program: Printing a Line of Text


• printf("Welcome to C!\n");
• Printf is used to print a string of characters given in
quotes.
• All statements like printf must end with semicolon (;)
• \ is used to specify an escape character. In this example \n
is the newline character.
• \n Newline
• \t Horizontal tab
• \a Alert
• \\ Backslash
• \" Double quote
7

First C Program: Printing a Line of Text


• return 0;
• It shows that C program terminated succesfully without
any problem.
• } right brace indicates main function ends.
8

First C Program: Printing a Line of Text


1 /* Fig. 2.3: fig02_03.c
2 Printing on one line with two printf statements */
3 #include <stdio.h>
4
5 /* function main begins program execution */
6 int main( void )
7 {
8 printf( "Welcome " );
9 printf( "to C!\n" );
10
11 return 0; /* indicate that program ended successfully */
12
13 } /* end function main */

Welcome to C!

1 /* Fig. 2.4: fig02_04.c


2 Printing multiple lines with a single printf */
3 #include <stdio.h>
4
5 /* function main begins program execution */
6 int main( void )
7 {
8 printf( "Welcome\nto\nC!\n" );
9
10 return 0; /* indicate that program ended successfully */
11
12 } /* end function main */

Welcome
to
C!
9

Second C Program: Adding Two Integers


1 /* Fig. 2.5: fig02_05.c
2 Addition program */
3 #include <stdio.h>
4
5 /* function main begins program execution */
6 int main( void )
7 {
8 int integer1; /* first number to be input by user */
9 int integer2; /* second number to be input by user */
10 int sum; /* variable in which sum will be stored */
11
12 printf( "Enter first integer\n" ); /* prompt */
13 scanf( "%d", &integer1 ); /* read an integer */
14
15 printf( "Enter second integer\n" ); /* prompt */
16 scanf( "%d", &integer2 ); /* read an integer */
17
18 sum = integer1 + integer2; /* assign total to sum */
19
20 printf( "Sum is %d\n", sum ); /* print sum */
21
22 return 0; /* indicate that program ended successfully */
23
24 } /* end function main */

Enter first integer


45
Enter second integer
72
Sum is 117
10

Second C Program: Adding Two Integers


• int integer1, integer2, sum; is used to define variables.
• Variable names include letters and digits. They are case
sensitive.
• Variable declarations are placed before executable
statements.
• int variables hold integers.
11

Second C Program: Adding Two Integers


• scanf("%d", &integer1 ); is used to get a value from
user.
• %d shows that the input will be an integer
• &integer1 shows the location in which the input value
will be stored.

Yükleniyor…
• = assignment operator is used to assign a value to a
variable.
• printf("Sum is %d\n", sum ); is used to print a string of
characters and a value of a variable.
• %d shows that a decimal integer will be printed.
• sum is the variable name to be printed on the screen.
• Calculations can be performed inside printf statements.
12

Memory Concepts
• The variable names actually correspond to locations in
the computer’s memory.
• Each variable has a name, type, size and a value.
• Reading variables does not modify their values.
• When you place a new value to a variable, it overwrites
the old value.
13

Arithmetic
• +, - addition and subtraction
• *, / multiplication and division
• İnteger division produce integer result.
• % operator finds the remainder
• 9%4 returns 1
• Some operators have precedence over other operators
• Multiplication and division have higher precedence than
addition and subtraction
• You may use parenthesis.
14

Arithmetic
Arithmetic Algebraic
C opetration C expression
operator expression

Addition + f+7 f + 7
Subtraction – p–c p - c
Multiplication * bm b * m

Division / x
x y or xor ÷ xy / y
y

Remainder % r mod s r % s

Operator(s) Operation(s) Order of evaluation (precedence)


( ) Parentheses Evaluated first. If the parentheses are
nested, the expression in the innermost pair is
evaluated first. If there are several pairs of
parentheses “on the same level” (i.e., not nested),
they are evaluated left to right.
* Multiplication Evaluated second. If there are several, they are
/ Division evaluated left to right.
% Remainder
+ Addition Evaluated last. If there are several, they are
- Subtraction evaluated left to right.
15

Decision Making
• If the condition given in a if control statement is true,
the body of if statement is executed.
• If the condition given in a if control statement is false,
the body of if statement is not executed.
• 0 is false, non-zero values is true.
16

Decision Making

Standard algebraic C equality or


Example of
equality operator or relational Meaning of C condition
C condition
relational operator operator

Equality operators
= == x == y x is equal to y

≠ != x != y x is not equal to y
Relational operators

> > x > y x is greater than y

< < x < y x is less than y

≥ >= x >= y x is greater than or equal to y

≤ <= x <= y x is less than or equal to y


17

Decision Making
1 /* Fig. 2.13: fig02_13.c
2 Using if statements, relational
3 operators, and equality operators */
4 #include <stdio.h>
5
6 /* function main begins program execution */
7 int main( void )
8 {
9 int num1; /* first number to be read from user */
10 int num2; /* second number to be read from user */
11
12 printf( "Enter two integers, and I will tell you\n" );
13 printf( "the relationships they satisfy: " );
14
15 scanf( "%d%d", &num1, &num2 ); /* read two integers */
16
17 if ( num1 == num2 ) {
18 printf( "%d is equal to %d\n", num1, num2 );
19 } /* end if */
20
21 if ( num1 != num2 ) {
22 printf( "%d is not equal to %d\n", num1, num2 );
23 } /* end if */
24
25 if ( num1 < num2 ) {
26 printf( "%d is less than %d\n", num1, num2 );
27 } /* end if */
28
18

Decision Making
29 if ( num1 > num2 ) {
30 printf( "%d is greater than %d\n", num1, num2 );
31 } /* end if */
32
33 if ( num1 <= num2 ) {
34 printf( "%d is less than or equal to %d\n", num1, num2 );
35 } /* end if */
36
37 if ( num1 >= num2 ) {
38 printf( "%d is greater than or equal to %d\n", num1, num2 );
39 } /* end if */
40
41 return 0; /* indicate that program ended successfully */
42
43 } /* end function main */
43 } /* end function main */

Enter two integers, and I will tell you


the relationships they satisfy: 3 7
3 is not equal to 7
3 is less than 7
3 is less than or equal to 7

(continued on next slide… )


19

Decision Making
(continued from previous slide…)

Enter two integers, and I will tell you


the relationships they satisfy:
22 is not equal to 12
22 is greater than 12
22 is greater than or equal to 12

Enter two integers, and I will tell you


the relationships they satisfy:
7 is equal to 7
7 is less than or equal to 7
7 is greater than or equal to 7
20

Operators Associativitiy

Operators Associativity
() left to right
* / % left to right
+ - left to right
< <= > >= left to right
== != left to right
= right to left

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