Fundamentals of C Programming

Similar documents
Intro to Computer Programming (ICP) Rab Nawaz Jadoon

Data Types and Variables in C language

C Language, Token, Keywords, Constant, variable

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

IECD Institute for Entrepreneurship and Career Development Bharathidasan University, Tiruchirappalli 23.

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

BSM540 Basics of C Language

DEPARTMENT OF MATHS, MJ COLLEGE

BASIC ELEMENTS OF A COMPUTER PROGRAM

Today. o main function. o cout object. o Allocate space for data to be used in the program. o The data can be changed

Basic Elements of C. Staff Incharge: S.Sasirekha

Week 3 Lecture 2. Types Constants and Variables

Fundamentals of Programming Session 4

Computer System and programming in C

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

These are reserved words of the C language. For example int, float, if, else, for, while etc.

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

Fundamentals of Programming. Lecture 3: Introduction to C Programming

Fundamental of Programming (C)

ME 172. Lecture 2. Data Types and Modifier 3/7/2011. variables scanf() printf() Basic data types are. Modifiers. char int float double

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

Standard 11. Lesson 9. Introduction to C++( Up to Operators) 2. List any two benefits of learning C++?(Any two points)

Basics of Programming

BCA-105 C Language What is C? History of C

INTRODUCTION 1 AND REVIEW

CS102: Variables and Expressions

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

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

CS149: Elements of Computer Science. Fundamental C++ objects

Fundamentals of Programming

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

ANSI C Programming Simple Programs

Creating a C++ Program

Computer Programming : C++

BTE2313. Chapter 2: Introduction to C++ Programming

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

Chapter 2 THE STRUCTURE OF C LANGUAGE

Chapter 2: Overview of C++

Programming for Engineers Introduction to C

Programming and Data Structures

Have the same meaning as variables in algebra Single alphabetic character Each variable needs an identifier that distinguishes it from the others a =

Full file at C How to Program, 6/e Multiple Choice Test Bank

Fundamental of C programming. - Ompal Singh

VARIABLES AND CONSTANTS

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

CHAPTER-6 GETTING STARTED WITH C++

CSc Introduction to Computing

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

MATVEC: MATRIX-VECTOR COMPUTATION LANGUAGE REFERENCE MANUAL. John C. Murphy jcm2105 Programming Languages and Translators Professor Stephen Edwards

CS242 COMPUTER PROGRAMMING

Unit. Programming Fundamentals. School of Science and Technology INTRODUCTION

Our Strategy for Learning Fortran 90

Full file at

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Variables and Literals

Arithmetic Expressions in C

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

C - Basic Introduction

Preview from Notesale.co.uk Page 6 of 52

Lecture 2 Tao Wang 1

Programming in C++ 4. The lexical basis of C++

Faculty: Pushpendra K Rajput Session:

2. Numbers In, Numbers Out

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

COMPUTER SCIENCE HIGHER SECONDARY FIRST YEAR. VOLUME II - CHAPTER 10 PROBLEM SOLVING TECHNIQUES AND C PROGRAMMING 1,2,3 & 5 MARKS

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

Programming in C++ 6. Floating point data types

Datatypes, Variables, and Operations

Differentiate Between Keywords and Identifiers

Number Systems. Binary Numbers. Appendix. Decimal notation represents numbers as powers of 10, for example

ADARSH VIDYA KENDRA NAGERCOIL COMPUTER SCIENCE. Grade: IX C++ PROGRAMMING. Department of Computer Science 1

Use of scanf. scanf("%d", &number);

Lab Session # 1 Introduction to C Language. ALQUDS University Department of Computer Engineering

Chapter 2. Lexical Elements & Operators

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

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

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

Mirage. Language Reference Manual. Image drawn using Mirage 1.1. Columbia University COMS W4115 Programming Languages and Translators Fall 2006

CCE RR REVISED & UN-REVISED KARNATAKA SECONDARY EDUCATION EXAMINATION BOARD, MALLESWARAM, BANGALORE G È.G È.G È..

C: How to Program. Week /Mar/05

