0% found this document useful (0 votes)
2 views21 pages

C Introduction

The document provides an introduction to the C programming language, detailing its history, structure, and basic syntax. It covers essential components such as the C compiler, program structure, data types, and the significance of the main() function. Additionally, it explains concepts like tokens, identifiers, keywords, constants, and the distinction between lvalues and rvalues in C.

Uploaded by

aishikaroy2003
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)
2 views21 pages

C Introduction

The document provides an introduction to the C programming language, detailing its history, structure, and basic syntax. It covers essential components such as the C compiler, program structure, data types, and the significance of the main() function. Additionally, it explains concepts like tokens, identifiers, keywords, constants, and the distinction between lvalues and rvalues in C.

Uploaded by

aishikaroy2003
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

C INTRODUCTION

Anwesa Das
Assistant Professor,
IEM,Kolkata
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 a man named Dennis
Ritchie.
 The C programming language is a general-
purpose, high-level language.
 C was invented to write an operating system
called UNIX.
 The language was formalized in 1988 by the
American National Standard Institute. (ANSI).
 The UNIX OS was totally written in C by 1973.

 C Is a Structured Language
THE C COMPILER
 The source code written in source file is the
human readable source for your program. It
needs to be "compiled", to turn into machine
language so that your CPU can actually execute
the program as per instructions given.
 This C programming language compiler will be
used to compile your source code into final
executable program.
C HELLO WORLD EXAMPLE
A C program basically consists of the following
parts:
 Preprocessor Commands

 Functions

 Variables

 Statements & Expressions

 Comments
CONT..
#include <stdio.h>
int main()
{
/* my first program in C */
printf("Hello, World! \n");

return 0;
}
CONT..
 /* Calculation of simple interest */
/* Author: gekay Date: 25/06/2016 */
# include <stdio.h>
int main( )
{
int p, n ;
float r, si ;
p = 1000 ;
n=3;
r = 8.5 ;
/* formula for simple interest */
si = p * n * r / 100 ;
printf ( "%f\n" , si ) ;
return 0 ;
}
CONT..
1. The first line of the program #include is a
preprocessor command, which tells a C compiler to
include stdio.h file before going to actual compilation.
2. The next line int main() is the main function where
program execution begins.
3. The next line /*...*/ will be ignored by the compiler
and it has been put to add additional comments in the
program. So such lines are called comments in the
program.
4. The next line printf(...) is another function available
in C which causes the message "Hello, World!" to be
displayed on the screen.
5. The next line return 0; terminates main()function
and returns the value 0.
C BASIC SYNTAX
 The C Character Set -A character denotes any
alphabet, digit or special symbol used to
represent information
 Alphabets A, B, ….., Y, Z a, b, ….., y,z

 Digits- 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

 Special symbols- ~ ‘ ! @ # % ^ & * ( ) _ - + = | \ { }

[]:;"'<>,.?/$
TOKENS
 A C program consists of various tokens and a
token is either a keyword, an identifier, a
constant, a string literal, or a symbol. For
example, the following C statement consists of
five tokens:
 printf("Hello, World! \n");
The individual tokens are:
printf
(
"Hello, World! \n"
)
;
SEMICOLONS ;
 In C program, the semicolon is a statement
terminator. That is, each individual statement
must be ended with a semicolon. It indicates the
end of one logical entity. For example, following
are two different statements: printf("Hello,
World! \n"); return 0;
COMMENTS
 Comments are like helping text in your C
program and they are ignored by the compiler.
They
 start with /* and terminates with the characters
*/ as shown below:
 /* my first program in C */
IDENTIFIERS
 identifier starts with a letter A to Z or a to z or an
underscore _ followed by zero or more letters,
underscores, and digits 0to9.
 C does not allow punctuation characters such as
@, $, and % within identifiers.
 C is a case sensitive programming language.
Thus, Manpower and manpower are two different
identifiers in C. Here are some examples of
acceptable identifiers:
Mohd, zara, abc, move_name, a_123,
myname50 _temp, j, a23b9, retVal
KEYWORDS
 There are only 32 keywords available in C
example
 auto else long switch
break enum register typedef
case extern return union
char float short unsigned
const for signed void
continue goto sizeof volatile
default if static while
do int struct _Packed
double
WHITESPACE IN C
 A line containing only whitespace, possibly with a
comment, is known as a blank line, and a C
compiler totally ignores it.
 Whitespace is the term used in C to describe
blanks, tabs, newline characters and comments.
Whitespace separates one part of a statement
from another and enables the compiler to identify
where one element in a statement, such as int,
ends and the next element begins. Therefore, in
the following statement:
int age;
CONSTANTS
 A constant is an entity that doesn’t change,
whereas, a variable is an entity that may change.
Types of C Constants
C constants can be divided into two major
categories:
(a) Primary Constants-Integer,Real,Character
(b) Secondary Constants –Array , Pointer,
Structure, Enum etc.
WHAT IS MAIN( )?
 main( ) forms a crucial part of any C program.
Let us understand its purpose as well as its
intricacies.
(a) main( ) is a function. A function is nothing
but a container for a set of statements. In a C
program there can be multiple functions. To
begin with, we would concentrate only on those
programs which
have only one function. The name of this function
has to be main( ), it cannot be anything else. All
statements that belong to main( ) are enclosed
within a pair of braces { }
CONT..
 int main( )
{
statement 1 ;
statement 2 ;
statement 3 ;
}
Some compilers like Turbo C/C++ even permit us
to return nothing from main( ). In such a case
we should precede it with the keyword void. But
this is non-standard way of writing the main( )
function.
DATA TYPES
Types and Description
 Basic Types: They are arithmetic types and
consists of the two types: a integer types and b
floating point types.
 Enumerated types: They are again arithmetic
types and they are used to define variables that
can only be assigned certain discrete integer
values throughout the program.
 The type void: The type specifier void indicates
that no value is available.
 Derived types: They include a Pointer types, b
Array types, c Structure types, d Union types and
e Function types
INTEGER TYPES
 Following table gives you details about standard
integer types with its storage sizes and value ranges:
 Type Storage size Value range
 char 1 byte -128 to 127 or 0 to 255
 unsigned char 1 byte 0 to 255
 signed char 1 byte -128 to 127
 int 2 or 4 bytes -32,768 to 32,767 or -2,147,483,648 to
2,147,483,647
 unsigned int 2 or 4 bytes 0 to 65,535 or 0 to
4,294,967,295
 short 2 bytes -32,768 to 32,767
 unsigned short 2 bytes 0 to 65,535
 long 4 bytes -2,147,483,648 to 2,147,483,647
 unsigned long 4 bytes 0 to 4,294,967,295
FLOATING-POINT TYPES
Following table gives you details about standard
floating-point types with storage sizes and value
ranges and their precision:
 Type Storage size Value range Precision

 float 4 byte 1.2E-38 to 3.4E+38 6 decimal places

 double 8 byte 2.3E-308 to 1.7E+308 15 decimal


places
 long double 10 byte 3.4E-4932 to 1.1E+4932 19
decimal places
LVALUES AND RVALUES IN C:
 There are two kinds of expressions in C:
 lvalue : An expression that is an lvalue may appear as
either the left-hand or right-hand side of an
assignment.
 rvalue : An expression that is an rvalue may appear
on the right- but not left-hand side of an assignment.
 Variables are lvalues and so may appear on the left-
hand side of an assignment. Numeric literals are
rvalues and so may not be assigned and can not
appear on the left-hand side. Following is a valid
statement:
int g = 20;
But following is not a valid statement and would generate
compile-time error:
10 = 20;

You might also like