8/14/2019 C Programming language.pdf
1/29
C Programming language
Section 1
8/14/2019 C Programming language.pdf
2/29
Program: is a set of statements that solves a problemAll programs work on data, which is the fuel of the program.
So, we will learn the types of data, how to deal with them, andthe different arrangement of data
The program itself is trying to solve a real world problem. So,
we have:
Real World Computer World
This is the problem The Program
The transformation from the real world to the computer world
is an industry called programming
8/14/2019 C Programming language.pdf
3/29
Why Learn C?
Compact, fast, and powerful
Mid-level Language
Standard for program development (wide acceptance) It is everywhere! (portable) Supports modular programming style
Useful for all applications
C is the native language of UNIX
Easy to interface with system devices/assembly routines C is terse
8/14/2019 C Programming language.pdf
4/29
The following program is written in the C programming
language:
#include int main(){printf("Hello World \n");
}
8/14/2019 C Programming language.pdf
5/29
The Preprocessor :The preprocessor are compiler directive to direct the
compiler before compiling.
The preprocessor statements are statements that execute
before compiling This statement begin with # example:
#include
The header files are file that contain the function prototype
of the function defined in the library file.
Before compilation, the compiler will compile the header
file then complete the program.
8/14/2019 C Programming language.pdf
6/29
8/14/2019 C Programming language.pdf
7/29
8/14/2019 C Programming language.pdf
8/29
The data type:Char (1byte) (-128 to 127)
int (2byte) (-32768 to 32767)
float (4byte) (3.4e-38 to 3.4e38)
double (8byte) (1.7e-308 to 1.7e308)
The difference between float and double is inthe precision The float has 7 number after the
decimal point The double has 15 number after
the decimal point.
8/14/2019 C Programming language.pdf
9/29
There is also short int (2byte)
long int (4byte) (-2147483648 to 2147483647)
long double (10byte) 23 number after the decimal point
unsigned int (2bytes) (0 to 65535)
unsigned short int (2bytes) (0 to 65535)
unsigned long int (4bytes) (0 to 4294967295
8/14/2019 C Programming language.pdf
10/29
Constant:It is a fixed value that the program may not change. It can have any
of the data type above.
It is declared with one of the two ways:-In the preprocessor:
# define constant-name constant-value
Ex: - # define x 3 this mean x is an integer with value 3
# define ch a this mean ch is a character with value a
-In the program
const data-type constant-name = constant-value;
Ex:- const int x=3;
const char ch=a;
8/14/2019 C Programming language.pdf
11/29
8/14/2019 C Programming language.pdf
12/29
Note1. The variable may be declared:
Locally: It is declared in the function. So, we can use it only in this
function and it end with the end of function.
Global: The variables are declared out of any function. So, we can
use them in any part of the program and also we can use them in any
function2. Any variable declared in the main () are locally for
the main only.
3. It is not recommended to use global variable.
8/14/2019 C Programming language.pdf
13/29
The program statementsThe program statement consists of many things:
Input function, Expression and operation, Output functionEach statement must terminate with ;
8/14/2019 C Programming language.pdf
14/29
The input function:These function are used to get data from users.
The following function can be used: scanf() function:it has the format:
scanf(format specifier, &var-name);
example: scanf(%d,&x);
it is in the header file stdio.h
8/14/2019 C Programming language.pdf
15/29
Format specifier:%d integer
%f float
%c char
%s string%u unsigned integer
%x hexadecimal
%ld long integer
%lf double
%Lf long double
8/14/2019 C Programming language.pdf
16/29
The output function:These functions are used to display the results on the screen.
The following function can be used:
printf() function:It has the format: printf(message sequence, variable);
Example: printf(hello %c\n,ch);It is in the header file stdio.h
putchar() function:it display a character on the screen
it has the format: putchar(int );
example: putchar(23);
It is in the header file stdio.h
8/14/2019 C Programming language.pdf
17/29
8/14/2019 C Programming language.pdf
18/29
The Expression and Operators:1 OperatorsThere are several operators in C language:It may be unary operator which has one operand or binary
operator which has two operands. All operation must have a
value.
The operator may be classified as:The Mathematical operator:+ sign add the two operands
- sign subtract the second operands from the first operands
* sign multiply the two operands
/...sign divided the first operand by the second
%.sign modulus operator (it give the reminder of the division)
E l
8/14/2019 C Programming language.pdf
19/29
Example:
# include
void main()
{int i,x,y;
x=100;
y=7425;
i=y%x;
printf(%d,i);}
The value will be 25
Note:*) In the division, if we divide int by int the result will be integer.
*) To divide two integer and to have the value float we must make one of the operandsacts as float. This is done by using type casting:
The type casting: You force an expression to be of a specific type during a certainstatement only. It has the form: (new_type) expression so we can use it to have a decimal
value when dividing two integer
8/14/2019 C Programming language.pdf
20/29
Example:
# include
int main()
{
float x,y;
float i;
x=3;
y=7;
i=y/x;
printf(%f,i);
return 0;
}
The value of i will be 2.3333333
8/14/2019 C Programming language.pdf
21/29
The Relational operator
It allows the programmer to compare two values and return non zero
value if the comparison is right or zero if the comparison is wrong.
Usually used for conditional statements.
> greater than
< lower than
>= greater than or equal
8/14/2019 C Programming language.pdf
22/29
The Increment/Decrement operator
++ increment by 1-- decrement by 1
The pre-increment: ++x
The post-increment: x++The difference between pre-increment and post-
increment can be seen in
the following examples
8/14/2019 C Programming language.pdf
23/29
#include
void main()
{
int x,y;x=10;
y=++x;
printf(%d \n,x);
printf(%d \n,y);}
the value of x=11
the value of y=11
x=x+1;
y=x;
increment then assigning
8/14/2019 C Programming language.pdf
24/29
#include
void main()
{
int x,y;x=10;
y=x++;
printf(%d \n,x);
printf(%d \n,y);}
the value of x=11
the value of y=10
y=x;
x=x+1;
assign then increment
8/14/2019 C Programming language.pdf
25/29
The assignment operator= equal x = 5;
+= x += 5; equivalent to x = x+5;-= x -= 5; equivalent to x = x-5;
*= x *= 5; equivalent to x = x*5;
/= x /= 5; equivalent to x = x/5;
%= x %= 5; equivalent to x = x%5;Note:
The assignment puts the value at the Right Hand Side(RHS) in the variable at the Left Hand Side (LHS).
Then the operation of the assignment has a value whichis the RHS. This helps us to perform multiple
assignment which is valid in C Language.
i.e. : x = y = z = 4;
8/14/2019 C Programming language.pdf
26/29
2 ExpressionA) Selection (conditional) statements
1 The if statements:if (condition)
{
statements;
}The statement between { } will be valid if the condition is true(non
zero) other wise it will be escaped and continue the program.
The if statement is used when some extra instruction will be done if
the condition is valid then continues the program and if thecondition is not valid, it continues the program:
8/14/2019 C Programming language.pdf
27/29
8/14/2019 C Programming language.pdf
28/29
3)The Switch case:
It is a multiple branch selection, it has the form:
switch (expression)
{
case constant1:
{
statement; break;
}
case constant2:
{
statement; break;
}
default:
{statement;}
}
8/14/2019 C Programming language.pdf
29/29
Example1:Write a program to enter 1, 2, or 3 then the program
write one, two or three otherwise write Out of range