Вы находитесь на странице: 1из 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

[1]
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
32!)
 Has no input/output statements!
It uses function calls.

[2]
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
wheel
 Your program building blocks
 Functions you write yourself (User Defined Functions)
 Library functions (enhance the portability of your
program)

[3]
The C Programming Process

 Design your solution Your


Inputs, outputs and C Source
 Editor
Getting Started

logical steps to achieve Program

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

[4]
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
instructions.
 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, …

[5]
Getting Started
Compilation and Linking

[6]
Interpreters

output from
Getting Started

interpreter
program

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

[7]
The Smallest C Program

MINIMAL.C
Getting Started

main( )
{
}

HOW WILL THIS PROGRAM BEHAVE ?????

[8]
The Hello World Program

#include <stdio.h>
main()
{
Getting Started

printf("Hello,world!\n");
}
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
computer.

Output from HELLO.C


Hello,world!

[9]
Studying an outline

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

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

[10]
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

[11]
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);

[12]
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
'}'.

[13]
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

WHAT WILL IT PRINT ?????

ARGUMENTS("Hello, World!");

[14]
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

[15]
The Header Files

"Where do we find the function printf?"


Getting Started

#include <stdio.h>

[16]
Preprocessor Directives

 commands to the preprocessor - typically placed at


column 1 at the top of your program
Getting Started

#include <stdio.h>
main()
{ …
 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
commands

[17]
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
or
#include “filename” /* The pre-processor
searches in the same directory as the file
being compiled for the file to be included
*/

[18]
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

[19]
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 */

[20]
HELLO2.C
/*============================== 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("Hello,world!\n");

printf("Good morning!");
WHAT WILL THIS DO ??????
return 0;

} /* end of main */

[21]
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

[22]
CIRCLE.C - A Small C Program
Getting Started

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

[23]
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");
}

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

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


%f",radius,area);
}
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;
return(a);
} /* end of find_area */

[25]
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;}

[26]
Constants in C

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

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


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

[27]
Points to Remember
 Compilers and interpreters are two ways to execute a
program.
 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".

[28]
Bon Voyage

LADIES AND GENTLEMEN,


Getting Started

WELCOME ABOARD!
YOUR EXCITING JOURNEY
INTO THE MYSTERIES OF 'C'
PROGRAMMING HAS JUST
BEGUN !!!

[29]

Вам также может понравиться