cs156 introduction to c
Download
Skip this Video
Download Presentation
CS156: Introduction to C

Loading in 2 Seconds...

play fullscreen
1 / 18

CS156: Introduction to C - PowerPoint PPT Presentation


  • 282 Views
  • Uploaded on

CS156: Introduction to C Lecture 1 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.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'CS156: Introduction to C' - andrew


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
what is a program
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.

CS156: Introduction to C

what is a programming language
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.

CS156: Introduction to C

what is c
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.

CS156: Introduction to C

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

CS156: Introduction to C

an example c program
An Example C Program
  • All C programs must have a main function.
  • The program execution begins with the first statement in this function.

#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

CS156: Introduction to C

an example c program7
An Example C Program
  • #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

#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

CS156: Introduction to C

an example c program8
An Example C Program
  • 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

#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

CS156: Introduction to C

the type of main
The type of main
  • main() returns an int
  • main() does not return void

I don’t 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.

CS156: Introduction to C

an example c program10
An Example C Program
  • // Print Hello World
    • Comment
      • Begins with //
      • The rest of the line is ignored by the compiler

#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

CS156: Introduction to C

an example c program11
An Example C Program
  • printf ( “Hello, world!\n” );
    • printf = name of method
    • In ( ) are parameters
    • printf prints the parameter values to the screen
    • \n = new line character

#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

CS156: Introduction to C

an example c program12
An Example C Program
  • return 0;
    • The method main has a return type of int,so we must return an integer value

#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

CS156: Introduction to C

creating a c program in unix
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.

CS156: Introduction to C

gcc the gnu c compiler
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.

CS156: Introduction to C

common gcc options
Common gcc Options
  • Many gcc options you won’t need to remember, but some are used often.
    • -o file – place output in a file named fileinstead 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

CS156: Introduction to C

example continued
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 12:15 a.out

-rw------- 1 cs156 class 95 Feb 20 12:10 hello_world.c

preisner> ./a.out

Hello, world!

preisner>

CS156: Introduction to C

example continued using o
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 12:15 hello

-rw------- 1 cs156 class 95 Feb 20 12:10 hello_world.c

preisner> ./hello

Hello, world!

preisner>

CS156: Introduction to C

example continued using o18
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>

CS156: Introduction to C

ad