Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

Brief Introduction to C Programming

Language Akshata B. Angadi


Assistant Professor,
Dept. of CSE
G.I.T.,Belagavi
Structure of a simple
program :

Akshata Angadi, Assistant Professor, CSE Dept., GIT To Alg./Flow.


Key Note
• Every C has a Basic/specific Structure.
• Execution of C starts from main( ) function always.
• Without main( ) function – program doesn’t exists.
• So a C Program should consist of at least one function i.e.
main( )
• Every statement should end with semicolon.
• Executable statements of Every function must be placed within
opening brace { and a closing brace }.

Akshata Angadi, Assistant Professor, CSE Dept., GIT


General Structure of C program
Documentation section
Link section
Definition section
Global declaration section
main ( )
{
declaration part;

executable statements;

}
user defined functions
Akshata Angadi, Assistant Professor, CSE Dept., GIT
• The Documentation Section
• consists of a set of comment lines giving the name of the program and other
details.
• The Link Section
• provides instructions to the compiler to link functions from the system library.
• The Definition Section
• defines all symbolic constants.
• The Global Declaration Section:
• Variables are declared outside the function so that it is easily accessible to all
other functions defined in a program.
• main() function:
• Every C program must have one main function section. This section contains
two parts, declaration and executable part.
• It is an entry point of all program.
Akshata Angadi, Assistant Professor, CSE Dept., GIT
Main() consists of 2 parts :-

• Declaration Part
• declares all the variables used in the executable part.
• Execution Part
• There should be at least one statement in the executable part which
contains instructions to perform certain task.
• The declaration and executable part must appear between the
opening and closing braces.
• All statements in the declaration part should end with the semicolon.

• The Subprogram Section


• contains all the user defined functions that are called in the
main function.
Akshata Angadi, Assistant Professor, CSE Dept., GIT
Few Header Files..
Link Section includes header files.
• stdio.h
• printf( ) , scanf( ) , getchar( ), putchar ( )
• math.h
• sqrt( ) , pow( )
• string.h
• strlen( ) , strcat( )

Note : use #include preprocessor directive to define header file.


Syntax: #include< header_file_name >
Akshata Angadi, Assistant Professor, CSE Dept., GIT
Key Note
• Instructions are formed with character sets.
• Letters, special characters, Digits and whitespaces all together -
character set.
• There are 6 types of C Tokens.
• Tokens - individual words and punctuation marks
C Tokens :
1. Keywords
2. Identifiers
3. Constants 4. Strings
5. Operators 6. Special Symbols
Akshata Angadi, Assistant Professor, CSE Dept., GIT
Keywords :
• Basic building blocks for program statements
• Lowercase letters
• Meanings are fixed, cannot be altered & is known to C Compiler,
• Cannot be used as variable names
Ex: if , else , while , for , int , float , case , switch etc..

Identifiers
• Refers to names given to the variables, functions and arrays.
Rules :
• Keywords cannot be used as Identifiers
• Case sensitive
• First character should be alphabet or underscore.
• Must consist of only letters, digits or underscore.
• Must not contain white space
Akshata Angadi, Assistant Professor, CSE Dept., GIT
Constants :
• Constants -> fixed values
• The values will not be changed during execution of program
• Can define using const word or using symbolic constants

Strings :
• Sequence of Characters is said to be Strings.
• Character is written in single quote where as Strings are placed
in double quotes.

Akshata Angadi, Assistant Professor, CSE Dept., GIT


Data types
Data types specify what type of data we enter data into our
programs.
Data types in C refer to an extensive system used for
declaring variables or functions of different types.

C supports two classes of data types:


i.Primary or fundamental data type.[int, float, double,
char, void].
ii.Derived data type.[arrays, pointers].

Akshata Angadi, Assistant Professor, CSE Dept., GIT


Basic Types
Integer Types
Type Storage size Value range (16 bit machine)
char 1 byte -128 to 127

int 2 bytes or 4 bytes -32,768 to 32,767

Float Types
Type Storage size Value range (16-bit machine) Precision
float 4 byte 3.4E-38 to 3.4E+38 6 decimal places

double 8 byte 1.7E-308 to 1.7E+308 15 decimal places

