Last week: We talked about: History of C Compiler for C programming

Slides:



Advertisements
Similar presentations
1 C++ Syntax and Semantics The Development Process.
Advertisements

Introduction to C Programming
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Introduction to C Programming
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Structure of a C program
Introduction to Computers and Programming Lecture 3: Variables and Input Professor: Evan Korth New York University.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
A simple C++ program /* * This program prints the phrase "Hello world!" * on the screen */ #include using namespace std; int main () { cout
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
Chapter 2 Data Types, Declarations, and Displays
Introduction to C Programming
String Escape Sequences
Introduction to C Topics Compilation Using the gcc Compiler
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.
Objectives You should be able to describe: Data Types
A First Book of ANSI C Fourth Edition
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
C Tokens Identifiers Keywords Constants Operators Special symbols.
CPS120: Introduction to Computer Science
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.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Fundamental Programming: Fundamental Programming Introduction to C++
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
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.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Introduction to Programming
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
CHAPTER 2 C++ SYNTAX & SEMANTICS #include using namespace std; int main() { cout
C++ Programming Lecture 3 C++ Basics – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
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.
INTRODUCTION TO PROGRAMING System Development Mansoura October 2015.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 1.
C LANGUAGE UNIT 3. UNIT 3 Arrays Arrays – The concept of array – Defining arrays – Initializing arrays.
Introduction to C Topics Compilation Using the gcc Compiler
C++ First Steps.
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
Chapter Topics The Basics of a C++ Program Data Types
User-Written Functions
Basic Elements of C++.
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
ICS103 Programming in C Lecture 3: Introduction to C (2)
Introduction to C Topics Compilation Using the gcc Compiler
Basic Elements of C++ Chapter 2.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Chapter 2 - Introduction to C Programming
Introduction to CS Your First C Programs
2.1 Parts of a C++ Program.
Introduction to C++ Programming
Chapter 2 - Introduction to C Programming
MSIS 655 Advanced Business Applications Programming
C++ Data Types Data Type
Govt. Polytechnic,Dhangar
Introduction to C Topics Compilation Using the gcc Compiler
Chapter 2 - Introduction to C Programming
Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?
C++ Programming Lecture 3 C++ Basics – Part I
Introduction to C Topics Compilation Using the gcc Compiler
Chapter 2 - Introduction to C Programming
Introduction to C Topics Compilation Using the gcc Compiler
Introduction to C Programming
Getting Started With Coding
Presentation transcript:

Last week: We talked about: History of C Compiler for C programming C applications Anatomy of a C program

Running your code: How to run your C code? First save your code in your machine using a “.c” extension. For example, you can call your “hello world” program hello.c Then use your C compiler to convert human-readable code to machine-readable code gcc hello.c -o hello The above command tells gcc to compile hello.c into a form that machine can execute. “-o” tells it to name the compiled program 'hello' The above command will create a hello.exe file if you are using windows. If your code is error-free(more in next slides), you can then execute your code in the command prompt window.

Running your code: cont'd To execute your hello.exe in windows machine.. Just type “hello” in the command prompt window. The output looks something similar to this one: IMP note: Please make sure that your current working directory in command prompt window is the same one where your C program is saved. Running a C code on Linux/Mac will follow very similar steps. Only few tweaks are needed. For example, terminal should be used in Linux instead of cmd.

Various Error in C Compile Error Runtime Syntax Logical

Compile or Syntax Errors Refers to a state when a compiler fails to compile a piece of computer program source code, either due to errors in code, or, more unusually, due to errors in the compiler itself. A compilation error message often helps programmers debugging the source code for possible errors Code: Error message: #include <stdio.h> /* To shorten example, not using argp */ int main() { printf ("Hello, world!\n") return 0; } semicolon.c: In function `main': somefile.cpp:24: parse error before `return' In this example: a semicolon (;) at the end of the line printf is missing. Adding a semicolon will get rid of this error.

Runtime Errors Runtime errors occur when the program is running, and thus, they can only occur if there is a program to run (i.e., it must have compiled and linked without errors). When you run the executable and something goes wrong then we call it a run-time error. Code: Error message: int points = 95; int iter = 0; int avg; avg = points / iter; The program would crash saying: Floating exception Here, we tried to divide points with iter(=0)

Logical Errors Logical Error This is an error that the programmer injected into the program No error Is given the program will yield incorrect output

Programming Style Bad Coding: Good Coding: “There is no programming language, no matter how structured, that will prevent programmers from making bad programs.” ~ Larry Flon A working but uncommented program is like a time bomb waiting to explode. Code that looks obvious to a programmer as he writes it is often confusing and cryptic when he revisits it a week later. The comment starts with /* The comment ends with */ So, please make a habit of commenting your code wherever necessary. Only one rule exists for good programming: Make your program clear, concise and simple as possible. Bad Coding: Good Coding:

Elements of a Program Variables: Instructions Data (variables) In computer programming, you need two things: Data (variables) Instructions (Code) Variables: Variables are the basic building blocks of a program. We must order our materials before we start constructing a building. Similarly, we must declare our variables before we can use them. C allows us to store values in variables. Each variable has a variable type. Variables are case sensitive. Also, you can not use reserved words for variable names. Such as, int, while, for, float … Instructions Instructions tell the computer what to do with the variables.

Variable types: int - numerical value char - alpha/numeric character array double – precision no less than a float ( roughly double the size of a float, refer to IEEE 754 standard or refer to float.h) float – precision is not less than a double Pointer (int, double, char) – a memory location

Integer Variables An int variable can store a value in the range of -32768 to +32767. Think of an int variable as no fractional parts are allowed. Declare an int you can use the following; int x; int x = 10; Note the “=“ symbol is use for assignment operations

Double and Floats Variables Float, of floating point, number has about seven digits of precision and a range of about 1.E-36 to 1.E+36. A floats takes four bytes to store Double, of double precision, number has about 13 digits of precision and a range of about 1..E-303 to 1.E+303. A double takes eight bytes to store. float x; double y; float x = 10.0; double y = 2.567;

Character Variables C only has the concept of numbers and characters. A character of ‘A’ != ‘a’, as these values refer to ASCII values A character is a single character stored in one byte char c; or char c = ‘X’.. Difference between “X” and ‘X’ ‘X’ – means a character of 1 byte long containing the value ‘X’ “X” – s string with a length of 2 bytes containing the value “X”

Data types:

Variable nomenclature: Characters Allowed : Underscore(_) Capital Letters ( A – Z ) Small Letters ( a – z ) Digits ( 0 – 9 ) Blanks & Commas are not allowed No Special Symbols other than underscore(_) are allowed First Character should be alphabet or Underscore Variable name Should not be Reserved Word (int, for, if,... etc) Valid names Invalid names Num1 _NUM iNt num 1 1num int