[go: up one dir, main page]

0% found this document useful (0 votes)
24 views16 pages

Co1 Part 1

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

Co1 Part 1

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

Department of Computer Science and Engineering

Computer Concepts and C Programming


INTRODUCTION TO C LANGUAGE:

C is a structured programming language developed at AT & T’s Bell Laboratories of USA in


1972. It was designed and written by Dennis Ritchie. In the late seventies C began to replace the
more familiar languages of that time like PL/I, ALGOL, etc. C was originally designed for and
implemented on the UNIX operating system on the DEC PDP-11, by Dennis Ritchie. The
operating system, the C compiler, and essentially all UNIX applications are written in C. C is not
tied to any particular hardware or system, however, and it is easy to write programs that will run
without change on any machine that supports C.

HISTORY OF C LANGUAGE:

Ken Thompson created a language which was based upon a language known as BCPL and it was
called as B. B language was created in 1970, basically for UNIX operating system, Dennis
Ritchie used ALGOL, BCPL and B as the basic reference language from which he created C. In
1983, the American National Standards Institute (ANSI) began the definition of a standard of a c.
It was approved in December 1989. In 1990, The International standards Organization (ISO)
adopted the ANSI standard. This version of c is known as C89.
STRUCTURE OF C PROGRAM:

Every C program is made of one or more preprocessor commands, a global declaration section
and one or more functions.

1. The preprocessor commands are special instructions to the preprocessor that specifies how to
prepare the program for compilation. One of the most important preprocessor commands we use
commonly is include. The include command tells the preprocessor that we need information
from selected libraries known as header files.

2. The global declaration section comes at the beginning of the program. The global variables are
declared here.

3. The execution of the program starts from the function called main(). All functions in a
program, including main, are divided into two sections one declaration section and the statement
section.
Declaration section: it is at the beginning of the function. It describes the data that you will be
using in the function.
Statement Section: It contains the instructions to the computer that perform specific task.
Comments: used to make our code unreadable means the compiler ignores these
comments when it translates the program into executable code.
Block Commenting: it uses two pair of tokens /* and */ to comment entire block of code.
Creating and Running Programs:

There are four steps in this process.

1. Writing and editing the program using Text editor (source code).
2. Compile the program using any C compiler.(object file)
3. Linking the program with the required library modules(object file)
4. Executing the program. (.Exe file)

Creating and Editing a C Program in C Programming Language compiler: Writing or


creating and editing source program is a first step in c language. Source code is written in c
programming language according to the type of problem or requirement, in any text editor.
Saving C Program in C Programming Language: Source code is saved on the secondary
storage. Source code is saved as text file. The extension of file must be ".c". Example the file
name is "learn c programming language.c"
Compiling C program in C Programming Language: Computer does not understand c
programming language. It understands only 0 and 1 means machine language. So c
programming language code is converted into machine language. The process of converting
source code in to machine code is called compiling. Compiler is a program that compiles source
code. Compiler also detects errors in source program. If compiling is successful source program
is converted into object program. Object program is saved on disk. The extension of file is ".obj"
Linking in C programming Language: There are many built in functions available in c
programming language. These functions are also called library functions. These functions are
stored in different header files.
Loading program: The process of transferring a program from secondary storage to main
memory for execution is called loading a program. A program called loader does loading.
Executing program: Execution is the last step. In this step program starts execution. Its
instructions start working and output of the program display on the screen.
Translators are system software used to convert high-level language program into machine-
language code.
Compiler: Coverts the entire source program at a time into object code file, and saves it in
secondary storage permanently. The same object machine code file will be executed several
times, whenever needed.
Interpreter: Each statement of source program is translated into machine code and executed
immediately. Translation and execution of each and every statement is repeated till the end of the
program. No object code is saved. Translation is repeated for every execution of the source
program.

C Character Set:

The character set in C Language can be grouped into the following categories.

