1. Chapter # 1
INTRODUCTION
Creating Your 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 course related 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
• Compiler and Interpreter
• IDE
• Header file and library files
• Differentiate escape sequence & format
specifier.
• Constants & variables.
• Types of data
5. • High level languages
– 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
7. The C Programming Language
• Developed by Dennis Ritchie at AT&T Bell Laboratories
in the early 1970s
8. The first C program
#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 in C
• 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 running C programs
Editor
Compiler
Linker
Source code
file.c
Object code
file.obj
Executable code
file.exe
Libraries
IDE (Integrated
Development
Environment)
12. 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
14. Displaying multiple lines of 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 !
16. Variables
• Programs can use 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
20. Using and Displaying Variables
#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 in a 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 in a 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;
}