Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1.

Slides:



Advertisements
Similar presentations
Chapter 11 Introduction to Programming in C
Advertisements

Overview of programming in C C is a fast, efficient, flexible programming language Paradigm: C is procedural (like Fortran, Pascal), not object oriented.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Kernighan/Ritchie: Kelley/Pohl:
Introduction to C Programming
Scripting Languages Chapter 6 I/O Basics. Input from STDIN We’ve been doing so with $line = chomp($line); Same as chomp($line= ); line input op gives.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
CS100A, Fall 1997, Lectures 221 CS100A, Fall 1997 Lecture 22, Tuesday 18 November Introduction To C Goal: Acquire a reading knowledge of basic C. Concepts:
Guide To UNIX Using Linux Third Edition
CMSC 104, Version 9/011 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program 104 C Programming Standards and Indentation.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Console and File I/O - Basics Rudra Dutta CSC Spring 2007, Section 001.
CS 161 Introduction to Programming and Problem Solving Chapter 13 Console IO Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim from.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
1 uClinux course Day 3 of 5 The uclinux toolchain, elf format and ripping a “hello world”
Computer Science 210 Computer Organization Introduction to C.
1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming.
Introduction to C++ - How C++ Evolved Most popular languages currently: COBOL, Fortran, C, C++, Java (script) C was developed in 1970s at AT&T (Richie)
Homework Reading Programming Assignments
C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
C Programming language
Isecur1ty training center Presented by : Eng. Mohammad Khreesha.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
A First Book of ANSI C Fourth Edition Chapter 3 Processing and Interactive Input.
Programming With C.
1 計算機程式設計 Introduction to Computer Programming Lecture01: Introduction and Hello World 9/10/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction.
1 Programming in C Hello World! Soon I will control the world! Soon I will control the world!
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Lexical Elements, Operators, and the C Cystem. C overview recap functions –structured programming –return value is typed –arguments(parameters) pointers.
1 Homework Done the reading? –K&R –Glass Chapters 1 and 2 Applied for cs240? (If not, keep at it!) Gotten a UNIX account? (If not, keep at it!)
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
CSc 352: Basic C Programming Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 1 Scott Marino.
C is a high level language (HLL)
CSE 303 Concepts and Tools for Software Development Richard C. Davis UW CSE – 10/11/2006 Lecture 7 – Introduction to C.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
L071 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program Reading Sections
Chapter 1 slides1 What is C? A high-level language that is extremely useful for engineering computations. A computer language that has endured for almost.
Hank Childs, University of Oregon April 13 th, 2016 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / /
Introduction to C Topics Compilation Using the gcc Compiler
CSCE 206 Structured Programming in C
Computer Science 210 Computer Organization
A bit of C programming Lecture 3 Uli Raich.
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Revision Lecture
Introduction to C CSE 2031 Fall /3/ :33 AM.
Introduction to C Topics Compilation Using the gcc Compiler
Getting Started with C.
Introduction to C Topics Compilation Using the gcc Compiler
Administrative things
Programming in C Input / Output.
Computer Science 210 Computer Organization
Programming in C Input / Output.
A First Book of ANSI C Fourth Edition
Format String.
Introduction to C Topics Compilation Using the gcc Compiler
Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?
Programming in C Input / Output.
Introduction to C Topics Compilation Using the gcc Compiler
Module 5 Getting Help.
Introduction to C EECS May 2019.
EECE.2160 ECE Application Programming
Introduction to C Topics Compilation Using the gcc Compiler
Introduction to C Programming
Introduction to C CSE 2031 Fall /15/2019 8:26 AM.
Functions, Part 2 of 42 Topics Functions That Return a Value
Presentation transcript:

Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1

CSc 352: Basic C Programming

What’s new relative to Java? Some syntactic differences – a lot of the basic syntax is similar assignment, conditionals, loops, etc. The biggest differences are conceptual – procedural rather than object-oriented no notion of classes and inheritance hierarchies – much closer to the machine debugging sometimes requires thorough understanding of what’s going on at the machine level – explicit dynamic memory management (malloc, free) – pointers 3

What’s new relative to Java? No Garbage Collection No array boundary protection You have much more control – This means you can write faster programs – This also means code can be hard to debug and security vulnerabilities are much more likely C is generally compiled to “machine code” (java programs require java “machine” to be installed) 4

The program development process 5 source code gcc executable code C program created with an editor ASCII text human-readable machine code created using compiler binary file not human-readable Simple programs (single source file) compiler

The program development process 6 file1.c source files executable code More complex programs (many source files) file2.c file n.c … object files file1.o file2.o gcc -c compiler gcc -c linker file n.o

A simple program 7

8 a simple C program that prints out “hello” Points to note: execution begins at main(): every program must have one printf() is a standard C library routine

A simple program… 9 invoking the C compiler compiler warning executable file produced by compiler executing the program

Gcc options: -Wall 10 gcc –Wall generates warnings on questionable constructs if no return type is specified for a function, it defaults to int need to supply prototype for “printf”