CC112 Structured Programming

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Basic Science and Humanities

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

Presented By : Gaurav Juneja

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

Ex: If you use a program to record sales, you will want to remember data:

I Internal Examination Sept Class: - BCA I Subject: - Principles of Programming Lang. (BCA 104) MM: 40 Set: A Time: 1 ½ Hrs.

bphanikrishna.wordpress.com

Integer Representation. Variables. Real Representation. Integer Overflow/Underflow

C Programming Multiple. Choice

AMCAT Automata Coding Sample Questions And Answers

Data Types. Data Types. Integer Types. Signed Integers

C-Language Tutorial. Apple Macintosh system software was formerly written in Pascal, but is now almost always written in C.

2. Numbers In, Numbers Out

Chapter 2 - Introduction to C Programming

Java Notes. 10th ICSE. Saravanan Ganesh

Transcription:

Introduction, Constants, variables, keywords, Comments, I/O Functions, Data types, Compilation & Execution Tejalal Choudhary tejalal.choudhary@gmail.com, tejalal.choudhary@sdbct.ac.in Department of Computer Science & Engineering Sushila Devi Bansal College of Technology, Indore

Program It is the simplification of the computations Set of instructions, which when executed provides the desired functions, features and performance. Programming Language: Is the notation for writing the program. What is C C is a programming language developed at AT & T s Bell Laboratories of USA in 1972. It was designed and written by a man named Dennis Ritchie. Steps for learning C The C character set A character denotes any alphabet, digit or special symbol used to represent information. Below table shows the valid alphabets, numbers and special symbols allowed in C. Prepared by: Tejalal Choudhary, Asst. Prof. Page 2

The alphabets, numbers and special symbols when properly combined form constants, variables and keywords. Constants A constant is an entity that doesn t change. For example 10, 20 and 30 are integer constants, where 10.5 is the floating point constant and M is the character constant. Rules for Constructing Integer Constants An integer constant must have at least one digit. It must not have a decimal point. It can be either positive or negative. If no sign precedes an integer constant it is assumed to be positive. No commas or blanks are allowed within an integer constant. The range (minimum and maximum value) of an Integer constant depends upon the compiler. Rules for Constructing Real Constants Real constants are often called Floating Point constants. The real constants could be written in two forms Fractional form and Exponential form. Following rules must be observed while constructing real constants expressed in fractional form: A real constant must have at least one digit. It must have a decimal point (for example 10.5). It could be either positive or negative. Default sign is positive. No commas or blanks are allowed within a real constant. In exponential form of representation, the real constant is represented in two parts. The part appearing before e is called mantissa, whereas the part following e is called exponent. Following rules must be observed while constructing real constants expressed in exponential form: The mantissa part and the exponential part should be separated by a letter e. The mantissa part may have a positive or negative sign. Default sign of mantissa part is positive. The exponent must have at least one digit, which must be a positive or negative integer. Default sign is positive. Range of real constants expressed in exponential form is -3.4e38 to 3.4e38. Ex.: +3.2e-5 4.1e8-0.2e+3 Prepared by: Tejalal Choudhary, Asst. Prof. Page 3

Rules for Constructing Character Constants A character constant is a single alphabet, a single digit or a single special symbol enclosed within single inverted commas. Both the inverted commas should point to the left. For example, A is a valid character constant whereas A is not. The maximum length of a character constant can be 1 character. Ex.: 'A' 'I' '5' '=' Variable A variable is an entity that may change; variable is a named memory location. Variable names are names given to locations in memory. These locations can contain integer, real or character constants. In any language, the types of variables that it can support depend on the types of constants that it can handle. This is because a particular type of variable can hold only the same type of constant. For example, an integer variable can hold only an integer constant, a real variable can hold only a real constant and a character variable can hold only a character constant. Rules for making variables For constructing variable names of all types the same set of rules apply these are: A variable name is any combination of 1 to 31 alphabets, digits or underscores. Some compilers allow variable names whose length could be up to 247 characters. The first character in the variable name must be an alphabet or underscore. No commas or blanks are allowed within a variable name. No special symbol other than an underscore (as in gross_sal) can be used in a variable name. Do not create unnecessarily long variable names as it adds to your typing effort. Do not create unnecessarily long variable names as it adds to your typing effort. Variable names should be meaningful. Variable names are case sensitive. The general syntax for variable declaration is: data-type variable-name; Prepared by: Tejalal Choudhary, Asst. Prof. Page 4

