KEMBAR78
C Prog Unit 1 | PDF | C (Programming Language) | Computer Engineering
0% found this document useful (0 votes)
6 views17 pages

C Prog Unit 1

C is a programming language developed in 1972 by Dennis Ritchie at AT&T's Bell Laboratories, designed to address issues in previous languages and primarily used in UNIX operating systems. It features portability, fast execution, and a structured approach, evolving from earlier languages like ALGOL, BCPL, and B. The document also outlines the general structure of a C program, types of tokens, variables, constants, and data types, along with rules for identifiers and examples of programming syntax.

Uploaded by

palanikumar.p
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views17 pages

C Prog Unit 1

C is a programming language developed in 1972 by Dennis Ritchie at AT&T's Bell Laboratories, designed to address issues in previous languages and primarily used in UNIX operating systems. It features portability, fast execution, and a structured approach, evolving from earlier languages like ALGOL, BCPL, and B. The document also outlines the general structure of a C program, types of tokens, variables, constants, and data types, along with rules for identifiers and examples of programming syntax.

Uploaded by

palanikumar.p
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Unit I

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 Dennis Ritche. Dennis Ritchie is known as the founder of c
language.
 It was developed to overcome the problems of previous languages such as B, BCPL etc.
 Initially, C language was developed to be used in UNIX operating system.
Features of C

 Portability or machine independent

 Sound and versatile language

 Fast program execution.

 An extendible language.

 Tends to be a structured language.

Historical developments of C(Background)

 The root of all modern languages is ALGOL, introduced in the early 1960s. ALGOL was

the first computer language to use a block structure. Although it never became popular in

USA, it was widely used in Europe. ALGOL gave the concept of structured programming to

the computer science community. Computer scientists like Corrado Bohm, Guiseppe

Jacopini and Edsger Dijkstra popularized this concept during 1960s. Subsequently, several

languages were announced.

 In 1967, Martin Richards developed a language called BCPL (Basic Combined

Programming Language) primarily for writing system software. In 1970, Ken Thompson

created a language using many features of BCPL and called it simply B. B was used to

create early versions of UNIX operating system at Bell Laboratories. Both BCPL and B

were “typeless” system programming languages.

 C was evolved from ALGOL, BCPL and B by Dennis Ritchie at the Bell Laboratories in

1972. C uses many concepts from these languages and added the concept of data types and

1
other powerful features. Since it was developed along with the UNIX operating system, it is

strongly associated with UNIX. This operating system, which was also developed at Bell

Laboratories, was coded almost entirely in C. UNIX is one of the most popular network

operating systems in use today and the heart of the Internet data superhighway.

 For many years, C was used mainly in academic environments, but eventually with the

release of many C compilers for commercial use and the increasing popularity of UNIX, it

began to gain widespread support among computer professionals. Today, C is running under

a variety of operating system and hardware platforms.

 During 1970s, C had evolved into what is now known as “traditional C”. The language

became more popular after publication of the book „The C Programming Language‟ by

Brian Kerningham and Dennis Ritchie in 1978. The book was so popular that the language

came to be known as “K&R C” among the programming community. The rapid growth of C

led to the development of different versions of the language that were similar but often

incompatible. This posed a serious problem for system developers.

 To assure that the C language remains standard, in 1983, American National Standards

Institute (ANSI) appointed a technical committee to define a standard for C. The committee

approved a version of C in December 1989 which is now known as ANSI C. It was then

approved by the International Standards Organization (ISO) in 1990. This version of C is

also referred to as C89.

 During 1990‟s, C++, a language entirely based on C, underwent a number of improvements

and changes and became an ANSI/ISO approved language in November 1977. C++ added

several new features to C to make it not only a true object-oriented language but also a more

versatile language. During the same period, Sun Microsystems of USA created a new

language Java modelled on C and C++.

2
General Structure of a C program:

The general format is

/* Documentation section */
/* Link section */
/* Definition section */
/* Global declaration section */
main()
{
Declaration part
Executable part (statements)
}
/* Sub-program section */
 The documentation section is used for displaying any information about the

program like the purpose of the program, name of the author, date and time written etc, and

3
this section should be enclosed within comment lines. The statements in the documentation

section are ignored by the compiler.

 The link section consists of the inclusion of header files.

 The definition section consists of macro definitions, defining constants etc,.

