Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 44

By: ♥Zhee ♥

C OVERVIEW
Invented by Dr. Dennis Ritchie at AT&T Bell
Laboratories in early 1970’s. It is a general purpose
programming language and it started to run under Unix
environment.
C is considered as middle-level programming
language, because it combined the power of low-level
language (such as Assembly language) and the elegance
of high-level language like Pascal.
FORMAT OF A TURBO C PROGRAM

<comment>
<include directive>
<global variable declaration>
main()
{
<local variable declaration>
<Turbo C statements>
}
COMMENT
 Starts with /* and ends with */

 Programmers insert comments to document programs


and improve program readability.

 Can be placed anywhere in the program.

 Comments are ignored by the C compiler.


INCLUDE DIRECTIVE
 Is the preprocessing directive to the C preprocessor.

 Lines beginning with # are processed by the


preprocessor before the program is compiled.

 This tells the compiler what additional files are needed


before processing the actual program.

 Contains the information needed by the program to


ensure correct operation of Turbo C’s standard library
functions.
INCLUDE DIRECTIVE
 Example:

#include<stdio.h>

 This specific line tells the preprocessor to include the


contents of the standard input/output header file (stdio.h)
in the program.
VARIABLE DECLARATION

 This is the place where you declare the variables that


will be used in the program.

 Types of variable declaration:

a) Global variable declaration


b) Local variable declaration
VARIABLES DEFINED
 Variables are identifiers that can store changeable value.

 Variables are important since they contain the values


needed for manipulation and evaluation of data.

 Variable names are stored in the computer’s memory.


RULES FOR NAMING VARIABLES
1) It must consist only of letters, digits and
underscore.
2) It should not begin with a digit.
3) An identifier defined in the C standard library
should not be redefined.
4) It is case sensitive (uppercase is not equal to
lowercase letters).
5) Do not include embedded blanks.
6) Do not use any of the C language keywords as
identifiers/variables.
7) Do not call your variable/identifier by the same
name as other functions.
KEYWORDS
 Keywords are reserved words that have a special
meaning in a programming language.

auto double int struct


break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while
GLOBAL VS LOCAL VARIABLES
 Global Variables
Variables that are declared outside a function.
They are known throughout the entire program.

 Local Variables
Variables that are declared inside a function.
They can only be referenced inside that function. They
are also called “automatic variables”.
GLOBAL VS LOCAL VARIABLES
 Global Variables  Local Variables

#include <stdio.h> #include <stdio.h>


int a,b,c, x,y,z; main()
main() {
{ int a,b,c;
}
}
function()
function() {
{ int x,y,z;
}
}
VARIABLE DECLARATION
 All variables must be declared before they may be used.

Syntax:
type variable_list;
Where:

type is any valid data type


variable_list is 1 or more variable names
with comma separator
DATA TYPES
 There are 5 basic data types in Turbo C:

a) Character (char) – use to hold ASCII characters.

b) Integer (int) – use to hold whole number values

c) Floating Point (float) – use to hold real numbers

d) Double Floating Point (double) - use to hold real numbers

e) Void (void) – use to declare a function with no return values,


functions with no parameters and to create generic pointers.
DATA TYPES
TYPE BITWIDTH RANGE

char 8 0 to 255 (ASCII)

int 16 -32768 to 32767

float 32 3.4E-32 to 3.4 E+38

double 64 1.7E-308 to 1.7 E+308

void 0 valueless
DATA TYPES

TYPE EXAMPLES

char ‘A’ ‘b’ ‘$’ ‘9’

int 1 250 4500

float 3.5 42.56 345.6789

double 3.5647290… 486.145875...

void valueless
VARIABLE DECLARATION
Examples:
char name;

int x, y,z;

float number;

float n1, n2, sum;

double counter;
VARIABLE INITIALIZATION
 Giving a value to a variable during the variable’s
declaration is called “variable initialization.”

Syntax:

type variable_name = value;

Example:

int count = 100;


float grade = 3.75;
char status = ‘S’;
MAIN()
 Is a part of every C program.

 The parenthesis after main indicate that main is program


building block called a function.

 C programs contain one or more functions, one of which


must be main.

 Every program in C begins executing at the function


main.
{ AND }
 The left brace, must begin the body of every function.

 The right brace, ends the body of every function.

 The pair of braces and the portion of the program


between the braces is also called a block.

 All statements that will be executed by the program


should be placed inside the { and }.
TURBO C STATEMENTS

 Valid Turbo C statements that will be executed by your


program.

 Every statement in Turbo ends with a semicolon (;)


which is the “statement terminator” in Turbo C.
CLEAR SCREEN STATEMENT

 clrscr();

 This is the clear screen function in Turbo C.

 This is used to clear the command prompt every time the


program is executed.
GETCH STATEMENT
 getch();

 Last statement that will be placed before the closing


brace }

 Used to input a single character from the keyboard


without echoing the character on the monitor.

 Causes delay in displaying the output once the program


is executed
THE PRINTF() STATEMENT

 This is used to display argument list on the monitor.

 This an output function in Turbo C

Syntax 1:
printf (“argument”);

Examples:
printf (“De La Salle – Dasmariñas”);
printf (“Computer Programming is Easy!\n”);
Note:
\n is an escape sequence for NEW LINE
\t is an escape sequence for TAB.
THE PRINTF() STATEMENT
Syntax 2:

printf (“control string code”, argument list);

Example:
a=100;
printf (“%d”, a);
printf (“The value is %d”, a);

 The control string code contains the format command that tells
printf() how to display arguments and how many arguments are on
the list.
THE PRINTF() STATEMENT
Control String Code Data Type

