SlideShare a Scribd company logo
2
Most read
4
Most read
8
Most read
Introduction Overview of C
C Programming
Dr. P. Poongothai
Kristu Jayanti College
Introduction Overview of C:
 C is a general-purpose programming language created by Dennis Ritchie at
the Bell Laboratories in 1972.
 It is a very popular language, despite being old.
 The main reason for its popularity is because it is a fundamental language in
the field of computer science.
 C is strongly associated with UNIX, as it was developed to write the UNIX
operating system.
Importance of C:
 C is called as a robust language, which has so many built-in functions and
operations, which can be used to write any complex program.
 The C programming language is used to create operating systems,
compilers, interpreters, third-party libraries, and databases.
 ‘C’ Programs are efficient and fast.
 ‘C’ language is best for structured programming, where the user can think of
a problem in terms of function modules (or) blocks.
 It has the ability to extend itself.
 C is a general-purpose structured language that promotes code
organization through functions.
Basic structure of a C program:
 The basic structure of a C program is divided into 6 Section which
responsible for the proper execution of a program.
1. Documentation Section
2. Preprocessor Section (Link Section)
3. Definition Section
4. Global Declaration Section
5. Main() Function Section
6. Sub Programs Section
1. Documentation
 It consists of the description of the program, the name of the program, and
the creation date and time of the program.
// description, name of the program, programmer name, date, time etc.
2. Preprocessor Section
 All the header files of the program will be declared in the
preprocessor section of the program.
#include<stdio.h>
#include<math.h>
3. Definition
 Preprocessors are the programs that process our source code before the
process of compilation.
 Preprocessor directives start with the ‘#’ symbol.
 The #define preprocessor is used to create a constant throughout the
program.
#define long long ll
4. Global Declaration
 The global declaration section contains global variables, function
declaration, and static variables.
int num = 18;
5. Main() Function
 Operations like declaration and execution are performed inside the curly
braces of the main program.
 The return type of the main() function can be ‘int’ as well as ‘void’ too.
 void() main tells the compiler that the program will not return any value.
 The int main() tells the compiler that the program will return an integer
value.
void main()
int main()
6. Sub Programs
 User-defined functions are called sub programs of the program.
 The control of the program is shifted to the called function whenever they
are called from the main or outside the main() function.
 These are specified as per the requirements of the programmer.
int sum(int x, int y)
{
return x+y;
}
Structure of C Program with example:
// Documentation Section
/**
* file: sum.c
* author: you
* description: program to find sum.
*/
#include <stdio.h> // Link Section
#define X 20 // Definition Section
int sum(int y); // Global Declaration Section
int main(void) // Main() Function Section
{
int y = 55;
printf("Sum: %d", sum(y));
return 0;
}
int sum(int y) // Subprogram Section
{
return y + X;
}
Output: Sum: 75
Sections Description
// Documentation
/**
* file: sum.c
* author: you
* description: program to find
sum.
*/
It is the comment section and is part of the
description section of the code.
#include<stdio.h> Header file which is used for standard input-
output. This is the preprocessor section.
#define X 20 This is the definition section. It allows the use
of constant X in the code.
int sum(int y) This is the Global declaration section includes
the function declaration that can be used
anywhere in the program.
int main() main() is the first function that is executed in
the C program.
{…} These curly braces mark the beginning and
end of the main function.
printf(“Sum: %d”, sum(y)); printf() function is used to print the sum on
the screen.
return 0; We have used int as the return type so we
have to return 0 which states that the given
program is free from the error and it can be
exited successfully.
int sum(int y)
{
return y + X;
}
This is the subprogram section. It includes the
user-defined functions that are called in the
main() function.
Introduction Overview of C
C Programming
Dr. P. Poongothai
Kristu Jayanti College
Tokens in C:
 The tokens of C language can be classified into six types based on the
functions they are used to perform.
1. Keywords
2. Identifiers
3. Constants
4. Strings
5. Special Symbols
6. Operators
1. C Token – Keywords:
 The keywords are pre-defined or reserved words in a programming
language.
 Each keyword is meant to perform a specific function in a program.
 Since keywords are referred names for a compiler, they can’t be used as
variable names.
 If we are trying to assign a new meaning to the keyword which is not
allowed.
 C language supports 32 keywords which are given below:
2. C Token – Identifiers:
 Identifiers are used as the general terminology for the naming of variables,
functions, and arrays.
 These are user-defined names consisting of a long sequence of letters and
digits with either a letter or the underscore ( _ ) as a first character.
 Identifier names must differ in spelling and case from any keywords.
 We cannot use keywords as identifiers; they are reserved for special use.
