Introduction To C Programming
Introduction To C Programming
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 */
#include<stdio.h>
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
Syntax:
type variable_list;
Where:
void 0 valueless
DATA TYPES
TYPE EXAMPLES
void valueless
VARIABLE DECLARATION
Examples:
char name;
int x, y,z;
float number;
double counter;
VARIABLE INITIALIZATION
Giving a value to a variable during the variable’s
declaration is called “variable initialization.”
Syntax:
Example:
clrscr();
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:
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:
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.
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.
%c character
%d integer
%f float
%lf double
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:
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.
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
& 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