C Introduction Lesson CS1313 Spring 2009 1 C Introduction Lesson Outline 1.C Introduction Lesson Outline 2. hello_world.c 3.C Character Set 4.C is Case.

Slides:



Advertisements
Similar presentations
Standard I/O Lesson Outline
Advertisements

Standard I/O Lesson CS1313 Spring Standard I/O Lesson Outline 1.Standard I/O Lesson Outline 2.Output via printf 3.Placeholders 4.Placeholders for.
Lecture 2 Introduction to C Programming
Introduction to C Programming
 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. A Simple Program: Print a Line of Text // My First C++ Program #include int main( ) { cout
C Introduction Lesson CS1313 Spring C Introduction Lesson Outline 1.C Introduction Lesson Outline 2. hello_world.c 3.C Character Set 4.C is Case.
 C++ programming facilitates a disciplined approach to program design. ◦ If you learn the correct way, you will be spared a lot of work and frustration.
Introduction to C Programming
Software Lesson #1 CS1313 Spring Software Lesson 1 Outline 1.Software Lesson 1 Outline 2.What is Software? A Program? Data? 3.What are Instructions?
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
CS1061 C Programming Lecture 2: A Few Simple Programs A. O’Riordan, 2004.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
Introduction to C Programming
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.
Introduction to C Topics Compilation Using the gcc Compiler
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Introduction to C Language
Using C Programming Language.  The programs that run on a computer are referred to as software.  You’ll learn key programming methodology that are enhancing.
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 Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Gator Engineering 1 Chapter 2 C Fundamentals Copyright © 2008 W. W. Norton & Company. All rights reserved.
CS 106 Introduction to Computer Science I 01 / 25 / 2010 Instructor: Michael Eckmann.
Numeric Data Types Lesson CS1313 Spring Numeric Data Types Outline 1.Numeric Data Types Outline 2.Data Types 3.Integers in Mathematics 4.Integers.
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
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.
© 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.
CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE 1 st semester H 1 King Saud University College of Applied studies and Community Service Csc 1101 By:
1 Programming in C Hello World! Soon I will control the world! Soon I will control the world!
Chapter 2 part #1 C++ Program Structure
Variables & Constants Lesson 2 Outline
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
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.
CSC 110 – Intro to Computing - Programming
1 Structure of Simple C++ Program Chapter 1 09/09/13.
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
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.
STRUCTURED PROGRAMMING Complete C++ Program. Content 2  Main Function  Preprocessor directives  User comments  Escape characters  cout statement.
C++ First Steps.
Programming what is C++
Topic Pre-processor cout To output a message.
Chapter 2 Introduction to C++ Programming
CSC201: Computer Programming
CSE 220 – C Programming C Fundamentals.
INC 161 , CPE 100 Computer Programming
Chapter 2 - Introduction to C Programming
Computer Programming Chapter 1: Introduction
Chapter 2, Part I Introduction to C Programming
Chapter 2 part #1 C++ Program Structure
Introduction to C Language
Chapter 2 - Introduction to C Programming
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Chapter 2 – Getting Started
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Introduction to CS Your First C Programs
Introduction to C++ Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Introduction to C++ Programming
Chapter 2 - Introduction to C Programming
Variables Lesson Outline
Introduction to C Programming
Chapter 2 part #1 C++ Program Structure
Presentation transcript:

C Introduction Lesson CS1313 Spring C Introduction Lesson Outline 1.C Introduction Lesson Outline 2. hello_world.c 3.C Character Set 4.C is Case Sensitive 5.Character String Literal Constant 6.String Literal Cannot Use Multiple Lines 7.Multi-line String Literal Example 8.Newline 9.Newline Example 10.Statements 11.Statement Terminator 12.Standard Input & Standard Output 13.Block Delimiters 14.What Is a Comment? #1 15.What Is a Comment? #2 16.Are Comments Necessary? 17. hello_world.c with Comments 18. hello_world.c without Comments 19.Flowchart for hello_world.c

C Introduction Lesson CS1313 Spring hello_world.c /* ************************************************* *** Program: hello_world *** *** Author: Henry Neeman *** *** Course: CS Spring 2009 *** *** Lab: Sec 011 Fridays 10:30am *** *** Description: Prints the sentence *** *** "Hello, world!" to standard output. *** ************************************************* */ #include int main () { /* main */ /* ******************************** *** Execution Section (body) *** ******************************** * * Print the sentence to standard output * (i.e., to the terminal screen). */ printf("Hello, world!\n"); } /* main */

C Introduction Lesson CS1313 Spring C Character Set These are the characters that C recognizes. Letters (upper case and lower case) A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z Digits Special Characters (punctuation etc) space (also known as blank) ’ " ( ) * + - / : = ! & $ ; % ?,. ˆ ˜ ‘ { } [ ] \ |

C Introduction Lesson CS1313 Spring C is Case Sensitive C is case sensitive: it distinguishes between UPPER case (CAPITAL) and lower case (small) letters. Keywords in C — for example, the keyword int — MUST be in lower case. For example: #include int main () { /* main */ int height_in_cm; height_in_cm = 160; printf("My height is %d cm.\n", height_in_cm); } /* main */

C Introduction Lesson CS1313 Spring Character String Literal Constant A character string literal constant is a sequence of characters delimited by a double quote at the beginning and a double quote at the end. A character string literal constant is also known as a character string literal or a string literal for short. For example, in this printf statement: printf("This is a printf.\n"); the following is a string literal: "This is a printf.\n" The output of this printf statement is: This is a printf. followed by a newline, also known as a carriage return.

