Introduction to C
Introduction to C language – Structure of C program - Character set –
token – identifiers – reserved words – Comments - data types –
constants – printf() function - variables – scanf() function - operators –
expression – declaration statement – assignment statement -
conversion of algorithm in to program – Solving simple problems
involving arithmetic computations and sequential logic to solve.
1
WHY WE LEARN C?
• C language is the most commonly used programming language.
• It is used for writing operating systems.
• UNIX was the first operating system written in C.
• Later Microsoft Windows, Mac OS X, and GNU/Linux were all written in
C.
• Not only is C the language of operating systems, it is the precursor and
inspiration for almost all of the most popular high-level languages available
today.
• In fact, Perl, PHP, Python and Ruby are all written in C.
FEATURES OF C LANGUAGE
• Robust language, which can be used to write any complex
program.
• Well-suited for writing both system software and business
applications.
• Dynamic memory allocation
• C is highly portable. This means that ‘C’ programs written from one
computer can be run on another computer with no modification.
• A ‘C’ program is basically a collection of functions that are supported
by the ‘C’ library
FEATURES OF C LANGUAGE CONT.…
• Program written in C language are efficient & fast.
• ‘C’ is a free form language.
• Case sensitive.
• C has 32 keywords.
• ‘C’ is a structure or procedural programming language that are
use top-down approach.
• Compactness & Reusability
Advantages of C
• General Purpose Language
• Portable Language
• C Provides a collection of Library files
• It support good graphics
• It supports System Programming
• It supports number of operators
• It supports arrays, pointers ,structures and union
• It is used to implementing Data Structure
• It supports structured programming and modularity
Disadvantages in C
•There is no run type checking in c Language
•Non Uniformity in associativity
•It uses same operator for multiple purpose
•It does not support exception handling
•No direct input and output facility
Valid Steps in C Language
• Every step should end with semicolon
• A function name is to be followed by a pair of paranthesis and
arguments are separated by commas
• Single quote(‘) is used for character constant and double quote
(“) is used for string constant
• Compound statements are enclosed within curly braces { }
• Comments are written to improve the readability and
understanding of programs(/*...*/)
Valid Steps in C Language
•All statements should be written in lower case
•Proper header file like <stdio.h> ,<conio.h> are to be used for
systematic programming and linking
•Uppercase characters are used for symbolic constants
•Blank spaces can be inserted between words
•Blank spaces should not be used while declaring a variable
,keyword ,constant and function
•User can write one or more statements in a line separated by
semicolon
•The number of opening and closing braces should be balanced
•Example: a=b+c;m=p*q;
Structure of C program
•C Program is divided into different sections.
•There are six main sections to a basic c program.
The six sections are,
•Documentation
•Link
•Definition
•Global Declarations
•Main Functions
•Subprograms
Structure of C program
Structure of C program
Structure of C program
Structure of C program
Structure of C program
Documentation Section
The documentation section is the part of the program where the
programmer gives the details associated with the program. He/she
usually gives the name of the program, the details of the author and
other details like the time of coding and description. It gives anyone
reading the code the overview of the code.
Example
/**
* File Name: Helloworld.c
* Author: SIT
* description: a program to display hello world
* no input needed
*/
Structure of C program
Link Section
This part of the code is used to declare all the header files that will be
used in the program. This leads to the compiler being told to link the
header files to the system libraries.
Example
#include<stdio.h>
Definition Section
In this section, we define different constants. The keyword define is
used in this part.
#define PI=3.14
Structure of C program
Global Declaration Section
This part of the code is the part where the global variables are declared.
All the global variable used are declared in this part. The user-defined
functions are also declared in this part of the code.
EXAMPLE
float area(float r);
int a=7;
Structure of C program
Main Function Section
Every C-programs needs to have the main function. Each main function
contains 2 parts. A declaration part and an Execution part. The
declaration part is the part where all the variables are declared. The
execution part begins with the curly brackets and ends with the curly
close bracket. Both the declaration and execution part are inside the
curly braces.
EXAMPLE
int main(void)
{
int a=10;
printf(" %d", a);
return 0;
}
Structure of C program
Sub Program Section
• All the user-defined functions are defined in this section of the
program.
• EXAMPLE
• int add(int a, int b)
• {
• return a+b;
• }
Structure of C program
• Sample Program
• The C program here will find the area of a circle using a user-defined function and a global variable pi holding the value of pi
• /** File Name: areaofcircle.c //Documentation section
• * Author: Manthan Naik
• * date: 09/08/2019
• * description: a program to calculate area of circle
• *user enters the radius
• **/
• #include<stdio.h> //link section
• #define pi 3.14; //definition section
• float area(float r); //global declaration
• int main() //main function
• {
• float r;
• printf(" Enter the radius:n");
• scanf("%f",&r);
• printf("the area is: %f",area(r));
• return 0;
• }
• float area(float r)
• {
• return pi * r * r; //sub program
• }
• Output
Character set
• Character is a part of a word ,sentences or paragraph.
• A character set defines the valid characters that can be used in
source programs or interpreted when a program is running.
• The source character set is the set of characters available for the
source text.
C Tokens
Keywords
•Reserved words
•32 keywords
•Fixed meaning which are already defined. It cannot be altered
Identifiers
• C identifiers represent the name in the C program, for example,
variables, functions, arrays, structures, unions, labels, etc.
• An identifier can be composed of letters such as uppercase,
lowercase letters, underscore, digits, but the starting letter should be
either an alphabet or an underscore.
• Example
Identifiers
• Valid Identifiers
name,age,sub,tot_marks,a10
• Invalid Identifiers
2sub,$price,date of birth,auto
Constants
Constants
Comments
Multiline Comments
Variables
• Each variable in C has a specific type, which determines the size
and layout of the variable's memory; the range of values that can be
stored within that memory; and the set of operations that can be
applied to the variable.
• Syntax:
• Data_type variable_name;
• Declaration of Variables : int a=20; int a,b,c;
• float num=10.5;
Rules for declare the Variables

