CSE 220 – C Programming C Fundamentals.

Slides:



Advertisements
Similar presentations
Computer Programming w/ Eng. Applications
Advertisements

C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
Structure of a C program
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
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.
By: Mr. Baha Hanene Chapter 3. Learning Outcomes We will cover the learning outcome 02 in this chapter i.e. Use basic data-types and input / output in.
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)
CS140: Intro to CS An Overview of Programming in C by Erin Chambers.
Gator Engineering 1 Chapter 2 C Fundamentals Copyright © 2008 W. W. Norton & Company. All rights reserved.
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
C Programming Lecture 4 : Variables , Data Types
Input, Output, and Processing
Computer Programming I Hour 2 - Writing Your First C Program.
Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.
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.
Chapter 2 part #1 C++ Program Structure
CS140: Intro to CS An Overview of Programming in C by Erin Chambers.
Khalid Rasheed Shaikh Computer Programming Theory 1.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
Computer Programming A simple example /* HelloWorld: A simple C program */ #include int main (void) { printf (“Hello world!\n”); return.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
2. C FUNDAMENTALS. Example: Printing a Message /* Illustrates comments, strings, and the printf function */ #include int main(void) { printf("To C, or.
NOTE: C programs consist of functions one of which must be main. C programs consist of functions one of which must be main. Every C program begins executing.
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
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.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
STRUCTURED PROGRAMMING Complete C++ Program. Content 2  Main Function  Preprocessor directives  User comments  Escape characters  cout statement.
Introduction to C Topics Compilation Using the gcc Compiler
Lecture2.
Basic concepts of C++ Presented by Prof. Satyajit De
Chapter 1.2 Introduction to C++ Programming
UMBC CMSC 104 – Section 01, Fall 2016
CSCE 206 Structured Programming in C
Chapter 1.2 Introduction to C++ Programming
Topics Designing a Program Input, Processing, and Output
User-Written Functions
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 2 - Introduction to C Programming
Chapter 2, Part I Introduction to C Programming
Chapter 2 part #1 C++ Program Structure
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.
ICS103 Programming in C Lecture 3: Introduction to C (2)
Getting Started with C.
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Govt. Polytechnic,Dhangar
Introduction to C Topics Compilation Using the gcc Compiler
Chapter 2 - Introduction to C Programming
Introduction to C++ Programming
Lecture3.
Topics Designing a Program Input, Processing, and Output
Introduction to C Topics Compilation Using the gcc Compiler
Topics Designing a Program Input, Processing, and Output
Chapter 2 - Introduction to C Programming
EECE.2160 ECE Application Programming
C – Programming Language
An Overview of C.
CSCE 206 Lab Structured Programming in C
Introduction to C Topics Compilation Using the gcc Compiler
Introduction to C Programming
Chapter 1 c++ structure C++ Input / Output
Functions, Part 2 of 42 Topics Functions That Return a Value
Chapter 2 part #1 C++ Program Structure
Presentation transcript:

CSE 220 – C Programming C Fundamentals

Administration The first (real) HackerRank Assignment will be emailed out on Friday. As always, the assignment will be due on Thursday at 10pm. Be sure to click the button "I'm done with this test", else you won't receive credit. The content will include material from Wednesday's and next Monday's lectures. http://image.slidesharecdn.com/thoughtasasystemvsmatrixasasystem-150723034819-lva1-app6892/95/the-matrix-as-a-system-vs-thought-as-a-system-2-638.jpg?cb=1437629254

Outline Structure of a C program Constants Identifiers Functions Layout Comments Variables Printing output Reading input http://www.imd.org/uupload/imd.website/wcc/Top_banners_fundamentals.jpg

Program Design The goal is to write a program That has no compilation errors Does what it is supposed to do (no logical errors) What are the steps to accomplish the goal? How should the steps be written?

Example Steps: Ask the user for the first number Write a program that takes two numbers from the user and outputs their sum Steps: Ask the user for the first number Record the first number Ask the user for the second number Record the second number Compute the sum as 1st number + 2nd number Record the result Display the sum on the screen

