You are on page 1of 30

DAY – I

BASICS OF C
What will you learn?
 What is Programming Language
 Types of Programming Languages
 Introduction to C Language
 Structure of C Program
 Compilation and Execution of C Programs
WHAT IS PROGRAMMING ?
Programming is letting the computer
to learn
how to do the things.
-------------------------------------
Programming is giving training to computer
to do things
as it is non – intelligent body
------------------------------------
Programming is meant for
solving the problems by the Computer
TYPES OF PROGRAMMING LANGUAGES
To write a program for a computer, we must use a computer
language. Over the years computer languages have evolved from
machine languages to natural languages.

1. 1940’s Machine level Languages

2. 1950’s Symbolic Languages

3. 1960’s High-Level Languages


MACHINE LANGUAGES
 In the earliest days of computers, the only programming languages
available were machine languages.

 Each computer has its own machine language, which is made of streams
of 0’s and 1’s.

 Instructions in machine language must be of 0’s and1’s because the


computers are made up of electronic components those can be in one of
two states: off or on.

 The off state is represented by 0, the on state is represented by1.

 The only language understood by computer hardware is machine


language.
SYMBOLIC LANGUAGES:-
 In early1950’sAdmiralGraceHopper, A mathematician and naval
officer developed the concept of a special computer program that
would convert programs into machine language.

 Computer does not understand symbolic language it must be


translated to the machine language.

 A special program called assembler translates symbolic code into


machine language.
HIGH LEVEL LANGUAGES:-
1. Symbolic languages greatly improved programming efficiently but
they still required programmers to concentrate on the hardware
that they were using.

2. Working with symbolic languages was also very tedious because


each machine instruction has to be individually coded.

3. The desire to improve programmer efficiency and to change the


focus from the computer to the problem being solved led to the
development of high-level language.
INTRODUCTION TO ‘C’ LANGUAGE
C language facilitates a very efficient approach to the development
and implementation of computer programs. The History of C started in 1972
at the Bell Laboratories, USA, where Dennis M. Ritchie proposed this
language. In 1983 the American National Standards Institute (ANSI)
established committee whose goal was to produce “an unambiguous and
machine independent definition of the language C” which still retaining its
spirit.

C is the programming language most frequently associated with UNIX.


Since the 1970s, the bulk of the UNIX operating system and its applications
have been written in C. Because the C language does not directly rely on any
specific hardware architecture, UNIX was one of the first portable operating
systems. In other words, the majority of the code that makes up UNIX does
not know and does not care which computer it is actually running on.
CONTINUED.,
C was first designed by Dennis Ritchie for use with UNIX on DEC PDP-11
computers. The language evolved from Martin Richard's BCPL, and one of its earlier forms
was the B language, which was written by Ken Thompson for the DEC PDP-7. The first
book on C was The C Programming Language by Brian Kernighan and Dennis Ritchie,
published in1978.

In 1983, the American National Standards Institute (ANSI) established a committee


to standardize the definition of C. The resulting standard is known as ANSI C, and it is the
recognized standard for the language, grammar, and a core set of libraries. The syntax is
slightly different from the original C language, which is frequently called K&R for
Kernighan and Ritchie. There is also an ISO (International Standards Organization)
standard that is very similar to the ANSI standard.

It appears that there will be yet another ANSI C standard officially dated 1999 or
in the early 2000 years; it is currently known as "C99."
Learning Analogy for C

Alphabets Words Sentences Paragraphs

Grammar
Rules
Learning Analogy for C

Character Programs
Tokens Statements
Set

Syntax
Rules
CREATING AND RUNNING PROGRAMS

To create and run the programs the following steps are to be


performed:

1. Writing and editing programs


2. Compiling the program
3. Linking the program with library modules
4. Executing the program
STRUCTURE OF C PROGRAM
The program written in C language follows this basic structure.
1. Documentation section
2. Linking section
3. Definition section
4. Global declaration section
5. Main function section
{
Declaration Section
Executable section
}

6. Sub program or function section


COMPILATION AND EXECUTION

Source Pre Compile Object Executable


Linker
File processor r Files Files

Library
Library Files
Files
TOKENS OF C
1. Literals
2. Identifiers
3. Keywords
4. Operators
5. Special Characters
LITERAL CONSTANTS
 The constants refer to fixed values that the program may not alter during its
execution. These fixed values are also called literals.

 Constants can be of any of the basic data types like an integer literal, a floating
literal, a character literal, or a string literal.

Integer literals

 An integer literal can be a decimal, octal, or hexadecimal constant.

 Examples: 25, +34, -45

