C Class01
C Class01
Day - 01
C Programming
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"); }
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*/
no
Steps in learning C
Alphabets Digits Special-symbols Constants Variables Keywords Instruction Program
13
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
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
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