void – NO RETURN TYPE , used to specify the type of functions.


Akshata Angadi, Assistant Professor, CSE Dept., GIT
Variable
Naming of an address (??) is known as variable
Variable is a storage area to hold a data, that our programs
can manipulate.
Identifier assigning a Unique Name to a variable.

Each variable in C has a specific type(data type),


➢ 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
Akshata Angadi, Assistant Professor, CSE Dept., GIT
Rules for declaring
▪ It must begin with either a letter or an underscore.
▪ The name of a variable can be composed of letters, digits and
the underscore character.
▪ Upper and lowercase letters are distinct.

C is case-sensitive.
Example (Valid):
Salary_Employee, var1, var_2, _v3 , _V3 , sum
(Invalid)
^sal, dfg), #var1 , 1var3 , $sum
Akshata Angadi, Assistant Professor, CSE Dept., GIT
Variable Definition and Initialization
Variable Definition/Declaration
<datatype> var_list;
Variable Initialization
<datatype> var_name= var_value;

Eg:
int A,B,C; // variable definition
A=3; // variable initialization
B=4;
C=10;
Akshata Angadi, Assistant Professor, CSE Dept., GIT
Basic Data Types

Format
Type Description
Specifier
Typically a single octet(one byte). This is an character
char %c
type.
int The most natural size of integer for the machine. %d

long int Long Integer values %ld

float A single-precision floating point value. %f

double A double-precision floating point value. %lf

Akshata Angadi, Assistant Professor, CSE Dept., GIT


Formatted input & output funcions
i. scanf ( ) ii. printf ( )

i. scanf- Syntax:
scanf ( “ control string “ , &var1, &var2, --- &varn) ;
where
control string  controls the format in which data is read.
&var1,&var2  addresses of variables
ii. printf-Syntax:
printf ( “ control string “ , var1, var2, --- varn) ;
where
control string  controls the format in which data is displayed.
var1,var2  values of variables

All these functions are present in a header file stdio.h


(standard input output header
Akshatafile)
Angadi, Assistant Professor, CSE Dept., GIT
printf( ) function
printf( ) used to display text and to display values present in variables using
format specifiers.

Example:
Write a C Program to print Hello World.
// Program to print Hello World
#include<stdio.h> //Link Section
main( )
{
printf(“Hello World”) ; //Executable Statement
return 0 ;
} Akshata Angadi, Assistant Professor, CSE Dept., GIT
Sample Programs
• Write a program to print your Full Name (single line).
• Write a program to print your Mom Name twice.
• Write a Program to print your Home Address (multi line statement).
• Write a program to print : (PRINT ALL characters given below)
****”Hurray !!! My Team (India), WON the Match”.****

Akshata Angadi, Assistant Professor, CSE Dept., GIT


Sample Questions
1. List two types of primary memory.
2. List two categories of constants.
3. List three programming languages.
4. Give the syntax of formatted input function in C.
5. Explain about C tokens.
6. Define Identifier. List the rules for framing identifiers
with example.
7. List primary data-types.

Akshata Angadi, Assistant Professor, CSE Dept., GIT


Sample Questions
9. Name the translator which is used to convert high
level language to low level language.
10. Write the syntax for declaration of variable.
11. Give one example for a device which acts as both
input & output devices..

Akshata Angadi, Assistant Professor, CSE Dept., GIT


ASSIGN-2
• WAP to read 2 numbers and print the SUM of 2.
• WAP to evaluate MEAN of given 5 numbers.
• WAP to Calculate Area and Circumference of Circle.
• WAP to Calculate Area of Triangle.
• WAP to print Quotient, Remainder of given 2 numbers .
• In all the above programs classify variables , data types used, operators used, functions included , header
files declared and keywords present in it .
• Write a C Program to solve following expressions:
(A=5, B=3, C=6, D=2, E=1, F=9, G=4)
▪ A+B-C/D
▪ (A + B) * C - (D - E) * (F + G)
(a=9,b=12,c=13)
• x = a – b / 3 + c * 2 – 1;
• y = a – b / (3 + c) * (2 – 1);
• z = a – ( b / (3 + c) * 2) – 1;Akshata Angadi, Assistant Professor, CSE Dept., GIT

You might also like