Floating-point literals

 A floating-point literal has an integer part, a decimal point, a fractional part, and an
exponent part. You can represent floating point literals either in decimal form or
exponential form.

 Examples: 3.14159, 314159E-5L


LITERAL CONSTANTS
Character constants

 Character literals are enclosed in single quotes, e.g., 'x' and can be stored in a simple

variable of char type.

 A character literal can be a plain character (e.g., 'x')

String literals

 String literals or constants are enclosed in double quotes "". A string contains

characters that are similar to character literals: plain characters, escape sequences,

and universal characters.

 Examples:

"hello, dear"

"hello, dear"
VARIABLES
 A variable is defined as a meaningful name given to the data storage
location in the computer memory. A variable value can be allowed to
change during the execution of a program.

 A variable to be used in a program must be declared.

 To declare a variable, the following syntax must be followed:

<datatype> <variable_name>;
 Example:

1. int rollno;
2. char grade;
3. float temperature;
DATA TYPES
BASIC DATA TYPES IN C
Data types are used to specify two things to the compiler:

1. How much memory has to be allocated for an identifier to store


the data

2. What type of data item has been stored in an identifier


Data type Size in bytes Range Format specifier
char 1 -128 to + 127 %c

unsigned char 1 0 to 255 %c

signed char 1 -128 to + 127 %c

Int 2 -32768 to +32767 %d

unsigned int 2 0 to 65535 %u

signed int 2 -32768 to +32767 %d

short int 2 -32768 to +32767 %d

unsigned short int 2 0 to 65535 %d

signed short int 2 -32768 to +32767 %d

long int 4 -2147483648 to +2147483647 %ld

unsigned long int 4 0 to 424967295 %lu

signed long int 4 -2147483648 to +2147483647 %ld

float 4 3.4 E-38 to +3.4E+38 %f

double 8 1.7E-308 to +1.7E+308 %lf

long double 10 3.4E-4932 to 1.1E+4932 %Lf


C KEYWORDS
C has a set of reserved words known as keywords that cannot be used
as identifier. All the keywords are basically a sequence of characters that have
a fixed meaning. All the keywords must be written in small case letters.

The following is the list of C keywords:


auto break case char
const continue default double
else enum extern float
for goto int long
register return short signed
sizeof struct switch typedef
union unsigned void volatile
do if static while
INPUT AND OUTPUT STATEMENTS IN C
OUTPUT Statement:

 The printf ( ) function is used to display the information required by the


user and also prints the values of the variables.

 The syntax of the printf ( ) function can be given as

printf (“CONTROL STRING”, Variables list);

 This function accepts two arguments or parameters – control string and


variable list, where variable list is list of variables to be displayed and
these are displayed as formatted in the control string.

 Control string may also contain text, captions, identifiers or any other
text that is to be readable.
INPUT AND OUTPUT STATEMENTS IN C
INPUT Statement:

 The function scanf ( ) is used to read the data from the keyboard. This
function takes the text from keyboards and formats the data according to the
format specified in the control string and then stores the data in the
variables.

 The syntax of the scanf ( ) function can be given as

scanf (“Control string”, &arg1, &arg2, ……, argN);

 The control string specified the type and format of the data that has to be
obtained from the keyboard and stored in the memory locations pointed by
the arguments arg1, arg2, arg3,….., argN.

 Arguments are nothing but the variables used in the program.


EXAMPLES:
int x;

 To input a value for x from Keyboard:

scanf(“%d”, &x);

 To display the value of x onto Screen:

printf(“%d”, x);
EXAMPLES:
float x;

 To input a value for x from Keyboard:

scanf(“%f”, &x);

 To display the value of x onto Screen:

printf(“%f”, x);
EXAMPLES:
 It is possible to read and write multiple variables at the same time
char x;
float y;
int z;

 To input a value for x from Keyboard:

scanf(“%c %f %d”, &x, &y, &z);

 To display the value of x onto Screen:

printf(“%c %f %d”, x, y, z);


EXAMPLES:
char x;

 To input a value for x from Keyboard:

scanf(“%c”, &x);

 To display the value of x onto Screen:

printf(“%c”, x);
OPERATORS OF C
An Operator is a symbol that performs some mathematical or logical
operation. In C there are different categories of Operators:

1. Arithmetic ( +, -, *, /, %)

2. Relational ( <, >, <=, >=, ==, != )

3. Logical (&&, ||, ! )

4. Assignment (=, +=, -=, *=, /=, %=)

5. Unary (++, --)

6. Bitwise ( &, |, ~, <<, >>, ^)

7. Conditional (? : )

8. Special Operators

You might also like