Overview of a C Program

Size: px
Start display at page:

Download "Overview of a C Program"

Transcription

1 Overview of a C Program Programming with C CSCI 112, Spring 2015 Patrick Donnelly Montana State University

2 Programming with C (CSCI 112) Spring / 42 C Language Components Preprocessor Directives Comments The main function Variable Declarations and Data Types Executable Statements Reserved Words Identifiers

3 Programming with C (CSCI 112) Spring / 42 General Outline of a C Program preprocessor directives main function heading { declarations executable statements }

4 Programming with C (CSCI 112) Spring / 42 Example C Program # include <stdio.h> // printf, scanf definitions # define KMS_PER_MILE // conversion constant int main ( int argc, char ** argv ) { double miles ; // distance in miles double kms ; // equivalent distance in kilometers // get the distance in miles printf (" Enter the distance in miles > "); scanf ("%lf", & miles ); // convert the distance to kilometers kms = KMS_PER_MILE * miles ; // display the distance in kilometers printf (" That equals %f kilometers.\n", kms ); } return (0);

5 Anatomy of a C Program Programming with C (CSCI 112) Spring / 42

6 Programming with C (CSCI 112) Spring / 42 Example C Program: Preprocessor Directives #include <stdio.h> #define KMS PER MILE int main ( int argc, char ** argv ) { double miles ; // distance in miles double kms ; // equivalent distance in kilometers // get the distance in miles printf (" Enter the distance in miles > "); scanf ("%lf", & miles ); // convert the distance to kilometers kms = KMS_PER_MILE * miles ; // display the distance in kilometers printf (" That equals %f kilometers.\n", kms ); } return (0);

7 Programming with C (CSCI 112) Spring / 42 Example C Program: Declarations # include <stdio.h> // printf, scanf definitions # define KMS_PER_MILE // conversion constant int main ( int argc, char ** argv ) { double miles; double kms; // get the distance in miles printf (" Enter the distance in miles > "); scanf ("%lf", & miles ); // convert the distance to kilometers kms = KMS_PER_MILE * miles ; // display the distance in kilometers printf (" That equals %f kilometers.\n", kms ); } return (0);

8 Programming with C (CSCI 112) Spring / 42 Example C Program: Comments # include <stdio.h> // printf, scanf definitions # define KMS_PER_MILE // conversion constant int main ( int argc, char ** argv ) { double miles ; // distance in miles double kms ; // equivalent distance in kilometers } // get the distance in miles printf (" Enter the distance in miles > "); scanf ("%lf", & miles ); // convert the distance to kilometers kms = KMS_PER_MILE * miles ; // display the distance in kilometers printf (" That equals %f kilometers.\n", kms );

9 Programming with C (CSCI 112) Spring / 42 Example C Program: Executable Statements # include <stdio.h> // printf, scanf definitions # define KMS_PER_MILE // conversion constant int main ( int argc, char ** argv ) { double miles ; // distance in miles double kms ; // equivalent distance in kilometers // get the distance in miles printf("enter the distance in miles> "); scanf("%lf", &miles); // convert the distance to kilometers kms = KMS PER MILE * miles; // display the distance in kilometers printf("that equals %f kilometers.\n", kms); } return(0);

10 Programming with C (CSCI 112) Spring / 42 Our C Program # include <stdio.h> /* lab01. c ( name of your file ) * Philip J. Fry ( your name ), CSCI112, Lab 01 * 08/28/2014 ( current date ) */ int main ( void ) { printf (" Hello return (0); } World \n");

11 Programming with C (CSCI 112) Spring / 42 Preprocessor Directives Definition: Preprocessor Directives Preprocessor Directives are commands that give instructions to the C preprocessor. Definition: Preprocessor The Preprocessor is a program that modifies your C program before it is compiled. Preprocessor Directives start with a # symbol. #include #define #if #else #endif #pragma

12 Programming with C (CSCI 112) Spring / 42 #include #include is used to include other source files into your source file. #include gives a program access to a library/header file. stdio.h math.h time.h Libraries are a collection of useful functions and symbols. Standard libraries are predefined by the ANSI C language. You must include stdio.h if you want to use printf and scanf library functions. #include <stdio.h> inserts their definitions into your program before compilation.

13 Programming with C (CSCI 112) Spring / 42 #define The #define directive instructs the preprocessor to replace every occurance of a particular text with a given constant value. The replacement occurs before compilation. Your source code: #d e f i n e PI #d e f i n e EULERS NUMBER i n t main ( v o i d ) { double x = 5 PI ; double y = EULERS NUMBER 7 ; r e t u r n ( 0 ) ; } What actually gets compiled: i n t main ( v o i d ) { d o u b l e x = ; d o u b l e y = ; r e t u r n ( 0 ) ; }

14 Programming with C (CSCI 112) Spring / 42 Constants #define Constants Consists of 3 parts: #define constant name constant value There is no = sign or ; at the end # define MY_CONSTANT_NAME 123 # define ANSWER 42

15 Comments Comments provide extra information, making it easier for humans to understand the program. These comments are completely ignored by the compiler. Comments take two forms: /* */ - anything between asterisks is a comment. // - anything after is a comment, but only until EOL. Comments are used for documentation that helps other programmers read and understand the program. Programs should contain a comment at the top with the programmer s name, the date, and a brief description of what the program does. COMMENT YOUR CODE! Programming with C (CSCI 112) Spring / 42

16 Programming with C (CSCI 112) Spring / 42 The main function The heading int main(void) marks the beginning of the main function where your program execution begins. Every C program has a main function. Braces/Curly brackets: { } mark the beginning and end of the body of the function. A function body has two parts: declarations: (names of variables and data types) tell the compiler what memory cells are needed in the function and how they should be processed. executable statements: they reflect actions of your algorithm. These are translated into machine language by the compiler and later executed.

