Creating, Compiling and Executing

Similar documents
C Fundamentals & Formatted Input/Output. adopted from KNK C Programming : A Modern Approach

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

COP 3275: Chapter 02. Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA

27-Sep CSCI 2132 Software Development Lecture 10: Formatted Input and Output. Faculty of Computer Science, Dalhousie University. Lecture 10 p.

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Pointers as Arguments

Differentiate Between Keywords and Identifiers

A Fast Review of C Essentials Part I

C Language, Token, Keywords, Constant, variable

C: How to Program. Week /Mar/05

Variables in C. Variables in C. What Are Variables in C? CMSC 104, Fall 2012 John Y. Park

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

Procedures, Parameters, Values and Variables. Steven R. Bagley

Chapter 2 - Introduction to C Programming

3/13/2012. ESc101: Introduction to Computers and Programming Languages

ANSI C Programming Simple Programs

6.096 Introduction to C++ January (IAP) 2009

Fundamental of Programming (C)

Fundamentals of Programming

Programming. Data Structure

PROGRAMMAZIONE I A.A. 2017/2018

Presented By : Gaurav Juneja

Variables in C. CMSC 104, Spring 2014 Christopher S. Marron. (thanks to John Park for slides) Tuesday, February 18, 14

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

CS16 Exam #1 7/17/ Minutes 100 Points total

BLM2031 Structured Programming. Zeyneb KURT

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

Week 2 C Fundamentals; Formatted Input/Output; int and float Types

CS-211 Fall 2017 Test 1 Version Practice For Test on Oct. 2, Name:

Chapter 2: Overview of C. Problem Solving & Program Design in C

Arithmetic Expressions in C

Lecture 3: C Programm

Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and

CIS 2107 Computer Systems and Low-Level Programming Fall 2011 Midterm

A3-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH 'C' LANGUAGE

Syntax and Variables

Name :. Roll No. :... Invigilator s Signature : INTRODUCTION TO PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70

Data Types. Data Types. Integer Types. Signed Integers

Declaration. Fundamental Data Types. Modifying the Basic Types. Basic Data Types. All variables must be declared before being used.

!"#$% &'($) *+!$ 0!'" 0+'&"$.&0-2$ 10.+3&2),&/3+, %&&/3+, C,-"!.&/+"*0.&('1 :2 %*10% *%7)/ 30'&. 0% /4%./

CSCI 2132 Software Development. Lecture 8: Introduction to C

Introduction to C An overview of the programming language C, syntax, data types and input/output

CSCI 2132: Software Development. Norbert Zeh. Faculty of Computer Science Dalhousie University. Introduction to C. Winter 2019

Fundamentals of Programming. Lecture 3: Introduction to C Programming

Chapter 1 & 2 Introduction to C Language

.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar..

Chapter 3. Section 3.10 Type of Expressions and Automatic Conversion. CS 50 Hathairat Rattanasook

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

Programming for Engineers Introduction to C

A Crash Course in C. Steven Reeves

Introduction to C. Systems Programming Concepts

C Programming Review CSC 4320/6320

Lecture 2: C Programming Basic

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants

C-Programming. CSC209: Software Tools and Systems Programming. Paul Vrbik. University of Toronto Mississauga

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

Fundamentals of Programming Session 4

Data Types and Variables in C language

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

BSM540 Basics of C Language

CS-211 Fall 2017 Test 1 Version A Oct. 2, Name:

Day05 A. Young W. Lim Sat. Young W. Lim Day05 A Sat 1 / 14

Basic Elements of C. Staff Incharge: S.Sasirekha

Introduction to Computing Lecture 01: Introduction to C

Lecture 03 Bits, Bytes and Data Types

Computer Systems Programming. Practice Midterm. Name:

First of all, it is a variable, just like other variables you studied

AMCAT Automata Coding Sample Questions And Answers

Intermediate Programming, Spring 2017*

Midterm Exam. CSCI 2132: Software Development. March 4, Marks. Question 1 (10) Question 2 (10) Question 3 (10) Question 4 (10) Question 5 (5)

Lecture 02 C FUNDAMENTALS

Programming and Data Structures

Programming. C++ Basics

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

Basic Assignment and Arithmetic Operators

IEEE 754 Floating-Point Format

Approximately a Final Exam CPSC 206

Creating a C++ Program

Data types, variables, constants

COMP s1 Lecture 1

SU2017. LAB 1 (May 4/9) Introduction to C, Function Declaration vs. Definition, Basic I/O (scanf/printf, getchar/putchar)

MA 511: Computer Programming Lecture 2: Partha Sarathi Mandal

printf( Please enter another number: ); scanf( %d, &num2);

PRINCIPLES OF OPERATING SYSTEMS

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

C - Basics, Bitwise Operator. Zhaoguo Wang

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 14

DEPARTMENT OF MATHS, MJ COLLEGE

DETAILED SYLLABUS INTRODUCTION TO C LANGUAGE

The component base of C language. Nguyễn Dũng Faculty of IT Hue College of Science

Lecture 3. More About C

Subject: Fundamental of Computer Programming 2068

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Introduction to C programming. By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE

University of Kelaniya Sri Lanka

Topic 6: A Quick Intro To C

1/25/2018. ECE 220: Computer Systems & Programming. Write Output Using printf. Use Backslash to Include Special ASCII Characters

Decision Making -Branching. Class Incharge: S. Sasirekha

Transcription:

