0% found this document useful (0 votes)
31 views54 pages

Learn C Language Basics Part1 - Mazen Nehme

Uploaded by

otera.poko
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views54 pages

Learn C Language Basics Part1 - Mazen Nehme

Uploaded by

otera.poko
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 54

1

1-What is Programming ?

– Programming is simply writing a computer program to solve a given


problem.

– Computer programs tell the computer what to do, which information to


access, how to process it, and what equipment to use..

- A Program is a set of instructions or statements used in a programming


language such as: C , C + +, Visual Basic, JAVA, VB.net, C#, ASP.net…

- The AUTHOR of computer programs is named: the programmer.

C- Programming Language
2
Given by: M@ZEN NEHME
2- Generations of Programming Languages
2.1- First Generation: Machine Language

- A computer can directly understand only its own machine language.


Programs written in any other type of language must be translated
into machine language by the computer before they can be executed.

- The machine language uses the binary system (0, 1) to represent


the data and the instructions. The first programs were written in
binary form (0,1), which was a difficult task and charged with errors.

C- Programming Language
3
Given by: M@ZEN NEHME
2.2- Second Generation: Assembly Language

- As computers became more popular, instead of using the machine


language, programmers began using English-like-abbreviations to
represent the data and the instructions.

- Example:

- A program written in Assembly Language is translated into Machine


Language using a program translator named: the Assembler.

C- Programming Language
4
Given by: M@ZEN NEHME
2.3- Third Generation: High Level Languages

- High-level languages (such as: Pascal, C..) were developed to simplify


programming and to speed up the programming process. A single statement
could be written to accomplish a complete task. Ex: Average= Sum/NB

- 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.

- The translator programs that convert a program written in high level


language into machine-language are called ‘COMPILERS’, ex: (Borland
C++, Turbo C, Microsoft Visual Studio / Visual C++)

C- Programming Language / chapter 1


5
Given by: M@ZEN NEHME
Development of Programming Languages

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.

- Examples: Microsoft Visual Studio , Turbo C++, Free C/C++ compiler…

www.turboexplorer.com

C- Programming Language
8
Given by: M@ZEN NEHME
4- C Program Development Cycle
The Program Development Cycle has 4 steps:

1- In the first step, you use an editor


to create a file containing your source code,
and save your file using the extension “.c”,
example: myprog.c

2- In the second step, you compile the source code


to create an object file  myprog.obj

3- In the third step, the compiled code


Is linked to create an executable file.

4- In the fourth step the program


is executed.

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.

5.1- Constants and Variables:


 A constant is a value that never changes.

 Variables correspond to locations in the computer's memory. A variable can be


used to represent different values.

Example: I = 1; //the variable I contains the constant value: 1

- Every variable has: a name, a type, a size and a value:

- Example:

 int I = 1; // an integer reserves 2 bytes of memory space.


 float a = 5.13; // a float number reserves 4 bytes of memory space.
 char x = ‘a’ ; char x = ‘1’; // a char reserves 2 bytes of memory space.
 char name [ ] = “Ali”;
C- Programming Language
11
Given by: M@ZEN NEHME
Using Variables:
- As a programmer, you will frequently want your program to
"remember" a value. For example, if your program requests a
value from the user or if it calculates a value, you will want to
remember it somewhere so you can use it later. The way your
program remembers things is by using variables.

- Example: int B ;

This line says, "I want to create a space called B that is able to
hold one integer value.”

- You can store a value in B by saying something like: B = 5;

-You can use the value in B by saying something like:


printf ( "%d", b);

The "%d" is a placeholder (for integers) that will be replaced by the


value of the variable B when the printf statement is executed.
C- Programming Language
12
Given by: M@ZEN NEHME
Naming a Variable:
- Valid Variable Name:

 Characters: A  Z and a  z.

 Numbers: 0  9. That can be used in any position except the first of a variable name.

 The underscore character: _

