Sie sind auf Seite 1von 27

GETTING STARTED WITH C

COMPARE WITH NATURAL


LANGUAGE LEARNING

CHARACTER SET

STRUCTURE OF COMPUTER MAIN MEMOR

Structure of computer memory:

Computer memory consists of bytes


Each bytes is 8 bits
A bit is the most elementary electrical
memory element, can remember 0 or
1 (it's an electrical switch: off or on)
Each byte is identified by an address
A byte can contain 28 = 256 different
values
SINCE first bit is reserved for sign
(+/-), it can contain 27 = 128 different
values

ASCII TABLE

WRITING YOUR FIRST PROGRAM


#include <stdio.h>
int main ( ) {
printf("Hello World \n");
return 0;
}

PROBLEM SET
Write a C program which will display your name
Write a C program which will show all of your
address in multiple lines.
Write a C program which would give a output
like below.

WHY WE NEED VARIABLES &


CONSTANTS?
Programs operate on data. A programming
language must give you a way of storing the data
you are processing, otherwise it is useless.
One way of storing data could be memorizing the
address of the memory location where information
is stores.

Is

it convenient?

Variables and constants in programming languages


are simply containers which hold values.
Variables and constants are always given a
meaningful name
They are accessed through their name.

DIFFERENCES BETWEEN
VARIABLES & CONSTANTS

The difference between a variable and a


constant is this:

you give a value to a constant, the value is


meant to be kept unchanged throughout the
script.
Value in a variable can be changed whenever
necessary.
Once

RULES FOR CONSTRUCTING


VARIABLE NAME
A variable name is any combination of 1 to 31
alphabets, digits or underscores.
The first character in the variable name must be
an alphabet or underscore.
No commas or blanks are allowed within a
variable name.
No special symbol other than an underscore (as
in gross_sal) can be used in a variable name.

RULES FOR CONSTRUCTING


VARIABLE NAME

Keywords must not be used as a variable name

DATA TYPES

Variable and constants can be mainly


three types
Character

(1 byte or 8 bits)
Integer (2 byte or 16 bits)
Real
Float (4 byte or 32 bits)
Double (8 byte or 64 bits)

Example:
char ch;
int num1;
float f1_num;
double _dnum;

HOW VARIABLES ARE MAPPED IN THE MEMORY

ASSIGNING VALUE IN A VARIABLE


int num = 10;
char ch = M;

Decimal to Binary
10 = 1010
M = 77 = 1001101

MORE ABOUT DATA TYPES

Name

Sign

Character

%c

Integer

%d

Floating Point

%f

Double

%lf

EXAMPLE

Write a C program to print the values stored in


three different variables, namely, A(int type),
B(double type) and C(char type).

RECEIVING INPUT FROM


KEYBOARD

Using scanf( ) function we can take input from the


keyboard.
Syntax:
scanf(variable type, &variable address);
For example: scanf(%d, &value); where value is
an integer type variable.

WHERE WE ARE

INSTRUCTIONS

ARITHMETIC INSTRUCTIONS

Integer mode arithmetic statement


int i, king, issac, noteit ;
i=i+1;
king = issac * 234 + noteit - 7689 ;
Real mode arithmetic statement
float qbee, antink, si, prin, anoy, roi ;
qbee = antink + 23.123 / 4.5 * 0.3442 ;
si = prin * anoy * roi / 100.0 ;
Mixed mode arithmetic statement
float si, prin, anoy, roi, avg ;
int a, b, c, num ;
si = prin * anoy * roi / 100.0 ;
avg = ( a + b + c + num ) / 4 ;

ARITHMETIC INSTRUCTIONS:
POINTS TO REMEMBER
C allows only one variable on left-hand side of =.
That is, z = k * l is legal, whereas k * l = z is
illegal.
In addition to the division operator C also
provides a modular division operator (%).
Arithmetic operations can be performed on ints,
floats and chars.
char x, y; int z ; x = 'a' ; y = 'b' ; z = x + y ;
No operator is assumed to be present.
a = c.d.b(xy) usual arithmetic statement
b = c * d * b * ( x * y ) C statement

ARITHMETIC INSTRUCTIONS:
INTEGER AND FLOAT CONVERSION
An arithmetic operation between an integer and
integer always yields an integer result.
An operation between a real and real always yields a
real result.
An operation between an integer and real always
yields a real result.

TYPE CONVERSION IN ASSIGNMENT


int k;
float a;

HIERARCHY OF OPERATORS

i=2*3/4+4/4+8-2+5/8
i = 6 / 4 + 4 / 4 + 8 - 2 + 5 / 8
i = 1 + 4 / 4 + 8 - 2 + 5 / 8
i = 1 + 1+ 8 - 2 + 5 / 8
i = 1 + 1 + 8 - 2 + 0
i = 2 + 8 - 2 + 0
i = 10 - 2 + 0
i = 8 + 0
i = 8

EXAMPLE SET
Example: Find the area of a Circle of radius r.

Step1:

START
Step2: Read R
Step3: Area PI*r*r //
calculation of area
Step4: Print Area
Step5: END

EXAMPLE SET

Example: Write an algorithm to read two


numbers and find their sum.

Algorithm:

Step1: Start

Step2: Read\input the first num1.

Step3: Read\input the second num2.

Step4: Sum num1+num2 //


calculation of sum

Step5: Print Sum

Step6: End

PROBLEM SET
Find out the summation of three numbers.
Write a C program which is going to convert Celsius
into Fahrenheit. (Fahrenheit = (9.0 / 5.0) * Celsius +
32.0)
Rameshs basic salary is input through the keyboard.
His dearness allowance is 40% of basic salary, and
house rent allowance is 20% of basic salary. Write a
program to calculate his gross salary.
If the marks obtained by a student in five different
subjects are input through the keyboard, find out the
aggregate (sum) marks and percentage marks obtained
by the student. Assume that the maximum marks that
can be obtained by a student in each subject is 100.

The End

Das könnte Ihnen auch gefallen