Sie sind auf Seite 1von 32

Computer Programming

R. Jothi
Computer Engineering
SOT PDPU
Overview
• Course Structure
• Why this course?
• Basics of Computer System
• Program/Software
• Introduction to C Language
• Turbo C Compiler
• C Program Example
• Text Books
Course Structure
• Regular Lab Exercises
• 2 Quiz
• Mid Semester Lab Exam
• End Semester Lab Exam

Each part is equally important


Why this course?
• Increase your employability in IT sector
• Implementing Control Systems better
• Understanding Computer Architecture become easy
– Design microcontrollers
• Embedded C Programmer
– LED program for lighting it, is easily written using
Embedded C

“Every electrical engineer have to do at least some


embedded programming at some point in their careers”
Computer System

INPUT PROCESS OUTPUT

KEYBOARD C.P.U. MONITOR


C.P.U.
KEYBOARD
C.U. MONITOR
Primary Memory

ROM A.L.U.
Auxiliary Memory
RAM
Cache
Memory Floppy Disks
Hard Disk
CD/Pen Drive
Text Books
• Programming in ANSI C,
Balagurusamy
• Let Us C, Yashavant
Kanetkar
• C: The Complete
Reference, Herbert Schildt
Program Vs. Software
• Program - A set of instructions telling a computer
what to do
– E.g. add two numbers
• Software – A set of programs to solve a particular
problem
– Programs + Associated manual/documentation
HISTORY OF C
• The C programming language was designed by
Dennis Ritchie at Bell Laboratories in the early 1970s

• ANSI standard in 1999


USE OF C
• OPERATING SYSTEMS
• EDITORS
• COMPILER CONSTRUCTION
• INTERPRETERS
• ASSEMBLERS
• DBMS/RDBMS
• APPLICATION SOFTWARE
• GENERAL-PURPOSE SOFTWARE
USE OF C
• DEVELOPING GAMES
• COMMUNICATION PROTOCOLS
• DEVICE DRIVER PROGRAMMING
• PROGRAMS FOR MOBILE DEVICES
• ROBOTICS
STEPS FOR WRITING A PROGRAM

PROGRAM

INSTRUCTION INSTRUCTION INSTRUCTION

CONSTANTS
VARIABLES
KEYWORDS

SPECIAL
ALPHABETS DIGITS SYMBOLS
C CHARACTER SET
• ALPHABETS : A … Z , a … z
• DIGITS : 0…9
• SPECIAL
SYMBOLS : ~ ` ! @
# % ^ & *
() _ - + =
| \ {} [] :
; “ ‘ < >
, . ? /
Structure of C Program
A Simple C Program to display “Hello
Electrical Engineers” Source
Code
/* This is my first program */

#include <stdio.h>

void main()
{
printf(“Hello Electrical Engineers”);
}
Hello.c
How does a computer execute a
program
Turbo C
• Integrated Development Environment and Compiler
for the C programming Language
• How to open it
– Run ->
– Type C:\TC\BIN\TC.EXE
First program in Turbo C
1. Open Turbo C
2. Type the program
3. Save the file as Hello.c
Use Key F2
4. Compile the program
Use ALT + C
5. Run the program
Use ALT + R
First program in Turbo C
1. Open Turbo C
2. Type the program
3. Save the file as Hello.c
Use Key F2
4. Compile the program
Use ALT + C and press Enter
5. Run the program
Use ALT + R press Enter

To see the output on the screen, press ALT + F5


Algorithm to Add Two Numbers

INPUT A, B
C=A+B
PRINT C
Few functions
scanf() : for reading input from user (keyboard)
scanf(“control string”, argument)
e.g. scanf(“%d”, &number1)
- read an integer value to the variable number1 and store it in the
memory location ( &number1);

“Always use an & in front of each variable”

printf() : for displaying output on the screen


printf(“ Hello”); // print message hello
printf(“ The sum is %d”, number1);
// print the value of integer variable number 1

clrscr() : clear screen

getch() : output screen waits for some time until a key is hit
Program to Add Two Numbers
#include <stdio.h>
void main()
{
int a , b, c ; // declaration of variables
printf( " Enter first number ");
scanf(" %d ", &a);
printf( " Enter second number ");
scanf(" %d ", &b);
c = a+ b;
printf( " Sum of given two numbers is %d ", c );

}
Use getch() to see the o/p without using
#include <stdio.h>
ALT + F5
#include <conio.h>
void main()
{ int a , b, c;
clrscr();
printf( " Enter first number \n");
scanf(" %d ", &a);
printf( " \nEnter second number ");
scanf(" %d ", &b);
c = a+ b;
printf( " \nSum of given two numbers is %d ", c );
getch();
}
Remember
1. C Language is case sensitive
a and A are different
2. Every statement in C program must be terminated
by a semicolon.
3. Every C program must have one main function.
4. Check for parenthesis balance
{ }
5. Save the program then and there using ALT+ F 
Save
6. Every variable must be declared.
7. clrscr() must be used after variable declaration.
8. Include <conio.h> while using getch() and clrscr();
Remember
Always start your program this way
#include <stdio.h>
void main()
{
// variable declaration
clrscr(); // to clear previous o/p from screen
// actual computation
getch()
}
Input - scanf()
scanf(“control string”, argument)

e.g. scanf(“%d”, &number1)

Control string Data type meaning


%d int integer
%f float float
%lf double double
%c char Character
%s string Character array
Reading Two Numbers in single scanf
#include <stdio.h> // header file
#include <conio.h> // header file
void main( ) // function
{ /* Program to add 2 Nos. */
int a, b, c;
clrscr();
printf( " Enter 2 Nos. ");
scanf ("%d %d",&a,&b); // input
c = a + b; // add 2 nos.
printf("%d + %d = %d", a,b,c);
getch();
}
2+3=5
Program to Add , subtract, mult, div Two
Integer Numbers
#include <stdio.h> // header file
#include <conio.h> // header file
void main( ) // function
{ /* Program to add 2 Nos. */
int a, b, c;
clrscr();
printf( " Enter 2 Nos. ");
scanf ("%d %d",&a,&b); // input
c = a + b;
printf("%d + %d = %d\n", a,b,c);
c = a - b;
printf("%d + %d = %d\n", a,b,c);
getch();
} 2+3=5
Program to Add , subtract, mult, div Two
Floating Numbers
#include <stdio.h> // header file
#include <conio.h> // header file
void main( ) // function
{ /* Program to add 2 Nos. */
float a, b, c;
clrscr();
printf( " Enter 2 Nos. ");
scanf ("%f %f",&a,&b); // input
c = a + b; // add 2 nos.
printf("%f + %f = %d\n", a,b,c);
c = a - b; // subtract 2 nos.
printf("%f + %f = %d\n", a,b,c);
getch();
} 2.2 + 3.5 = 5.7

Das könnte Ihnen auch gefallen