Learn C Language Basics Part1 - Mazen Nehme
Learn C Language Basics Part1 - Mazen Nehme
1-What is Programming ?
C- Programming Language
2
Given by: M@ZEN NEHME
2- Generations of Programming Languages
2.1- First Generation: Machine Language
C- Programming Language
3
Given by: M@ZEN NEHME
2.2- Second Generation: Assembly Language
- Example:
C- Programming Language
4
Given by: M@ZEN NEHME
2.3- Third Generation: High Level Languages
- These languages are shorter and easier than (Machine & Assembly
languages). they allows programmers to write instructions that look almost
like everyday English and math formulas.
C- Programming Language
6
Given by: M@ZEN NEHME
3- The C-language
- Developed by Dennis Ritchie at AT&T Bell Labs between 1969–1973.
- C is a scientific programming language, that is portable and efficient.
- C language was used to write operating systems (UNIX, Ms Windows)
- Many slight variations of C existed, and were incompatible. A modified
version of C was standardized by ANSI 1989 ( ANSI-C).
C- Programming Language
7
Given by: M@ZEN NEHME
- C is a “high level” language. A compiler is needed to translate a C-program
into machine language.
www.turboexplorer.com
C- Programming Language
8
Given by: M@ZEN NEHME
4- C Program Development Cycle
The Program Development Cycle has 4 steps:
C- Programming Language
9
Given by: M@ZEN NEHME
C- Programming Language
10
Given by: M@ZEN NEHME
5- Elements of a C Program
- A C program is made up of basic elements such as: expressions, statements,
constants and variable.
- Example:
- Example: int B ;
This line says, "I want to create a space called B that is able to
hold one integer value.”
Characters: A Z and a z.
Numbers: 0 9. That can be used in any position except the first of a variable name.
A variable name cannot contain any other special symbols such as: *, @, #, ?, and so on.
o 4flags =1 ;
o sum-result =1 ;
o method*4 = x ;
o what size? = m ;
- C Keywords:
The C language reserves certain keywords that have special meanings to the language.
Never use the C keywords as variable names , constants, or function names in your program.
The 32 C keywords are:
C- Programming Language
15
Given by: M@ZEN NEHME
5.3- A Simple C Program
#include <stdio.h>
Void main ( )
C- Programming Language
16
Given by: M@ZEN NEHME
A Complex-Obfuscated C Program
C- Programming Language
17
Given by: M@ZEN NEHME
Review Questions
1. Why do you need to put comments into your programs?
5. What error messages will you get when you're trying to compile the following program?
void main ( )
{
Printf ("Hello, this is my first C program.\n“)
C- Programming Language
18
Given by: M@ZEN NEHME
6- Data Types in C
C- Programming Language
19
Given by: M@ZEN NEHME
7- Output or display on the screen
#include <stdio.h>
void main()
{
int a, b, c;
a = 5;
b = 7;
c = a + b;
C- Programming Language
20
Given by: M@ZEN NEHME
Example:
#include <stdio.h>
void main()
{
Printf ( “ The temperature is “ );
NB: You can print all of the normal C types with printf by using different
placeholders :
C- Programming Language
21
Given by: M@ZEN NEHME
8- Reading from Keyboard
C- Programming Language
22
Given by: M@ZEN NEHME
C- Programming Language
23
Given by: M@ZEN NEHME
Example:
C- Programming Language
24
Given by: M@ZEN NEHME
Example:
C- Programming Language
25
Given by: M@ZEN NEHME
Example:
Write a C-program that accept 3 different integers from the user, and display the sum,
the average, and the product of these numbers. The output screen should be as follows:
C- Programming Language
26
Given by: M@ZEN NEHME
Example:
Write a C-program that calculate the square of a number. The output screen
should be as follows:
C- Programming Language
27
Given by: M@ZEN NEHME
9- Assignment Statement
C- Programming Language
28
Given by: M@ZEN NEHME
Example:
Write a C-program that display the integer equivalents of the following characters: A, B,
c, a, b, C, $, *, +, /
C- Programming Language
29
Given by: M@ZEN NEHME
10- Operators in C language
10.1- Arithmetic Operators:
C- Programming Language
30
Given by: M@ZEN NEHME
10.2- Increment und Decrement Operators
C- Programming Language
31
Given by: M@ZEN NEHME
10.3- Compressed Assignment Operators
B. Assign the sum of X and Y to Z, and then increment the value of X by 1 after
the calculation.(use only 1 statement).
C. Use one statement to decrement the variable X by 1 , and then subtract it from
the variable total.
C- Programming Language
33
Given by: M@ZEN NEHME
10.3- Decision Making: Relational Operators or Comparison Operators
C- Programming Language
34
Given by: M@ZEN NEHME
10.4- Boolean Expressions:
C- Programming Language
35
Given by: M@ZEN NEHME
11- The if – else Statement:
- In its basic form, the if statement evaluates a condition and directs
program execution depending on the result of that evaluation.
If expression evaluates to true, statement is executed.
If expression evaluates to false, statement is not executed.
In either case, execution then passes to whatever code follows the if statement.
if (condition)
Statement ;
if (condition)
{
Block of Statement s ;
}
if (condition)
Statement ;
else
Statement ;
Write a program that reads an integer and determines and prints whether it is odd or even.
Hints:
• Any even number leaves a remainder of zero when divided by 2. ex: 4%2 = 0 , 7%2 = 1
C- Programming Language
37
Given by: M@ZEN NEHME
Example:
Write a program that reads in two integers, and determines if the first is a multiple
of the second. (Use the modulus operator).
C- Programming Language
38
Given by: M@ZEN NEHME
Example: Write a program that calculates the net salary of an employee.
Given the following information: The amount of sales (AS), The Tax Rate (TR), and The Salary (S).
Consider the following:
• If the amount of sales is greater than 10000, the employee gets a commission of 4% of the sales
amount. Otherwise, he will get only 2%.
C- Programming Language
39
Given by: M@ZEN NEHME
Example:
Write a program that solve a quadratic equation: ax2 + bx + c = 0, using delta = b2 – 4ac
Suppose: a= 1, b= 2, c= -3
C- Programming Language
40
Given by: M@ZEN NEHME
C- Programming / Loops
-Looping includes:
2
3
4.1- THE FOR LOOP STATEMENT
The for statement executes a block of one or more statements
for a certain number of times. Example: displaying the numbers
from 0 to 10.
4
HOW FOR LOOP WORKS?
Example: displaying the numbers from 1 to 20.
5
4.2- THE WHILE LOOP STATEMENT
int N =10;
int i = 0;
while (i <= N)
{
printf (“%d “, i);
i = i + 1;
}
6
Example1: Write a Program to display the numbers from 1 to 100
using the For Loop.
7
Example2: Write a Program to display the numbers from 1 to 100
using the While Loop.
8
4.3- THE DO WHILE STATEMENT
Unlike for and while loops, which test the loop condition at the top
of the loop, the do...while loop checks its condition at the bottom
of the loop. A do...while loop is similar to a while loop, except the
fact that it is guaranteed to execute at least one time.
9
Example3: Write a Program to display the numbers from 1 to 100
using the do while Loop.
10
11
4.4- THE NULL STATEMENT
12
EXERCISES
13
Exercise 3: Write a program that accepts N grades and determine the average
and the result (succeeded or failed). N >0 is given by the user.
Exercise 4: Write a program that accepts N integers and count the number of
even integers within N. N >0 is given by the user.
Exercise 5: Write a program that accepts 10 numbers and determine the SUM of
positive numbers and the SUM of negative numbers.
Exercise 6: Write a program that accepts 10 numbers and determine the
average of positive numbers and the average of negative numbers.
Exercise 7: Write a program that displays L lines of the following Shape. L>0 is
given by the user. Here, L = 10.
14