Download as pdf or txt
Download as pdf or txt
You are on page 1of 31

PROGRAMMING BASICS AND

OVERVIEW OF C
ANATOMY OF A C PROGRAM
program header comment

preprocessor directives (if any)

int main ( void )


{
statement(s)
}
PROGRAM HEADER COMMENT
▪ A comment is descriptive text used to help a reader of
the program understand its content.
▪ All comments must begin with the characters /* and
end with the characters */
▪ It provides supplementary information but is ignored
by the preprocessor and compiler

/* Get the distance in miles */


PREPROCESSOR DIRECTIVES
▪ Lines that begin with a # are called
preprocessor directives (commands) which
provide an instruction to the preprocessor.
▪ The include directive tells the preprocessor to
include a special file together with your source
program.
#include <stdio.h>
▪ The define directive is used to create names
for values
#define KMS_PER_MILE 1.609
LIBRARIES
▪ When we write our programs, there are libraries
of functions to help us so that we do not have to
write the same code over and over.

▪ Some of the functions are very complex and long.


Not having to write them ourselves make it easier
and faster to write programs.
#include <stdio.h>

library
CONSTANT
▪ constant macro
▪ a name that is replaced by a particular constant value
before the program is sent to the compiler

#define KMS_PER_MILE 1.609


constant macro
constant

kms = KMS_PER_MILE * miles;


CONSTANTS
▪ Numeric constant examples:
▪ 123
▪ 98.6
▪ 1000000

▪ Character constant examples:


▪ ‘A’
▪ ‘a’
▪ ‘$’
▪ ‘4’
FUNCTION MAIN
▪ Every C program must have a function
called main. This is where program
execution begins.

Int main (void)


RESERVED WORD

▪ a word that has a special meaning in C


▪ identifiers from standard library and names
for memory cells
▪ appear in lowercase
▪ cannot be used for other purposes
KEYWORDS / RESERVED WORDS
STATEMENTS
▪ Statements are actually the commands that we
are going to give to our computer in order to
perform the tasks that we want.

▪ Note that each C Statement ends with a semi-


colon!

kilograms = pounds * 0.453592;


VARIABLE (IDENTIFIER) DECLARATIONS
▪ variable
▪ a name associated with a memory cell whose value can
change
▪ variable declarations
▪ statements that communicate to the compiler the names of
variables in the program and the kind of information stored
in each variable
VARIABLE (IDENTIFIER) DECLARATIONS
▪ C requires you to declare every variable used in a
program.
▪ A variable declaration begins with an identifier that
tells the C compiler the type of data store in a
particular variable.

int hours;

double miles;
VARIABLE (IDENTIFIER) DECLARATIONS
▪ There are two type of identifiers: standard identifiers
and user-defined identifiers.
▪ Standard identifiers, like scanf and printf, are
defined in standard libraries.
▪ Avoid using standard identifiers as variable names.
▪ User-defined identifiers are the names we give to
our constants, variables, functions, etc. in our
programs. We should obey some rules while we are
choosing our identifiers.
RULES FOR NAMING VARIABLES
▪ Can be of anything length, but only the first 31
characters are taken into consideration
▪ C is case sensitive. It matters whether an identifier,
such as a variable name, is uppercase or lowercase.
▪ SUM, Sum, and sum are considered as different variables.

▪ Cannot begin with a digit.


▪ Must begin with a letter and the rest can be letters,
digits, and underscores (_).
▪ A C reserved word can not be used as a variable.
WHICH OF THE FOLLOWING VARIABLES ARE
VALID?
▪ 1stclass
▪ class1
▪x
▪ what?
▪ _no_of_stu
▪ double
▪ DOUBLE
▪ my name
▪ my_name
▪ Füsun
▪ Fusun
▪ ageOfMyGrandFather
WHICH OF THE FOLLOWING VARIABLES ARE
VALID?
▪ 1stclass  begins with a digit
▪ class1 ✓
▪x ✓
▪ what?  ? is not allowed
▪ _no_of_stu ✓
▪ double  reserved word
▪ DOUBLE ✓
▪ my name  blank is not allowed
▪ my_name ✓
▪ Füsun  Turkish letter (ü) is not allowed
▪ Fusun ✓
▪ ageOfMyGrandFather ✓
CHOOSING MEANINGFUL NAMES FOR VARIABLES
▪ Programmers generally agree on the following
conventions for naming variables.
▪ Begin variable names with lowercase letters
▪ Separate “words” within variables with underscores or
mixed upper and lower case.
▪ Examples: surfaceArea, surface_Area, surface_area