17 Programming with C (CSCI 112) Spring / 42 Declarations of Variables Variable: A memory cell used to store program data. A variables value can change with time. We use its name as a simple reference (instead of the actual address in memory). Variable declaration: Statement that communicates to the compiler the names of variables in the program and the data type they represent. Example: double miles ; This tells the compiler to create a space for a variable of type double in memory and refer to it each time we use the name miles in our program.

18 Programming with C (CSCI 112) Spring / 42 Numeric Data Types Definition: Data Type Data Type: a set of values and a set of operations that can be performed on those values. Numeric Data Types: int: Stores integer value (whole number). Examples: 65, -123 double: Stores real number (uses a decimal point). Examples: , 1.23e5 ( ) Arithmetic operations (+,-,*,/, %) can be used on ints and doubles.

19 Programming with C (CSCI 112) Spring / 42 Character Data Type Character Data Type: char: An individual character value. Examples: a,, 5, * (can be a letter, digit, or special symbol) Character Declaration Use single quotes, NOT double quotes char my_char = Z ; Comparison operations (<, >,, ) can be used on ints, doubles and chars.

20 Programming with C (CSCI 112) Spring / 42 ASCII Code A < B < C <... < X < Y < Z

21 Programming with C (CSCI 112) Spring / 42 Executable Statements Executable Statements: C statements used to write or code the algorithm. The C compiler translates the executable statements into machine code. Examples: Input/Output Operations and Functions printf function scanf function Assignment Statement (=) return Statement/Operation

22 Programming with C (CSCI 112) Spring / 42 Executable Statements: Input/Output Operations Input operation: data transfer from outside the program into computer memory that is usable by the program. Input may be received from: A program user An external file Another program Output operation: program results that can be displayed to the program user. Results can be printed to: The standard console output An external file

23 Programming with C (CSCI 112) Spring / 42 Executable Statements: Functions A function takes input parameters and produces an output. A function definition specifies the return type, the input parameters, and the body. A function call is used to call or activate a function. Calling a function means asking another piece of code to do some work for you.

24 Programming with C (CSCI 112) Spring / 42 Executable Statements: Functions - printf() By calling the printf() function, we are asking for the function to print to standard output. The first argument to printf is the formatting code (in string form), and all following arguments are the the variables to be used in the placeholders. double miles = 2.7; printf (" That is %f miles.\n", miles );

25 Programming with C (CSCI 112) Spring / 42 Executable Statements: Functions - printf() Multiple placeholders can be specified, and the corresponding arguments are then provided in order. Different types of variables require different formatting codes. int a = 1; double b = 2.0; char *c = " three "; printf (" Easy as %d, %f, %s!\n", a, b, c); Details on formatting codes can be found in the handout.

26 Programming with C (CSCI 112) Spring / 42 Executable Statements: Functions - scanf() Reading input data in a program is done via the scanf() function. The concept is very much the same as the printf() function. In this case, the format code is just the variable code for what data will be received (%d, %c, %lf), and the following arguments are the variables that will receive the data. double miles ; scanf ("%lf", & miles ); The & is the address operator in C. The & operator in front of variable miles informs the scanf() function about the location of variable miles in the computer s memory.

27 Programming with C (CSCI 112) Spring / 42 printf()/scanf() statements printf()/scanf() statements Use commas to deliminate printf/scanf arguments. char a =! ; int b = 7; float c = 3.7; double d = 1.5; printf ("%c %d %f %lf scanf ("%lf", &d); %c \n", a, b, c, d, a); Remember that %f is for floats, %lf is for doubles For printf(), doubles can be implicitly converted to floats printf ("%f\n", &d); printf ("%lf\n", &d); scanf ("%lf", &d);

28 Programming with C (CSCI 112) Spring / 42 Assignment Statements An assignment statement stores a value in a variable. The value assigned can be the result of another computation. kms = KMS_PER_MILE * miles ; The above assigns a value (content) to the variable kms. The value assigned in this case is the result of the multiplication of the constant KMS PER MILE by the variable miles. We should interpret an assignment statement: value = expression as: value expression This should be read as becomes, gets, or takes the value of rather than equals.

29 Programming with C (CSCI 112) Spring / 42 Assignment Statements Effect of kms = KMS PER MILE * miles;

30 Programming with C (CSCI 112) Spring / 42 Assignment Statements = is NOT an algebraic operator. Consider the following: sum = sum + item ; This is obviously not a correct algebraic equation (if item 0). This statement instructs the computer to add the current value of sum to the value of item. Afterward the result is stored back into sum. Equality: To test for equality, use the == operator. Be sure to keep these concepts separate. Remember! Every variable has contents, even before assignment.

31 Programming with C (CSCI 112) Spring / 42 Assignment Statements Effect of sum = sum + item;

32 Programming with C (CSCI 112) Spring / 42 Arithmetic Operators Operator Meaning Examples + addition = = 7.0 subtraction 5 2 = = 3.0 * multiplication 5 * 2 = * 2.0 = 10.0 / division 5.0 / 2.0 = / 2 = 2 % remainder (mod) 5 % 2 = 1 5 / 2 = 2

33 Programming with C (CSCI 112) Spring / 42 Expression Example Area of a Circle a = πr 2 can be written in C as: area = PI * radius * radius ; where PI is a constant macro set to

34 Expression Evaluation Programming with C (CSCI 112) Spring / 42

35 Programming with C (CSCI 112) Spring / 42 Expression Example 3 Evaluation Tree and Evaluation for v = (p2 - p1) / (t2 - t1);

