Introduction to C Programming
3
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 check it
make list
Requirements List design algorithm
verify
Algorithm verify
code program
Program test and debug
Introduction to Programming
Typical Programming Environment Program is encoded in a text editor and stored on a disk
Editor
Object code is created by a compiler and stored on a disk
Compiler
Executable code is created by the linker by linking the object codes and stored on disk
Linker
Source code
Object file
Disk
Executable file
Introduction to Programming
Typical Programming Environment Loader places the executable program in memory
Loader
CPU
Disk
CPU fetches each instruction from memory, executes it, and possibly stores new data as the program executes
Introduction to Programming
Primary memory
Typical Programming Environment In A Nutshell Edit
Compile
Execute
source code
object code
executable 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 generalpurpose 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 code
source code
Preprocessor File to be included
File to be included Resulting file
Introduction to Programming
Header Files • A header file is a collection of useful functions and symbols that may be accessed by a program • Its file name ends with a .h extension • Example: #include <stdio.h> #include <string.h>
Introduction to Programming
FILE NULL
EOF output
input
STDIO.H
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