Sei sulla pagina 1di 35

CS113: Introduction to Programming

Samin Khaliq
Outline
Writing a simple C Program

Printing output

Integrated Development Environment (IDE)

Compiling, Linking and Executing the program

Skeleton of a C Program
# directives
int main ()
{
statements ;
}

Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
5
A Simple C Program:
Printing a Line of Text
int main()
C programs contain one or more functions, exactly one of
which must be main
Defines entry point of the program
Parenthesis ( ) are used to indicate a function
int means that main "returns" an integer value
Braces { } indicate a block
{ begin
} end
The bodies of all functions must be contained in braces
Note: Block can be a function or a piece of code
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
6
A Simple C Program:
Printing a Line of Text
printf( Hello World\n" );
Instructs computer to perform an action
Specifically, prints the string of characters within quotes (" ")
Entire line called a statement
All statements must end with a semicolon ;
Escape character \
Indicates that printf should do something out of the ordinary
\n is the newline character
Functions
User defined
Pre-defined (library functions)
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
7
A Simple C Program:
Printing a Line of Text
Escape Sequence Description
\n
Newline. Position the cursor at the beginning of the next line.
\t
Horizontal tab. Move the cursor to the next tab stop.
\a
Alert. Sound the system bell.
\\
Backslash. Insert a backslash character in a string.
\"
Double quote. Insert a double quote character in a string.
Fig. Some common escape sequences.


Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
8
A Simple C Program:
Printing a Line of Text

return 0;
A way to exit a function
return 0, in this case, means that the program terminated
normally

Right brace }
Indicates end of main has been reached
Printing on one line using two printf()
statements
/* Name: firstProg.c
Purpose: Prints one line with two printf() statements
Author: Samin Khaliq
Date: 9/16/2014
*/

#include <stdio.h>
// Function main begins program execution
int main()
{
printf("Welcome");
printf("to C !!!");
return 0; // indicates program ended successfully
}




Welcome to C !!!
Printing multiple lines with a single printf ()
/* Name: firstProg.c
Purpose: Prints multiple lines with one printf() statements
Author: Samin Khaliq
Date: 9/16/2014
*/
#include <stdio.h>

int main()
{
printf("Welcome\nto\nC!!!");
return 0;
}



Welcome
to
C !!!

Integrated Development Environment (IDE)
Software Package for program
development
A source code editor
A compiler
Linker
Execution program tool
A Debugger

Build automation tools

Microsoft Visual Studio 2013

IDE (MS Visual Studio 2013)

Editor, Solution Explorer, Output window

Output of Hello World Program

Hello World

Compiler converts
human readable
language to a language
which is
understandable by the
operating
system/hardware

Examples of
C/C++ compilers
of today:
Visual C++
GCC/G++
DJGPP (open source
for windows like
GCC)
Borland C
Turbo (obsolete and
not recommended)



Development to Execution
Compiler
Linker
Basics of a Typical C Program Development Environment
Phases of C Programs:
1. Edit

2. Preprocess

3. Compile

4. Link

5. Load

6. Execute

Program is created
in the editor and
stored on disk.





Preprocessor
program processes
the code.


Loader puts
program in memory.
CPU takes each
instruction and
executes it, possibly
storing new data
values as the
program executes.

Compiler creates
object code and
stores it on disk.
Linker links the
object code with the
libraries or
additional files. Loader

Primary Memory

Compiler

Editor

Preprocessor

Linker



Primary Memory
.
.
.

.
.
.

.
.
.

.
.
.

Disk
Disk

Disk

CPU
Disk
Disk
From C to an executable file
Preprocessing
Stage 1: Preprocessing

Performed by a program called the preprocessor
Modifies the source code 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.

Preprocessor Directive
#include <stdio.h>

Tells computer to load contents of a stdio file

<stdio.h> allows standard input/output operations
Compilation
Stage 2: Compilation

o Performed by a program called the compiler
o Translates the preprocessor-modified source code into
object code (machine code)
o Checks for syntax (grammatical) errors and
warnings
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.
Object code
Machine language code that contains code
specific to one Operating System (OS)
Hence, object code written by one compiler is
not only hardware dependent but also OS
dependent.
E.g. if you have Linux and Windows operating
systems, then object file compiled by one
Operating System (OS) will not get executed
on the other OS
Linking
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 and a.exe in Windows.
If any linker errors are received, no executable file will
be generated.
Linker
Linker
When a function is called, linker locates it in the
library
Inserts it into object program
If function name is misspelled, the linker will produce
an error because it will not be able to find function in
the library

Compiler
Compiler translates complete code
A language translator program that converts
the entire program into a machine language
before computer execute the program
McGraw-Hill
Compiled Languages
Pascal, C, C++, Ada
Interpreter
Interpreter translates code one line at a time
Translates language statements into machine
language and execute it immediately, statement by
statement McGraw-Hill

Interpreted Languages
Basic
JavaScript
LISP
Errors
// #include <stdio.h> // Not included

int main()
{
printf("Welcome\nto\nC!!!");
return 0;
}



1>------ Build started: Project: firstProg, Configuration: Debug Win32 ------
1> firstProg.c
1>c:\users\samin.khaliq\documents\visual studio
2010\projects\firstprog\firstprog\firstprog.c (12): error C3861: 'printf': identifier not
found
========= Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========


Warnings
#include <stdio.h>

int main()
{
int x = 30000000000; // Warning
printf("Welcome");
return 0;
}

1>------ Build started: Project: firstProg, Configuration: Debug Win32 ------
1> firstProg.c
1>c:\users\samin.khaliq\documents\visual studio
2010\projects\firstprog\firstprog\firstprog.c(12): warning C4305: 'initializing' :
truncation from '__int64' to 'int'
1>c:\users\samin.khaliq\documents\visual studio
2010\projects\firstprog\firstprog\firstprog.c (12): warning C4309: 'initializing' :
truncation of constant value
1> firstProg.vcxproj -> c:\users\samin.khaliq\documents\visual studio
2010\Projects\firstProg\Debug\firstProg.exe
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========


C the way it should not be written

Indentation styles

Case Sensitive
CaSe MaTtErS iN C


A != a
Syntax and Semantics
Syntax
Computer Program (language rules and grammar)

Semantics
Meaning of the program
Comments
Used to describe program, use liberally
Helps programmer and others understand program
Compiler ignores comments (reduce file size and
execution time)
Two ways to write comments
/* Filename.c or Filename.h
Author:
Program version:
Program Description:
Date: */
// This statement will print Hello World
Home Exercise
Write your first Hello World program. Your code
should have proper comments i.e. introducing
what this file does and what each statement
does in your code. Just like example given in the
lecture.

Your output must be
1
st
line: Welcome to the World of C programming
2
nd
line: Your name
3
rd
line: Your batch and section
4
th
line: CS 113 Introduction to Programming

34
An important piece of advice
Understand the problem clearly
You may discuss a problem or logic
with each other but always write code
yourself
Debug code yourself
Practice, Practice and more Practice

Potrebbero piacerti anche