Rules for Naming Identifiers:
 They must begin with a letter or underscore ( _ ).
 They must consist of only letters, digits, or underscore. No other special
character is allowed.
 It should not be a keyword.
 It must not contain white space.
 It should be up to 31 characters long as only the first 31 characters are
significant.
 For example,
main: method name.
a: variable name.
3. C Token – Constants:
 The constants refer to the variables with fixed values.
 Constants may belong to any of the data types.
 Examples of Constants in C
const int a = 20;
const int* const ptr = &a;
 Types of Constants in C
 Integer Constant
 Character Constant
 Floating Point Constant
 Double Precision Floating Point Constant
 Array Constant
 Structure Constant
4. C Token – Strings:
 Strings are nothing but an array of characters ended with a null character
(‘0’).
 This null character indicates the end of the string.
 Strings are always enclosed in double quotes.
 Whereas, a character is enclosed in single quotes in C and C++.
 Examples of String
char string[10] = {‘b’, ’c’, ‘a’, ‘0’};
char string[10] = “bca”;
char string [] = “bca”;
5. C Token – Special Symbols:
The following special symbols are used in C having some special meaning.
 Brackets [ ]: It is used as array element references. These indicate single and
multidimensional subscripts.
 Parentheses ( ): These special symbols are used to indicate function calls and
function parameters.
 Braces{ }: These opening and ending curly braces mark the start and end of
a block of code containing more than one executable statement.
 Comma , : It is used to separate more than one statement like for separating
parameters in function calls.
 Colon : : It is an operator that essentially invokes something called an
initialization list.
 Semicolon ; : It is known as a statement terminator. It indicates the end of
one logical entity. That’s why each individual statement must be ended with
a semicolon.
 Asterisk * : It is used to create a pointer variable and for the multiplication
of variables.
 Assignment operator = : It is used to assign values and for logical operation
validation.
 Pre-processor # : The preprocessor is a macro processor that is used
automatically by the compiler to transform your program before actual
compilation.
 Period . : Used to access members of a structure or union.
 Tilde ~ : Bitwise One’s Complement Operator.
6. C Token – Operators:
Operators are symbols that trigger an action when applied to C variables and
other objects. The data items on which operators act are called operands.
Depending on the number of operands that an operator can act upon, operators
can be classified as follows:
 Unary Operators: Those operators that require only a single operand to
act upon are known as unary operators. Example increment and decrement
operators
 Binary Operators: Those operators that require two operands to act upon
are called binary operators. Binary operators can further are classified
into:
1. Arithmetic operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
5. Bitwise Operator
 Ternary Operator: The operator that requires three operands to act upon
is called the ternary operator. Conditional Operator(?) is also called the
ternary operator.
Variables:
 Variables are containers for storing data values, like numbers and characters.
 In C, there are different types of variables (defined with different keywords),
for example:
 int - stores integers (whole numbers), without decimals, such as 123 or -123
 float - stores floating point numbers, with decimals, such as 19.99 or -19.99
 char - stores single characters, such as 'a' or 'B'. Characters are surrounded
by single quotes
Declaring (Creating) Variables:
Syntax: type variableName = value;
Example: int myNum = 15;
(OR)
Example: int myNum; // Declare a variable
myNum = 15; // Assign a value to the variable
The general rules for naming variables are:
 Names can contain letters, digits and underscores
 Names must begin with a letter or an underscore ( _ )
 Names are case-sensitive (myVar and myvar are different variables)
 Names cannot contain whitespaces or special characters like !, #, %, etc.
 Reserved words (such as int) cannot be used as names
Data Types in C:
 Each variable in C has an associated data type.
 It specifies the type of data that the variable can store like integer, character,
floating, double, etc.
 Each data type requires different amounts of memory and has some specific
operations which can be performed over it.
 The data type is a collection of data with values having fixed values
The data types in C can be classified as follows:
Types Description
Primitive Data
Types
Primitive data types are the most basic data types that are
used for representing simple values such as integers, float,
characters, etc.
User Defined
Data Types
The user-defined data types are defined by the user himself.
Derived Types
The data types that are derived from the primitive or built-in
datatypes are referred to as Derived Data Types.
 Different data types also have different ranges up to which they can store
numbers.
 These ranges may vary from compiler to compiler.