C Introduction Lesson CS1313 Spring String Literal Cannot Use Multiple Lines A character string literal constant can only use one line; that is, both of its delimiters MUST be on the same line of source code text. So, this is correct: printf("This string literal takes one line"); printf(" and so does this string literal.\n"); And this is WRONG WRONG WRONG: printf("This string literal takes more than one line so it’s WRONG!\n"); Some compilers will accept this but won’t be happy; other compilers will simply reject it. Regardless, if this appears in a program in CS1313, YOU WILL BE SEVERELY PENALIZED!

C Introduction Lesson CS1313 Spring Multi-line String Literal Example % cat bad_string_literal.c #include int main () { /* main */ printf("This string literal takes more than one line so it's WRONG!\n"); } /* main */ % gcc -o bad_string_literal bad_string_literal.c bad_string_literal.c:5:12: warning: multi-line string literals are deprecated Deprecated: severely frowned upon

C Introduction Lesson CS1313 Spring Newline In C, you can place a newline, also known as a carriage return, inside a string literal using: \n If a newline appears inside a string literal in the source code, then when the string literal is output, the newline causes the output to move to a new line.

C Introduction Lesson CS1313 Spring Newline Example % cat newline.c #include int main () { /* main */ printf("Howdy do!\n"); printf("This string literal contains a newline in the\nmiddle "); printf("but this string literal contains a newline at the end.\n"); printf("So there!\n"); } /* main */ % gcc -o newline newline.c % newline Howdy do! This string literal contains a newline in the middle but this string literal contains a newline at the end. So there! Note: In general, it’s better programming practice to put newlines only at the end of your string literals, not in the middle, because in the middle they can be difficult for programmers (e.g., graders) to see.

C Introduction Lesson CS1313 Spring Statements A statement in a program is like a sentence in a natural language: it’s the smallest possible collection of words and punctuation that can stand by itself and have meaning. For example: printf("Hello, world.\n"); This statement tells the compiler to output to the terminal screen the string literal Hello, world. followed by a newline.

C Introduction Lesson CS1313 Spring Statement Terminator In C, every statement ends with a semicolon, which is known as the statement terminator. For example: int height_in_cm; height_in_cm = 160; printf("My height is %d cm.\n", height_in_cm); Notice: a statement CAN take more than one line (but recall that a string literal CAN’T take more than one line). The way you find the end of a statement is by finding its statement terminator.

C Introduction Lesson CS1313 Spring Standard Input & Standard Output Standard input is when a user types at the keyboard. It is sometimes shortened to stdin, pronounced “standard in.” Standard output is when the computer outputs to the terminal screen. It is sometimes shortened to stdout, pronounced “standard out.” In C: a scanf statement always inputs from stdin, and a printf statement always outputs to stdout. More on this later.

C Introduction Lesson CS1313 Spring Block Delimiters The open curly brace, also known as the left brace, { acts as the start of a block and is known as the block open. The close curly brace, also known as the right brace, } acts as the end of a block and is known as the block close. The block open and block close are said to delimit the block: they indicate where the block begins and where the block ends. Delimit: Indicate where something begins and ends.

C Introduction Lesson CS1313 Spring What Is a Comment? #1 A comment is a piece of text in a source file that: tells human beings (for example, programmers) something useful about the program, but is ignored by the compiler, so it has absolutely no affect on how the program runs. In C, the start of a comment is indicated by /* and the end of a comment is indicated by */ All text appearing between these comment delimiters are part of the comment, and therefore are ignored by the compiler.

C Introduction Lesson CS1313 Spring What Is a Comment? #2 A comment is a piece of text in a source file that: tells human beings (for example, programmers) something useful about the program, but is ignored by the compiler, so it has absolutely no affect on how the program runs. In C, the start of a comment is indicated by /* and the end of a comment is indicated by */ A comment can use multiple lines of text. The delimiters do not have to be on the same line.

C Introduction Lesson CS1313 Spring Are Comments Necessary? Comments are ignored by the compiler, so strictly speaking they aren’t needed to compile and run. But, if you don’t put them into one of your CS1313 programming projects, YOU MAY LOSE A FULL LETTER GRADE OR MORE on that project. Why? Comments tell human beings useful things about your program. They help programmers – including you, a month later when you’ve forgotten everything about your program – to understand your program. They also tell graders that you know what the heck you’re doing.

C Introduction Lesson CS1313 Spring hello_world.c with Comments /* ************************************************* *** Program: hello_world *** *** Author: Henry Neeman *** *** Course: CS Spring 2009 *** *** Lab: Sec 011 Fridays 10:30am *** *** Description: Prints the sentence *** *** "Hello, world!" to standard output. *** ************************************************* */ #include int main () { /* main */ /* ******************************** *** Execution Section (body) *** ******************************** * * Print the sentence to standard output * (i.e., to the terminal screen). */ printf("Hello, world!\n"); } /* main */

C Introduction Lesson CS1313 Spring hello_world.c without Comments #include int main () { printf("Hello, world!\n"); }

C Introduction Lesson CS1313 Spring Flowchart for hello_world.c int main () { printf("Hello, world!\n"); } An oval denotes either the start or the end of the program, or a halt operation within the program (which we’ll learn about later). A parallelogram denotes either an input operation or an output operation. An arrow denotes the flow of the program. References: StartEnd Output “Hello, world!”