COP 3275 COMPUTER PROGRAMMING USING C Instructor: Diego Rivera-Gutierrez

Slides:



Advertisements
Similar presentations
ARDUINO CLUB Session 1: C & An Introduction to Linux.
Advertisements

Computer Programming w/ Eng. Applications
I/O with scanf()/printf() I/O stands for “Input/Output” In CSE 142, you will use: –scanf() (reading from the keyboard) –printf() for output (printing to.
CSC Programming for Science Lecture 5: Actual Programming.
Lecture 2 Introduction to C Programming
Introduction to C Programming
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
CS16 Week 2 Part 2 Kyle Dewey. Overview Type coercion and casting More on assignment Pre/post increment/decrement scanf Constants Math library Errors.
Lab 5 rC language Elements rVariables Declarations and Data Types rGeneral Form of a C Program rArithmetic Expressions rFormatting Numbers rExercises Note:
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Expressions and Operators Program Style.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
Introduction to C Programming
1 Agenda Variables (Review) Example Input / Output Arithmetic Operations Casting Char as a Number (if time allow)
Basic Input/Output and Variables Ethan Cerami New York
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Lecture No: 16. The scanf() function In C programming language, the scanf() function is used to read information from standard input device (keyboard).
The preprocessor and the compilation process COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
COMPUTER SCIENCE I C++ INTRODUCTION
Files COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
COP 3275 COMPUTER PROGRAMMING USING C Instructor: Diego Rivera-Gutierrez
EPSII 59:006 Spring Introduction to C More Administrative Details The C Programming Language How a computer processes programs Your first C program.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
Week 1 Algorithmization and Programming Languages.
COP 3275 COMPUTER PROGRAMMING USING C Instructor: Diego Rivera-Gutierrez
Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
CSC 107 – Programming For Science. The Week’s Goal.
COP Structures Instructor: Diego Rivera-Gutierrez I’m back baby!
COP 3275 COMPUTER PROGRAMMING USING C Instructor: Diego Rivera-Gutierrez
Introduction to Programming
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 3: Introduction to C: Input & Output, Assignments, Math functions.
Khalid Rasheed Shaikh Computer Programming Theory 1.
COP 3275 – Iteration and loops Instructor: Diego Rivera-Gutierrez.
Pointers *, &, array similarities, functions, sizeof.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Types of C Variables:  The following are some types of C variables on the basis of constants values it has. For example: ○ An integer variable can hold.
This will all add up in the end. Assignment operator =Simple Assignment operator Arithmetic Operators +Additive operator – Subtraction operator * Multiplication.
C++ Programming Lecture 3 C++ Basics – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Arrays as pointers and other stuff COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
COP 3275 – Character Strings Instructor: Diego Rivera-Gutierrez.
COP 3275 – Character Strings and Introduction to Pointers Instructor: Diego Rivera-Gutierrez.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
Program Development Cycle 1.Edit program 2.Compile program - translates it from C to machine language 3. Run/execute your program. 4. If not satisfied,
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Gator Engineering Project 1 Grades released Re-grading –Within one week –TA: Fardad, or office hours: MW 2:00 – 4:00 PM TA Huiyuan’s office hour.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Lecture2.
A bit of C programming Lecture 3 Uli Raich.
Chapter 2 - Introduction to C Programming
Input/output.
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Introduction to C Topics Compilation Using the gcc Compiler
Chapter 2 - Introduction to C Programming
Lecture3.
Introduction to C Topics Compilation Using the gcc Compiler
Chapter 2 - Introduction to C Programming
DATA TYPES There are four basic data types associated with variables:
Introduction to C Programming
Data Types and Arithmetic in C
Presentation transcript:

COP 3275 COMPUTER PROGRAMMING USING C Instructor: Diego Rivera-Gutierrez

ADMINISTRATIVE STUFF New rule: whenever you participate tell me your name! A quick word on file extensions! The code file extension should always be “.c”. This tells the gcc compiler what programming language you are using! The output/executable extension doesn’t matter! a.out (Default!) a.o

ADMINISTRATIVE STUFF Reminder: First quiz will be on Friday (5/22). It will cover everything we’ve covered until this Wednesday (5/20) Homework percentages (these will generally apply for every homework) 15% correct compilation. No errors or warnings! 75% actual functionality (will be divided depending on the homework) For HW1: Each line of output is 25% 10% good coding practices! For HW1: you should get these points by default, for other homework these will include using good variable names and including good comments.

LAST FRIDAY Defining a variable: ; Assigning a value: = ; Combined definition and assignment: = ;

QUICK WORD ON FUNCTIONS We will cover a lot more on functions in a couple weeks. What is a function? A piece of code that we can easily reuse. What do they consist of? An identifier or name! (same rules as the ones for variables!) Parameters Return Type. We have been using two functions so far. Guesses? printf main

QUICK WORD ON FUNCTIONS For now we care about calling functions. The general pattern looks like this: ( ); Similar to how we do printf("Hello world!\n"); printf("I hope to get a %i in the HW", grade); Notice that when a function has more than 1 parameter, we separate the parameters by commas (‘,’)

PRINTING A VARIABLE We will use, surprise surprise printf How? By using a special character in the string… Each type uses a different character, we will need to learn them… TypePrintf characters int%i, %x, %o float%f, %e, %g, %a double%f, %e, %g, %a char%c _Bool%i, %u

PRINTING A VARIABLE #include int main(void) { int grade = 100; printf("I will get a %i in my first C HW\n", grade); return 0; }

PRINTING A VARIABLE #include int main(void) { char letter = 'D'; printf("I hope not to get a %c in my first C HW\n", letter); return 0; }

PRINTING TWO OR MORE VARIABLES #include int main(void) { char letter = 100; printf("I will get a %i in my first C HW\n“, sum); return 0; } #include int main(void) { char letter = 'D'; int grade = 100; printf("With a %i in my HW I won't get a %c in my C programming class\n", grade, letter); return 0; }

READING A VARIABLE FROM CONSOLE So far, all the programs are deterministic. They always return the same value. No user input. How do we change that? What if I want to be able to alter the result? We can use a powerful function called scanf. scanf comes from the same stdio.h library that printf comes from. It uses the same % characters. Let’s see an example of its usage.

READING A VARIABLE #include int main(void) { int grade; scanf("%i", &grade ); printf("I will get a %i in my first C HW\n", sum); return 0; }

READING A VARIABLE #include int main(void) { int grade; scanf("%i", &grade ); printf("I will get a %i in my first C HW\n", sum); return 0; }

SLIGHTLY BETTER: READING A VARIABLE #include int main(void) { int grade; printf("Input the grade you want to get: "); scanf("%i", &grade ); printf("I will get a %i in my first C HW\n", sum); return 0; }

ARITHMETIC OPERATORS What would you expect? '+', '-', '/', '*' We can also use parenthesis to force order of operations And a few others! So for example: int a = 4 + 2; int b = a * 2 + 3; float c = b/a; Let’s actually try this one! And see how it goes!

FLOATING POINT CONVERSIONS The previous example doesn’t work as expected Instead of a 2.5 for the last float, we get a 2. Why? How do we get the right result? Internally a and b are integers. The division between to integers is treated as an integer division (one that ignores the remainder)

FLOATING POINT CONVERSIONS The fix? Easy. We need to make an explicit conversion to float Instead of: float c = b/a; We do one of: float c = (float)b/a; float c = b/(float)a; float c = (float)b/(float)a; This technique is called casting. It doesn’t affect a or b! It just changes the value interpretation for the operation!

MODULUS Sometimes we actually want the remainder of a division. For that we use the modulus operator % int a = 4 + 2; int b = a * 2 + 3; int c = b % a; Remember that if the modulus is 0, then the second number exactly divides the first one.

ASSIGNMENT AND OPERATIONS Sometimes we want to do a quick operation on a variable and save the result in the variable itself. For example: int a = 8; a = a * 2; // get the double of 8. A quick shortcut for this is: a *= 2; //same as a = a * 2; This also works for other operators!

ASSIGNMENT AND OPERATIONS An even more specific common operation that has it’s own operand is: Adding or subtracting 1 from a variable! For example: int a = 8; a = a + 1; // adding 1 Of course we could do: a += 1; // adding 1 However an even easier way is a++; // adding 1 Same story for subtracting 1 (--).

TYPE SPECIFIERS Specifier long short unsigned signed So, can I have a infinitely large number stored in a variable of type int?

SIZE IN MEMORY OF DIFFERENT TYPES There is an operator called sizeof. One parameter: a type. Returns an unsigned value that uses the %zu value to printf. sizeof For example: Sizeof(int);