Data Type
Size
(bytes) Range
Format
Specifier
short int 2 -32,768 to 32,767 %hd
unsigned
short int
2 0 to 65,535 %hu
unsigned int 4 0 to 4,294,967,295 %u
int 4
-2,147,483,648 to
2,147,483,647
%d
long int 4
-2,147,483,648 to
2,147,483,647
%ld
Data Type
Size
(bytes) Range
Format
Specifier
unsigned long
int
4 0 to 4,294,967,295 %lu
long long int 8 -(2^63) to (2^63)-1 %lld
unsigned long
long int
8
0 to
18,446,744,073,709,551,615
%llu
signed char 1 -128 to 127 %c
unsigned
char
1 0 to 255 %c
float 4 1.2E-38 to 3.4E+38
%f
double 8
1.7E-308 to 1.7E+308 %lf
long double 16 3.4E-4932 to 1.1E+4932 %Lf
Note: The long, short, signed and unsigned are datatype modifier that can be used
with some primitive data types to change the size or length of the datatype.
The following are some MAIN PRIMITIVE DATA TYPES in C:
Basic Data Types
The data type specifies the size and type of information the variable will store.
Data
Type
Size Description Example
int 2 or 4
bytes
Stores whole numbers, without decimals 1
float 4 bytes Stores fractional numbers, containing one or more
decimals. Sufficient for storing 6-7 decimal digits
1.99
double 8 bytes Stores fractional numbers, containing one or more
decimals. Sufficient for storing 15 decimal digits
1.99
char 1 byte Stores a single character/letter/number, or ASCII
values
'A'
Basic Format Specifiers
There are different format specifiers for each data type.
Format Specifier Data Type
%d or %i int
%f or %F float
%lf double
%c char
%s Used for strings (text), which you will learn more
about in a later chapter
Input and Output: Input and output statement; Reading a character;
Writing characters; Formatted input and output statement.
Input and Output:
 C language has standard libraries that allow input and output in a program.
 The stdio.h or standard input output library in C that has methods for
input and output.
 scanf()
 printf()
scanf()
 The scanf() method, in C, reads the value from the console as per the type
specified and store it in the given address.
Syntax:
scanf("%X", &variableOfXType);
 where %X is the format specifier in C. It is a way to tell the compiler what
type of data is in a variable and & is the address operator in C, which tells
the compiler to change the real value of variableOfXType, stored at this
address in the memory.
 Example:
scanf(“%d”, &a);
printf()
 The printf() method, in C, prints the value passed as the parameter to it, on
the console screen.
Syntax:
printf("%X", variableOfXType);
 where %X is the format specifier in C. It is a way to tell the compiler what
type of data is in a variable and variableOfXType is the variable to be
printed.
 Example:
printf(“%d”, a);
The Syntax for input and output for these are:
 Integer:
Input: scanf("%d", &intVariable);
Output: printf("%d", intVariable);
 Float:
Input: scanf("%f", &floatVariable);
Output: printf("%f", floatVariable);
 Character:
Input: scanf("%c", &charVariable);
Output: printf("%c", charVariable);
Example:
// C program to show input and output
#include <stdio.h>
int main()
{
int num; // Declare the variables
char ch;
float f;
printf("Enter the integer: "); // Input the integer
scanf("%d", &num);
printf("nEntered integer is: %d", num); // Output the integer
while((getchar()) != 'n'); //For input Clearing buffer
printf("nnEnter the float: "); // Input the float
scanf("%f", &f);
printf("nEntered float is: %f", f); // Output the float
printf("nnEnter the Character: "); // Input the Character
scanf("%c", &ch);
printf("nEntered character is: %c", ch); // Output the Character
return 0;
}
Input and Output Functions in C:
 Reading input from the user and showing it on the console (output).
 C language provides libraries (header files) that contain various functions for
input and output.
The Standard Files in C:
 The C language treats all the devices as files.
 So, devices such as the "display" are addressed in the same way as "files".
 The following three files are automatically opened when a program executes
to provide access to the keyboard and screen.
Standard File File Device
Standard input Stdin Keyboard
Standard output Stdout Screen
Standard error Stderr Your screen
Types of Input and Output Functions:
We have the following categories of IO function in C −
 Unformatted character IO functions: getchar() and putchar()
 Unformatted string IO functions: gets() and puts()
 Formatted IO functions: scanf() and printf()
Unformatted Character Input & Output Functions: getchar() and putchar()
 These two functions accept a single character as input from the keyboard,
and display a single character on the outpt terminal, respectively.
 The getchar() function it reads a single key stroke without the Enter key.
int getchar(void)
 There are no parameters required. The function returns an integer