1. Letters
2. Digits
3. Special Characters
4. White Space
C Character-Set Table:
Letters Digits
Upper Case A to Z 0 to 9
Lower Case a to z

Special Characters

, Comma & Ampersand


. Period ^ Caret
; Semicolon * Asterisk
: Colon - Minus Sign
? Question Mark + Plus Sign
' Aphostrophe < Opening Angle (Less than sign)
" Quotation Marks > Closing Angle (Greater than sign)
! Exclaimation Mark ( Left Parenthesis
| Vertical Bar ) Right Parenthesis
/ Slash [ Left Bracket
\ Backslash ] Right Bracket
~ Tilde { Left Brace
_ Underscore } Right Brace
$ Dollar Sign # Number Sign
% Percentage Sign

White Spaces are ignored by the compiler until they are a part of string constant. White Space
may be used to separate words, but are strictly prohibited while using between characters of
keywords or identifiers.
White Space

1. Blank Space
2. Horizontal Tab
3. Carriage Return
4. New Line
5. Form Feed

Keywords:
Every word in C language is a keyword or an identifier. Keywords in C language cannot be
used as a variable name. They are specifically used by the compiler for its own purpose and
they serve as building blocks of a c program

The following are the Keyword set of C language.

auto else register union


break enum return unsigned
case extern short void
char float signed volatile
const for sizeof while
continue goto static
default if struct
do int switch
double long typedef

Identifiers:

Identifiers refer to the name of user-defined variables, array and functions. A variable should be
essentially a sequence of letters and or digits and the variable name should begin with a
character.

Both uppercase and lowercase letters are permitted. The underscore character is also permitted in
identifiers.

The identifiers must conform to the following rules.

1. First character must be an alphabet (or underscore)


2. Identifier names must consists of only letters, digits and underscore.
3. A identifier name should have less than 40 characters.
4. Any standard C language keyword cannot be used as a variable name.
5. A identifier should not contain a space.
Basic Data Types:

A type defines a set of values and set of operations that can be applied on those values.
C language data types can be broadly classified as

1. Primary data type


2. Derived data type
3. User-defined data type

Primary data type:

All C Compilers accept the following fundamental data types

1. Integer int
2. Character char
3. Floating Point float
4. Double precision floating point double
5. Void void

The size and range of each data type is given in the table below

DATA TYPE RANGE OF VALUES


char -128 to 127
int -32768 to +32767
float 3.4 e-38 to 3.4 e+38
double 1.7 e-308 to 1.7
e+308

Integer Type:

Integers are whole numbers with a machine dependent range of values. C has 3 classes of integer
storage namely short int, int and long int. All of these data types have signed and unsigned
forms. A short int requires half the space than normal integer values. Unsigned numbers are
always positive and consume all the bits for the magnitude of the number. The long and unsigned
integers are used to declare a longer range of values.
Floating Point Types:

Floating point number represents a real number with 6 digits precision. Floating point numbers
are denoted by the keyword float. When the accuracy of the floating point number is insufficient,
we can use the double to define the number. The double is same as float but with longer
precision. To extend the precision further we can use long double which consumes 80 bits of
memory space.

Character Type:

A single character can be defined as a defined as a character type of data. Characters are usually
stored in 8 bits of internal storage. The qualifier signed or unsigned can be explicitly applied to
char. While unsigned characters have values between 0 and 255, signed characters have values
from –128 to 127.

Size and Range of Data Types on 16 bit machine.

type SIZE (Bits) Range


char or signed char 8 -128 to 127
unsigned char 8 0 to 255
int or signed int 16 -32768 to 32767
unsigned int 16 0 to 65535
short int or signed short int 8 -128 to 127
unsigned short int 8 0 to 255
long int or signed long int 32 -2147483648 to 2147483647
unsigned long int 32 0 to 4294967295
float 32 3.4 e-38 to 3.4 e+38
double 64 1.7e-308 to 1.7e+308
long double 80 3.4 e-4932 to 3.4 e+4932