Example Steps: Ask the user for the square side Write a program that computes the area of the shaded section Steps: Ask the user for the square side Record the value (call it s) Ask the user for the circle radius Record the value (call it r) Compute the shaded area as s2 – 3.14*r2 Record the result Display the output on the screen

First Program Explained #include <stdio.h> int main(void) { printf("Hello World!\n"); return 0; } #include <stdio.h>: information in the header stdio.h needs to be included before the program is compiled main: a function, the main position to start the program at {}: delimit start and end of a function printf: a function, displays Hello World to the screen

Fundamentals of C directives: #include <stdio.h> int main(void) { printf(“Hello World!\n”); return 0; } directives int main(void) { statements } directives: commands for the preprocessor one line long begin with # No semicolon at the end

Fundamentals of C statements: #include <stdio.h> int main(void) { printf(“Hello World!\n”); return 0; } directives int main(void) { statements } statements: commands to be executed when the program runs End with semicolon (with some exceptions)

Examples #include <stdlib.h> directives #define RATE 0.8 … … …. … … …. y = 2x + 5; area = width * height; guess = rand(); rand(); directives statements

Fundamentals of C #include <stdio.h> int main(void) { printf(“Hello World!\n”); return 0; } directives int main(void) { statements } The main function: main: function, returns a value int: return value is an integer void: main does not take any arguments return: terminates the function, returns a value printf: a call to function printf

Calling a function Functions have four parts: A name (e.g. printf) A body, which is a series of statements that run when a function is called An argument list that gives information to a function A return type, the one value a function can return. However, to call (use) a function, you just need to know its name and what arguments (parameters) it needs. printf("Your age is %d", age); Name Arguments (2)

printf The printf function is used to output text to the screen. It takes one or more arguments: The first argument is a character string (demarcated by quotation marks For example: "The price is %d" The following optional arguments are used to replace the placeholder characters (%d, %f, and others). Don't forget the semicolon at the end of the statement. printf("The price for %d is %.2f", quantity, total); Special Characters \n (adds a newline character) %d (placeholder for an integer) %f (placeholder for a floating point number) %.3f (placeholder for a float with three decimal places)

Examples #include <stdio.h> int main(void) { printf("Ready Set Go!\n"); return 0; } Ready Set Go! #include <stdio.h> int main(void) { printf("Ready\n") printf("Set\n Go!\n"); return 0; } Ready Set Go!

Comments /* HelloWorld.c Purpose: prints greeting */ #include <stdio.h> int main(void) { /* Greet twice */ printf("Hello World!\n"); //1st printf("Hello World!\n"); //2nd return 0; } Provide documentation Ignored by the compiler May appear anywhere May extend over multiple lines (/* */) Cannot be nested // comments end at the end of the line

Comments Where does the first comment end? 1: /* ****************************** / 2: * HelloWorld.c 3: * Purpose: prints greeting 4: * ****************************** / 5: #include <stdio.h> 6: int main(void) { 7: /* Greeting #1 */ 8: printf(“Hello World!\n”); 9: /* Do not show 2nd greeting 10: /* Greeting #2 */ 11: printf(“Hello World!\n”); */ 12: return 0; 13: } Where does the first comment end? Where does second comment end? Some editors use different colors for comments. Helps in tracking comment termination.

Comments Syntax error Where does the first comment end? 1: /* ****************************** / 2: * HelloWorld.c 3: * Purpose: prints greeting 4: * ****************************** / 5: #include <stdio.h> 6: int main(void) { 7: /* Greeting #1 */ 8: printf(“Hello World!\n”); 9: /* Do not show 2nd greeting 10: /* Greeting #2 */ 11: printf(“Hello World!\n”); */ 12: return 0; 13: } Where does the first comment end? Where does second comment end? Some editors use different colors for comments. Help track comment termination Syntax error

At the first /* At the last /* At the first */ At the last */ Where does the multiline comment end? At the first /* At the last /* At the first */ At the last */

At the end of the line Somewhere else??? Where does the single line comment end? At the end of the line Somewhere else???

Example Write a program that takes two numbers from the user and outputs their sum #include <stdio.h> int main(void) { //Ask the user for the first number printf(“Enter the first number\n”); //Read and record the number that user enters … … … //Ask for second number, read it and record it … //Exit the function returning an integer return 0; }