Csc181 1 - Introduction To C Programming
Csc181 1 - Introduction To C Programming
C Programming Language
Lecture .1 Introduction
2
Grading
• 40 Final
3
1. Introduction
History of C
•C
– Evolved by Ritchie from two previous programming
languages, BCPL and B
– Used to develop UNIX
– Used to write modern operating systems
– Hardware independent (portable)
– By late 1970's C had evolved to "Traditional C"
•Standardization
– Many slight variations of C existed, and were incompatible
– Committee formed to create a "unambiguous, machine-
independent" definition
– Standard created in 1989, updated in 1999
4
• Comments
– Text surrounded by /* and */ is ignored by computer
– Used to describe program
• #include <stdio.h>
– Preprocessor directive
• Tells computer to load contents of a certain file
– <stdio.h> allows standard input/output operations
5
Welcome
to Program Output
C!
1 /* Fig. 2.5: fig02_05.c
11
2 Addition program */
3 #include <stdio.h>
4
5 /* function main begins program execution */ fig02_05.c
6 int main()
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 */
12
Enter first integer
45
Enter second integer
72
Sum is 117 Program Output
13
integer1 45
17
integer1 45
integer1 45
integer2 72
integer2 72
sum 117
18
2.5 Arithmetic
• Arithmetic calculations
– Use * for multiplication and / for division
– Integer division truncates remainder
• 7 / 5 evaluates to 1
– Modulus operator(%) returns the remainder
• 7 % 5 evaluates to 2
• Operator precedence
– Some arithmetic operators act before others (i.e.,
multiplication before addition)
• Use parenthesis when needed
– Example: Find the average of three variables a, b and c
• Do not use: a + b + c / 3
• Use: (a + b + c ) / 3
19
2.5 Arithmetic
• Arithmetic operators:
C operation Arithmetic operator Algebraic expression C expression
Addition + f+7 f + 7
Subtraction - p–c p - c
Multiplication * bm b * m
Division / x/y x / y
Modulus % r mod s r % s
Ste p 5. y = 65 + 7; (La st a d d it io n )
65 + 7 is 72
!= 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
1 /* Fig. 2.13: fig02_13.c
23
2 Using if statements, relational
3 operators, and equality operators */
4 #include <stdio.h>
5 fig02_13.c (Part 1
6 /* function main begins program execution */ of 2)
7 int main()
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 ) {
24
26 printf( "%d is less than %d\n", num1, num2 );
27 } /* end if */
28
29 if ( num1 > num2 ) { fig02_13.c (Part 2
30 printf( "%d is greater than %d\n", num1, num2 ); of 2)
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 */