Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
340 views

Chapter 2-Introduction To C Programming Language

1. The document explains key concepts in C programming including variables, data types, constants, expressions, assignment statements, and formatted output. 2. It provides examples of declaring and initializing variables and constants, writing expressions, and using the printf statement. 3. The proper steps to write a C program are given as understanding the problem, developing an algorithm and flowchart, writing and testing the program, and running it to obtain results.

Uploaded by

Falah Alkahteeb
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
340 views

Chapter 2-Introduction To C Programming Language

1. The document explains key concepts in C programming including variables, data types, constants, expressions, assignment statements, and formatted output. 2. It provides examples of declaring and initializing variables and constants, writing expressions, and using the printf statement. 3. The proper steps to write a C program are given as understanding the problem, developing an algorithm and flowchart, writing and testing the program, and running it to obtain results.

Uploaded by

Falah Alkahteeb
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 48

Chapter Two

Introduction to C Programming Language


OBJECTIVES LEARNING OUTCOMES (LO)
3. Explain the specific environment and 1. Demonstrate knowledge of

different elements (general form of a C different levels of computer


programming languages.
programs, Input/ Output functions, data
2. Write a C program, compile
types, variables-variable names and
it, link it, execute it and
variables declaration, constant- constant debug it.
names and constant declaration, 3. Define and initialize
statements, in C-programming.. variables.

4. Explain steps in programming with C- 4. Get data from input objects


and store the data in output
sample top down program code.
1
objects.
2.1 What is a Program?
• It is a set of instructions written in a
computer programming language to solve a
particular problem .
To write programs, a programmer should be
aware of:
1. Syntax of the Programming language:
Grammar or rules of writing statements in that
programming language.
2. Semantics:
Logics, skills and problem solving abilities
needed to write programs. 2
•In any programming language there
are:
A. Methods (statements) to input
data.
B. instructions (statements) to
process the data.
C. methods (statements) to output
the results.
3
Examples of statements to input data:
Through Initialization(static): Ex:
salary=510.6;

Dynamically(at the time of running the


program): Ex:

scanf("%d", &n);

4
Examples of statements to process data:
area= 3.14 * r *r ;
m= q*p*(1-d/100.0);

Examples of statements to output results:


printf("%f\n"”, area);

printf("%f %d %c\n", x, y, z);

5
Proper steps to write a program :
1. Understand the problem by:
• Knowing the inputs.
• Knowing the required outputs.
• Defining relationships between inputs
and outputs and identifying the required
processing.

2. Developing the algorithm and drawing


the flowchart.
6
3. Write the program using a
programming language like C using some
IDE (Integrated Development
Environment) like CodeBlocks.
4. Test and debug the program.
5. Run the program, input data, and get
the results from the computer.
2.2 The Compiler:
•A C program statements are written
using English words. 7
•A computer is a digital system that
understands and executes instructions that
are saved as binary codes called machine
language (using 0,1 digits only).
•A Compiler is a system software that does the
following:
1. Translating program statements to machine
language (0,1) so that the computer can
execute them.
2. Finding syntax errors in written programs so
that the programmer will correct them.
•We will use CodeBlocks TurboC compiler. 8
2.3 Structure of a C program:
•Any C program consists of a number of
functions.

•One of the functions is the main() which


will be the first function that the
computer executes.

•The general layout of the main() function


is:
9
preprocessor directives
void main( )
{
variables declaration statement 1;
variables declaration statement 2;
constants declaration statement 1;
input statement 1; /*comment*/
input statement 2;
processing statement 1; /*comment*/
processing statement 2;
output statement 1; /*comment*/
} 10
Example:
#include <stdio.h> /*preprocessor directive*/
void main( )
{ /*start of main() function*/
float x, y, average; /*variables declaration*/
scanf(“%f%f”, &x, &y); /*input statement*/
average= (x + y)/2; /*processing statement*/
printf(“ Average=%f \n”, average);
/*output statement*/
} /*end of main() function*/
11
Preprocessor Directives:
They are instructions to compiler to do
some actions before translating the
program to machine language.
Ex: #include<stdio.h>
It tells the C compiler to add contents of a
file to the program, in this case the header
file stdio.h.
stdio.h is library definition file for all Input
Output functions of C Language. Other
header files are: conio.h, math.h, … 12
void main ( )
It is used at the start of the main and is the
first function to be executed in a C program.

{ start of main function or any other


function or at the start of a block of code
within a function.

} end of the main function or any other


function or at the end of a block of code
within a function.
13
Comments
• can contain any text written between /*
and */
•can be more than one line.
•can be written anywhere in the program.
• have no effects on program processing,
just notes to explain program steps.
Examples:
/*This program is to calculate the maximum mark*/

/*First the grade should be checked


that it is above 2.50. */ 14
Statements:
• the processing part of the program.
• reflect the steps needed to solve the
problem according to its algorithm and
flowchart.
Two types of statements in C programs:
a. Simple Statement:
Each statement terminated by ‘ ; ’
Ex:
float x, y, average;
scanf(“%f%f”, &x, &y); 15
b. Compound statement:
Consists of a number of simple statements
enclosed between { }. Usually called a
Block of Code.
Ex:
if (x<=3)
{ x++;
printf("%d", x);
}
while (ch<=’b’)
{ printf("%c", ch);
ch+=2;
} 16
2.4 Data Types in C program:
•Any C program may have two types of data:
1. Variable data.
• Values that the program can change.
• Variables are usually stored in the
computer memory.
2. Constant data.
• Values that the program cannot change.
Variables & constants can be of different
data types. Some of these data types are:
17
18
2.5 Identifires:
Are names given by the programmer for
the different objects created in program
like constants, variables, functions, .....
To write identifiers use:
1. English letters from a to z and A to Z.
2. Digits from 0 to 9.
3. Underscore “_”.
Conditions to write identifiers:
19
- First character must be a letter or an
underscore.
-Don’ t use other symbols like @, ?,
space, $ in identifiers.
-Don’t use keywords as identifiers.

- Ex: float, int, double, char, main, return,


case, switch, do, static, else, for, while ...

20
Note:
C language is a case sensitive.
Ex: identifiers mark, MARK , Mark are
considered as different identifiers.

21
Examples of some Valid Identifiers:
name
PI
value1
_9_3
Student_Id
Examples of some Not Valid Identifiers:
float
6time
Student name
M,Y
do
full-mark 22
2.6 Variables Declaration :
-Any variable must be declared before using it
in the program.
-When a variable is declared, a location in
computer memory will be assigned by the
complier to store that variable.
-Any changes on the value of that variable will
be stored in this memory.

- Declaration of a variable is by giving it a


name (any valid identifier) and specifying its
data type. 23
Examples:
char grade;
float salary;
A number of variables of the same data
type can be declared in one statement.
Examples:
int x, y, sum;
float a, b, av;
24
Variables can be given some initial values
within declaration statements.
Of course the values of these variables
may change within the program.

Examples:

double m=-3.4, n=29.145;


char grade=’A’, t=’n’, s;

25
2.7 Constants Declaration :
•A constant may be declared before using it
in the program.

•Declaration of a constant is by giving it a


name (any valid identifier) and specifying its
data type preceded by the word “const”.

• With declaration the constant must be


given a value and that value can not be
changed by the program.. 26
Examples:
const float PI=3.1415;
const int M=10, N=20;
2.8 Expressions:
•An expression consists of variables and
constants separated by operators.

•There are different types of expressions


in C programs depending of the types of
operators used with them. 27
Examples:

x * 7.5+ y++ Arithmetic expression


-8 Arithmetic expression
t>=n Relational expression
a && b Logical expression

(a != 5) || !b++ Mixed Relational,


Logical and Arithmetic expression

28
2.9 Assignment Statements:
•They represent processing statements in C
programs where different calculations can be
performed.
Syntax:
variable = expression;
The operator “=“ is called the Assignment
operator.
Example:
{ float t, x=91.55;
int y=-4;
t= -y + x/5 ;} 29
Example: How to write the assignment
statement to calculate x from the
following equation:

y 2 + x =95
2
Answer:

x=sqrt( 95 – y*y);
30
2.10 Formatted Output on the screen:
•represents one of the methods used in C
programs to output (print) results on the
computer screen .
•The library function printf( ) is used for
that purpose.

•Preprocessor directive #include <stdio.h>


must be used at the start of the program
to add the header file <stdio.h> to the
program. 31
Syntax of printf():

•format control string may contain two


parts:
1. A constant string.
• represent the text to be printed on
screen.
• This text will be printed on screen as it
is written within printf( ) except :
32
a. letters that are preceded by the
backslash character ‘\’ .
b. letters that are preceded by the
percentage character ‘%’.

•The letters that are preceded by backslash


character ‘\’ represent control characters
(called Escape Sequences).

•They are used for other purposes and they


are not printable characters.
33
printf(“ The Number =\n %d”, .....);

The Number =

34
In most of our programs we will use the
following escape sequences.

2. The Format Specifiers.


• Letters that are preceded by %.
• Not printable.
• Used specify the type and format of
data that will be printed on the screen.
35
printf(“ The Number =\n100”);
The Number =
100

printf(“ The Number =\t100”);

The Number = 100


36
{ printf(“ My Na\nme is: ”);
printf(“ C \tteacher ”);
}

37
• Any Format Specifier will be replaced by
a value of either constant or variable or
expression that is written after the
Format Control String and that value will
be printed on the screen.
Some Format Specifirs are:

38
39
40
41
42
2.11 Formatted Input from Keyboard:
•Represents one of the methods used in C
programs to input different types of variable
data from the keyboard to the program.

•The library function scanf( ) is used for that


purpose.

•Preprocessor directive #include <stdio.h> must


be used at the start of the program to add the
header file <stdio.h> to the program.
43
Syntax of scanf():

•Used to input values of variables only from


the keyboard.
•The control string contains only the Format
Specifiers of the variables to be read.
•Number and type of format specifiers
depend on the number and type of variables
listed within scanf().
•Each variable name must be preceded by
symbol ‘&’ which is called Address Operator. 44
45
46
47
Exercise Problems Page 23: Solve all.

48

You might also like