Anything declared in the global declaration section is accessible throughout the program, i.e.

accessible to all the functions in the program.

 main() function is mandatory for any program and it includes two parts, the

declaration part and the executable part.

 The last section, i.e. sub-program section is optional and used when we require

including user defined functions in the program.

First C Program

To write the first c program, open the C console and write the following code:

#include <stdio.h>

#include <conio.h>

void main(){

printf("Hello C Language");

getch();

 #include <stdio.h> includes the standard input output library functions. The printf()

function is defined in stdio.h .

 #include <conio.h> includes the console input output library functions. The getch()

function is defined in conio.h file.

 void main() The main() function is the entry point of every program in c language. The

void keyword specifies that it returns no value.

 printf() The printf() function is used to print data on the console.

4
 getch() The getch() function asks for a single character. Until you press any key, it

blocks the screen.

C TOKENS

The smallest individual units are known as tokens. C has six types of tokens.

1. Identifiers
2. Keywords
3. Constants
4. Strings
5. Special Symbols
6. Operators
Identifiers

Identifiers refer to the names of variables, constants, functions and arrays. These are user-

defined names is called Identifiers. These identifier are defined against a set of rules.

Rules for an Identifier

 An Identifier can only have alphanumeric characters( a-z , A-Z , 0-9 ) and underscore( _ ).

 The first character of an identifier can only contain alphabet( a-z , A-Z ) or underscore ( _ ).

 Identifiers are also case sensitive in C. For example name and Name are two different

identifier in C.

 Keywords are not allowed to be used as Identifiers.

 No special characters, such as semicolon, period, whitespaces, slash or comma are

permitted to be used in or as Identifier.

 C compiler recognizes only the first 31 characters of an identifiers.

Example

Valid Invalid

STDNAME Return

SUB $stay

5
TOT_MARKS Roll Number

Y2K 1RECORD

keyword

A keyword is a reserved word. All keywords have fixed meaning that means we cannot change.

Keywords serve as basic building blocks for program statements. All keywords must be written in

lowercase. A list of 32 keywords in C language is given below:

Note that, Keywords we cannot use it as a variable name, constant name etc.

Constants

Constants refer to fixed values that do not change during the execution of a program.

6
Integer constants:

An integer constant is a numeric constant (associated with number) without any fractional or

exponential part. There are three types of integer constants in C programming:

 decimal constant(base 10)


 octal constant(base 8)
 hexadecimal constant(base 16)

For example:

 Decimal constants: 0, -9, 22 etc

 Octal constants: 021, 077, 033 etc

 Hexadecimal constants: 0x7f, 0x2a, 0x521 etc

In C programming, octal constant starts with a 0 and hexadecimal constant starts with a 0x.

Decimal Integer : the rules for represent decimal integer.

 Decimal Integer value which consist of digits from 0-9.

 Decimal Integer value with base 10.

 Decimal Integer should not prefix with 0.

 It allows only sign (+,-).

 No special character allowed in this integer.

For example,

Valid: 7 , 77, 777, -77

Invalid : $77 , 077

Octal : An integer constants with base 8 is called octal. These rules are :

 it consist of digits from 0 to 7.

 It should prefix with 0.

 It allows sign (+,-).

 No special character is allowed.

7
For example,

Valid: 0123 ,+0123, -0123

Invalid : 123, 0128

Hexadecimal : An integer constant with base value 16 is called Hexadecimal.

 It consist of digits from 0-9,a-f(capital letters & small leters.

Ex : 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

 it should prefix with 0X or 0x.

 it allows sign (+,-).

 No special character is allowed.

Example : OX1a, ox2f

Floating point/Real constants: A floating point constant is a numeric constant that has either a

fractional form or an exponent form. For example: -2.0, 0.0000234, -0.22E-5.

Note that, E-5 = 10-5

Real Constants : Real constant is base 10 number, which is represented in decimal 0r

scientific/exponential notation.

For example, real notation: 15.25, .75, 30. -9.52. -92, +.94.

Scientific/Exponential Notation: 1.5E-2, 100e+3, -2.05e2

Character Constant:

Single Character Constant : A character constant is either a single alphabet, a single digit, a

single special symbol enclosed within single inverted commas.

 it is value represent in „‟ (single quote).

 The maximam length of a character constant can be 1 character.

For example, Valid: „a‟, „A‟, „3‟ Invalid: „12‟, „ab‟

String constant : A string constant is a sequence of characters enclosed in double quote, the

characters may be letters, numbers, special characters and blank space etc.

8
For example, “rama” , “a” , “+123” , “1-/a”

"good" //string constant

"" //null string constant

" " //string constant of six white space

"x" //string constant having single character.

"Earth is round\n" //prints string with newline

Note that a character constant (e.g., „X‟) is not equivalent to the single character string constant.

Escape characters or backslash characters:


C supports some special backslash character constants that are used in output functions.

a) \n newline

b) \r carriage return

