You are on page 1of 12

Introduction to computer programming

CHAPTER -1

1. COMPUTER Is a well trained dogs which responds to specific set of predetermined set of
commands

2. 1. Instruction list - IL is similar to the alphabet lika a humans to the computers.


3. compiler transforme the human language to computer language and vice versa
4. one of the most important steps in a pragramming is the source code, source code is the place
where the program is written which could be run for future use.(for example the file to run is
meera.c which is source code)

5. to run a program we need a compiler to translate the written program to the machine level
programming and check for any errors.

5. In unix execuatable file will have output of form .out but in windows either .c or .exe

Installing c

1, one of the best environment of Installing C is with having IDE (INTEGRATED


DEVELOPMENT ENVIRONMENT) which has compiler, debugger and GUI

First program

1. # include<stdio.h>
# preprocessor
include directive
stdio.h header file

in this program we are dealing with include directive, when a preprocessor indicate the directive it
replace the directory with the content of the file with name stdio.h. the content does not change in
source file once the file is compiled it restores.

In another words, Include directive. Many programming languages and other computer files have a
directive,often called include (as well as copy and import ), that causes the contents of a second
file to be inserted into the original file. These included files are called copybooks or header files.

Let say we have a program puts(my name is meera) and we are trying to run this line, we does
not know if put is a valid command or not. Preliminary information about some commands for the
compiler to run is stored in .h files which are known as header files.

PRELIMINARY INFORMATION FOR THE COMPILER TO RUN IS HEADER FILES

2. Blocks:

Function Function(black Function resutls


parameters box)
we put something in the box and take something from the box, In c programming there may be
many function blocks but we must have a main block known as main without this program would
be incorrect.

With respect to the diagram of the box let us ask some questions

what is the result of the function?


what is the name of the function?
how many parameters does the function have and what are their names?

int main(void)
Answers
1. the result of the function is the integer
2. our function block is the main
3. function does not require any function parameters which is void.

After function block is initialised it does not intend to anything, if we have {} of the body which
indicates that the function is intended to do something we call it as function body, function main
body is what it has to do in previous example puts(my name is meera) ; puts is a function
invocation.

Note: All function must end with the semicolon and function invocation should be enclosed in
( and )

imp : strings in C programming should always enclosed in

when the function body is performing its task, the main function is suspended are made to sleep,
after the execution of the function body it returns to the main function.

1. return 0; denote the end of function execution and it should be within the function body. And it
indicates the program worked fine.

2. return 1 indicate that something is wrong.

INTEGERS VALUES AND INTEGERS VARIABLES:

1. Integers no fractional part - int


2. Float has fractional part - float

if the integers is preceded is by 0 we consider it a octal value


if the integers is preceded by 0x we consider it a hexa value

decimal to octal

0123 = 0 * 10^3 + 1*10^2 + 2* 10 ^1 + 3 *10^0 = 0 + 100+ 20+3 = 123

octal to decimal

0123 = 0 + 1* 8^2 + 2*8^1 + 3*1 = 0 + 64+ 16 + 3 = 83


same for hexa

Print f

1. to print integer number use = printf(%d\n , my name is int)


2. to print float number use = printf(%f\n , my name is int)

Storing the result

1. To store the operation we need a container known as the variable as from the name we cld
understand that it varies over the program. Variables can be a upper or lower case or a number but it
should begin with a alphabets and we can use underscore also.

2. How to define a variable???? it does by simple declaration for eg int a , b so the value of a and b
should be an integer.

A =1 (assignment )
The above statement reads: assign a value of 1 to a variable named a or a bit shorter assign
1 to a.

Now difficult understanding


1. x = x +1 now the value is incremented by 1 and it is assigned to the variable x

3. In c programming we do have specific keywords which are predefine words we could not
use a variable but u can change the sensitivity of the character.

4. comments in c programming should start with /* and end with */


CHAPTER 2

Floating point:

1) 9.4 is a floating point as it has a fractional ,


2) what is tht double, double is a type of flot let see an example if we declare float 8.9 *
10^15 we could see that the floating comands takes a long time since Float is a single
precision, 32-bit floating point data type; double is a double precision, 64-bit floating point data
type.

The double accommodates 15 to 16 digits, compared with float's seven digits