corresponding to the ASCII value of the character key input by the user.
Example:
The following program reads a single key into a char variable −
#include <stdio.h>
int main()
{
char ch;
printf("Enter a character: ");
ch = getchar();
puts("You entered: ");
putchar(ch);
return 0;
}
Output:
Enter a character: W
Example:
#include <stdio.h>
int main()
{
char ch;
for(ch = 'A' ; ch <= 'Z' ; ch++)
{
putchar(ch);
}
return(0);
}
Output:
ABCDEFGHIJKLMNOPQRSTUVWXYZ
Formatted String Input & Output Functions: gets(), fgets(), puts(), and fputs():
 The char *gets(char *str) function reads a line from stdin into the buffer
pointed to by str until either a terminating newline or EOF (End of File) is
encountered.
char *gets(char *str)
 This function has a single argument.
 It is the pointer to an array of chars where the C string is stored.
 The function returns "str" on success and NULL on error or when EOF occurs
while no characters have been read.
Example:
#include <stdio.h>
int main()
{
char name[20];
printf("Enter your name: ");
gets(name);
printf("You entered the name: %s", name);
return 0;
}
Output:
Enter your name: Ravikant
 scanf("%s") reads characters until it encounters a whitespace (space, tab,
newline, etc.) or EOF.
THANK YOU

More Related Content

PPTX
C Programming - Basics of c -history of c
DHIVYAB17
 
PPTX
Unit-1 (introduction to c language).pptx
saivasu4
 
PPT
history of c.ppt
arpanabharani
 
DOCX
Uniti classnotes
Sowri Rajan
 
PPTX
unit2.pptx
sscprep9
 
PPTX
Introduction%20C.pptx
20EUEE018DEEPAKM
 
PDF
Unit 2 introduction to c programming
Mithun DSouza
 
PDF
EC2311-Data Structures and C Programming
Padma Priya
 
C Programming - Basics of c -history of c
DHIVYAB17
 
Unit-1 (introduction to c language).pptx
saivasu4
 
history of c.ppt
arpanabharani
 
Uniti classnotes
Sowri Rajan
 
unit2.pptx
sscprep9
 
Introduction%20C.pptx
20EUEE018DEEPAKM
 
Unit 2 introduction to c programming
Mithun DSouza
 
EC2311-Data Structures and C Programming
Padma Priya
 

Similar to C Programming Language Introduction and C Tokens.pdf (20)

PPTX
C programming Training in Ambala ! Batra Computer Centre
jatin batra
 
PPTX
Aniket tore
anikettore1
 
PPTX
C language ppt
Ğäùråv Júñêjå
 
PPTX
C Program basic concepts using c knoweledge
priankarr1
 
PDF
CP c++ programing project Unit 1 intro.pdf
ShivamYadav886008
 
DOCX
Module 1 PCD.docx
VijayKumar886687
 
PPTX
C PROGRAMMING LANGUAGE.pptx
AnshSrivastava48
 
PPTX
UNIT 5 C PROGRAMMING, PROGRAM STRUCTURE
MUHUMUZAONAN1
 
DOCX
C programming languag for cse students
Abdur Rahim
 
PPTX
c programming session 1.pptx
RSathyaPriyaCSEKIOT
 
PDF
PROGRAMMING IN C UNIT II.pdfFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
dinesh620610
 
PDF
The New Yorker cartoon premium membership of the
shubhamgupta7133
 
PPT
Chap 1 and 2
Selva Arrunaa Mathavan
 
PPT
Introduction to C Programming
MOHAMAD NOH AHMAD
 
PPT
Unit 4 Foc
JAYA
 
PPT
IT22101 Programming for Problem Solvingunit-2.ppt
kavitham66441
 
PDF
C pdf
amit9765
 
PDF
Rr
VK AG
 
PDF
Introduction to the c programming language (amazing and easy book for beginners)
mujeeb memon
 
PPTX
C Programming UNIT 1.pptx
Mugilvannan11
 
C programming Training in Ambala ! Batra Computer Centre
jatin batra
 
Aniket tore
anikettore1
 
C language ppt
Ğäùråv Júñêjå
 
C Program basic concepts using c knoweledge
priankarr1
 
CP c++ programing project Unit 1 intro.pdf
ShivamYadav886008
 
Module 1 PCD.docx
VijayKumar886687
 
C PROGRAMMING LANGUAGE.pptx
AnshSrivastava48
 
UNIT 5 C PROGRAMMING, PROGRAM STRUCTURE
MUHUMUZAONAN1
 
C programming languag for cse students
Abdur Rahim
 
c programming session 1.pptx
RSathyaPriyaCSEKIOT
 
PROGRAMMING IN C UNIT II.pdfFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
dinesh620610
 
The New Yorker cartoon premium membership of the
shubhamgupta7133
 
Introduction to C Programming
MOHAMAD NOH AHMAD
 
Unit 4 Foc
JAYA
 