c) \t tab

d) \v vertical tab

e) \b backspace

f) \f form feed (page feed)

g) \a alert (beep)

h) \‟ single quote(„)

i) \” double quote(“)

j) \? Question mark (?)

k) \\ backslash (\)

Data Types/Types:

C language is rich in its data types. Storage representations and machine instructions to
handle constants differ from machine to machine. The variety of data types available allow the
programmer to select the type appropriate to the needs of the application as well as the machine.

9
There are 4 types of data types in C language.

Basic or Primary data type.

The basic data types are integer-based and floating-point based. C language supports both signed
and unsigned literals. The memory size of basic data types may change according to 32 or 64 bit
operating system.

10
Size and Ranges of Data Types with Type Qualifiers

Variables
A variable is a name of memory location. It is used to store data. Variables are changeable, we can

change value of a variable during execution of a program. . It can be reused many times.

Rules to write variable names:

They must begin with a letter. Some systems permit underscore as the first character.

ANSI standard recognizes a length of 31 characters. (In C99, at least 63 characters are

significant.)

Uppercase and lowercase are significant. That is, the variable Total is not the same as total

or TOTAL.

It should not be a keyword.

White space is not allowed.

For example, i, rank1, MAX, min, Student_name, StudentName, class_mark.

11
Declaration of Variables :

A variable can be used to store a value of any data type. The declaration of variables must be done

before they are used in the program. The syntax for declaring a variable is as follows:

Syntax : data_type v1,v2,……, vn;

v1, v2, ....vn are the names of variables. Variables are separated by commas.

A declaration statement must end with a semicolon.

For example, valid declarations are:

int x,y,z;

float a,b;

char m,n;

Assigning values to variables:

values can be assigned to variables using the assignment operator (=). The general format

statement is :

Syntax : variable = constant;

For example,

x=100;

a= 12.25;

m=‟f‟;

we can also assign a value to a variable at the time of the variable is declared. The general format

of declaring and assigning value to a variable is :

Syntax : data_type variable = constant;

For example,
int x=100;
float a=12.25;
char m=‟f‟;

12
Types of Variables in C. There are many types of variables in c:
1. local variable
2. global variable
3. static variable

Two ways to define constant in C


There are two ways to define constant in C programming.
1. const keyword
2. #define pre-processor (or) defining symbolic constants
1. const keyword
The const keyword is used to define constant in C programming.
For example, const float PI=3.14; Now, the value of PI variable can't be changed.
For consider a program using const keyword.
#include <stdio.h>
#include <conio.h>
void main(){
const float PI=3.14;
clrscr();
printf("The value of PI is: %f",PI);
getch();
}
Output:
The value of PI is: 3.140000

2. C #define preprocessor (or) defining symbolic constants


A constant is defined as follows:
Syntax: #define symbolic-name value of constant
For example,
#define STRENGTH 100
#define PASS_MARK 50
#define MAX 200
#define PI 3.14159
The following rules apply to a #define statement which define a symbolic constant:
1. Symbolic names have the same form as variable names.(written in capitals)
2. No blank space is allowed between the # symbol and the word define.
3. The first character in the line should be #.
4. A blank space is required between the constant name and #define and between the constant
name and the value.

13
5. A semicolon must not be used at the end of a #define statement.
6. After definition, the symbolic name should not be assigned any other value within the
program by using an assignment statement.
7. Symbolic names are NOT declared for data types. Its data type depends on the type of
constant.
8. Within the program, the symbolic constant should not be assigned any other value.
For example
Statement Valid / Invalid Remarks
#define STRENGTH 100 Valid -
#define PRICE$ 100 Invalid $ symbol is not permitted in name
# define MAX 10 Invalid No white space between # and define
#define M = 56; Invalid No semicolon at the end
#define N 5, M 100 Invalid A statement can define only one name
#Define ARRAY 5 Invalid define should be in lowercase letters