Fixing the compiler warnings 11 specifies prototype for printf() specifies return type explicitly

Summary execution starts at main() – every program should have one the return type for a function defaults to int – should specify explicitly: good style need to supply prototypes for functions imported from elsewhere (e.g., standard libraries) – specified using “#include …” 12

A simple program revisited 13

Gcc options: -Wall 14 gcc –Wall generates warnings on questionable constructs if no return type is specified for a function, it defaults to int need to supply prototype for “printf”

Fixing the compiler warnings 15 How do we know what file to include?

The man command displays documentation for commands (and more). Here is an abridged example—the "man page" for cat : % man cat CAT(1) User Commands CAT(1) NAME cat - concatenate files and print on the standard output SYNOPSIS cat [OPTION]... [FILE]... DESCRIPTION Concatenate FILE(s), or standard input, to standard output. -A, --show-all equivalent to –vET... With no FILE, or when FILE is -, read standard input. man uses less to display pages. Type space to go forwards, b to go backwards. Type /STRING to search for a string, then n to search for the next occurrence. h (for help) shows lots more less commands CSC 352 Fall 2015, Unix Slide 16 The man command

The UNIX "manual" is divided into these sections: (from man man ) 1 User commands 2 System calls (functions provided by the kernel) 3 Library calls (functions within program libraries) 4 Special files (usually found in /dev ) 5 File formats and conventions eg /etc/passwd 6 Games 7 Miscellaneous (including macro packages and conventions), e.g. man(7), groff(7) 8 System administration commands (usually only for root) 9 Kernel routines [Non standard] Recall that man cat showed CAT(1). That " (1) " tells us that cat is a user command. man malloc shows MALLOC(3). That " (3) " tells us that malloc is a library function. Manual sections

man scanf 18 Header file

man printf 19 Where is the header file?

man printf bottom 20 There is both a user command and library call called printf

man 3 printf 21

gcc -o Option We’ve seen the command to compile a c program is gcc – To compile the program hello_1.c type gcc hello_1.c – The compiler creates an executable file (if there are no errors) – By default that file is named a.out – This is an executable file, meaning one can type it as a command in the shell 22

gcc -o Option If we compile another program, say gcc test.c then a new executable called a.out is created. If there is an existing file, it is overwritten. 23 What if we want to keep both executables? – use the -o option

gcc -o option The -o option allows you to specify a name for the output (executable) file – gcc -o 24 Why did I call the output testing instead of test? (Let’s open a terminal)

Reminder of things done in class 25

Simple declarations and statements 26 two uninitialized global variables, x and y, of type int a variable, z, of type int, that is local to the function main; initialized to 12 simple arithmetic expressions and assignment statements print format specifier: %d = “print a decimal value”

Simple conditionals, while loops 27 if statement while statement error message: sent to stderr return value communicates normal/abnormal execution

For loops 28

Function calls, recursion 29 recursion function call

Formatted Output: printf() takes a variable no. of arguments: – printf(“…fmtStr…”, arg 1, arg 2, …, arg n ) “… fmtStr…” is a string that can contain conversion specifiers the no. of conversion specifiers should be equal to n “regular” (non-%) characters in fmtStr written out unchanged – each conversion specifier is introduced by ‘%’ this is followed by additional (optional) characters specifying how wide the output is to be, precision, padding, etc. the conversion specifier indicates how the specified value is to be interpreted: – d = decimal integer, e.g.: printf(“value = %d\n”, n); – x = hex integer, e.g.: printf(“hex value of %d is %x\n”, x, x); – f = floating point number, e.g.: printf(“area = %f\n”, A); 30 text: Ch. 3 Sec. 3.1

Function calls (cont’d) 31 What happens when this printf() is called? the arguments are evaluated (as in all C function calls) factorial(6) evaluated when factorial() returns, the printf is executed: printf(“ … ”, 720) This causes the string factorial(6) = 720 to be printed out

Formatted Input: scanf() takes a variable no. of arguments: – scanf(“…fmtStr…”, arg 1, arg 2, …, arg n ) “… fmtStr…” is a string that can contain conversion specifiers the no. of conversion specifiers should be equal to n arg i are locations where values that are read in should be placed each conversion specifier is introduced by ‘%’ – similar to conversions for printf execution behavior: – uses format string to read in as many input values from stdin as it can – return value indicates the no. of values it was able to read return value of EOF (-1) indicates no further input (“end of file”). 32 text: Ch. 3 Sec. 3.2

scanf() Specifying where to put an input value: – in general we need to provide a pointer to the location where the value should be placed for a scalar variable X, this is typically written as &X – Examples: scanf(“%d”, &n) : read a decimal value into a variable n scanf(“%d %d %d”, &x, &y, &z) : read three decimal values and put them into variables x, y, z respectively – suppose the input contains the values 12, 3, 71, 95, 101. Then: » x  12; y  3; z  71; return value = 3 – suppose the input contains the values 19, 23. Then: » x  19; y  23; z is unassigned; return value = 2 33