Introduction of c programming unit-ii ppt

  • 1.
    Introduction to C Introductionto C language – Structure of C program - Character set – token – identifiers – reserved words – Comments - data types – constants – printf() function - variables – scanf() function - operators – expression – declaration statement – assignment statement - conversion of algorithm in to program – Solving simple problems involving arithmetic computations and sequential logic to solve. 1
  • 2.
    WHY WE LEARNC? • C language is the most commonly used programming language. • It is used for writing operating systems. • UNIX was the first operating system written in C. • Later Microsoft Windows, Mac OS X, and GNU/Linux were all written in C. • Not only is C the language of operating systems, it is the precursor and inspiration for almost all of the most popular high-level languages available today. • In fact, Perl, PHP, Python and Ruby are all written in C.
  • 3.
    FEATURES OF CLANGUAGE • Robust language, which can be used to write any complex program. • Well-suited for writing both system software and business applications. • Dynamic memory allocation • C is highly portable. This means that ‘C’ programs written from one computer can be run on another computer with no modification. • A ‘C’ program is basically a collection of functions that are supported by the ‘C’ library
  • 4.
    FEATURES OF CLANGUAGE CONT.… • Program written in C language are efficient & fast. • ‘C’ is a free form language. • Case sensitive. • C has 32 keywords. • ‘C’ is a structure or procedural programming language that are use top-down approach. • Compactness & Reusability
  • 5.
    Advantages of C •General Purpose Language • Portable Language • C Provides a collection of Library files • It support good graphics • It supports System Programming • It supports number of operators • It supports arrays, pointers ,structures and union • It is used to implementing Data Structure • It supports structured programming and modularity
  • 6.
    Disadvantages in C •Thereis no run type checking in c Language •Non Uniformity in associativity •It uses same operator for multiple purpose •It does not support exception handling •No direct input and output facility
  • 7.
    Valid Steps inC Language • Every step should end with semicolon • A function name is to be followed by a pair of paranthesis and arguments are separated by commas • Single quote(‘) is used for character constant and double quote (“) is used for string constant • Compound statements are enclosed within curly braces { } • Comments are written to improve the readability and understanding of programs(/*...*/)
  • 8.
    Valid Steps inC Language •All statements should be written in lower case •Proper header file like <stdio.h> ,<conio.h> are to be used for systematic programming and linking •Uppercase characters are used for symbolic constants •Blank spaces can be inserted between words •Blank spaces should not be used while declaring a variable ,keyword ,constant and function •User can write one or more statements in a line separated by semicolon •The number of opening and closing braces should be balanced •Example: a=b+c;m=p*q;
  • 9.
    Structure of Cprogram •C Program is divided into different sections. •There are six main sections to a basic c program. The six sections are, •Documentation •Link •Definition •Global Declarations •Main Functions •Subprograms
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
    Structure of Cprogram Documentation Section The documentation section is the part of the program where the programmer gives the details associated with the program. He/she usually gives the name of the program, the details of the author and other details like the time of coding and description. It gives anyone reading the code the overview of the code. Example /** * File Name: Helloworld.c * Author: SIT * description: a program to display hello world * no input needed */
  • 15.
    Structure of Cprogram Link Section This part of the code is used to declare all the header files that will be used in the program. This leads to the compiler being told to link the header files to the system libraries. Example #include<stdio.h> Definition Section In this section, we define different constants. The keyword define is used in this part. #define PI=3.14
  • 16.
    Structure of Cprogram Global Declaration Section This part of the code is the part where the global variables are declared. All the global variable used are declared in this part. The user-defined functions are also declared in this part of the code. EXAMPLE float area(float r); int a=7;
  • 17.
    Structure of Cprogram Main Function Section Every C-programs needs to have the main function. Each main function contains 2 parts. A declaration part and an Execution part. The declaration part is the part where all the variables are declared. The execution part begins with the curly brackets and ends with the curly close bracket. Both the declaration and execution part are inside the curly braces. EXAMPLE int main(void) { int a=10; printf(" %d", a); return 0; }
  • 18.
    Structure of Cprogram Sub Program Section • All the user-defined functions are defined in this section of the program. • EXAMPLE • int add(int a, int b) • { • return a+b; • }
  • 19.
    Structure of Cprogram • Sample Program • The C program here will find the area of a circle using a user-defined function and a global variable pi holding the value of pi • /** File Name: areaofcircle.c //Documentation section • * Author: Manthan Naik • * date: 09/08/2019 • * description: a program to calculate area of circle • *user enters the radius • **/ • #include<stdio.h> //link section • #define pi 3.14; //definition section • float area(float r); //global declaration • int main() //main function • { • float r; • printf(" Enter the radius:n"); • scanf("%f",&r); • printf("the area is: %f",area(r)); • return 0; • } • float area(float r) • { • return pi * r * r; //sub program • } • Output
  • 20.
    Character set • Characteris a part of a word ,sentences or paragraph. • A character set defines the valid characters that can be used in source programs or interpreted when a program is running. • The source character set is the set of characters available for the source text.
  • 21.
  • 22.
    Keywords •Reserved words •32 keywords •Fixedmeaning which are already defined. It cannot be altered
  • 23.
    Identifiers • C identifiersrepresent the name in the C program, for example, variables, functions, arrays, structures, unions, labels, etc. • An identifier can be composed of letters such as uppercase, lowercase letters, underscore, digits, but the starting letter should be either an alphabet or an underscore. • Example
  • 24.
    Identifiers • Valid Identifiers name,age,sub,tot_marks,a10 •Invalid Identifiers 2sub,$price,date of birth,auto
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
    Variables • Each variablein C has a specific type, which determines the size and layout of the variable's memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable. • Syntax: • Data_type variable_name; • Declaration of Variables : int a=20; int a,b,c; • float num=10.5;
  • 30.
    Rules for declarethe Variables