36 Programming with C (CSCI 112) Spring / 42 Expression Example 4 Evaluation Tree and Evaluation for z - (a + b / 2) + w * -y;

37 Programming with C (CSCI 112) Spring / 42 The return statement In your main() function, the return statement will transfer control from your program to the operating system. return(0); returns a 0 to the operating system, which indicates that the program executed without error. This means that the end of the program was reached. It does not mean that the program did what it was supposed to do. There still may have been logical errors. Other functions also have return statements, which can return error codes, data, or nothing at all.

38 Programming with C (CSCI 112) Spring / 42 Punctuation & Special Symbols Semicolons: ; Mark the end of a statement (which may take more than one line). Curly Braces: { } Mark the beginning and end of the body of a function. Mathematical Symbols: *,/,+,- Are mainly used to compute numeric values. Note that some have additional meaning. In particular, we will talk about * later.

39 Programming with C (CSCI 112) Spring / 42 Reserved Words A reserved word is a word that has special meaning to C and can not be used for other purposes. These are words that C reserves for its own uses. They are often necessary for the compiler to analyze the language s grammar and to understand what the programmer wants to do (declaring variables, controlling data flow, etc). In example, you cannot have a variable named return or if. Reserved words are always lowercase. See your book or the reference links on the website for a complete list of reserved words.

40 Programming with C (CSCI 112) Spring / 42 Standard Identifiers An identifier is a name given to a variable or an operation. This is just a formal term for function names and variable names. A standard identifier is an identifier that is defined in the standard C libraries and has special meaning in this standard (such as ANSI C). Examples include printf and scanf Unlike reserved words, you can re-define a standard identifier It is not recommended to use standard identifiers in a non-standard way, especially when linking to other code that uses these standards.

41 Programming with C (CSCI 112) Spring / 42 User Defined Identifiers Variables: We choose our own identifiers to name memory cells that will hold data. Functions: We choose our own identifiers to name operations that we define. Common Rules for Naming Identifiers: May consist only of letters, digits, and underscores cannot begin with a digit reserved words cannot be used standard identifiers should not be redefined Valid Identifers: var1, inches, KM PER MILE Invalid Identifiers: 1var, the*var, return

42 Programming with C (CSCI 112) Spring / 42 Identifier Naming Guidelines Some compilers only recognize the first 31 characters of identifier names, so avoid long identifiers. Compilers are case-sensitive. Aviod very similar names (differ by case, or a single letter) ITEM is different than Item is different than item Note that conventional C code uses lowercase and underscores for variables (as opposed to camel-case used in Java) miles vehicle speed Constants and Macros usually use all uppercase KMS PER MILE is a defined constant Choose meaningful identifiers that are easily understood. distance = velocity * time is more intelligible than x = y * z.

C Syntax and Semantics

C Syntax and Semantics C Syntax and Semantics 1 C Program Structure C Language Elements Preprocessor directives Function Header, and Function body Executable statements Reserved word, Standard identifiers, user defined identifiers

More information

Figure 1.12 Entering, Translating, and Running a High-Level Language Program Pearson Education, Inc. All rights reserved.

Figure 1.12 Entering, Translating, and Running a High-Level Language Program Pearson Education, Inc. All rights reserved. Figure 1.12 Entering, Translating, and Running a High-Level Language Program 1 2007 Pearson Education, Inc. All rights reserved. 2 Figure 2.7 General Form of a C Program 2007 Pearson Education, Inc. All

More information

Punctuation in C. Identifiers and Expressions. Identifiers. Variables. Keywords. Identifier Examples

Punctuation in C. Identifiers and Expressions. Identifiers. Variables. Keywords. Identifier Examples Identifiers and Expressions CSE 130: Introduction to C Programming Spring 2005 Punctuation in C Statements are terminated with a ; Groups of statements are enclosed by curly braces: { and } Commas separate

More information

C Programming Dr. Hasan Demirel

C Programming Dr. Hasan Demirel C How to Program, H. M. Deitel and P. J. Deitel, Prentice Hall, 5 th edition (3 rd edition or above is also OK). Introduction to C Programming Dr. Hasan Demirel Programming Languages There are three types

More information

Introduction to C Programming

Introduction to C Programming Introduction to C Programming C HOW TO PROGRAM, 6/E 1992-2010 by Pearson Education, Inc. All Rights Reserved. 2.1 Introduction The C language facilitates a structured and disciplined approach to computer

More information

CSI 333 Lecture 2 Introduction to C: Part I 2 1 / 16

