EE171 Lecture 3 Introduction Computer Programming
EE171 Lecture 3 Introduction Computer Programming
Lecture 3:
Introduction to Computer Programming
1
Computer Programming?
Algorithm
Errors in Programming
Introduction to C Programming
2
Useful Links
https://www.studytonight.com/c/
https://www.javatpoint.com/c-programming-language-tutorial
3
Useful Apps (Android)
C4droid
CppDroid-C/C++ IDE
4
Computer Programming
5
Program Algorithm
Errors in Programming
Introduction to C Programming
8
Program Algorithm
9
10
Advantages of using Algorithm
11
14
Algorithm vs Flowchart vs Program
15
Computer Programming?
Program Algorithm
Errors in Programming
Introduction to C Programming
16
Errors in Computer Programming
17
language.
Must be corrected before the program can run
Program Algorithm
Errors in Programming
When you create a variable, you have to specify the data type
during the declaration.
int age;
Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
% Module division
Relational Operators
37
Relational Operators are symbols that are used to test the relationship
between two variables or between a variable and a constant.
Operator Description
> Greater than
< Less than
>= Greater than or Equal to
<= Less than or Equal to
== Equal to
!= Not equal to
Logical Operators
38
Operator Description
&& AND
|| OR
! NOT EQUAL
Bitwise Operators
39
These operators work only with int and char datatypes and cannot be
used with float and double type.
Operator Description
~ Bitwise NOT
| Bitwise OR
& Bitwise AND
^ Bitwise Exclusive OR (XOR)
>> Right Shift
<< Left Shift
Unary Operators
40
a %= b a=a%b
Conditional/Ternary Operator
42
int a, b, c;
a = 2;
b = 4;
c = b + a * a / b - a;
Operators Precedence
44
C is case-sensitive
All statements must end with semicolon.
A statement can be written in one line or it can split into multiple lines.
Braces must always match upon pairs, i.e., every opening brace {must have a
matching closing brace }.
Every complete C program must have the main( ) function.
Comments cannot be nested. E.g.: /* Welcome to C/* programming*/ */
A comment can be split into more than one line.
Get used to errors in programming
Input and Output Statements
47
#include <stdio.h>
int main() {
printf(“My name is James”); My name is James
}
printf() Function…
50
#include <stdio.h>
int main() {
printf(“My name is James \n”); My name is James
printf(“I like sunset”); I like sunset
}
Escape Sequence in C…
51
Escape
Meaning Description
Sequence
\n New line Shifts the cursor to the new line
\t Horizontal tab Shifts the cursor a single tab space on the same line
\a Alarm/Beep Generates a beep sound to the user
\r Carriage return Shifts the cursor to the beginning of the same line
\\ Backslash Displays backslash
\” Double quotes Displays double quotes
Escape Sequence in C…
52
#include <stdio.h>
int main() {
printf(“We need to \“chase\” our dreams \n”);
printf(“Sun is hot \t Earth is round”);
}
#include <stdio.h>
int main() { The value z is 16
int z = 16;
printf(“The value of z is %d”, z);
}
54
Write a C program that defines two integer
variables and displays their values to the screen.
#include <stdio.h>
int main()
{
int x = 16, y = 25;
printf(“Values of x and y are %d and %d”, x, y);
}
Value of A is 45
Value of A in Hexadecimal is 2D
56
#include <stdio.h>
int main() {
float B = 12.675;
printf(“Value of B is %f”, B);
printf(“Value of B is %0.2f”, B);
printf(“Value of B is %0.0f”, B);
}
Value of B is 12.675000
Value of B is 12.68
Value of B is 13
57
scanf( ) Function
58
#include <stdio.h>
int main() {
int number; Enter any number: 57
printf(“Enter any number: "); The number entered is 57
scanf(“%d”, &number);
printf(“The number entered is %d”, number);
}
59
What will happen if we ignore the address operator
(&) in the scanf() function????
#include <stdio.h>
int main() {
int number;
printf(“Enter any number: ");
?????????
scanf(“%d”, number);
printf(“The number entered is %d”, number);
}
60
Write a C program that prompts the user to enter any number. The
program should then calculate, and display the square of that number
#include <stdio.h>
int main() {
int number, square;
Enter an integer: 7
printf(“Enter an number: "); The square of n is 49
scanf(“%d”, &number);
square = number*number;
printf(“The square of n is %d”, square);
}
61
To Do List
Install a compiler for C programming language
Recommended compiler - codeblocks
Found at http://www.codeblocks.org/
Download, Install and Get familiar with it.
62