CS156: Introduction to C - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

CS156: Introduction to C

Description:

Print Hello World. Comment. Begins with // The rest of the line is ignored by the compiler ... printf ( 'Hello, world!n' ); printf = name of method. In ... – PowerPoint PPT presentation

Number of Views:95
Avg rating:3.0/5.0
Slides: 19
Provided by: csColo
Category:

less

Transcript and Presenter's Notes

Title: CS156: Introduction to C


1
CS156 Introduction to C
  • Lecture 1

2
What is a Program?
  • A program is set of instructions for a computer
  • Examples Unix, Shell, Find, cd
  • A compiled program is translated into a format
    that the CPU can read directly.
  • An interpreted program is not translated into
    machine code but executed by an interpreter.

3
What is a Programming Language?
  • A programming language is a language for
    describing programs.
  • Examples C, Java, Perl, VAX Assembly
  • Programming languages are defined by a set of
    rules defining valid programs, syntax, and a set
    of meaning for valid programs, semantics.

4
What is C?
  • C is an imperative programming language.
  • C was originally written for use in UNIX, but is
    now used on many operating systems.
  • It was designed for writing system software and
    much of UNIX is written in C.
  • C is a relatively low-level language that allows
    the programmer great control over how a program
    executes.

5
What is Compilation?
  • Compilation is a process of translating a program
    into machine readable code.

6
An Example C Program
include int main(int argc, char
argv) // Print Hello World printf("Hello,
world!\n") return 0
Preprocessor Directive
Function
Comment
Function Call
Function return value
  • All C programs must have a main function.
  • The program execution begins with the first
    statement in this function.

7
An Example C Program
include int main(int argc, char
argv) // Print Hello World printf("Hello,
world!\n") return 0
Preprocessor Directive
Function
Comment
Function Call
Function return value
  • include
  • stdio.h
  • Standard input/output
  • .h header file
  • Header files include
  • Declaration of functions, macros, templates and
    other prgrmg elements
  • include
  • Add a copy of the header file to this file during
    compilation

8
An Example C Program
include int main(int argc, char
argv) // Print Hello World printf("Hello,
world!\n") return 0
Preprocessor Directive
Function
Comment
Function Call
Function return value
  • int main ( int argc, char argv )
  • Function header
  • int return type (int integer)
  • main name of the function
  • In ( ) are parameters
  • designate beginning and end of function

9
The type of main
  • main() returns an int
  • main() does not return void
  • I dont care if it void works for your compiler.
    ANSI says that main() returns int, and ANSI wins
    all arguments.
  • We teach ANSI C in this class, not Microsoft C or
    GNU C or any particular compiler.

10
An Example C Program
include int main(int argc, char
argv) // Print Hello World printf("Hello,
world!\n") return 0
Preprocessor Directive
Function
Comment
Function Call
Function return value
  • // Print Hello World
  • Comment
  • Begins with //
  • The rest of the line is ignored by the compiler

11
An Example C Program
include int main(int argc, char
argv) // Print Hello World printf("Hello,
world!\n") return 0
Preprocessor Directive
Function
Comment
Function Call
Function return value
  • printf ( Hello, world!\n )
  • printf name of method
  • In ( ) are parameters
  • printf prints the parameter values to the screen
  • \n new line character

12
An Example C Program
include int main(int argc, char
argv) // Print Hello World printf("Hello,
world!\n") return 0
Preprocessor Directive
Function
Comment
Function Call
Function return value
  • return 0
  • The method main has a return type of int,so we
    must return an integer value

13
Creating a C Program in Unix
  • For this class you will be doing all programming
    on the Linux operating system.
  • You will create your programs using an editor
    pico, vim or emacs.
  • Advanced editors such as vim and emacs provide
    tools that help with program writing.
  • Syntax highlighting is the most common tool and
    is very helpful.

14
gcc The GNU C Compiler
  • The gcc is a front end for a number of
    compilation tools.
  • Preprocessor handle directives
  • Compiler Convert code to assembly
  • Assembler translate assembly to machine code
  • Linker combine machine code files into
    executable file
  • The gcc is equipped with many options that change
    its behavior. The options affect such things as
    optimization, output format, etc.

15
Common gcc Options
  • Many gcc options you wont need to remember, but
    some are used often.
  • -o file place output in a file named file
    instead of the default file a.out
  • -v give verbose output, show the commands
    being executed
  • --help give some basic usage information about
    the compiler

preisner gcc o hello hello_world.c
16
Example Continued
preisner ls hello_world.c preisner cat
hello_world.c include int main(int
argc, char argv) // Print Hello
World printf("Hello, world!\n") return 0
preisner gcc hello_world.c preisner ls
-l total 16 -rwx------ 1 cs156 class 4705 Feb 20
1215 a.out -rw------- 1 cs156 class 95 Feb 20
1210 hello_world.c preisner ./a.out Hello,
world! preisner
17
Example Continued, using -o
preisner ls hello_world.c preisner cat
hello_world.c include int main(int
argc, char argv) // Print Hello
World printf("Hello, world!\n") return 0
preisner gcc o hello hello_world.c preisner
ls -l total 16 -rwx------ 1 cs156 class 4705 Feb
20 1215 hello -rw------- 1 cs156 class 95 Feb
20 1210 hello_world.c preisner ./hello Hello,
world! preisner
18
Example Continued, using -o
preisner ls hello.c preisner cat
hello.c include int main() //
Print Hello World printf("Hello,
world!\n") return 0 preisner gcc
hello.c preisner./a.out Hello, world! preisner
Write a Comment
User Comments (0)
About PowerShow.com