CSI 333 Lecture 2 Introduction to C: Part I 2 1 / 16 CSI 333 Lecture 2 Introduction to C: Part I 2 1 / 16 Basics of C Remark: Skim Chapters 1 through 6 of Deitel & Deitel. You will notice the following: C is (more or less) a subset of Java. (So, you are

More information

Informatica e Sistemi in Tempo Reale

Informatica e Sistemi in Tempo Reale Informatica e Sistemi in Tempo Reale Introduction to C programming Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa October 25, 2010 G. Lipari (Scuola Superiore Sant Anna)

More information

Your first C program. Which one is best?

Your first C program. Which one is best? Your first C program #include void main(void) { printf( Hello, world!\n ); } #include void main(void) { printf( Hello, ); printf( world! ); printf( \n ); } Which one is best? #include

More information

Introduction to C Programming S Y STEMS

Introduction to C Programming S Y STEMS Introduction to C Programming CS 40: INTRODUCTION TO U NIX A ND L I NUX O P E R AT ING S Y STEMS Objectives Introduce C programming, including what it is and what it contains, which includes: Command line

More information

C programming. Intro to syntax & basic operations

C programming. Intro to syntax & basic operations C programming Intro to syntax & basic operations Example 1: simple calculation with I/O Program, line by line Line 1: preprocessor directive; used to incorporate code from existing library not actually

More information

Borland C++ Compiler: Hello World Program

Borland C++ Compiler: Hello World Program Introduction Borland C++ Compiler: Hello World Program This article explains how to display text in a console program using the Borland C++ Compiler. The hello program writes Hello World to the console.

More information

Chapter 2: Problem Solving Using C++

Chapter 2: Problem Solving Using C++ Chapter 2: Problem Solving Using C++ 1 Objectives In this chapter, you will learn about: Modular programs Programming style Data types Arithmetic operations Variables and declaration statements Common

More information

A Rudimentary Intro to C programming

A Rudimentary Intro to C programming A Rudimentary Intro to C programming Wayne Goddard School of Computing, Clemson University, 2008 Part 1: Variables and Data-types 1 Getting Started............................... A1 2 Some Basics.................................

More information

Dept. of CSE, IIT KGP

Dept. of CSE, IIT KGP Programming in C: Basics CS10001: Programming & Data Structures Pallab Dasgupta Professor, Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur Types of variable We must declare the

More information

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 2: Basic Elements of C++

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 2: Basic Elements of C++ C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Basic Elements of C++ Objectives In this chapter, you will: Become familiar with the basic components of a C++ program,

More information

PE1 Worksheet. 3) What are the three control structures for writing an algorithm in pseudocode?

PE1 Worksheet. 3) What are the three control structures for writing an algorithm in pseudocode? PE1 Worksheet Problem solving 1) What are the four stages of problem solving by programming? What shall be done in each stage? Stage 1: Stage 2: Stage 3: Stage 4: 2) What is the top-down design strategy

More information

Going from Python to C

Going from Python to C Going from Python to C Darin Brezeale December 8, 2011 Python is a high-level, interpreted language. C has many of the same types of programming constructs as in Python: arrays, loops, conditionals, functions,

More information

Programming for MSc Part I

Programming for MSc Part I Herbert Martin Dietze University of Buckingham herbert@the-little-red-haired-girl.org July 24, 2001 Abstract The course introduces the C programming language and fundamental software development techniques.

More information

Algorithms. Introduction to C. Writing C Programs. Topics

Algorithms. Introduction to C. Writing C Programs. Topics 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 the smallest integer. (Hint: Think about keeping

More information

Programming Language: Syntax. Introduction to C Language Overview, variables, Operators, Statements

Programming Language: Syntax. Introduction to C Language Overview, variables, Operators, Statements Programming Language: Syntax Introduction to C Language Overview, variables, Operators, Statements Based on slides McGraw-Hill Additional material 2004/2005 Lewis/Martin Modified by Diana Palsetia Syntax

More information

C Programming, Chapter 1: C vs. Java, Types, Reading and Writing

C Programming, Chapter 1: C vs. Java, Types, Reading and Writing C Programming, Chapter 1: C vs. Java, Types, Reading and Writing T. Karvi August 2013 T. Karvi C Programming, Chapter 1: C vs. Java, Types, Reading and Writing August 2013 1 / 1 C and Java I Although the

More information

C Programming Laboratory

C Programming Laboratory Sheet 1 of 14 LAB 1: Introduction to C Environment Objective: In this laboratory session you will learn: 1. How to create C programs 2. How C programs are organised 3. How to write a basic program to display

More information

C AND C++ PROGRAMMING

C AND C++ PROGRAMMING C AND C++ PROGRAMMING Bharathidasan University A Courseware prepared by University Informatics Centre Part I - Programming in C Getting Started This courseware is intended to be an introduction to C programming

More information

Programming Fundamental. Instructor Name: Lecture-2

Programming Fundamental. Instructor Name: Lecture-2 Programming Fundamental Instructor Name: Lecture-2 Today s Lecture What is Programming? First C++ Program Programming Errors Variables in C++ Primitive Data Types in C++ Operators in C++ Operators Precedence

More information

Programming Process. Simple Programs in C. Programming Languages. Why C? The Evolution of Programming Languages. Types of Errors

Programming Process. Simple Programs in C. Programming Languages. Why C? The Evolution of Programming Languages. Types of Errors Programming Process Simple Programs in C CSE 130: Introduction to C Programming Spring 2005 1. Specify the problem to be solved 2. Develop an appropriate algorithm 3. Code the algorithm using a programming

More information

Birla Institute of Technology &Science, Pilani Computer Programming (CS F111) Second Semester Lab-3 (Introduction to C Programming Language)

Birla Institute of Technology &Science, Pilani Computer Programming (CS F111) Second Semester Lab-3 (Introduction to C Programming Language) Topics to be covered: 1. C Program structure and execution 2. Data type 3. Reading input and printing output 4. Expression 5. Operator precedency 6. Typecasting Birla Institute of Technology &Science,

More information

Adjusted/Modified by Nicole Tobias. Chapter 2: Basic Elements of C++

Adjusted/Modified by Nicole Tobias. Chapter 2: Basic Elements of C++ Adjusted/Modified by Nicole Tobias Chapter 2: Basic Elements of C++ Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types

More information

C Programming Language CIS 218

C Programming Language CIS 218 C Programming Language CIS 218 Description C is a procedural languages designed to provide lowlevel access to computer system resources, provide language constructs that map efficiently to machine instructions,

More information

FORMAT MEANING VARIABLE TYPE

FORMAT MEANING VARIABLE TYPE Printf and Scanf Both formatted I/O Both sent to standard I/O location Printf Converts values to character form according to the format string Scanf Converts characters according to the format string,

More information

Moving from CS 61A Scheme to CS 61B Java

Moving from CS 61A Scheme to CS 61B Java Moving from CS 61A Scheme to CS 61B Java Introduction Java is an object-oriented language. This document describes some of the differences between object-oriented programming in Scheme (which we hope you

More information

INDEX. C programming Page 1 of 10. 5) Function. 1) Introduction to C Programming

