Você está na página 1de 21

Introduction to C Programming

Introduction to Programming
Objectives

After completing this lesson, you should be


able to do the following:
• Explain the process of program
development
• Identify and describe the basic program
structure
• Encode, compile and run simple
computer programs

Introduction to Programming
Introduction to Programming
• Programs are developed
to instruct computers to do
specific tasks, or solve
specific problems
• A finite set of instructions
that if followed
accomplishes a particular
task is called an algorithm
• Thus, programming is the
activity of communicating
algorithms to computers

Introduction to Programming
Program Development Process

Problem or Task
make list check it
Requirements List
design algorithm verify
Algorithm
code program verify

Program

test and debug

Introduction to Programming
Typical Programming Environment
Program is encoded
in a text editor and Editor Source code
stored on a disk

Object code is created Object file


by a compiler and Compiler Disk
stored on a disk

Executable code is
created by the linker
by linking the object Linker Executable file
codes and stored on
disk

Introduction to Programming
Typical Programming Environment

Loader places the


executable program in Loader
memory Primary
memory
CPU
CPU fetches each
instruction from memory,
executes it, and possibly
Disk stores new data as the
program executes

Introduction to Programming
Typical Programming Environment
In A Nutshell

Edit Compile Execute

source object executable


code code code

Introduction to Programming
The C Programming Language
• We will be learning
programming using the C
programming language

• C was developed by Dennis


Ritchie between 1969 and
1973 at the AT&T Bell Labs
• C was designed for systems
programming
• C is considered as a general-
purpose programming
language
Introduction to Programming
History of C
Basic Common Programming Language (BCPL)
(Martin Richards)

B
(Ken Thompson)

C
(Dennis Ritchie)

Introduction to Programming
ANSI C

• There are different C compilers developed


over the years such as Turbo C, Object-C
and Windows C
• The American National Standards Institute
(ANSI) adopted the ANSI standard for C
(ANSI C) in 1990
• C is widely used today because of its rich
standard library of functions

Introduction to Programming
Sample C Program 1: Hello World
Source code: hello.c
/* This program will display “Hello World” */
#include <stdio.h>

int main()
{
printf(“Hello World!”);
return 0;
}
Output

Hello World!

Introduction to Programming
C Program Structure

preprocessor directives

main function heading


{
statements
}

Introduction to Programming
Preprocessor Directives

• Preprocessor directives are commands that


give instructions to the C preprocessor
• Its job is to modify the text of a C program
before it is compiled
• It begins with a number symbol (#) as its first
non-blank character
• Example: #include
#define

Introduction to Programming
Preprocessor Directive: #include

source
source
code
code

Preprocessor File
to be
File included
to be
included
Resulting file

Introduction to Programming
Header Files

• A header file is a collection FILE


of useful functions and
NULL
symbols that may be
accessed by a program EOF

• Its file name ends with a .h


output
extension
• Example:
input
#include <stdio.h>
#include <string.h> STDIO.H

Introduction to Programming
Main Function

• Every C program has a main function


• Marks the beginning of the main function
where program execution begins
• Basic form:
main( )
{
main function body
}

Introduction to Programming
Program Statements

• Program statements are


elementary components of a
program

Introduction to Programming
Comments

• Comments are used to add remarks to a program


• Any number of comments can be placed anywhere in
the program without affecting its content and speed
• The compiler simply ignores them and treats them as
white spaces
• In C, comments are delimited by the following pairs of
characters /* and */
/* This is a comment */

/* Name: Juan de la Cruz *


* Student Number: 96-00000 *
* Course: BS Computer Science */
Introduction to Programming
Comments

...
printf( “ /* This is NOT a comment */ ” );
...

• Strings inside quotes are displayed as it is

Introduction to Programming
Sample C Program 2:
Average of Two Numbers

/* Filename: average.c */

#include <stdio.h>

int main(void)
{
float num1, num2, average;

printf(“Enter the first number: “);


scanf(“%f”,&num1);
printf(“Enter the second number: “);
scanf(“%f”,&num2);
average = (num1 + num2)/ 2.0;
printf(“The average of %f and %f is %f”,num1,num2,average);
return 0;
}

Introduction to Programming
Laboratory Exercise Overview

• Become familiar with the C programming


environment
• Learn how to encode, compile and run simple
C programs

Introduction to Programming

Você também pode gostar