Keywords Keywords are the words whose meaning has already been explained to the C compiler. The keywords cannot be used as variable names because if we do so we are trying to assign a new meaning to the keyword, which is not allowed by the computer. The keywords are also called Reserved words. There are only 32 keywords available in C Language these are: Following rules that are applicable to all C programs: Each instruction in a C program is written as a separate statement. Therefore a complete C program would comprise of a series of statements. The statements in a program must appear in the same order in which we wish them to be executed; Blank spaces may be inserted between two words to improve the readability of the statement. However, no blank spaces are allowed within a variable, constant or keyword. Every C statement must end with a semicolon ( ; ) semicolon acts as a statement terminator. Comments The normal language rules do not apply to text written within comments Thus we can type this text in small case, capital or a combination. This is because the comments are solely given for the understanding of the programmer or the fellow programmers and Comments are completely ignored by the compiler. Prepared by: Tejalal Choudhary, Asst. Prof. Page 5

Often programmers seem to ignore writing of comments. But when a team is building big software well commented code is almost essential for other team members to understand it. Single line comment //this is a single line comment Multi line comment /* */ this is a multi line comment Data types Data types specify the maximum and minimum value (range) that can be store in a particular variable of that type The number of bytes needed to store the variables value. Different format specifiers used for each data type. Prepared by: Tejalal Choudhary, Asst. Prof. Page 6

Input/Output(I/O) functions C does not contain any instruction to display output on the screen. All output to screen is achieved using readymade library functions. One such function is printf( ). The general form of printf( ) function is, printf ( "<format string>", <list of variables> ) ; <format string> can contain, %f for printing floating point values %d for printing integer values %c for printing character values int r=10; For example to print the value of above variable r we can write: printf( Value of r =%d,r); ww can also print the value of multiple variable in a single printf() function. int a=10, b=20; printf( Value of a =%d, value of b=%d,a,b); printf( ) can not only print values of variables, it can also print the result of an expression. An expression is nothing but a valid combination of constants, variables and operators. Thus, 3, 3 + 2, c and a + b * c d all are valid expressions. The results of these expressions can be printed as shown below: printf ( "%d %d %d %d", 3, 3 + 2, c, a + b * c d ) ; Note that 3 and c also represent valid expressions. Prepared by: Tejalal Choudhary, Asst. Prof. Page 7

Receiving Input from the user Fundamentals of C Programming To make the program general the program itself should ask the user to supply the values of p, n and r through the keyboard during execution. This can be achieved using a function called scanf( ). This function is a counter-part of the printf( ) function. printf( ) outputs the values to the screen whereas scanf( ) receives them from the keyboard. int r; printf( Enter radius of circle: ); scanf( %d, &r); we can also read values of more than variable in a single scanf() function. i.e int a,b; printff( Enter value of a and b ); scanf( %d%d,&a, &b); Note that the ampersand (&) before the variables in the scanf( ) function is a must. & is an Address of operator. It gives the location number used by the variable in memory. When we say &a, we are telling scanf( ) at which memory location should it store the value supplied by the user from the keyboard. Compilation and execution Assuming that you are using a Turbo C or Turbo C++ compiler here are the steps that you need to follow to compile and execute your first C program Select New from the File menu. Type the program. Save the program using F2 under a proper name (say Program1.c) Use Alt + F9 to compile the program. Use Ctrl + F9 to execute the program. Use Alt + F5 to view the output. Prepared by: Tejalal Choudhary, Asst. Prof. Page 8

Program 1: To find the sum of two entered numbers Output: Prepared by: Tejalal Choudhary, Asst. Prof. Page 9