INDEX. C programming Page 1 of 10. 5) Function. 1) Introduction to C Programming INDEX 1) Introduction to C Programming a. What is C? b. Getting started with C 2) Data Types, Variables, Constants a. Constants, Variables and Keywords b. Types of Variables c. C Keyword d. Types of C

More information

Sources: On the Web: Slides will be available on:

Sources: On the Web: Slides will be available on: C programming Introduction The basics of algorithms Structure of a C code, compilation step Constant, variable type, variable scope Expression and operators: assignment, arithmetic operators, comparison,

More information

Introduction to Java Applications. 2005 Pearson Education, Inc. All rights reserved.

Introduction to Java Applications. 2005 Pearson Education, Inc. All rights reserved. 1 2 Introduction to Java Applications 2.2 First Program in Java: Printing a Line of Text 2 Application Executes when you use the java command to launch the Java Virtual Machine (JVM) Sample program Displays

More information

INTRODUCTION TO FLOWCHARTING

INTRODUCTION TO FLOWCHARTING CHAPTER 1 INTRODUCTION TO FLOWCHARTING 1.0 Objectives 1.1 Introduction 1.2 Flowcharts 1.3 Types of Flowcharts 1.3.1 Types of flowchart 1.3.2 System flowcharts 1.4 Flowchart Symbols 1.5 Advantages of Flowcharts

More information

C A short introduction

C A short introduction About these lectures C A short introduction Stefan Johansson Department of Computing Science Umeå University Objectives Give a short introduction to C and the C programming environment in Linux/Unix Go

More information

1. Constants. 2. Variables. 3. Reserved words or key words. 4. Constants. Character set in C

1. Constants. 2. Variables. 3. Reserved words or key words. 4. Constants. Character set in C Character set in C We should use only the following characters in writing a C program. These characters can be combined to create C words. Alphabet: A, B, C, D.. Z, a, b, c, d..z Numeric digits: 0, 1,

More information

CP Lab 2: Writing programs for simple arithmetic problems

CP Lab 2: Writing programs for simple arithmetic problems Computer Programming (CP) Lab 2, 2015/16 1 CP Lab 2: Writing programs for simple arithmetic problems Instructions The purpose of this Lab is to guide you through a series of simple programming problems,

More information

MIT Aurangabad FE Computer Engineering

MIT Aurangabad FE Computer Engineering MIT Aurangabad FE Computer Engineering Unit 1: Introduction to C 1. The symbol # is called a. Header file c. include b. Preprocessor d. semicolon 2. The size of integer number is limited to a. -32768 to

More information

Huazhong University of Science and Technology JAVA Programming Language Lecture 2:Variables and Data Types

Huazhong University of Science and Technology JAVA Programming Language Lecture 2:Variables and Data Types JAVA Programming Language Lecture 2:Variables and Data Types Chengwei Zhang ( 张成伟 ) School of Electronic Information and Communications Huazhong University of Science and Technology Mar. 2015 Outline Quick

More information

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Chapter 2: Basic Elements of C++ Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates

More information

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C Embedded Systems A Review of ANSI C and Considerations for Embedded C Programming Dr. Jeff Jackson Lecture 2-1 Review of ANSI C Topics Basic features of C C fundamentals Basic data types Expressions Selection

More information

Introduction to C++ Programming

Introduction to C++ Programming Introduction to C++ Programming 1 Outline Introduction to C++ Programming A Simple Program: Printing a Line of Text Another Simple Program: Adding Two Integers Memory Concepts Arithmetic Decision Making:

More information

Outline. Compiling, interpreting, and running. The C Programming Language. Java vs. C. Administrative trivia Goals of the class Introduction to C

Outline. Compiling, interpreting, and running. The C Programming Language. Java vs. C. Administrative trivia Goals of the class Introduction to C Outline ompiling, interpreting, and running Administrative trivia Goals of the class Introduction to 1 2 The Programming Language Java vs. Systems programming language Originally used to write Unix and

More information

Outline TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA. What is DrJava? Dr Java

Outline TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA. What is DrJava? Dr Java 1 Outline TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson, and instructor materials prepared

More information

Introduction to C Language

Introduction to C Language Introduction to C Language A look at C We are going to learn a language called C. C is a very flexible and powerful programming language originally designed in the early 1970s. It is famous as the language

More information

Reading Assignment. Main Program in C. K.N. King Chapter 2. K.N. King Chapter 3. K.N. King Chapter 4. K.N. King Chapter 7. Our first C program

Reading Assignment. Main Program in C. K.N. King Chapter 2. K.N. King Chapter 3. K.N. King Chapter 4. K.N. King Chapter 7. Our first C program Reading Assignment Main Program in C In C the main program is a function called main The body of the function is enclosed in left ( ) and right ( ) curly braces. K.N. King Chapter 2 K.N. King Chapter 3

More information

3) Some coders debug their programs by placing comment symbols on some codes instead of deleting it. How does this aid in debugging?

3) Some coders debug their programs by placing comment symbols on some codes instead of deleting it. How does this aid in debugging? Freshers Club Important 100 C Interview Questions & Answers 1) How do you construct an increment statement or decrement statement in C? There are actually two ways you can do this. One is to use the increment

More information

A First Book of C++ Chapter 2 Data Types, Declarations, and Displays

