Вы находитесь на странице: 1из 36

C

Day - 01

C Programming

Programming Language Classification


A Programming language (PL) is a set of instructions used to communicate with the computer The set of instruction written in a PL is known as program All PL language can be divided into three categories
2

Programming Languages
Problem Oriented or High Level Languages Machine Oriented or Low Level Languages Middle Level Language

Language Translators
These are special programs, which accepts the user program and convert to corresponding machine language instructions There are two types of translators
Compilers Interpreters
4

History of C Language
Developed at AT&T s Bell Labs of USA in 1972 Designed and written by Mr. Dennis Ritchie American National Standards Institute (ANSI) standardized the definition of the C language as ANSI C in 1988

Structure of C program

Include files Global variable and function declaration Main functions Function subprogram
6

C Programming - An Introduction
A SIMPLE C PROGRAM
The following program is written in the C programming language
#include<stdio.h> main( ) { printf("Programming in C is easy.\n"); }

Sample Program Output


Programming in C is easy.
7

A NOTE ABOUT C PROGRAMS


C is Case sensitive The C programs starting point is identified by the word main( ) The two braces, { and }, signify the begin and end segments of the program
8

A NOTE ABOUT C PROGRAMS


#include <stdio.h> is to allow the use of the printf statement to provide program output printf() is actually a function in C that is used for printing variables and text All C statements must be terminated by a semi-colon ( ; )

Structure of a C Program
Every C program consists of one or more functions. The program will always begin by executing main function. Each function must contain
Function heading. Declarations. A compound statement.
10

Comment
Comment should be enclosed between /* */ It is used to increase the readability of the program. Any number of comments can be given at any place in the program. Comment cannot be nested It can be split over more than one line
11

A sample C Program.
#include<stdio.h> /*include information about
standard library */ /* program to print a message*/

main( ) /* define a function named main that receives


argument values */ {

no

printf( ECE welcomes you \n ); /*main calls library


function printf to print this sequence of characters ; \n represents the newline character*/ }
12

Getting started with C


Communicating with a computer involves speaking the language the computer understands.
Steps in learning English language
Alphabets Words Sentences Paragraph

Steps in learning C
Alphabets Digits Special-symbols Constants Variables Keywords Instruction Program

13

The C character Set


A character denotes any alphabet, digit or special symbol used to represent information.
Alphabets A,B, . ,Y, Z a,b, .. ,y, z 0,1,2,3,4,5,6,7,8,9 ~!@#%^&*( )_-+= |\{}[] :;< > , .? /
14

Digits Special Symbols

Constants, Variable and keywords


The alphabets, numbers and special symbol when properly combined form constants, variables and keywords A constant is a quantity that doesn t change A variable is a name given to the location in memory where the constant is stored

15

Types of C Constants
C constants can be divided into two major categories
Primary Constants Secondary Constants
C Constants Primary Constants Integer Constant Real Constant Character Constant Secondary constants Array, Pointer Structure, Union
16

Enum example in C #include <stdio.h> int main() { enum Days{Sunday,Monday,Tuesday,Wednesday,Thursday,Frid ay,Saturday}; Days TheDay; int j = 0; printf("Please enter the day of the week (0 to 6)\n"); scanf("%d",&j); TheDay = Days(j); if(TheDay == Sunday || TheDay == Saturday) printf("Hurray it is the weekend\n"); else printf("Curses still at work\n"); return 0; }

17

Integer Constants
An integer constant must have at least one digit It must not have a decimal point It could 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
18

Real Constants
Real constants(RC) must have atleast one digit It must have a decimal point It could be either positive or negative Default sign is positive No commas or blank are allowed within a RC
19

Real Constants in exponential form The mantissa part and exponential part should be separated by a letter e The mantissa part may have a positive or negative sign Default sign of mantissa and exponent is positive The exponent mush have at least one digit which must be a positive or negative integer. Default sign is positive
20