IT22101 Programming for Problem Solvingunit-2.ppt
kavitham66441
 
C pdf
amit9765
 
Rr
VK AG
 
Introduction to the c programming language (amazing and easy book for beginners)
mujeeb memon
 
C Programming UNIT 1.pptx
Mugilvannan11
 
Ad

More from poongothai11 (10)

PPTX
Data Structures Stack and Queue Data Structures
poongothai11
 
PPTX
Unix / Linux Operating System introduction.
poongothai11
 
PPTX
Introduction to Database Management Systems
poongothai11
 
PDF
Open System Interconnection model and its layers.pdf
poongothai11
 
PPTX
Computer Organization Introduction, Generations of Computer.pptx
poongothai11
 
PPTX
Stack and its operations, Queue and its operations
poongothai11
 
PPTX
Searching and Sorting Algorithms in Data Structures
poongothai11
 
PPTX
Thread Concept: Multithreading, Creating thread using thread
poongothai11
 
PPTX
Exception Handling Multithreading: Fundamental of Exception; Exception types;...
poongothai11
 
PPTX
Introduction to Data Structures, Data Structures using C.pptx
poongothai11
 
Data Structures Stack and Queue Data Structures
poongothai11
 
Unix / Linux Operating System introduction.
poongothai11
 
Introduction to Database Management Systems
poongothai11
 
Open System Interconnection model and its layers.pdf
poongothai11
 
Computer Organization Introduction, Generations of Computer.pptx
poongothai11
 
Stack and its operations, Queue and its operations
poongothai11
 
Searching and Sorting Algorithms in Data Structures
poongothai11
 
Thread Concept: Multithreading, Creating thread using thread
poongothai11
 
Exception Handling Multithreading: Fundamental of Exception; Exception types;...
poongothai11
 
Introduction to Data Structures, Data Structures using C.pptx
poongothai11
 
Ad

Recently uploaded (20)

PDF
The Picture of Dorian Gray summary and depiction
opaliyahemel
 
PDF
Types of Literary Text: Poetry and Prose
kaelandreabibit
 
PPTX
Understanding operators in c language.pptx
auteharshil95
 
PPTX
Congenital Hypothyroidism pptx
AneetaSharma15
 
PPTX
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
PPTX
IMMUNIZATION PROGRAMME pptx
AneetaSharma15
 
PPTX
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
PPTX
Strengthening open access through collaboration: building connections with OP...
Jisc
 
PDF
Wings of Fire Book by Dr. A.P.J Abdul Kalam Full PDF
hetalvaishnav93
 
PPTX
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
PDF
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
PPTX
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
RAKESH SAJJAN
 
PDF
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
PPTX
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
PDF
Virat Kohli- the Pride of Indian cricket
kushpar147
 
DOCX
Action Plan_ARAL PROGRAM_ STAND ALONE SHS.docx
Levenmartlacuna1
 
PPTX
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
PPTX
Skill Development Program For Physiotherapy Students by SRY.pptx
Prof.Dr.Y.SHANTHOSHRAJA MPT Orthopedic., MSc Microbiology
 
PPTX
Odoo 18 Sales_ Managing Quotation Validity
Celine George
 
PPTX
vedic maths in python:unleasing ancient wisdom with modern code
mistrymuskan14
 
The Picture of Dorian Gray summary and depiction
opaliyahemel
 
Types of Literary Text: Poetry and Prose
kaelandreabibit
 
Understanding operators in c language.pptx
auteharshil95
 
Congenital Hypothyroidism pptx
AneetaSharma15
 
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
IMMUNIZATION PROGRAMME pptx
AneetaSharma15
 
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
Strengthening open access through collaboration: building connections with OP...
Jisc
 
Wings of Fire Book by Dr. A.P.J Abdul Kalam Full PDF
hetalvaishnav93
 
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
RAKESH SAJJAN
 
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
Virat Kohli- the Pride of Indian cricket
kushpar147
 
Action Plan_ARAL PROGRAM_ STAND ALONE SHS.docx
Levenmartlacuna1
 
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
Skill Development Program For Physiotherapy Students by SRY.pptx
Prof.Dr.Y.SHANTHOSHRAJA MPT Orthopedic., MSc Microbiology
 
Odoo 18 Sales_ Managing Quotation Validity
Celine George
 
vedic maths in python:unleasing ancient wisdom with modern code
mistrymuskan14
 