A First Book of C++ Chapter 2 Data Types, Declarations, and Displays A First Book of C++ Chapter 2 Data Types, Declarations, and Displays Objectives In this chapter, you will learn about: Data Types Arithmetic Operators Variables and Declarations Common Programming Errors

More information

Introduction to the C++ Programming Language

Introduction to the C++ Programming Language L E S S O N S E T 2 Introduction to the C++ Programming Language PURPOSE PROCEDURE 1. To briefly introduce the C++ programming language 2. To show the use of memory in programming 3. To introduce variables

More information

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program. Name: Class: Date: Exam #1 - Prep True/False Indicate whether the statement is true or false. 1. Programming is the process of writing a computer program in a language that the computer can respond to

More information

The Basics of C Programming. Marshall Brain

The Basics of C Programming. Marshall Brain The Basics of C Programming Marshall Brain Last updated: October 30, 2013 Contents 1 C programming 1 What is C?................................. 2 The simplest C program, I........................ 2 Spacing

More information

Chapter 18 I/O in C. Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Chapter 18 I/O in C. Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 18 I/O in C Standard C Library I/O commands are not included as part of the C language. Instead, they are part of the Standard C Library. A collection of functions and macros that must be implemented

More information

[Page 70 (continued)] a. are used to document a program and improve its readability. [Page 71]

[Page 70 (continued)] a. are used to document a program and improve its readability. [Page 71] [Page 70 (continued)] Exercises 2.7 Discuss the meaning of each of the following objects: a. std::cin b. std::cout 2.8 Fill in the blanks in each of the following: a. are used to document a program and

More information

BSc (Hons) Computer Science with Network Security, BSc (Hons) Business Information Systems, BSc (Hons) Mathematics & BSc (Hons) Software Engineering

BSc (Hons) Computer Science with Network Security, BSc (Hons) Business Information Systems, BSc (Hons) Mathematics & BSc (Hons) Software Engineering BSc (Hons) Computer Science with Network Security, BSc (Hons) Business Information Systems, BSc (Hons) Mathematics & BSc (Hons) Software Engineering Cohort: BCNS/07/FT- BIS/07/FT- BIS/06/FT -BM/07/FT BSE/07/FT/PT-

More information

6.087 Lecture 1 January 11, 2010

6.087 Lecture 1 January 11, 2010 6.087 Lecture 1 January 11, 2010 Introduction to C Writing C Programs Our First C Program 1 What is C? Dennis Ritchie AT&T Bell Laboratories 1972 16-bit DEC PDP-11 computer (right) Widely used today extends

More information

Chapter 13 - The Preprocessor

Chapter 13 - The Preprocessor Chapter 13 - The Preprocessor Outline 13.1 Introduction 13.2 The#include Preprocessor Directive 13.3 The#define Preprocessor Directive: Symbolic Constants 13.4 The#define Preprocessor Directive: Macros

More information

C Primer. Fall Introduction C vs. Java... 1

C Primer. Fall Introduction C vs. Java... 1 CS 33 Intro Computer Systems Doeppner C Primer Fall 2016 Contents 1 Introduction 1 1.1 C vs. Java.......................................... 1 2 Functions 1 2.1 The main() Function....................................

More information