Character Constants
A character constant is either a single alphabet, a single digit or a single special symbol enclosed within single inverted commas The maximum length of a character constant can be 1 character
Eg a , 1 , 5 , = (Valid) asd , 12 (Invalid)
21

Identifiers
Identifiers are names given to various program elements, such as variables, functions and arrays A variable name is any combination of alphabets, digits or underscores The first character in the variable name must be an alphabet

22

Keywords
Keywords are the words whose meaning has already been explained to the C compiler They cannot be used as variable names. There are only 32 keywords available in c
auto break case char const continue short double else enum extern float far void if int long near register return static struct switch typedef union unsigned do goto signed while default for
23

C Instructions
There are basically four types of instructions in C
Type declaration instruction
Eg int sum; float ave; char name,code;

Input/Output instruction
Eg scanf(), printf()
24

C Instructions
Arithmetic instruction
Eg : x = sum +2;

Control instruction
Eg : while do statement for statement if statement

25

Data Types C Supports several different types of data, each of which may be represented differently within the computers memory. Basic data types are listed below:
Data Type int char float double Description Typical Memory 2 bytes 1 bytes 4 bytes 8 bytes integer quantity single character floating point number double-precision floating point number

26

Escape Sequences in C
Certain non printing characters can be expressed in terms of escape sequences
Character Escape Sequence
\b \t \v \n \f \r \ \? \\ \0

ASCII Value
008 009 001 010 012 013 034 063 092 000
27

backspace horizontal tab vertical tab newline formfeed carriage return quotation mark ( ) question mark(?) backslash (\) null

String Constants
A String Constant consists of any number consecutive characters enclosed in double quotation marks Escape sequence can be embedded within the string.

28

First C Program
/* library function for input and output */ #include<stdio.h> main() { int a,b,sum; /*variable declaration*/ printf( enter the first number::\n ); scanf( %d ,&a); /*receiving data from user */ printf( enter the second no: \n ); scanf( %d ,&b); sum =a+b; /* calculating the sum*/ printf( the sum of two numbers: %d\n ,sum); }
29

Preparing and Running a Complete C Program. Planning a program


Design the algorithm for the given program Statements must be selected and sequenced in the most effective manner

Writing a C program
Translate the algorithm into equivalent C instructions Comments should be included within a C program A well written program should generate clear, legible output. The program should be user interactive
30

Entering, Compiling and Executing The program can be entered into the computer using an EDITOR. (In unix vi editor is used)
Eg . vi filename.c

Compiling and executing the program


cc filename.c
The object code a.out is generated The program can be executed by entering a.out in the prompt Program asks for required input and generates the output
31

Assignment 01
Q1 : Which of the following are invalid variable names and why ?
interestpaid si-int AVERAGE Percent. 123 dist in km ot pay Name FLOAT
32

Assignment 01
Q2 : Point out the errors, if any, in the following C statements:
int =314.562 *150 ; name = Ajay ; 3.14 * r*r = area; k=a*b+c(2.5a+b); m_inst =rate of interest * numberofyears /100; area = 3.14 * r **2;
33

Assignment 01
Q3 : A C program contains the following statements:
#include<stdio.h> int i,j,k;

write appropriate scanf and printf functions to enter and print the numerical values for i,j and k Q4 : Each new c instruction has to be written on a separate line. (True/false)
34

Assignment 01
Q5 : Write C programs for the following :
(a) Rajesh s basic salary is input through the keyboard. His dearness allowance is 40% of basic salary, and house rent allowance is 20 % of basic salary. Write a program to calculate his gross salary. (b) Two numbers are input through the keyboard into two locations C and D . Write a program to interchange the contents of C and D. using temporary variable. without using temporary variable
35

Assignment 01
(c) If the marks obtained by a student in five different subjects are input through the keyboard, find out the aggregate marks and percentage marks obtained by the student. Assume that the maximum marks that can be obtained by a student in each subject is 100. (d) The length and breadth of a rectangle and radius of a circle are input through the keyboard. Write a program to calculate the area and perimeter of the rectangle, and the area and circumference of the circle.
36

Вам также может понравиться