C Programming Language Introduction and C Tokens.pdf

  • 1. Introduction Overview of C C Programming Dr. P. Poongothai Kristu Jayanti College
  • 2. Introduction Overview of C:  C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972.  It is a very popular language, despite being old.  The main reason for its popularity is because it is a fundamental language in the field of computer science.  C is strongly associated with UNIX, as it was developed to write the UNIX operating system. Importance of C:  C is called as a robust language, which has so many built-in functions and operations, which can be used to write any complex program.  The C programming language is used to create operating systems, compilers, interpreters, third-party libraries, and databases.  ‘C’ Programs are efficient and fast.  ‘C’ language is best for structured programming, where the user can think of a problem in terms of function modules (or) blocks.  It has the ability to extend itself.  C is a general-purpose structured language that promotes code organization through functions. Basic structure of a C program:  The basic structure of a C program is divided into 6 Section which responsible for the proper execution of a program. 1. Documentation Section 2. Preprocessor Section (Link Section)
  • 3. 3. Definition Section 4. Global Declaration Section 5. Main() Function Section 6. Sub Programs Section 1. Documentation  It consists of the description of the program, the name of the program, and the creation date and time of the program. // description, name of the program, programmer name, date, time etc. 2. Preprocessor Section  All the header files of the program will be declared in the preprocessor section of the program. #include<stdio.h> #include<math.h> 3. Definition  Preprocessors are the programs that process our source code before the process of compilation.  Preprocessor directives start with the ‘#’ symbol.  The #define preprocessor is used to create a constant throughout the program. #define long long ll 4. Global Declaration  The global declaration section contains global variables, function declaration, and static variables.
  • 4. int num = 18; 5. Main() Function  Operations like declaration and execution are performed inside the curly braces of the main program.  The return type of the main() function can be ‘int’ as well as ‘void’ too.  void() main tells the compiler that the program will not return any value.  The int main() tells the compiler that the program will return an integer value. void main() int main() 6. Sub Programs  User-defined functions are called sub programs of the program.  The control of the program is shifted to the called function whenever they are called from the main or outside the main() function.  These are specified as per the requirements of the programmer. int sum(int x, int y) { return x+y; } Structure of C Program with example: // Documentation Section /** * file: sum.c * author: you * description: program to find sum.
  • 5. */ #include <stdio.h> // Link Section #define X 20 // Definition Section int sum(int y); // Global Declaration Section int main(void) // Main() Function Section { int y = 55; printf("Sum: %d", sum(y)); return 0; } int sum(int y) // Subprogram Section { return y + X; } Output: Sum: 75 Sections Description // Documentation /** * file: sum.c * author: you * description: program to find sum. */ It is the comment section and is part of the description section of the code. #include<stdio.h> Header file which is used for standard input- output. This is the preprocessor section. #define X 20 This is the definition section. It allows the use of constant X in the code. int sum(int y) This is the Global declaration section includes the function declaration that can be used anywhere in the program.
  • 6. int main() main() is the first function that is executed in the C program. {…} These curly braces mark the beginning and end of the main function. printf(“Sum: %d”, sum(y)); printf() function is used to print the sum on the screen. return 0; We have used int as the return type so we have to return 0 which states that the given program is free from the error and it can be exited successfully. int sum(int y) { return y + X; } This is the subprogram section. It includes the user-defined functions that are called in the main() function.
  • 7. Introduction Overview of C C Programming Dr. P. Poongothai Kristu Jayanti College
  • 8. Tokens in C:  The tokens of C language can be classified into six types based on the functions they are used to perform. 1. Keywords 2. Identifiers 3. Constants 4. Strings 5. Special Symbols 6. Operators 1. C Token – Keywords:  The keywords are pre-defined or reserved words in a programming language.  Each keyword is meant to perform a specific function in a program.  Since keywords are referred names for a compiler, they can’t be used as variable names.
  • 9.  If we are trying to assign a new meaning to the keyword which is not allowed.  C language supports 32 keywords which are given below: 2. C Token – Identifiers:  Identifiers are used as the general terminology for the naming of variables, functions, and arrays.  These are user-defined names consisting of a long sequence of letters and digits with either a letter or the underscore ( _ ) as a first character.  Identifier names must differ in spelling and case from any keywords.  We cannot use keywords as identifiers; they are reserved for special use. Rules for Naming Identifiers:  They must begin with a letter or underscore ( _ ).  They must consist of only letters, digits, or underscore. No other special character is allowed.  It should not be a keyword.  It must not contain white space.
  • 10.  It should be up to 31 characters long as only the first 31 characters are significant.  For example, main: method name. a: variable name. 3. C Token – Constants:  The constants refer to the variables with fixed values.  Constants may belong to any of the data types.  Examples of Constants in C const int a = 20; const int* const ptr = &a;  Types of Constants in C  Integer Constant  Character Constant  Floating Point Constant  Double Precision Floating Point Constant  Array Constant  Structure Constant
  • 11. 4. C Token – Strings:  Strings are nothing but an array of characters ended with a null character (‘0’).  This null character indicates the end of the string.  Strings are always enclosed in double quotes.  Whereas, a character is enclosed in single quotes in C and C++.  Examples of String char string[10] = {‘b’, ’c’, ‘a’, ‘0’}; char string[10] = “bca”; char string [] = “bca”; 5. C Token – Special Symbols: The following special symbols are used in C having some special meaning.  Brackets [ ]: It is used as array element references. These indicate single and multidimensional subscripts.  Parentheses ( ): These special symbols are used to indicate function calls and function parameters.  Braces{ }: These opening and ending curly braces mark the start and end of a block of code containing more than one executable statement.  Comma , : It is used to separate more than one statement like for separating parameters in function calls.  Colon : : It is an operator that essentially invokes something called an initialization list.  Semicolon ; : It is known as a statement terminator. It indicates the end of one logical entity. That’s why each individual statement must be ended with a semicolon.
  • 12.  Asterisk * : It is used to create a pointer variable and for the multiplication of variables.  Assignment operator = : It is used to assign values and for logical operation validation.  Pre-processor # : The preprocessor is a macro processor that is used automatically by the compiler to transform your program before actual compilation.  Period . : Used to access members of a structure or union.  Tilde ~ : Bitwise One’s Complement Operator. 6. C Token – Operators: Operators are symbols that trigger an action when applied to C variables and other objects. The data items on which operators act are called operands. Depending on the number of operands that an operator can act upon, operators can be classified as follows:  Unary Operators: Those operators that require only a single operand to act upon are known as unary operators. Example increment and decrement operators  Binary Operators: Those operators that require two operands to act upon are called binary operators. Binary operators can further are classified into: 1. Arithmetic operators 2. Relational Operators 3. Logical Operators 4. Assignment Operators 5. Bitwise Operator
  • 13.  Ternary Operator: The operator that requires three operands to act upon is called the ternary operator. Conditional Operator(?) is also called the ternary operator. Variables:  Variables are containers for storing data values, like numbers and characters.  In C, there are different types of variables (defined with different keywords), for example:  int - stores integers (whole numbers), without decimals, such as 123 or -123  float - stores floating point numbers, with decimals, such as 19.99 or -19.99  char - stores single characters, such as 'a' or 'B'. Characters are surrounded by single quotes Declaring (Creating) Variables: Syntax: type variableName = value; Example: int myNum = 15; (OR) Example: int myNum; // Declare a variable myNum = 15; // Assign a value to the variable The general rules for naming variables are:  Names can contain letters, digits and underscores  Names must begin with a letter or an underscore ( _ )  Names are case-sensitive (myVar and myvar are different variables)  Names cannot contain whitespaces or special characters like !, #, %, etc.  Reserved words (such as int) cannot be used as names
  • 14. Data Types in C:  Each variable in C has an associated data type.  It specifies the type of data that the variable can store like integer, character, floating, double, etc.  Each data type requires different amounts of memory and has some specific operations which can be performed over it.  The data type is a collection of data with values having fixed values The data types in C can be classified as follows: Types Description Primitive Data Types Primitive data types are the most basic data types that are used for representing simple values such as integers, float, characters, etc. User Defined Data Types The user-defined data types are defined by the user himself. Derived Types The data types that are derived from the primitive or built-in datatypes are referred to as Derived Data Types.
  • 15.  Different data types also have different ranges up to which they can store numbers.  These ranges may vary from compiler to compiler. Data Type Size (bytes) Range Format Specifier short int 2 -32,768 to 32,767 %hd unsigned short int 2 0 to 65,535 %hu unsigned int 4 0 to 4,294,967,295 %u int 4 -2,147,483,648 to 2,147,483,647 %d long int 4 -2,147,483,648 to 2,147,483,647 %ld
  • 16. Data Type Size (bytes) Range Format Specifier unsigned long int 4 0 to 4,294,967,295 %lu long long int 8 -(2^63) to (2^63)-1 %lld unsigned long long int 8 0 to 18,446,744,073,709,551,615 %llu signed char 1 -128 to 127 %c unsigned char 1 0 to 255 %c float 4 1.2E-38 to 3.4E+38 %f double 8 1.7E-308 to 1.7E+308 %lf long double 16 3.4E-4932 to 1.1E+4932 %Lf Note: The long, short, signed and unsigned are datatype modifier that can be used with some primitive data types to change the size or length of the datatype.
  • 17. The following are some MAIN PRIMITIVE DATA TYPES in C: Basic Data Types The data type specifies the size and type of information the variable will store. Data Type Size Description Example int 2 or 4 bytes Stores whole numbers, without decimals 1 float 4 bytes Stores fractional numbers, containing one or more decimals. Sufficient for storing 6-7 decimal digits 1.99 double 8 bytes Stores fractional numbers, containing one or more decimals. Sufficient for storing 15 decimal digits 1.99 char 1 byte Stores a single character/letter/number, or ASCII values 'A' Basic Format Specifiers There are different format specifiers for each data type. Format Specifier Data Type %d or %i int %f or %F float %lf double %c char %s Used for strings (text), which you will learn more about in a later chapter
  • 18. Input and Output: Input and output statement; Reading a character; Writing characters; Formatted input and output statement. Input and Output:  C language has standard libraries that allow input and output in a program.  The stdio.h or standard input output library in C that has methods for input and output.  scanf()  printf() scanf()  The scanf() method, in C, reads the value from the console as per the type specified and store it in the given address. Syntax: scanf("%X", &variableOfXType);  where %X is the format specifier in C. It is a way to tell the compiler what type of data is in a variable and & is the address operator in C, which tells the compiler to change the real value of variableOfXType, stored at this address in the memory.  Example: scanf(“%d”, &a); printf()  The printf() method, in C, prints the value passed as the parameter to it, on the console screen. Syntax: printf("%X", variableOfXType);
  • 19.  where %X is the format specifier in C. It is a way to tell the compiler what type of data is in a variable and variableOfXType is the variable to be printed.  Example: printf(“%d”, a); The Syntax for input and output for these are:  Integer: Input: scanf("%d", &intVariable); Output: printf("%d", intVariable);  Float: Input: scanf("%f", &floatVariable); Output: printf("%f", floatVariable);  Character: Input: scanf("%c", &charVariable); Output: printf("%c", charVariable); Example: // C program to show input and output #include <stdio.h> int main() { int num; // Declare the variables char ch; float f;
  • 20. printf("Enter the integer: "); // Input the integer scanf("%d", &num); printf("nEntered integer is: %d", num); // Output the integer while((getchar()) != 'n'); //For input Clearing buffer printf("nnEnter the float: "); // Input the float scanf("%f", &f); printf("nEntered float is: %f", f); // Output the float printf("nnEnter the Character: "); // Input the Character scanf("%c", &ch); printf("nEntered character is: %c", ch); // Output the Character return 0; } Input and Output Functions in C:  Reading input from the user and showing it on the console (output).  C language provides libraries (header files) that contain various functions for input and output. The Standard Files in C:  The C language treats all the devices as files.  So, devices such as the "display" are addressed in the same way as "files".  The following three files are automatically opened when a program executes to provide access to the keyboard and screen.
  • 21. Standard File File Device Standard input Stdin Keyboard Standard output Stdout Screen Standard error Stderr Your screen Types of Input and Output Functions: We have the following categories of IO function in C −  Unformatted character IO functions: getchar() and putchar()  Unformatted string IO functions: gets() and puts()  Formatted IO functions: scanf() and printf() Unformatted Character Input & Output Functions: getchar() and putchar()  These two functions accept a single character as input from the keyboard, and display a single character on the outpt terminal, respectively.  The getchar() function it reads a single key stroke without the Enter key. int getchar(void)  There are no parameters required. The function returns an integer corresponding to the ASCII value of the character key input by the user. Example: The following program reads a single key into a char variable − #include <stdio.h> int main() {
  • 22. char ch; printf("Enter a character: "); ch = getchar(); puts("You entered: "); putchar(ch); return 0; } Output: Enter a character: W Example: #include <stdio.h> int main() { char ch; for(ch = 'A' ; ch <= 'Z' ; ch++) { putchar(ch); } return(0); } Output: ABCDEFGHIJKLMNOPQRSTUVWXYZ
  • 23. Formatted String Input & Output Functions: gets(), fgets(), puts(), and fputs():  The char *gets(char *str) function reads a line from stdin into the buffer pointed to by str until either a terminating newline or EOF (End of File) is encountered. char *gets(char *str)  This function has a single argument.  It is the pointer to an array of chars where the C string is stored.  The function returns "str" on success and NULL on error or when EOF occurs while no characters have been read. Example: #include <stdio.h> int main() { char name[20]; printf("Enter your name: "); gets(name); printf("You entered the name: %s", name); return 0; } Output: Enter your name: Ravikant  scanf("%s") reads characters until it encounters a whitespace (space, tab, newline, etc.) or EOF.