The char Data Type. Character and String Processing. Another Example /* Capitalize all lowercase letters */ while ((c = getchar())!

The char Data Type. Character and String Processing. Another Example /* Capitalize all lowercase letters */ while ((c = getchar())! Character and String Processing CSE 130: Introduction to C Programming Spring 2005 The char Data Type A char value can be thought of as either a character or a small integer printf( %d, a ); /* prints

More information

7 Introduction to C++

7 Introduction to C++ 7 Introduction to C++ 7.1 Introduction C++ is an extension to C Programming language. It was developed at AT&T Bell Laboratories in the early 1980s by Bjarne Stroustrup. It is a deviation from traditional

More information

High-level programming (Ch 11-14)

High-level programming (Ch 11-14) High-level programming (Ch 11-14) hardware software H/w s/w interface Problems Algorithms Prog. Lang & Interfaces Instruction Set Architecture Microarchitecture (Organization) Circuits Devices (Transistors)

More information

Introduction to Programming Block Tutorial C/C++

Introduction to Programming Block Tutorial C/C++ Michael Bader Master s Program Computational Science and Engineering C/C++ Tutorial Overview From Maple to C Variables, Operators, Statements Functions: declaration, definition, parameters Arrays and Pointers

More information

Today s topics. Java programs. Java Virtual Machine (JVM) Bytecodes. HelloWorld.java javac HelloWorld.class

Today s topics. Java programs. Java Virtual Machine (JVM) Bytecodes. HelloWorld.java javac HelloWorld.class Today s topics Java programs Parsing Java Programming Notes from Tammy Bailey Reading Great Ideas, Chapter 3 & 4 Java programs are created as text files using a text editor (like emacs) Save to disk with.java

More information

OSMIC. CSoft ware. C Language manual. Rev Copyright COSMIC Software 1999, 2003 All rights reserved.

OSMIC. CSoft ware. C Language manual. Rev Copyright COSMIC Software 1999, 2003 All rights reserved. OSMIC CSoft ware C Language manual Rev. 1.1 Copyright COSMIC Software 1999, 2003 All rights reserved. Table of Contents Preface Chapter 1 Historical Introduction Chapter 2 C Language Overview C Files...2-1

More information

A Rudimentary Intro to C programming

A Rudimentary Intro to C programming A Rudimentary Intro to C programming Wayne Goddard School of Computing, Clemson University, 2008 Part 4: Strings and Pointers 18 Strings.................................... D1 19 String Functions..............................

More information

Branching. We ll continue to look at the basics of writing C++ programs including. Overview

Branching. We ll continue to look at the basics of writing C++ programs including. Overview Branching Overview We ll continue to look at the basics of writing C++ programs including Boolean Expressions (need to know this before we can using branching and looping!) Branching J.S. Bradbury CSCI

More information

Chapter 2. Values, Variables, and Types. 2.1 Java Values in DrJava s Interaction Pane

Chapter 2. Values, Variables, and Types. 2.1 Java Values in DrJava s Interaction Pane 14 Chapter 2 Values, Variables, and Types In this chapter we explore the primitive building blocks that are used to develop Java programs. We experiment with the following concepts within DrJava s interactive

More information

an array of 10 characters would require 10 bytes of storage for data. On the other hand, would require 10*sizeof(int) amount of storage.

an array of 10 characters would require 10 bytes of storage for data. On the other hand, would require 10*sizeof(int) amount of storage. Lecture 05 C Arrays & pointers In this lecture Introduction to 1D arrays Array representation, access and updates Passing arrays to functions Array as a const pointer Dynamic arrays and resizing Introduction

More information

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share. LING115 Lecture Note Session #4 Python (1) 1. Introduction As we have seen in previous sessions, we can use Linux shell commands to do simple text processing. We now know, for example, how to count words.

More information

GETTING STARTED WITH C++ C++ BASICS - 1 -

GETTING STARTED WITH C++ C++ BASICS - 1 - - 1 - GETTING STARTED WITH C++ Programming is a core activity in the process of performing tasks or solving problems with the aid of a computer. An idealised picture is: PROBLEM COMPUTER SOLUTION Unfortunately

More information

The Fundamentals of C++

The Fundamentals of C++ The Fundamentals of C++ Basic programming elements and concepts JPC and JWD 2002 McGraw-Hill, Inc. Program Organization Program statement Definition Declaration Action Executable unit Named set of program

More information

Each statement is ended by a semi-colon (;). The formatting is free.

Each statement is ended by a semi-colon (;). The formatting is free. Example 1 cout x; double err, tol=1e-12, x1; int it, maxit=100; it = 0; err = tol + 1; while( err > tol && it < maxit ) x1 = x - (x-cos(x))/(1+sin(x)); err = fabs(

More information

Fundamentals of Programming

Fundamentals of Programming Fundamentals of Programming Introduction to the C language Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa February 29, 2012 G. Lipari (Scuola Superiore Sant Anna) The C language

More information

First Java Programs. V. Paúl Pauca. CSC 111D Fall, 2015. Department of Computer Science Wake Forest University. Introduction to Computer Science

First Java Programs. V. Paúl Pauca. CSC 111D Fall, 2015. Department of Computer Science Wake Forest University. Introduction to Computer Science First Java Programs V. Paúl Pauca Department of Computer Science Wake Forest University CSC 111D Fall, 2015 Hello World revisited / 8/23/15 The f i r s t o b l i g a t o r y Java program @author Paul Pauca

More information

Chapter One Introduction to Programming

Chapter One Introduction to Programming Chapter One Introduction to Programming 1-1 Algorithm and Flowchart Algorithm is a step-by-step procedure for calculation. More precisely, algorithm is an effective method expressed as a finite list of

More information

MGM s JNEC Question Bank Subject: Computer Engineering

MGM s JNEC Question Bank Subject: Computer Engineering MGM s JNEC Question Bank Subject: Computer Engineering 1. All of the following are examples of computer input units EXCEPT: a) Scanner b) Speaker c) Bar code reader d) Keyboard Answer: b 2. Which of the

More information

The C Programming Language course syllabus associate level

The C Programming Language course syllabus associate level TECHNOLOGIES The C Programming Language course syllabus associate level Course description The course fully covers the basics of programming in the C programming language and demonstrates fundamental programming

More information

CS 241 Data Organization Coding Standards

CS 241 Data Organization Coding Standards CS 241 Data Organization Coding Standards Brooke Chenoweth University of New Mexico Spring 2016 CS-241 Coding Standards All projects and labs must follow the great and hallowed CS-241 coding standards.

More information

198:211 Computer Architecture

198:211 Computer Architecture 198:211 Computer Architecture Topics: Lecture 8 (W5) Fall 2012 Data representation 2.1 and 2.2 of the book Floating point 2.4 of the book 1 Computer Architecture What do computers do? Manipulate stored

More information

CS101 Computer programming and utilization

CS101 Computer programming and utilization CS101 Computer programming and utilization Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal Rekhi Building IIT Bombay Lecture 3, Basic features of C++ Tuesday, 2 August 2011

More information

arrays C Programming Language - Arrays

arrays C Programming Language - Arrays arrays So far, we have been using only scalar variables scalar meaning a variable with a single value But many things require a set of related values coordinates or vectors require 3 (or 2, or 4, or more)

More information

Variables and constants linked with operators. Uses relational and logical operators Evaluates to 1 or 0 (true or false) only

Variables and constants linked with operators. Uses relational and logical operators Evaluates to 1 or 0 (true or false) only Expressions 1 Expressions Variables and constants linked with operators Arithmetic expressions Uses arithmetic operators Can evaluate to any value Logical expressions Uses relational and logical operators

More information

Convert Temperature CMPE-013/L. Constants and Literals. The Code #include Gabriel Hugh Elkaim Winter 2014

Convert Temperature CMPE-013/L. Constants and Literals. The Code #include <stdio.h> Gabriel Hugh Elkaim Winter 2014 CMPE-013/L Constants and Literals Gabriel Hugh Elkaim Winter 2014 Convert Temperature Want to write a program to convert the temperature from Fahrenheit to Celsius for a range of temperatures. Demonstrates

More information

()A Crash Course in Programming with C++ and the Ubuntu O.S. June 8, / 47

()A Crash Course in Programming with C++ and the Ubuntu O.S. June 8, / 47 A Crash Course in Programming with C++ and the Ubuntu O.S. Dr. Daniel A. Ray MCS Dept UVa-Wise June 8, 2010 ()A Crash Course in Programming with C++ and the Ubuntu O.S. June 8, 2010 1 / 47 Outline What

More information

INTI COLLEGE MALAYSIA

INTI COLLEGE MALAYSIA CSC112 (F) / Page 1 of 5 INTI COLLEGE MALAYSIA CERTIFICATE IN COMPUTING AND INFORMATION TECHNOLOGY PROGRAMME CSC 112 : FUNDAMENTALS OF PROGRAMMING FINAL EXAMINATION : DECEMBER 2002 SESSION This paper consists

More information

CIS 190: C/C++ Programming. Lecture 1 Introduction and Getting Started

CIS 190: C/C++ Programming. Lecture 1 Introduction and Getting Started CIS 190: C/C++ Programming Lecture 1 Introduction and Getting Started This course will teach you the basics of C and C++ give you more programming experience be appropriate for majors and non-majors not

More information

Introduction to Visual C++.NET Programming. Using.NET Environment

Introduction to Visual C++.NET Programming. Using.NET Environment ECE 114-2 Introduction to Visual C++.NET Programming Dr. Z. Aliyazicioglu Cal Poly Pomona Electrical & Computer Engineering Cal Poly Pomona Electrical & Computer Engineering 1 Using.NET Environment Start

More information

Solution Exercise 1.1

Solution Exercise 1.1 Solution Exercise 1.1 Part I Fill in the blanks with the most appropriate answers. 1. A Program is a sequence of step-by-step instructions that a computer executes in order to solve a problem or to perform

More information

Introduction to Python

Introduction to Python WEEK ONE Introduction to Python Python is such a simple language to learn that we can throw away the manual and start with an example. Traditionally, the first program to write in any programming language

More information

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters The char Type ASCII Encoding The C char type stores small integers. It is usually 8 bits. char variables guaranteed to be able to hold integers 0.. +127. char variables mostly used to store characters

More information

Chapter 4 Intro to Programming in C++

Chapter 4 Intro to Programming in C++ Chapter 4 Intro to Programming in C++ 4-1 Computer Programming Earlier we defined an algorithm to be a step by step process for solving a problem. Computer programming is the process of implementing the

More information

Problem Solving With C++ Ninth Edition

Problem Solving With C++ Ninth Edition CISC 1600/1610 Computer Science I Programming in C++ Professor Daniel Leeds dleeds@fordham.edu JMH 328A Introduction to programming with C++ Learn Fundamental programming concepts Key techniques Basic

More information

Syntax and logic errors

Syntax and logic errors Syntax and logic errors Teacher s Notes Lesson Plan Length 60 mins Specification Link 2.1.7/p_q Learning objective Candidates should be able to: (a) describe syntax errors and logic errors which may occur

More information

Lecture 1 Notes: Introduction

Lecture 1 Notes: Introduction Introduction to C++ January 4, 2011 Massachusetts Institute of Technology 6.096 Lecture 1 Notes: Introduction 1 Compiled Languages and C++ 1.1 Why Use a Language Like C++? At its core, a computer is just

More information

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner. Handout 1 CS603 Object-Oriented Programming Fall 15 Page 1 of 11 Handout 1 Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner. Java

More information

Introduction to Computer Science. Programming in C. Dr inŝ. Lidia Jackowska - Strumiłło. Computer Engineering Department TECHNICAL UNIVERSITY OF LODZ

Introduction to Computer Science. Programming in C. Dr inŝ. Lidia Jackowska - Strumiłło. Computer Engineering Department TECHNICAL UNIVERSITY OF LODZ Introduction to Computer Science Programming in C Dr inŝ. Lidia Jackowska - Strumiłło Computer Engineering Department TECHNICAL UNIVERSITY OF LODZ C language features C is a very effective programming

More information

Which of the following is the correct operator to compare two variables? A. := B. = C. equal D. ==

Which of the following is the correct operator to compare two variables? A. := B. = C. equal D. == Group 1 Question 1. What is the correct value to return to the operating system upon the successful completion of a program? A. -1 B. 1 C. 0 D. Programs do not return a value. Question 2. What is the only

More information

Basic Common Unix commands: Change to directory d

Basic Common Unix commands: Change to directory d Basic Common Unix commands: cd d Change to directory d mkdir d rmdir d mv f1 [f2...] d mv d1 d2 ls [d] [f...] ls -1 [f...] vi [f] emacs [f] more f cp f1 f2 mv f1 f2 rm f gcc [-o f1] f2 gnuplot Create new

More information

Variables and Constants. Variable A location in computer's memory in which a value may be stored from which that value may be retrieved

Variables and Constants. Variable A location in computer's memory in which a value may be stored from which that value may be retrieved Variables and Constants Variable A location in computer's memory in which a value may be stored from which that value may be retrieved Storage of Variables Setting Aside Memory Defining a variable in C++

More information

C programming: exercise sheet L2-STUE (2011-2012)

C programming: exercise sheet L2-STUE (2011-2012) C programming: exercise sheet L2-STUE (2011-2012) Algorithms and Flowcharts Exercise 1: comparison Write the flowchart and associated algorithm that compare two numbers a and b. Exercise 2: 2 nd order

More information

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T) Unit- I Introduction to c Language: C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating

More information