- Now, let's see what you cannot use in variable naming:

 A variable name cannot starts with a number.

 A variable name cannot contain any dots ( . )

 A variable name cannot contain any apostrophes ( ‘ ) or ( “ )

 A variable name cannot contain any other special symbols such as: *, @, #, ?, and so on.

 A variable name cannot contain C - Keywords.

C- Programming Language / chapter 3


13
Given by: M@ZEN NEHME
- Examples of invalid variable names:

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 / chapter 3


14
Given by: M@ZEN NEHME
5.2- The C Library:

A library is a set of predefined functions and instructions that can you


use in your program in order to reduce time and effort.

C- Programming Language
15
Given by: M@ZEN NEHME
5.3- A Simple C Program

/* Printing a Line of Text */

#include <stdio.h>

Void main ( )

printf (“ Hello,This is my first C program ");

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?

2. Why is the main() function needed in your program?

3. What does the #include directive do?

4. Can a C compiler see the comments within your C program?

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;

printf("%d + %d = %d", a, b, c);

C- Programming Language
20
Given by: M@ZEN NEHME
Example:

#include <stdio.h>

void main()
{
Printf ( “ The temperature is “ );

Printf ( “ %d ", b);

Printf ( " degrees\n “ );


}

/* An easier way is to say this:


printf ( “ The temperature is %d degrees\n ", b); */

NB: You can print all of the normal C types with printf by using different
placeholders :

•int (integer (whole number) values) uses %d


•float (floating point values) uses %f
•char (single character values like 'm' or 'Z') uses %c
•character strings (arrays of characters, discussed later) use %s

C- Programming Language
21
Given by: M@ZEN NEHME
8- Reading from Keyboard

Syntax: scanf ( “ %d “ , & myNumber );

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, $, *, +, /

The output should look like:

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

C- Programming Language / chapter 3


32
Given by: M@ZEN NEHME
Example:

A. Write 4 different C statements, that each add 1 to an integer variable X.

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.

D. Calculate the remainder after Q is divided by D and assign the result to Q.


(write it in 2 different ways).

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:

10.5- Logical Operators:

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 ;

Practicing C++ Programming Language


36
Given by: M@ZEN NEHME
Example:

Write a program that reads an integer and determines and prints whether it is odd or even.

 Hints:

• Use the modulus operator % .

• 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%.

• Gross Salary = Salary + commission.

• Tax = Gross salary * Tax Rate.

• Net Salary = Gross salary – Tax

 The Output must be as follows:

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

 if delta is < 0  no solution


If delta = 0  2 equal roots; x1=x2 = -b/2a
If delta >0  2 distinct roots;

Suppose: a= 1, b= 2, c= -3

 ax2 + bx + c = 0 x2 + 2x - 3 = 0 Delta = 4 - 4(1)(-3)= 4 + 12 = 16


 delta is positive  we have 2 distinct roots: x1=(-2-4)/2= -3 and x2=(-2+4)/2=1

C- Programming Language
40
Given by: M@ZEN NEHME
C- Programming / Loops

Repetition or Doing the Same Thing Over and Over


1
LOOPS

-Looping, also called iteration, is used to repeat the same set


of statements until a certain condition is met.

-Looping includes:

The for statement


The while statement
The do-while statement

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

The while statement executes a block of one or more statements


while a given condition is true.

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

Example: for (i=1; i<10; i++);


Here the for statement is followed by a semicolon immediately.
In other words, a null statement is a statement with no
expression. It will just loop for some seconds without doing any
action.

4.5- THE INFINITE LOOP

Example: for (i=1; ; i++);

The statements inside the statement block will be executed over


and over without stopping.

12
EXERCISES

Exercise 1: Write a program that prints the cubes of the


numbers from 1 to 10, using a two-column format. Here’s the
output:

Exercise 2: Write a program that accepts 10 integers and


calculate the Sum.

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

You might also like