Introduction to c
Introduction to c
PROGRAMMING IN C
UNIT I
BASICS OF C PROGRAMMING
the functions.
ex : int a,b;
float c,d;
STRUCTURE OF C PROGRAM
Function main :
Every program written in C Language must containing
main() function.
The program starts from the opening brace{ and ends with
closeing brace } , the program should declare this section
contain two parts declaration and executable part.
ex : main()
STRUCTURE OF C PROGRAM
Declaration part
The entire variables that are used in exactable part.
Executable part
The part contains a set of statements following the declaration
of the variables.
Ex :
{
c=a+b;
printf ("the sum is=%d",c) ;
}
STRUCTURE OF C PROGRAM
Sample C program
/* This is sample program */
# include <stdio.h>
#include <conio.h>
void main()
{
int a=10,b=20;
printf("Hello, welcome to C program\n");
c=a+b;
printf("the sum of c is =%d",c);
}
Output
Hello,welcome to C program
the sum of c is=30
Programming rules
3. The programmer can also write the statement within the two
brace
5. Every program must have exactly one main function, more that
one main function the compiler cannot understand.
2.Digits (0 to 9)
Decimal Constant
◦ An Decimal constantt consists of whole number which has no
decimal point
◦ Embedded spaces, commas and non-digit characters are not
permitted between digits.
Eg:123,-321 etc.,
Octal Constant
◦ An octal integer constant consists of any combination of digits from
the set 0 through 7,with a leading 0.
Eg: 1) 037 2) 0435
Hexadecimal Constant
◦ A sequence of digits preceded by 0x or 0X is considered as
hexadecimal integer. They may also include alphabets A through F or
a through f.
Eg: 1) 0X2 2) 0x9F 3) 0Xbcd
Constant
Real Constants
It contains are the numbers which hold the
arrays.
They are user-defined names and consist of a sequence of
Ex : Value,code,rec_no (Vaid)
Ex: 5bc,int,rec#,avg no (invalid)
Variable
Rules for defining variables
They must begin with a letter. Some systems permit underscore as the first
character.
DECLARATION OF VARIABLES
The syntax is
Data-type v1,v2…..vn;
Ex : int a,b,c;
Ex : int a, float b, char c;
Ex : int a=10,float =0.5,char c=‘x’, string=“welcome”;
Rules for Constructing Identifiers
in C
Capital letters A-Z, lowercase letters a-z,
digits 0-9, and the underscore character
First character must be a letter or
underscore
Usually only the first 32 characters are
significant
There can be no embedded blanks
Keywords cannot be used as identifiers
Identifiers are case sensitive
A data type is
◦ A set of values AND
◦ A set of operations on those values
A data type is used to
◦ Identify the type of a variable when the variable is
declared
◦ Identify the type of the return value of a function
◦ Identify the type of a parameter expected by a
function
A Data Type (continued)
point type
int *, float *, char * – used to denote a
grossProduct = 4567.89;
inputSymbol = 'a';
return (0);
} // End main
Derived Data Types
struct operationsStruct
{
double heatReading;
int temperatureValue;
float speedMeter;
char actionCode;
}; // End struct
typedef struct
{
double heatReading;
int temperatureValue;
float speedMeter;
char actionCode;
} operationsRecordType;
operationsRecordType currentOperations;
operationsRecordType futureOperations;
Enumumerated
Another user defined data types is enumerated data type
which can be used to declare variables that can have one of
the values enclosed within the braces.
include <stdio.h>
int main()
{
enum Days
{Sunday,Monday,Tuesday,
Wednesday,Thursday,
Friday,Saturday};
….
….
}
Storage class
Declaration of storage class
Variables in C can have not only data type but also storage class that provides information
about their locality and visibility.
Here the variable m is called the global variable. It can be used in all the functions in the
program. The variables bal, sum and i are called local variables. Local variables are visible
and meaningful only inside the function in which they are declared
/*Example of storage class*/
int m;
main()
{
int i;
float bal;
……
……
function1();
}
function1()
{
int i;
float sum;
……
……
}
Variables
ASSIGNING VALUES TO VARIABLES
The syntax is
Variable name=constant
Eg:1) int a=20;
2) bal=75.84;
3) yes=’x’;
C permits multiple assignments in one line.
Exe:
initial_value=0;final_value=100;
Declaring a variable as constant
Eg: 1) const int class_size=40;
This tells the compiler that the value of the int variable
class_size must not be modified by the program.
READING DATA FROM KEYWORD
Another way of giving values to variables is to input data
scanf(“control string”,&variable1,&variable2,….);
The ampersand symbol & before each variable name is an
Eg: 1) scanf(“%d”,&number);
INTRODUCTION TO C
DEFINING SYMBOLIC CONSTANTS
We often use certain unique constants in a program. These constants
may appear repeatedly in a number of places in the program.
ex : 3.142, represent to “pi”.
We face two problems in the subsequent use of such programs.
◦ Problem in modification of the programs.
◦ Problem in understanding the program
◦ No blank space between the sign ‘#’ and the word define is permitted
◦ A blank space is required between #define and symbolic name and between the
symbolic name and the constant.
◦ After definition, the symbolic name should not be assigned any other value
within the program by using an assignment statement.
◦ Symbolic names are NOT declared for data types. Their data types depend on
the type of constant.