Variables:

Variable is an identifier used to store the value of a type in the memory.


Variable Declaration: Each variable in your program must be declared and defined. The
declaration does two things.
Tells the compiler the variables name.
Specifies what type of data the variable will hold. The general
format of any declaration:
datatype v1, v2, v3, ……….. vn;
Where v1, v2, v3 are variable names. Variables are separated by commas. A declaration
statement must end with a semicolon.

Example:
int sum;
int number, salary; double
average, mean;

Datatype Keyword Equivalent


Character char
Unsigned Character unsigned char
Signed Character signed char
Signed Integer signed int (or) int
Signed Short Integer signed short int (or) short int (or) short
Signed Long Integer signed long int (or) long int (or) long
UnSigned Integer unsigned int (or) unsigned
UnSigned Short Integer unsigned short int (or) unsigned short
UnSigned Long Integer unsigned long int (or) unsigned long
Floating Point float
Double Precision Floating Point double
Extended Double Precision Floating long double
Point

Constants:

A constant value is the one which does not change during the execution of a program. C supports
several types of constants.

1. Integer Constants
2. Real Constants
3. Single Character Constants
4. String Constants

Integer Constants:

An integer constant is a sequence of digits. There are 3 types of integers namely decimal integer,
octal integers and hexadecimal integer.

Decimal Integers consists of a set of digits 0 to 9 preceded by an optional + or - sign. Spaces,


commas and non digit characters are not permitted between digits. Examples for valid decimal
integer constants are
123 -
31
562321 +
78
Octal Integers constant consists of any combination of digits from 0 through 7 with a O at the
beginning. Some examples of octal integers are
O26
O
O347
O676
Hexadecimal integer constant is preceded by OX or Ox, they may contain alphabets from A to F
or a to f. The alphabet A to F refers to 10 to 15 in decimal digits. Examples of valid hexadecimal
integers are
OX2
OX8C
OXbcd
Ox

Real Constants:

Real Constants consists of a fractional part in their representation. Integer constants are
inadequate to represent quantities that vary continuously. These quantities are represented by
numbers containing fractional parts like 26.082. Examples of real constants are
0.0026
-0.97
435.29
+487.0
Single Character Constants:

A Single Character constant represent a single character which is enclosed in a pair of quotation
symbols.

Example for character constants are ‘5’ ‘x’ ‘;’ ‘ ‘

All character constants have an equivalent integer value which is called ASCII Values.
String Constants:

A string constant is a set of characters enclosed in double quotation marks. The characters in a
string constant sequence may be a alphabet, number, special character and blank space. Example
of string constants are

“VISHAL”
”1234”
”God Bless”
”!.....?”
Backslash Character Constants [Escape Sequences]:

Backslash character constants are special characters used in output functions. Although they
contain two characters they represent only one character. Given below is the table of escape
sequence and their meanings.
Constant Meaning
'\a' Audible Alert (Bell)
'\b' Backspace
'\f' Formfeed
'\n' New Line
'\r' Carriage Return
'\t' Horizontal tab
'\v' Vertical Tab
'\'' Single Quote
'\"' Double Quote
'\?' Question Mark
'\\' Back Slash
'\0' Null

C Programming - Managing Input and Output Operations:

One of the essential operations performed in a C language programs is to provide input values
from keyboard to the program and output the data produced by the program to a standard output
device. We can assign values to variable through assignment statements such as x = 5 a = 0 ; and
so on.
Another method to Input data the function we use scanf() function which can be used to read
data from a key board. For outputting results we have used extensively the function printf()
which sends results out to a terminal. There exists several functions in ‘C’ language that can
carry out input output operations. These functions are collectively known as standard
Input/Output Library. Each program that uses standard input / output function must contain the
statement.
# include < stdio.h > at the beginning.

Single character input output:

The basic operation done in input output is to read a character from the standard input device
such as the keyboard and to output or writing it to the output unit usually the screen. The
getchar() function can be used to read a character from the standard input device. The scanf() can
also be used to achieve the function.
The getchar() has the following form.
variable name = getchar():

Variable name is a valid ‘C’ variable, that has been declared already and that possess the type
char.

The putchar function which in analogus to getchar function can be used for writing characters
one at a time to the output terminal.
The general form is: putchar
(variable name);
Where variable is a valid C type variable that has already been declared Ex:-
putchar(name);

Displays the value stored in variable C to the standard screen.

Formatted Input For scanf():

The formatted input refers to input data that has been arranged in a particular format. Input
values are generally taken by using the scanf function.
The scanf function has the general form.
scanf (“Control string”, arg1, arg2, arg3 ………….argn);

The format field is specified by the control string and the arguments
arg1, arg2, …………….argn specifies the addrss of location where address is to be stored.
The control string specifies the field format which includes format specifications and optional
number specifying field width and the conversion character % and also blanks, tabs and
newlines.
The Blanks tabs and newlines are ignored by compiler. The conversion character % is followed
by the type of data that is to be assigned to variable of the assignment. The field width specifier
is optional.
The general format for reading a integer number is: % x d
Here percent sign (%) denotes that a specifier for conversion follows and x is an integer
number which specifies the width of the field of the number that is being
read. The data type character d indicates that the number should be read in integer mode.
Example :

scanf (“%3d %4d”, &sum1, &sum2);

If the values input are 175 and 1342 here value 175 is assigned to sum1 and 1342 to sum 2.
Suppose the input data was follows 1342 and 175.

The number 134 will be assigned to sum1 and sum2 has the value 2 because of %3d the number
1342 will be cut to 134 and the remaining part is assigned to second variable sum2. If floating
point numbers are assigned then the decimal or fractional part is skipped by the computer.

To read the long integer data type we can use conversion specifier % ld & % hd for short integer.

Input specifications for real number:

Field specifications are not to be use while representing a real number therefore real numbers are
specified in a straight forward manner using % f specifier.
The general format of specifying a real number input is

Scanf (% f “, &variable);

Example:

Scanf (“%f %f % f”, &a, &b, &c);

With the input data

321.76, 4.321, 678 The values

321.76 is assigned to a , 4.321 to b & 678 to C.

If the number input is a double data type then the format specifier should be % lf

instead of %f.
Input specifications for a character.

Single character or strings can be input by using the character specifiers. The general format is %
xc or %xs
Where C and S represents character and string respectively and x represents the

field width.

The address operator need not be specified while we input strings.

Example :

Scanf (“%C %15C”, &ch, nname):

Here suppose the input given is a, Robert then a is assigned to ch and name will be assigned to
Robert.

Printing One Line:

printf();

The most simple output statement can be produced in C’ Language by using printf statement. It
allows you to display information required to the user and also prints the variables we can also
format the output and provide text labels. The simple statement such as
Printf (“Enter 2 numbers”);
Prompts the message enclosed in the quotation to be displayed.
Conversion Strings and Specifiers:

The printf ( ) function is quite flexible. It allows a variable number of arguments, labels and
sophisticated formatting of output. The general form of the printf ( ) function is
Syntax:

printf (“conversion string”, variable list);

The conversion string includes all the text labels, escape character and conversion specifiers
required for the desired output. The variable includes all the variable to be printed in order
they are to be printed. There must be a conversion specifies after each variable.
Specifier Meaning
%c – Print a character %d
– Print a Integer
%i – Print a Integer
%e – Print float value in exponential form. %f –
Print float value
%g – Print using %e or %f whichever is smaller %o –
Print actual value
%s – Print a string
%x – Print a hexadecimal integer (Unsigned) using lower case a – F
%X – Print a hexadecimal integer (Unsigned) using upper case A – F
%a – Print a unsigned integer. %p –
Print a pointer value
%hx – hex short
%lo – octal long
%ld – long

You might also like