1. int variables had a large processing speed with less cache

2. to write powers we use a commands 3E8 3 * 10 to the power of 8

Operators

1. division in c / dividend/divisor
2. remainder operator int i, j, k ; k = i %j for eg let i = 15 j = 5 ; remainder is 0
3. * / % have the same order of precedence so to calculate the expression we use the concept of
binding we performs operation from left to right this is possible only if the operators has
same hierarchy.
4.
For example
int sheep;
sheep = 0;
sheep = sheep +1;
in this example, we can find initially the value of sheep 0, then it becomes 1, 2,
3.
Which in short we can write in c programming as

Sheep ++
Similarly for decrement we use

Sheep the ++ and -- can be placed prefix the variablr.

5. Variable ++ - post increment


6. ++ variable - pre increment
7. Variable - - - post decrement
8. - - variable pre decrement

Post increment Pre increment


Int i , j ; int i, j;
i = 1; i = 1;
j = i++; j = ++ i;
in this case the value of i++ implies i = i +1 In this case the i has the values of 2;
hence i = 2, j is assigned to i the increased value is assigned to j
Shortcut operator :

1. i = i +1 i++;
2. i = i+2*j i+ = 2* j;
3. var = var/2 var / = 2;

Note : variable = variable op expression;


then we write as variable op = expression;

CHARACTER:
Character are single letter in simple meanings , the char is the command used for assigning the
character; ASCII uses the specific number for different letter.
Characters should be enclosed by the single quotes
Important: char \ , is used as the escape command , \ n new lines escapes in to line \ r return to the
beginning of the line, \a alarm u will hear a beep sound \ 0 null
Conditional Instructions
1. we do come across the certain situation we have a condition is true execute the statement
such conditional clause in C programming is define by
1. if condtion
2. If else condition

If statement
if (the sheep >=120)
{
Makeabread
Eat
Shower
}
When multiple execution is to be made it enclosed with the curly braces
Input and Output
1. Now to understand the connectivity between the computer and the outside world.
2. eg : puts function could not be used to print the integer as it print only the string character.
because using this command in the input coding, would display the corresponding material at the
output screen.
3.Printf command for printing all type of characters.
Its is a decimal point of type integer values = printf( %d/n, sheep count) %x is hexadecimal
number %i integer, % o = octal number, %c character; % f float; %% displays percentage sign.
% d = are called specifiers
Sheep count = arguments

#include <stdio.h> int main(void) {


int VarInt;
char VarChar;
float VarFloat;
VarInt = 2012;
VarChar = 'r';

