You are on page 1of 29

Getting Started

Welcome to C
Welcome to C
Compilers and Interpreters
The Hello World program
Comments in C
Bugs and Programs
CIRCLE.C-A Small C Program
Use of Whitespace in C
Constants in C

Why C?
 C is the common
 Originated at AT&T Bell Labs,
denominator of many of Dennis Ritchie – implemented
today’s languages 1972
Getting Started

 C’s strong points  Development language for

 very efficient UNIX operating system
 weakly typed language
 Small structured language with
many operators
 Small set of keywords (ONLY
 Has no input/output statements!
It uses function calls.

C Standard Library

 C programs consist of functions

 Most C programmers take advantage of existing
Getting Started

functions in the C standard library

 Learn the C language
 Learn how to use the library – avoid reinventing the
 Your program building blocks
 Functions you write yourself (User Defined Functions)
 Library functions (enhance the portability of your

The C Programming Process

 Design your solution Your

Inputs, outputs and C Source
 Editor
Getting Started

logical steps to achieve Program

the outputs
 Test your solution
 Code your solution Preprocessed
C Source Program
 Compile Edit your
 Handling errors
C Object
 Run and Test your File
program File

Before and after Compilation

 Editor – editing your source file

 Compiler – routes your program first through Preprocessor
Getting Started

(pre-compiler) to prepare it for compilation. The compiler produces

object code (in machine language of processor chip). It translates
high level language instructions into low level machine language
 Linker – the linker sends runtime information (such as memory
addresses) or combines several compiled programs into one
executable file
 Loader - Your executable program is loaded to memory to
Execute and test and perhaps re-edit, re-compile, …

Getting Started
Compilation and Linking


output from
Getting Started


• No object code and executable files created.
• Slower execution as interpreter has to translate
high level language and then execute the

The Smallest C Program

Getting Started

main( )


The Hello World Program

#include <stdio.h>
Getting Started

Compile and run the above program on your computer. You may get a
warning from the compiler saying "Function must return a value". For
the time being, we will ignore the warning and focus on the following
output of the program which will be displayed on the monitor of your

Output from HELLO.C


Studying an outline

#include <stdio.h>
“#” read as “hash”
A function
Getting Started

(procedure) main()
{ Preprocessor Directives
… Code (# commands) go here
… goes
… here
A block … Multiple functions and
… blocks are optional
… depending on the program’s

Studying a Program

 Programs always begin executing from the

Getting Started

main function. This calls for a brief

explanation of what is a function
 For explaining the function we will take an
example for chop operation

Arguments of a Function

 Suppose that the master chef calls this assistant

and shouts. "Chop!" The assistant shouts back.
Getting Started

"Chop what?"
 The answer to this question make up the
arguments to the function which are placed
inside a set of parentheses after the name of the
function For example:
 chop(onions);
 chop(beans);

Studying a Program

 Next, we observe the body of the main

Getting Started

function enclosed in a pair of braces { }.

Every function begins with an opening
brace '{' and ends with a closing brace

Studying a Program

 Inside, we find a call to the function

Getting Started

printf, i.e., the function main wants the

function printf to perform a task


ARGUMENTS("Hello, World!");

Studying a Program

 The '\n' sequence at the end of the string

Getting Started

constant is a special escape sequence

known as the newline character.
 Escape sequences provide a convenient
way of representing various hard-to-type
(i.e., difficult to type from a standard
computer keyboard) or invisible characters

The Header Files

"Where do we find the function printf?"

Getting Started

#include <stdio.h>

Preprocessor Directives

 commands to the preprocessor - typically placed at

column 1 at the top of your program
Getting Started

#include <stdio.h>
{ …
 begin with a hash sign (#) - Never put a semicolon
at the end of the preprocessor directives because
they are pre-processor commands and not C

Preprocessor Directives

 The #include preprocessor directive merges a disk file

into your source program.  
Getting Started

#include <filename> /* The search for the file

is performed in an implementation-dependent
manner i.e. in pre-designated directories */
usually the “include” subdirectory
#include “filename” /* The pre-processor
searches in the same directory as the file
being compiled for the file to be included

A Typical Compiler Installation
 bin sub directory - contains all programs like
compilers, debuggers, etc. (*.exe files)
 include subdirectory - contains all library header files
Getting Started

(*.h files)
 lib subdirectory - contains object code of all library
functions. (*.lib, *.olb, *.obj files)
 other subdirectories may contain other files (help,
examples, etc.).
 Most C compilers are also C++ compilers!!
(compilation depends on file extensions)
.c for C files
.cpp for C++ files

Comments in C

 Readability is IMPORTANT
 Use Comments to explain what’s going on to yourself
Getting Started

(for later reading) and other programmers

/* any mixture of characters in upper or lower case

and can go on for many lines */
 DO NOT nest comments
(no comments inside comments)
/* start comment 1 /* comment 2 */ finish comment 1 */

/*============================== HELLO2.C =====================
Introductory program from "The C Programming Language"
by Kernighan and Ritchie (2nd. ed.) PHI
Getting Started

Modification: comments added for documentation

#include <stdio.h>
int main() /* start of main */

printf("Good morning!");
return 0;

} /* end of main */

Bugs and Programs

 A bug is an error or defect in a program

which prevents it from fulfilling the desired
Getting Started

task of the program

 The simplest kinds of bugs are caused by
errors in syntax
 The compiler also generates a number of
warnings which may be ignored

CIRCLE.C - A Small C Program
Getting Started

Don’t Worry
Just watch it like a movie trailer !!!!!!!!!!!!!!!!!!

CIRCLE.C - A Small C Program
/*============================== CIRCLE.C =======================
Program to calculate the area of a circle.
#include <stdio.h>
Getting Started

#define PI 3.141592
int main() /* start of main */
float radius, area; /* variable declaration */
float find_area(float); /* function declaration */
printf("\n\nEnter radius >> "); /* output statement */
scanf("%f",&radius); /* input statement*/
if(radius < 0) /* input validation */
/* action on error */
printf("\nERROR: Radius must be non-negative");

CIRCLE.C - A Small C Program
area = find_area(radius); /* call to function */
Getting Started

printf("\nArea of circle with radius of %f =

return 0;
} /* end of main */
/* find_area: function to calculate area of circle
given its radius as input */
float find_area(float r) /* start of find_area */
float a;
a = PI * r * r;
} /* end of find_area */

Use of Whitespace in C
 Spaces, tabs, blank lines, formfeed characters (special
characters which tell printers to move to the next sheet),
and carriage returns are known as whitespace characters.
Getting Started

 The presence of whitespace characters in a C source code

file is ignored by the C compiler.
 For example, the HELLO.C program could be written in the
following manner without encountering any problems.

#include <stdio.h> int main() {printf("Hello,world!\n");return 0;}

Constants in C

 Integer Constants (e.g., 45, -567)
Getting Started

 Real or Floating Point Constants (e.g., 456.78,

-4.e35, -66.0E-98)
 Single Character Constants (e.g., 'K', 'a', '1',
 String Constants (e.g., "Hello,world", "1", "")

Points to Remember
 Compilers and interpreters are two ways to execute a
 A C program needs to compiled and linked before it
Getting Started

can be executed (run).

 Every C program consists of a number of functions,
each of them carrying out a specialized task.
 Every C program must have one and only one function
named main.
 Whitespace is ignored by the C compiler except when
it occurs within a string constant.
 Whitespace should be used judiciously to make a
program more "readable".

Bon Voyage


Getting Started