%c character
%d integer
%f float/double

Notes:

1. Control string code and argument list should agree in type.

2. They should have a one-to-one correspondence.


THE PRINTF() STATEMENT
Example:

int x = 10;
int y = 25;
float z = 123.1234;
printf( “x = %d and y = %d\n”, x, y);
printf (“ z = %f\n”, z);
printf (“ z = %.2f\n”, z);
Output:
x = 10 and y = 25
z = 123.1234
z = 123.12
THE SCANF STATEMENT
 This is used to input a single character or sequence of
characters from the keyboard.

 It needs the control string codes to be recognized.

 Spaces are not accepted upon inputting.

 Terminated by pressing spacebar.

 This is an input function in Turbo C.


THE SCANF STATEMENT
Syntax:

scanf (“format control string”, &input list);

Examples:
int x,y;
float grade;
char letter;
scanf (“%d”, &x);
scanf (“%f”, &grade);
scanf (“%c”, &letter);
scanf (“%d %d”, &x, &y);
THE SCANF STATEMENT
 The format control string indicates the type of data that should be input by
the user.

format control string Data Type

%c character
%d integer
%f float
%lf double

 The ampersand (&) is called the address operator in C. This is placed


before the variable name in a scanf() statement.
 The address operator in C tells scanf() the location in memory in which the
variable will be stored. The computer then stores the value of the variable
at that location.
THE ASSIGNMENT STATEMENT
 This is used to assign a value to a variable.
 This is used for computations needed in the program.
 Equal sign (=) is the assignment operator in Turbo C.
Syntax:
variable_to_hold_value = expession;
Example:

sum = integer1 + integer2;


TURBO C’S SHORTHAND

x = x + 5; x+=5;

x = x - 7; x-=7;

x = x * 12; x*=12;

x = x / y; x/=y;
SAMPLE C PROGRAM: ADDING 2
NUMBERS
1 /* Addition Program */
2
3 #include < stdio.h >
4
5 main( )
6 {
7 int integer1, integer2, sum;
8 clrscr();
9 printf ( “Enter first integer : ” );
10 scanf ( “%d”, &integer1 );
11 printf ( “Enter second integer: ” );
12 scanf ( “%d”, &integer2 );
13 sum = integer1 + integer2;
14 printf ( “Sum is %d \n”, sum );
15 getch();
16 }
CONSTANTS
 Constants are identifiers that can store a value that cannot be changed
during program execution.
 Like variables, constants are declared before they are used in the
program.

Syntax:
const type variable_name = value;
Where:

const is a Turbo C keyword


type is any valid data type
variable_name is the name given to a constant
value fixed value given to a constant
Example:
const int count = 100;
OPERATORS
 Operator is a symbol that tells the compiler to perform
specific mathematical or logical manipulations.

 There are 3 classes of operators in Turbo C:

a) Arithmetic Operators
b) Relational & Logical Operators
c) Bitwise Operators
ARITHMETIC OPERATORS
 Arithmetic operators are used to perform arithmetic
computations.

+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus Division (MOD)
-- Decrement
++ Increment
ARITHMETIC OPERATORS
 When division (/) is applied to an integer, any remainder is
truncated.
 % is only used on integer data type.
 Any number preceded by - sign switches it’s sign.
 The increment (++) operator adds one to its operand and the
decrement operator (--) subtracts one.
 Both ++ and -- may either precede or follow the operand (e.g. ++x
or x++).
 When an increment or decrement operator precedes it operand, C
performs the increment or decrement operation prior to using the
operands value. If it follows its operands, C uses the operand’s value
before incrementing or decrementing it.
RELATIONAL & LOGICAL OPERATORS
 Relational operators show the relationship values have
with one another.

 Logical operators show the ways these relationships can


be connected together using rules of formal logic.

 The key to the concepts of true relationship between


relational and logical operators is the idea of TRUE and
FALSE. In turbo C, TRUE is any value other than zero
(0). FALSE is zero (0).
RELATIONAL & LOGICAL OPERATORS

A. Relational Operators B. Logical Operators


> greater than ! Not
>= greater than or equal && And
< less than || Or
<= less than or equal
== equal
!= not equal
RELATIONAL & LOGICAL OPERATORS

Truth Table
P Q P && Q P || Q !P

0 0 0 0 1
0 1 0 1 1
1 1 1 1 0
1 0 0 1 0
BITWISE OPERATORS

 Bitwise Operators are the testing, setting or shifting of


the actual bits in a byte or word, which corresponds to
C’s character and integer data types and variants.

 Bitwise operators cannot be used on type float, double,


long double, void or other complex types.
BITWISE OPERATORS

& And
| Or
^ Exclusive Or (XOR)
~ One’s Complement
>> Shift Right
<< Shift Left
BITWISE OPERATORS
1) & (and). Any of the operand is 0, the outcome is set to 0.
2) | (or). Any bit on the operand is 1, the outcome is 1

3) ^ (XOR). If exactly one of the operator is 1, the outcome is one.


4) ~ (One’s complement). Reverse the state of each bit in the specified
variable. That is, all 1’s are set to 0, and all 0’s are set to 1.

5) >> (Shift Right). Move all bits in a variable to the right.

6) << (Shift Left). Move all bits in a variable to the left.


EXPRESSIONS
 Is any valid combination of operators, constants and
variables that evaluates to a value.
 Operators’ Precedence
() Highest
!, unary +, unary –
*, /, %
Binary +, binary –
<, <=, >, >=
==, !=
&&
|| Lowest

You might also like