VarFloat = 3.1415 ;
print("The year is %d. The radius is denoted as %c while PI is
equal to %f", VarInt, VarChar, VarFloat);
return 0;

4. scanf command is used to get the information from the user scanf (%d, &sheep count) and &
represent the laoction of the place of the variable name

int main(void)
{
int value,square; Print f displays the information to
print("Give me a number and I will square it!\n"); get the values from the user.
scanf("%d", &value);
Using scanf statement we are
square = value * value;
getting the value of a number from
print("You've given %d\n",value);
the user which should be a
print("The squared value is %d\n",square); decimal with the variable name
return 0; value
}

5. Sqrt is the function used to obtain the square root of the value, to obtain the mathematical
directory use math.h
CHAPTER 3

CONDITIONAL STATEMENT

If (if the weather is good) {


Go for a walk()
Have fun()
}
Else {
Stay at home()
Watch movie()
}

For the if else statement, for the multiple instruction we use the blocks, blocks are noting but that
follows a curly brace for the condition.

If the weather is fine, well go for a walk. If we find a nice restaurant, well have lunch there.
Otherwise, well eat a sandwich. If the weather is poor, we'll go to the theater. If there are no
tickets, well go shopping in the nearest mall.

If (weather is fine)
go for the walk();
If (find nice restaurant)
Have lunch();
Nested if
Else
eat sandwich();
else
If (ticket available)
go to the theatre();

else
go to the shopping();

I. Integers

To specify the memory requirement for the data types we use the command known as modifiers

1. long = larger range of int is required


2. Short opp of long
3. unsigned is used to specify that the variable is only for the non negative numbers; we can use
unsigned with char also.

For the short int counter;


We can rewrite as short counter;
4. other example unsigned long long ants; long and short can be used along with only intergers
commands.

1. Long and short could not be used with char like long char etc;

I. Float

1. Long
2. Short cannot be used with float
3. Double
4. Unsigned cannot be used along with floats

Since because adding the long and short float could actually results in neglecting some values, we
cannot perform adding or subtracting the number of floats.

I. Metamorphosis or conversion of data


Conversions: changing the type of data to when the change in values is made is called conversion
and its results in inaccuracy.
1. Implicit conversion: where the program automatically recognises and changes it type based on
the predefined information.
2. Explicit conversion = where the conversion of data types is made by user commands.
Implicit conversion

data of type char or short int will be converted to type int (this is called an integer
promotion);

if there is any value of type float in the expression, the other data will be converted to float;

if theres any value of type double in the expression, the other data will be converted to
double;

if theres any value of type long int in the expression, the other data will be converted to
long int.
1. Explicit conversion is made by using the type command, type is double in the following case
float x;
double y;
y = (double) x; (type) value;

2. Program to find the largest of two number


#include<stdio.h>
Int main(void)
{
Int number1, number 2, max;
Scanf(%d, &number1);
Scanf(%d, &number2);
max = number1;
if (number2 > max)
max = number2;
print( the maximum of the number is %d,max) ;
return 0;
}
2. Program to find largest of three number
int number1, number2, number3, max;
Scanf(%d, &number1);
Scanf(%d, &number2);
Scanf(%d, &number3)
Max = number1
if(number2> max)
max = number2

if(number3>max)
max = number3
print(the largest number is %d, max)
return 0;

how to find the largest of the number which is higher


int number;
int max = -1000000;
scanf(%d, number)
while(number!= -1){
if (number > max)
max = number;
scanf(%d,&number)

return 0;
if obtain the statement once but while statement continues until the statement becomes true
LOOPS
Ability to perform the certain number of loops repeatedly in a program is
While
Do while
For loop
WHILE
1. If we want to do something for the certain number of times is done using while loop
While statement check the condition before executing the if or for loop, if the condition
become false it will not enter the loop
2. Do Loop: The condition is checked at the end of the loop, loop body is executed atleast once if
the condition is not met,

do
statement;
while(condition);

do {
statement_1;
statement_2;
:
:
statement_n;
} while(condition);

3. For loop.

Initialisation;
While (checking condition) {

Modification
}

Can be modified as

For( i=0; i < 100; i++); if any of three condition is missing it assumes to be 1 and the condition
statement is assume to be true in which it has infinite loops never ending loops to be not true it
assume to be true. All the three condition should be separated by semi colons.

Multiple initialisation and increment can be done in for loop for eg for ( i=0 j= 1; i <=2; i++) it
has 2 initialisation such as i=0 and j=1 however only one initialisation is done.

ODD LOOPS
1. When are initiating writing a program, beforehand we are not unaware to know the how
much input should be given for the particular condition, we do use odd loop for example
# include<stdio.h>
Int main()
{

Int num;
Printf(enter the number to be squared);
Scanf(%d, &num);
printf(the square %d of the number is %d\n,num , num*num);
printf(want to square another number y/n);
fflush(stdin);
scanf(%c, &another);

} while (another = = y);

Return 0;

THE SWITCH CASE CONTROL STRUCTURES

The control statement that allow us to decide from the number of choices is called switch.

You can use a char as a case in switch for eg

Int main()
{
Char c = x;
Switch( c)

{
Case v
Printf( I m in case v\n)

Case x
Printf( I m in case x\n)

}
1. In switch we can have only int and char constant, Even float is not allowed.
2. We can check the value of the expression in a switch and break is necessary after each case
condition.
3. Cases can never have a variable expression like case 1+2 its illegal
4.

Computer logic

2. Connection of condition are called conjuction and logical conjuction in c is && (and)
3. Either one of the condition is true is disjunction or logical disjunction in c is | | or
4. If the condition is true if use this ! it turns out to be false and vice versa.
5. From the De Morgan Law,
The negation of the conjuction is the disjunction of the negation (p && q) = = ! p | | !q
the negation of the disjunction is the conjuction of the negation ! (p || q) == !p && !q

You might also like