Shell Commands & Vi Compiling C programs Creating, Compiling and Executing Creating program #i n c l u d e <s t d i o. h> i n t main ( ) { p r i n t f ( AA\n ) ; p r i n t f ( A A\n ) ; p r i n t f ( A A\n ) ; p r i n t f ( A A\n ) ; p r i n t f ( AAAAAAAAAA\n ) ; p r i n t f ( A A\n ) ; p r i n t f ( A A\n ) ; p r i n t f ( A A\n ) ; } R. K. Ghosh (IIT-Kanpur) C Programming January 5, 2011 10 / 25

Shell Commands & Vi Compiling C programs Creating, Compiling and Executing Compiling and Executing Name the above program as biga.c Compile the program by gcc -o biga biga.c It produces a file named biga in your directory. Now execute it as./biga in your directory. If -o switch is not used, i.e., you use gcc biga default object file a.out will be produced. R. K. Ghosh (IIT-Kanpur) C Programming January 5, 2011 10 / 25

Shell Commands & Vi Compiling C programs Creating, Compiling and Executing Example 1 #i n c l u d e <s t d i o. h> i n t main ( ) { // Adds two numbers, d e c l a r e b e f o r e use. i n t f i r s t, second ; / We a r e y e t to l e a r n about v a r i a b l e s and t y p e s. But use two i n t v a r i a b l e s f i r s t and second h e r e. / } p r i n t f ( E n t e r two i n t e g e r s : ) ; s c a n f ( %d %d, &f i r s t, &second ) : p r i n t f ( The two numbers a r e : %d %d\n, f i r s t, second ) ; p r i n t f ( T h e i r sum : %d\n, f i r s t + second ) ; R. K. Ghosh (IIT-Kanpur) C Programming January 5, 2011 10 / 25

Shell Commands & Vi Compiling C programs Creating, Compiling and Executing Compiling and Executing Name the above program as sumtwonumbers.c Compile the program by gcc -o sumtwonumbers sumtwonumbers.c It produces a file named sumtwonumbers in your directory. Now execute it as./sumtwonumbers in your directory. Default object file a.out gets produced if -o not used. We talk more about other simple compiler switches when needed. R. K. Ghosh (IIT-Kanpur) C Programming January 5, 2011 10 / 25

Comment & Preprocessor A C program is preprocessed before compilation. Preprocessor commands are directives which start with a # character. Eg., C s standard I/O library has number of headers like <stdio.h>. Comments are essential for annotating programs. A comment in a single line: // Comment A multiline comment: /* Winged comments */

Functions Functions form building blocks of C programs. Function is block of statements provided with a name. A function computes a value, and returns it. C program can consists of several functions but main() is mandatory. main() returns a value (status code) for OS. Program still terminates even if there is no return in main(). Two types of functions: Library functions, User defined functions

Comments & Printing Strings #i n c l u d e <s t d i o. h> i n t main ( ) { p r i n t f ( H e l l o ) ; / F o r g o t to c l o s e comment.... p r i n t f ( Everybody ) ; p r i n t f ( i n L15 \n ) ; / Did not f o r g e t to c l o s e / p r i n t f ( World \n ) ; }

Variables Most programs will need a sequence of computations. Storing of temporary result is important. Storage locations are given symbolic names and known as variable Each variable has a type (defining kind of data) associated with.

Types Some native types: int, float, double, char The largest value int can store is 2,147,483,647. The smallest value int can store is -2,147,483,647. float can store larger than an int variable. Sometime float variable gives an approximation to stored number, eg., 0.1 can be output as 0.09999999999999987.

Declaration of Variable Declaration of a variable should precede its use. A valid identifier: a sequence of one or more letters, digits or, but not a reserved word. letter = a... z A... Z digit = 0... 9 Identifier = [ ]letter + digit Integers = digit + Real number = digit.digit +

Reserved Words: Declaration auto, char, double, e x t e r n, f l o a t, i n t, long, r e g i s t e r, s h o r t, s i g n e d, s t a t i c, s t r u c t, t y p e d e f, union, unsigned, void, v o l a t i l e Reserved Words: Others break, case, c o n t i n u e, d e f a u l t, do, e l s e, f o r, goto, i f, r e t u r n, s i z e o f, s w i t c h, w h i l e

Assignments A value in a variable stored through assignment #i n c l u d e <s t d i o. h> #i n c l u d e <s t d i o. h> i n t main ( ) { i n t main ( ) { i n t h e i g h t ; h e i g h t = 8 ; i n t l e n g t h, b r e a d t h ; i n t h e i g h t ; // WRONG h e i g h t = 8 ; i n t l e n g t h, b r e d t h ; } } Mixing float with int in assignment is possible but not safe. Type conversion will be shortly. i n t i ; f l o a t f ; i = 7 2. 9 5 ; // s t o r e s 72 i n i f = 1 3 5 ; // s t o r e s 1 3 5. 0 i n f

Simple C programs Writing Simple Programs Output Use printf for display of current value of variable. printf("height = %d\n", height); %d is place holder for value to be filled by decimal. For float use %f which display number with 6 digits after decimal point. To force n digits after decimal point use %.nf R. K. Ghosh (IIT-Kanpur) C Programming January 5, 2011 12 / 25

Simple C programs Writing Simple Programs Example 2 #i n c l u d e <s t d i o. h> i n t main ( ) { i n t h e i g h t = 8 ; i n t l e n g t h = 3 2 ; i n t b r e a d t h = 1 6 ; i n t volume, w e i g h t ; // 166 = a l l o w a b l e c u b i c i n c h e s p e r pound volume = h e i g h t l e n g t h b r e a d t h ; w e i g h t = volume / 1 6 6 ; } p r i n t f ( w e i g h t o f t h e box = %d kg \n, w e i g h t ) ; R. K. Ghosh (IIT-Kanpur) C Programming January 5, 2011 12 / 25