▪ Single letter names are readable where their usage is


obvious, like array indices i, j, k. The scope of single
letter names should be kept strictly limited.
CHOOSING MEANINGFUL NAMES FOR VARIABLES
▪ Pay attention to use meaningful names, that reflect the
value we will store in that variable
▪ For example, the name salary will be a good name for a memory
location that will be used to store a person’s salary, whereas the
name s is a bad choice.
▪ Make the meaning clear (E.g. status, speed, count)
▪ Add more meaning whenever necessary (E.g. door_status,
wind_speed, node_count)
▪ Using very long names reduces the readibilty of a program (E.g.
standard_word_enhancement_code)
▪ 15 characters is a practical (meaningful) limit for the name length.
▪ Use abbreviations to reduce the length of a name to make it
managable.
▪ For example, num_of_stud for "number of students"
DATA TYPES
C has three basic predefined data types:

▪ Integers (whole numbers, such as –3, 0, 15)


▪ int

▪ Floating point (real numbers, such as 3.0, 0.75, -


2.47)
▪ double

▪ Characters must be enclosed in single quotes (‘)


in a program,. E.g., ‘A’, ‘z’, ‘2’, ‘5’, ‘*’, ‘:’, ‘ ‘ (blank
character).
▪ char
DATA TYPES
▪ In most programs, we need several variables with different
types. You can declare them in any order. Ex:
double salary1, salary2; int age;
int age; double salary1;
char gender; char gender;
double salary2;
DECLARING VARIABLES
▪ When we declare a variable
▪ Space is set aside in memory to hold a value of the specified
data type
▪ That space is associated with the variable name
▪ That space is associated with a unique address

▪ Visualization of the declaration


int meatballs ;

meatballs

garbage

FE07 int
USING VARIABLES: INITIALIZATION
▪ Variables may be be given initial values, or initialized, when
declared. Examples:

length
int length = 7 ; 7

diameter
5.9
double diameter = 5.9 ;
initial
‘A’
char initial = ‘A’ ;
USING VARIABLES: INITIALIZATION (CON’T)

▪ You can initialize a variable when declaring


or use a separate assignment statement.
▪ Example:
int height ; /* rectangle height */
int width = 6 ; /* rectangle width */
int area ; /* rectangle area */
height = 8;
area = width * height;
CONSTANTS
▪ A constant is a value that does not change during the
execution of a program.
▪ Use all uppercase for constants

#define PI 3.14159
#define MAX 1000
#define BLANK ' '

▪ The appearance of the constant's value indicates its


data type. For instance, in the above definitions, PI is a
double constant, MAX is integer and BLANK is
character.
PUNCTUATORS
WRITING C PROGRAMS
▪ A programmer uses a text editor to create or
modify files containing C code.
▪ Code is also known as source code.
▪ A file containing source code is called a
source file.
▪ After a C source file has been created, the
programmer must invoke the C compiler
before the program can be executed (run).
3 STAGES OF COMPILATION
Stage 1: Preprocessing

▪ Performed by a program called the preprocessor


▪ Modifies the source code (in RAM) according to
preprocessor directives (preprocessor commands)
embedded in the source code
▪ Strips comments and white space from the code

▪ The source code as stored on disk is not modified.


3 STAGES OF COMPILATION (CON’T)
Stage 2: Compilation

▪ Performed by a program called the compiler


▪ Translates the preprocessor-modified source code into object
code (machine code)
▪ Checks for syntax errors and warnings
▪ Saves the object code to a disk file, if instructed to do so (we
will not do this).
o If any compiler errors are received, no object code file will be
generated.
o An object code file will be generated if only warnings, not errors,
are received.
3 STAGES OF COMPILATION (CON’T)
Stage 3: Linking

▪ Combines the program object code with other object code to


produce the executable file.
▪ The other object code can come from the Run-Time Library,
other libraries, or object files that you have created.
▪ Saves the executable code to a disk file. On the Linux system,
that file is called a.out. On the Windows system, that file is
called a.exe.
o If any linker errors are received, no executable file will be
generated.
PROGRAM DEVELOPMENT
Editor

Source File pgm.c

Preprocessor

Modified Source Code in RAM

Compiler
Program Object Code File pgm.o
Other Object Code Files (if any)

Linker

Executable File a.out / a.exe

You might also like