For consider a program using symbolic constant.


#include <stdio.h>
#include <conio.h>
#define PI 3.14
void main(){
clrscr();
printf("The value of PI is: %f", PI);
getch();
}
Output:
The value of PI is: 3.140000

Formatted and Unformatted Console I/O Functions.


In C language, two types of Input/Output functions are available, and all input and output
operations are carried out through function calls. Several functions are available for input / output
operations in C. These functions are collectively known as the standard i/o library.
Input: In any programming language input means to feed some data into program. This can be
given in the form of file or from command line.
Output: In any programming language output means to display some data on screen, printer or in
any file.
Input / Output functions are classified into two types

14
Formated I/O Functions : formatted I/O functions operates on various types of data.
1 : printf() : output data or result of an operation can be displayed from the computer to a standard
output device using the library function printf(). This function is used to print any combination of
data.
Syntax : printf(“control string “, variable1, variable2, -----------, variablen);
Control string consists of three types of items:
1. Characters that will be printed on the screen as they appear.
For example, Ex : printf(“3977”); Output: 3977
2. Format specifications that define the output format for display of each item.
For example, Ex : printf(“%d”,3977); Output: 3977
3. Escape sequence characters such as \n, \t, and \b.
For example, Printf(“Name \t age \t place”); // escape characters used and print in vertically
Output: Name age place
Printf(“the value:\n”); // escape characters used
printf(“%d”,3977); // it print in new line
Output: the value:
3977

15
The control string indicates how may variables follow and what their typrs are. The variables
variable1, variable2, ...., variable are the variables whose values formatted and printed according to
the specifications of the control string. Note that the variables should match in number, order and
type with the format specifications.
Formating string : A simple format specification has the following form:
Syntax: %w.p type specifier
Where w is an integer number that specifies the total number of columns for the output value and p is
another integer that specifies the number of digits to the right of the decimal point. Both w and p are
optional.
Type Type specifier
Integer number d
Floating (or) real number f
Single character c
Strings s

For example, Format Output

printf(“%d”,3977); 3 9 7 7

printf(“%6d”,3977); 3 9 7 7
//right justified for w=6 is greater than number of digit (3977) 4

printf(“%2d”,3977); 3 9 7 7
// no justified for w=2 is less than number of digit (3977) 4

printf(“%-6d”, 3977); 3 9 7 7 // left justified using –

printf(“%7.4d”, 98.7654); 9 8 . 7 6 5

printf(“%7.2d”, 98.7654); 9 8 . 7 7
// right justified with only 2 decimals

printf(“%-7.2d”, 98.7654); 9 8 . 7 7
// left justified with only 2 decimals

16
Consider the string A= “New Delhi 110011” containing 16 characters (including white
spaces)
Outputs are in different style:
printf(“%s”, A); N e w D e l h i 1 1 0 0 1 1

printf(“%20s”, A); N e w D e l h i 1 1 0 0 1 1

printf(“%20.10s”, A); D e N e w D e l h i

printf(“%0.5s”, A); N e w D

scanf() : input data can be entered into the computer using the standard input C library function
called scanf(). This function is used to enter any combination of input.
Syntax : scanf(“control string”, &var1, &var2,..., &varn);
The scanf() function is used to read information from the standard input device (keyboard).
Ex : scanf(“ %d”,&a); -> hello
Each variable name (argument) must be preceeded by an ampersand (&). The (&) symbol
gives the meaning “address of ” the variable.
Unformatted I/O functions:
a) Character I/O b) String I/O
a) character I/O:
1. getchar(): Used to read a character from the standard input
2. putchar(): Used to display a character to standard output
3. getch() and getche(): these are used to take the any alpha numeric characters from the standard
input
getche() read and display the character
getch() only read the single character but not display
4. putch(): Used to display any alpha numeric characters to standard output
b) String I/O:
1. gets(): Used for accepting any string from the standard input(stdin) eg:gets()
2. puts(): Used to display a string or character array Eg:puts()

3. Cgets():read a string from the console eg; cgets(char*st)

4. Cputs():display the string to the console eg; cputs(char*st)

17

You might also like