Chapter # 1
INTRODUCTION
CreatingYour First Program
Compiling and Running Program Using Variables
Displaying Values in Variable
made by : Engr.Hira Zahid
Working with Data Types
Adding Comments
2.
Learning Objectives
â˘First courserelated to Computers
â No previous knowledge is assumed !
â˘By the end of the course, students will:
â Understand fundamental concepts of
computer programming/imperative structured
programming languages
â Design algorithms to solve (simple) problems
â Use the C programming language
3.
How it works
â˘How does a computer execute a program ? (example
programs: a computer game, a word processor, etc)
⢠the instructions that comprise the program are
copied from the permanent secondary memory into
the main memory
⢠After the instructions are loaded, the CPU starts
executing the program.
⢠For each instruction, the instruction is retrieved
from memory, decoded to figure out what it
represents, and the appropriate action carried out.
(the fetch- execute cycle)
⢠Then the next instruction is fetched, decoded and
executed.
4.
Lecture outcome
⢠Compilerand Interpreter
⢠IDE
⢠Header file and library files
⢠Differentiate escape sequence & format
specifier.
⢠Constants & variables.
⢠Types of data
5.
⢠High levellanguages
â Writing portable programs, using more abstract
instructions
â A high level instruction (statement) is translated into
many machine instructions
â Translation of high level language into machine
instructions: done by special computer programs â
compilers or interpreters
The C ProgrammingLanguage
⢠Developed by Dennis Ritchie at AT&T Bell Laboratories
in the early 1970s
8.
The first Cprogram
#include <stdio.h>
#include <conio.h>
void main(void)
{
printf ("Programming is fun.n");
getch();
}
uses standard library
input and output functions
(printf)
the program
begin of program
end of program
statements
main: a special name that indicates where the program must begin execution. It is
a special function.
first statement: calls a routine named printf, with argument the string of characters
âProgramming is fun nâ
Getch() -ď it holds the screen
9.
The format inC
⢠Statements are terminated with semicolonsâ;â
⢠Indentation is nice to be used for increased readability.
⢠Free format: white spaces and indentation is ignored by
compiler
⢠C is case sensitive â pay attention to lower and upper
case letters when typing !
â All C keywords and standard functions are lower case
â Typing INT, Int, etc instead of int is a compiler error
⢠Strings are placed in double quotes
⢠New line is represented by n (Escape sequence)
10.
Compiling and runningC programs
Editor
Compiler
Linker
Source code
file.c
Object code
file.obj
Executable code
file.exe
Libraries
IDE (Integrated
Development
Environment)
Syntax and Semantics
â˘Syntax errors: violation of programming
language rules (grammar)
â "Me speak English good."
â Use valid C symbols in wrong places
â Detected by the compiler
⢠Semantics errors: errors in meaning:
â "This sentence is excellent Italian."
â Programs are syntactically correct but donât produce
the expected output
â User observes output of running program
Displaying multiple linesof text
#include <stdio.h>
#include <conio.h>
void main (void)
{
printf("Testing...n..1n...2n....3n");
getch();
}
Output:
Testing...
..1
...2
....3
It is not necessary
to make a separate
call to printf for each
line of output !
Variables
⢠Programs canuse symbolic names for
storing computation data and results
⢠Variable: a symbolic name for a memory
location
â programmer doesnât has to worry about
specifying (or even knowing) the value of the
locationâs address
⢠In C, variables have to be declared before
they are used
⢠Rules for constructing variable
Using and DisplayingVariables
#include <stdio.h>
#include <conio.h>
void main (void)
{
int sum;
sum = 50 + 25;
printf ("The sum of 50 and 25 is %in", sum);
getch();
}
Variable sum declared of type int
Variable sum assigned expression 50+25
Value of variable sum is printed in place of %i
The printf routine call has now 2 arguments: first argument a string containing also a
format specifier (%i), that holds place for an integer value to be inserted here
21.
Displaying multiple values
#include<stdio.h>
#include <conio.h>
void main (void)
{
float value1, value2, sum;
value1 = 5.1;
value2 = 2;
sum = value1 + value2;
printf ("The sum of %f and %f is %.2fn",value1, value2, sum);
getch();
}
The format string must contain as many placeholders as expressions to be printed
22.
Using comments ina program
⢠Comment statements are used in a program to
document it and to enhance its readability.
⢠Useful for human readers of the program â compiler
ignores comments
⢠Ways to insert comments in C:
â When comments span several lines: start marked with /*, end
marked with */
â Comments at the end of a line: start marked with //
23.
Using comments ina program
/* This program adds two integer values
and displays the results */
#include <stdio.h>
int main (void)
{
// Declare variables
int value1, value2, sum;
// Assign values and calculate their sum
value1 = 50;
value2 = 25;
sum = value1 + value2;
// Display the result
printf ("The sum of %i and %i is %in",
value1, value2, sum);
return 0;
}