0% found this document useful (0 votes)
29 views

L 1.1 Final

C is a general-purpose programming language developed in the early 1970s by Dennis Ritchie at Bell Labs. It has high portability and runs on multiple operating systems. C programs begin execution at the main function and use functions, keywords, identifiers, variables, constants, data types, and input/output statements. Type conversion, both implicit and explicit, allows changing data between compatible types.

Uploaded by

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

L 1.1 Final

C is a general-purpose programming language developed in the early 1970s by Dennis Ritchie at Bell Labs. It has high portability and runs on multiple operating systems. C programs begin execution at the main function and use functions, keywords, identifiers, variables, constants, data types, and input/output statements. Type conversion, both implicit and explicit, allows changing data between compatible types.

Uploaded by

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

Computer Programming : C

Dr.Bharanidharan N
Asst. Professor Sr. Grade I, SCORE
C Language
C Language:
• C is general purpose programming language
• Has economical expressions, modern control
flow, new data structures, rich operators
• Designed for and implemented by Dennis
Ritchie in 1972
• Has high portability. Runs on multiple OS (IBM,
Honeywell, Sperry-Rand, etc)
• UNIX OS, C compiler and all libraries were
written in C language itself
Structure of C program
• Initially, APL – A Programming Language was
written.
• Then refined to get BCPL – Basic Computer
Programming Language or B. Then C was
developed
• C is highly structured language
• Most of repetitive jobs has different functions
• Starts its execution at ‘main(argument) ‘ function
• All keywords are in lower case as it is case
sensitive
Structure of C program
Structure of C program
Structure of C program
C tokens

7
Types of token

8
Keywords
• Keyword is a reserved word that has a
particular meaning in the programming
language.
• The meaning of keyword is predefined.
• A keyword cannot be used as an identifier
name in C language.
• There are 32 keywords available in C.

9
Standard Keywords

10
Identifiers
• An identifiers refers to the name of an object.
• It can be a variable name, a label name, a
function name, a typedef name, a macro
name or a macro parameter, a tag or a
member of a structure, a union or an
enumeration.

11
Identifiers Rules
1. Identifier name in C can have letters, digits or
underscores.
2. The first character of an identifier name must
be a letter (either uppercase or lower case) or
an underscore and cannot be a digit.
3. No special character (except underscore), blank
space and comma can be used in an identifier
name.
4. Keywords or reserved words cannot form a
valid identifier name. 12
5. The maximum number of characters allowed
in an identifier name is compiler dependent,
but the limit imposed by all the compilers
provides enough flexibility to create
meaningful identifier names.
• The following identifier names are valid in C:
Student_Name, StudentName, student_name,
student1, _student.
• The following identifier names are invalid in C:
Student Name, Name&Rollno, 1_student, for.

13
Variables
Rules for constructing variable names
Declaring & Initializing variables

Declaration:
int emp_num,b;
float salary;
char grade;
double balance_amt;

Initialization while declaring:


int emp_num = 7;
float salary = 5000.47;
char grade = ‘A’;
int count,flag = 1;
Int count =0, flag = 1;
Constants
• A constant is an entity whose value remains
the same throughout the execution of a
program.
Qualified constants
• They are created by using const qualifier.
• Eg: const char a=’A’;
• const float pi = 3.14;
Escape Sequences

• Sometimes, it is necessary to use characters that


cannot be typed or has special meaning in C
programming. For example: newline(enter), tab,
question mark etc.

• In order to use these characters, escape sequences are


used.
Escape Sequences
Escape Sequences Character
\b Backspace
\f Form feed
\n Newline
\r Return
\t Horizontal tab
\v Vertical tab
\\ Backslash
\' Single quotation mark
\" Double quotation mark
\? Question mark
\0 Null character
Data Types
• Data type or just type is one of the most
important attributes of an identifier.
• It determines the possible values that an
identifier can have and the valid operations
that can be applied on it.
• C also has a special data type called void,
which indicates that any data type ie., no data
type, does not describe the data items.

23
Classification of Data types
• They are classified into
1) Basic data types
i) Character char
ii) Integer int
iii) Single precision floating point float (occupies
32bit in computer memory)
iv) Double precision floating point double
(occupies 64bit in computer memory).
v) No value available void
24
Classification of Data types
2) Derived data types
i) Array type char[], int[] etc
ii) Pointer type char*,int* etc
iii) Function type int(int,int) , float(int) etc

3) User defined data types


iv) Structure
v) Union
vi) Enumeration
25
Classification of Data types
Type Size (bytes) Format Specifier
int at least 2, usually 4 %d, %i
char 1 %c
float 4 %f
double 8 %lf
short int 2 usually %hd
unsigned int at least 2, usually 4 %u
long int at least 4, usually 8 %ld, %li
long long int at least 8 %lld, %lli
unsigned long int at least 4 %lu

unsigned long long int at least 8 %llu

signed char 1 %c
unsigned char 1 %c
at least 10, usually 12 or
long double %Lf
16
Type conversion
• Converting an expression of a given type into
another type is known as type-casting.
Typecasting is more useful in C programming.
• Here, It is best practice to convert lower data
type to higher data type to avoid data loss.
• Data will be truncated when the higher data
type is converted to lower. For example, if a
float is converted to int, data which is present
after the decimal point will be lost.
28
Implicit conversion
• Implicit conversions do not require any
operator for to be converted. They are
automatically performed when a value is
copied to a compatible type in the program.
• Here, the value of “a” has been promoted
from int to double and we have not had to
specify any type-casting operator. This is
known as a standard conversion.

29
Implicit conversion

// An example of implicit conversion


#include<stdio.h>
int main()
{
int x = 10; // integer x

// x is implicitly converted to float


float z = x + 1.0;

printf("x = %d, z = %f", x, z);


return 0;
}

30
31
Explicit conversion
• In C language, Many conversions, especially
those that imply a different interpretation of
the value, require an explicit conversion.
• They are not automatically performed when a
value is copied to a compatible type in the
program.
• This process is also called type casting and it
Syntax: (type) expression
is user defined.

32
Explicit conversion

#include<stdio.h>
int main()
{
int i=20;
float p;
p = (float) i; // Explicit conversion
printf("Explicit value is %f",p);
return 0;
}

33
Input / Output statements
printf(): writing output function
scanf(): getting user input

You might also like