SlideShare a Scribd company logo
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
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
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.
Lecture outcome
• Compiler and Interpreter
• IDE
• Header file and library files
• Differentiate escape sequence & format
specifier.
• Constants & variables.
• Types of data
• 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
Compilers/Interpreters
Compiler
Source
Code
Machine
Code
Executable
Program
Input
data
Output
data
Interpreter
Source
Code
Input
data
Output
data
Compiler: analyzes program and
translates it into machine language
Executable program: can be run
independently from compiler as
many times => fast execution
Interpreter: analyzes and executes
program statements at the same
time
Execution is slower
Easier to debug program
The C Programming Language
• Developed by Dennis Ritchie at AT&T Bell Laboratories
in the early 1970s
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
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)
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)
Debugging program errors
Editor
Compiler
Linker
Source code
file.c
Object code
file.obj
Executable code
file.exe
Libraries
Syntactic
Errors
Semantic
Errors
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
Second program
#include <stdio.h>
#include <conio.h>
void main (void)
{
printf ("Programming is fun.n");
printf ("And programming in C is even more fun.n");
getch();
}
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 !
Constant & Variable
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
lec 1 for ITC Introduction to computing and AI
lec 1 for ITC Introduction to computing and AI
keywords
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
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
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 //
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;
}

More Related Content

PDF
Programming Fundamentals and basic knowledge
PDF
EC2311-Data Structures and C Programming
PPTX
C lang7age programming powerpoint presentation
PPTX
C LANGUAGE UNIT-1 PREPARED BY M V BRAHMANANDA REDDY
PPTX
PPTX
PPT
Basics of c
PPTX
4 Introduction to C.pptxSSSSSSSSSSSSSSSS
Programming Fundamentals and basic knowledge
EC2311-Data Structures and C Programming
C lang7age programming powerpoint presentation
C LANGUAGE UNIT-1 PREPARED BY M V BRAHMANANDA REDDY
Basics of c
4 Introduction to C.pptxSSSSSSSSSSSSSSSS

Similar to lec 1 for ITC Introduction to computing and AI (20)

PPTX
Unit ii
PPTX
c_pro_introduction.pptx
PDF
Learning the C Language
PPT
424769021-1-First-C-Program-1-ppt (1).ppt
PPTX
Lecture 1
PPTX
UNIT 5 C PROGRAMMING, PROGRAM STRUCTURE
PPTX
Fundamentals of Data Structures Unit 1.pptx
PPTX
Fundamental programming Nota Topic 2.pptx
PPTX
cmp104 lec 8
PPTX
Unit-1 (introduction to c language).pptx
PPTX
Introduction of c programming unit-ii ppt
PPTX
structure of a c program - slideshare.pptx
PPT
Presentation 2.ppt
PPTX
C++ Introduction to basic C++ IN THIS YOU WOULD KHOW ABOUT BASIC C++
PDF
Prog1-L1.pdf
PPTX
STRUCTURED PROGRAMMING (USING C PROGRAMMING).pptx
PPTX
Basics of c Nisarg Patel
PPTX
computer programming omputer programming
PPTX
C_Programming_Notes_ICE
Unit ii
c_pro_introduction.pptx
Learning the C Language
424769021-1-First-C-Program-1-ppt (1).ppt
Lecture 1
UNIT 5 C PROGRAMMING, PROGRAM STRUCTURE
Fundamentals of Data Structures Unit 1.pptx
Fundamental programming Nota Topic 2.pptx
cmp104 lec 8
Unit-1 (introduction to c language).pptx
Introduction of c programming unit-ii ppt
structure of a c program - slideshare.pptx
Presentation 2.ppt
C++ Introduction to basic C++ IN THIS YOU WOULD KHOW ABOUT BASIC C++
Prog1-L1.pdf
STRUCTURED PROGRAMMING (USING C PROGRAMMING).pptx
Basics of c Nisarg Patel
computer programming omputer programming
C_Programming_Notes_ICE
Ad

Recently uploaded (20)

PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PDF
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
PDF
PPT on Performance Review to get promotions
PPTX
Geodesy 1.pptx...............................................
PPTX
web development for engineering and engineering
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
Sustainable Sites - Green Building Construction
PDF
III.4.1.2_The_Space_Environment.p pdffdf
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
Fundamentals of safety and accident prevention -final (1).pptx
PDF
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
PPTX
Safety Seminar civil to be ensured for safe working.
PDF
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
PPT on Performance Review to get promotions
Geodesy 1.pptx...............................................
web development for engineering and engineering
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Sustainable Sites - Green Building Construction
III.4.1.2_The_Space_Environment.p pdffdf
Foundation to blockchain - A guide to Blockchain Tech
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Embodied AI: Ushering in the Next Era of Intelligent Systems
Fundamentals of safety and accident prevention -final (1).pptx
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
Safety Seminar civil to be ensured for safe working.
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
CYBER-CRIMES AND SECURITY A guide to understanding
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Operating System & Kernel Study Guide-1 - converted.pdf
Ad

lec 1 for ITC Introduction to computing and AI

  • 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
  • 6. Compilers/Interpreters Compiler Source Code Machine Code Executable Program Input data Output data Interpreter Source Code Input data Output data Compiler: analyzes program and translates it into machine language Executable program: can be run independently from compiler as many times => fast execution Interpreter: analyzes and executes program statements at the same time Execution is slower Easier to debug program
  • 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)
  • 11. Debugging program errors Editor Compiler Linker Source code file.c Object code file.obj Executable code file.exe Libraries Syntactic Errors Semantic Errors
  • 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
  • 13. Second program #include <stdio.h> #include <conio.h> void main (void) { printf ("Programming is fun.n"); printf ("And programming in C is even more fun.n"); getch(); }
  • 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; }