0% found this document useful (0 votes)
6 views20 pages

STUCOR - CS8251-HR (1) - 1-20 - Compressed-1-20

The document provides an overview of programming in C, detailing its structure, data types, storage classes, and constants. It highlights the advantages and disadvantages of C, particularly its lack of object-oriented programming features. Additionally, it explains various data types, including primary, derived, and user-defined types, as well as the concept of constants and enumeration in C programming.

Uploaded by

vini t
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)
6 views20 pages

STUCOR - CS8251-HR (1) - 1-20 - Compressed-1-20

The document provides an overview of programming in C, detailing its structure, data types, storage classes, and constants. It highlights the advantages and disadvantages of C, particularly its lack of object-oriented programming features. Additionally, it explains various data types, including primary, derived, and user-defined types, as well as the concept of constants and enumeration in C programming.

Uploaded by

vini t
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
You are on page 1/ 20

DOWNLOADED FROM STUCOR APP

Programming in C PUBLISHED IN STUCOR

• The modular structure makes code debugging, maintenance and testing easier.
Disadvantages of C
• C does not provide Object Oriented Programming (OOP) concepts.
• There is no concepts of Namespace in C.
• C does not provide binding or wrapping up of data in a single unit.
• C does not provide Constructor and Destructor.
Object-oriented programming
Object-oriented programming (OOP) languages were created, such as Simula, Smalltalk,
C++, C#, Eiffel, PHP, and Java . In these languages, data and methods to manipulate it are kept
as one unit called an object. The only way that another object or user can access the data is via
the object's methods. Thus, the inner workings of an object may be changed without affecting
any code that uses the object.

1.2 STRUCTURE OF C PROGRAM

1. Documentation section:
The documentation section consists of a set of comment lines giving the name of the
program, the author and other details, which the programmer would like to use later.
2. Link section: The link section provides instructions to the compiler to link functions
from the system library such as using the #include directive.

DOWNLOADED FROM STUCOR APP


DOWNLOADED FROM STUCOR APP

Programming in C PUBLISHED IN STUCOR

3. Definition section: The definition section defines all symbolic constants such using
the #define directive.
4. Global declaration section: There are some variables that are used in more than one
function. Such variables are called global variables and are declared in the global
declaration section that is outside of all the functions. This section also declares all
the user-defined functions.
5. main () function section: Every C program must have one main function section. This
section contains two parts; declaration part and executable part
i. Declaration part: The declaration part declares all the variables used in the
executable part.
ii. Executable part: There is at least one statement in the executable part. These two
parts must appear between the opening and closing braces. The program
execution begins at the opening brace and ends at the closing brace. The closing
brace of the main function is the logical end of the program. All statements in the
declaration and executable part end with a semicolon.
6. Subprogram section: If the program is a multi-function program then the subprogram
section contains all the user-defined functions that are called in the main () function.
User-defined functions are generally placed immediately after the main () function,
although they may appear in any order.
All section, except the main () function section may be absent when they are not required.

1.3 C PROGRAMMING: DATA-TYPES


A data-type in C programming is a set of values and is determined to act on those values.
C provides various types of data-types which allow the programmer to select the appropriate type
for the variable to set its value.
The data-type in a programming language is the collection of data with values having
fixed meaning as well as characteristics. Some of them are integer, floating point, character etc.
Usually, programming languages specify the range values for given data-type.
C Data Types are used to:
• Identify the type of a variable when it declared.
• Identify the type of the return value of a function.

DOWNLOADED FROM STUCOR APP


DOWNLOADED FROM STUCOR APP

Programming in C PUBLISHED IN STUCOR

• Identify the type of a parameter expected by a function.


ANSI C provides three types of data types:
1. Primary(Built-in) Data Types:void, int, char, double and float.
2. Derived Data Types:Array, References, and Pointers.
3. User Defined Data Types:Structure, Union, and Enumeration.
Primary Data Types
Every C compiler supports five primary data types:

void As the name suggests it holds no value and is generally used for specifying
the type of function or what it returns. If the function has a void type, it
means that the function will not return any value.

int Used to denote an integer type.

char Used to denote a character type.

float, double Used to denote a floating point type.

int *, float *, char Used to denote a pointer type.


*
Declaration of Primary Data Types with Variable Names

After taking suitable variable names, they need to be assigned with a data type. This is
how the data types are used along with variables:
Example:
int age;
char letter;
float height, width;
Derived Data Types
C supports three derived data types:

Data Description
Types

Arrays Arrays are sequences of data items having homogeneous values . They have
adjacent memory locations to store values.

References Function pointers allow referencing functions with a particular signature.

Pointers These are powerful C features which are used to access the memory and deal with

DOWNLOADED FROM STUCOR APP


DOWNLOADED FROM STUCOR APP

Programming in C PUBLISHED IN STUCOR

their addresses.
User Defined Data Types

C allows the feature called type definition which allows programmers to define their own
identifier that would represent an existing data type. There are three such types:

Data Description
Types

It is a package of variables of different types under a single name . This is done


Structure
to handle data efficiently. “struct” keyword is used to define a structure.

These allow storing various data types in the same memory location.
Union Programmers can define a union with different members but only a single
member can contain a value at given time.

Enumeration is a special data type that consists of integral constants and each of
Enum them is assigned with a specific name. “enum” keyword is used to define the
enumerated data type.

Let's see the basic data types. Its size is given according to 32 bit architecture.

Data Types Memory Size Range

char 1 byte −128 to 127

signed char 1 byte −128 to 127

unsigned char 1 byte 0 to 255

short 2 byte −32,768 to 32,767

signed short 2 byte −32,768 to 32,767

unsigned short 2 byte 0 to 65,535

int 2 byte −32,768 to 32,767

signed int 2 byte −32,768 to 32,767

unsigned int 2 byte 0 to 65,535

short int 2 byte −32,768 to 32,767

signed short int 2 byte −32,768 to 32,767

DOWNLOADED FROM STUCOR APP


DOWNLOADED FROM STUCOR APP

Programming in C PUBLISHED IN STUCOR

unsigned short int 2 byte 0 to 65,535

long int 4 byte -2,147,483,648 to 2,147,483,647

signed long int 4 byte -2,147,483,648 to 2,147,483,647

unsigned long int 4 byte 0 to 4,294,967,295

float 4 byte

double 8 byte

long double 10 byte

Example for Data Types and Variable Declarations in C


#include <stdio.h>
int main()
{
int a = 4000; // positive integer data type
float b = 5.2324; // float data type
char c = 'Z'; // char data type
long d = 41657; // long positive integer data type
long e = -21556; // long -ve integer data type
int f = -185; // -ve integer data type
short g = 130; // short +ve integer data type
short h = -130; // short -ve integer data type
double i = 4.1234567890; // double float data type
float j = -3.55; // float data type
}
The storage representation and machine instructions differ from machine to
machine. sizeof operator can use to get the exact size of a type or a variable on a particular
platform.
Example:
#include <stdio.h>
#include <limits.h>

DOWNLOADED FROM STUCOR APP


DOWNLOADED FROM STUCOR APP

Programming in C PUBLISHED IN STUCOR

int main()
{
printf("Storage size for int is: %d \n", sizeof(int));
printf("Storage size for char is: %d \n", sizeof(char));
return 0;
}

1.4 STORAGE CLASSES


Storage classes are used to define scope and life time of a variable . There are four storage
classes in C programming.
o auto
o extern
o static
o register

Storage Storage Default


Scope Life-time
Classes Place Value

Garbage
auto RAM Local Within function
Value

Till the end of main program, May be


extern RAM Zero Global
declared anywhere in the program

Till the end of main program, Retains


static RAM Zero Local
value between multiple functions call
Garbage
register Register Local Within function
Value

1) auto
The auto keyword is applied to all local variables automatically. It is the default
storage class that is why it is known as automatic variable.
#include<stdio.h>
int main()
{

DOWNLOADED FROM STUCOR APP


DOWNLOADED FROM STUCOR APP

Programming in C PUBLISHED IN STUCOR

int a=10;
auto int b=10;//same like above
printf("%d %d",a,b);
return 0;
}
Output:
10 10
2) register
The register variable allocates memory in register than RAM. Its size is same of
register size. It has a faster access than other variables.
It is recommended to use register variable only for quick access such as in counter.
We can’t get the address of register variable.
Example: register int counter=0;
3) static
The static variable is initialized only once and exists till the end of the program. It
retains its value between multiple functions call.
The static variable has the default value 0 which is provided by compiler.
Example:
#include<stdio.h>
int func()
{
static int i=0;//static variable
int j=0;//local variable
i++;
j++;
printf("i= %d and j= %d\n", i, j);
}
int main() {
func();
func();
func();

DOWNLOADED FROM STUCOR APP


DOWNLOADED FROM STUCOR APP

Programming in C PUBLISHED IN STUCOR

return 0;
}
Output:
i= 1 and j= 1
i= 2 and j= 1
i= 3 and j= 1
4) extern
The extern variable is visible to all the programs . It is used if two or more files are
sharing same variable or function .
Example: extern int counter=0;

1.5 CONSTANTS
A constant is a value or variable that can't be changed in the program, for example: 10,
20, 'a', 3.4, "c programming" etc.
There are different types of constants in C programming.
List of Constants in C

Constant Example

Decimal Constant 10, 20, 450 etc.

Real or Floating-point Constant 10.3, 20.2, 450.6 etc.

Octal Constant 021, 033, 046 etc.

Hexadecimal Constant 0x2a, 0x7b, 0xaa etc.

Character Constant 'a', 'b', 'x' etc.

String Constant "c", "c program", "c in javatpoint" etc.


2 ways to define constant in C
There are two ways to define constant in C programming.
1. const keyword
2. #define preprocessor
1) C const keyword
The const keyword is used to define constant in C programming.

10

DOWNLOADED FROM STUCOR APP


DOWNLOADED FROM STUCOR APP

Programming in C PUBLISHED IN STUCOR

Example: const float PI=3.14;


Now, the value of PI variable can't be changed.
#include<stdio.h>
int main(){
const float PI=3.14;
printf("The value of PI is: %f",PI);
return 0;
}
Output:
The value of PI is: 3.140000
If you try to change the the value of PI , it will render compile time error.
#include<stdio.h>
int main(){
const float PI=3.14;
PI=4.5;
printf("The value of PI is: %f",PI);
return 0;
}
Output:
Compile Time Error: Cannot modify a const object
2) C #define preprocessor
The #define preprocessor directive is used to define constant or micro substitution. It can
use any basic data type.
Syntax:
#define token value

Let's see an example of #define to define a constant.


#include <stdio.h>
#define PI 3.14
main() {
printf("%f",PI);
}

11

DOWNLOADED FROM STUCOR APP


DOWNLOADED FROM STUCOR APP

Programming in C PUBLISHED IN STUCOR

Output:
3.140000
Backslash character constant
C supports some character constants having a backslash in front of it. The lists of
backslash characters have a specific meaning which is known to the compiler. They are also
termed as “Escape Sequence ”.
Example:
\t is used to give a tab
\n is used to give new line

Constants Meaning Constants Meaning

\a beep sound \v vertical tab

\b backspace \’ single quote

\f form feed \” double quote

\n new line \\ backslash

\r carriage return \0 null

\t horizontal tab

1.6 ENUMERATION CONSTANTS


An enum is a keyword, it is an user defined data type. All properties of integer are
applied on Enumeration data type so size of the enumerator data type is 2 byte . It work like
the Integer.
It is used for creating an user defined data type of integer. Using enum we can create
sequence of integer constant value.
Syntax:

enum tagname{value1,value2,value3,….};

• In above syntax enum is a keyword. It is a user defined data type.


• In above syntax tagname is our own variable. tagname is any variable name.
• value1, value2, value3, ..... are create set of enum values.

12

DOWNLOADED FROM STUCOR APP


DOWNLOADED FROM STUCOR APP

Programming in C PUBLISHED IN STUCOR

It is start with 0 (zero) by default and value is incremented by 1 for the sequential
identifiers in the list. If constant one value is not initialized then by default sequence will be start
from zero and next to generated value should be previous constant value one.
Example of Enumeration in C:
enum week{sun,mon,tue,wed,thu,fri,sat};
enum week today;
• In above code first line is create user defined data type called week.
• week variable have 7 value which is inside { } braces.
• today variable is declare as week type which can be initialize any data or value among
7 (sun, mon, ... ).
Example:
#include<stdio.h>
#include<conio.h>
enum abc{x,y,z};
void main()
{
int a;
clrscr();
a=x+y+z; //0+1+2
printf(“sum: %d”,a);
getch();

13

DOWNLOADED FROM STUCOR APP


DOWNLOADED FROM STUCOR APP

Programming in C PUBLISHED IN STUCOR

}
Output:
Sum: 3

1.7 KEYWORDS
A keyword is a reserved word. You cannot use it as a variable name, constant name etc.
There are only 32 reserved words (keywords) in C language.
A list of 32 keywords in c language is given below:

auto break case char const continue default do

double else enum extern float for goto if

int long register return short signed sizeof static

struct switch typedef union unsigned void volatile while

1.8 OPERATORS: PRECEDENCE AND ASSOCIATIVITY


Operator is a special symbol that tells the compiler to perform specific mathematical
or logical Operation.

• Arithmetic Operators

• Relational Operators

• Logical Operators

• Bitwise Operators

• Assignment Operators

• Ternary or Conditional Operators

14

DOWNLOADED FROM STUCOR APP


DOWNLOADED FROM STUCOR APP

Programming in C PUBLISHED IN STUCOR

Arithmetic Operators
Given table shows all the Arithmetic operator supported by C Language. Lets suppose
variable A hold 8 and B hold 3.

Operator Example (int A=8, B=3) Result

+ A+B 11
- A-B 5

* A*B 24
/ A/B 2
% A%4 0
Relational Operators
Which can be used to check the Condition, it always return true or false. Lets suppose
variable A hold 8 and B hold 3.

Operators Example (int A=8, B=3) Result

< A<B False

<= A<=10 True


> A>B True
>= A<=B False
== A== B False

15

DOWNLOADED FROM STUCOR APP


DOWNLOADED FROM STUCOR APP

Programming in C PUBLISHED IN STUCOR

!= A!=(-4) True
Logical Operator
Which can be used to combine more than one Condition?. Suppose you want to
combined two conditions A<B and B>C, then you need to use Logical Operator like (A<B)
&& (B>C). Here && is Logical Operator.

Operator Example (int A=8, B=3, C=-10) Result

&& (A<B) && (B>C) False


|| (B!=-C) || (A==B) True
! !(B<=-A) True

Truth table of Logical Operator

C1 C2 C1 && C2 C1 || C2 !C1 !C2

T T T T F F

T F F T F T
F T F T T F
F F F F T T
Assignment operators
Which can be used to assign a value to a variable. Lets suppose variable A hold 8
and B hold 3.

Operator Example (int A=8, B=3) Result

+= A+=B or A=A+B 11

-= A-=3 or A=A+3 5
*= A*=7 or A=A*7 56
/= A/=B or A=A/B 2
%= A%=5 or A=A%5 3

a=b Value of b will be assigned to a


Increment and Decrement Operator
Increment Operators are used to increased the value of the variable by one
and Decrement Operators are used to decrease the value of the variable by one in C
programs.

16

DOWNLOADED FROM STUCOR APP


DOWNLOADED FROM STUCOR APP

Programming in C PUBLISHED IN STUCOR

Both increment and decrement operator are used on a single operand or variable, so it is
called as a unary operator. Unary operators are having higher priority than the other operators
it means unary operators are executed before other operators.
Increment and decrement operators are cannot apply on constant.
The operators are ++, --
Type of Increment Operator

• pre-increment
• post-increment
pre-increment (++ variable)
• In pre-increment first increment the value of variable and then used inside the
expression (initialize into another variable).
Syntax:
++variable;

post-increment (variable ++)


In post-increment first value of variable is used in the expression (initialize into another
variable) and then increment the value of variable.
Syntax:
variable++;

Example:
#include<stdio.h>
#include<conio.h>
void main()
{
int x,i;
i=10;
x=++i;
printf(“Pre-increment\n”);
printf(“x::%d”,x);
printf(“i::%d”,i);
i=10;
x=i++;

17

DOWNLOADED FROM STUCOR APP


DOWNLOADED FROM STUCOR APP

Programming in C PUBLISHED IN STUCOR

printf(“Post-increment\n”);
printf(“x::%d”,x);
printf(“i::%d”,i);
}
Output:
Pre-increment
x::10
i::10
Post-increment
x::10
i::11
Type of Decrement Operator
• pre-decrement
• post-decrement
Pre-decrement (-- variable)
In pre-decrement first decrement the value of variable and then used inside the
expression (initialize into another variable).
Syntax:
--variable;

post-decrement (variable --)


In Post-decrement first value of variable is used in the expression (initialize into another
variable) and then decrement the value of variable.
Syntax:
variable--;

Example:
#include<stdio.h>
#include<conio.h>
void main()
{
int x,i;
i=10;

18

DOWNLOADED FROM STUCOR APP


DOWNLOADED FROM STUCOR APP

Programming in C PUBLISHED IN STUCOR

x=--i;
printf(“Pre-decrement\n”);
printf(“x::%d”,x);
printf(“i::%d”,i);
i=10;
x=i--;
printf(“Post-decrement\n”);
printf(“x::%d”,x);
printf(“i::%d”,i);
}
Output:
Pre-decrement
x::9
i::9
Post-decrement
x::10
i::9
Ternary Operator
If any operator is used on three operands or variable is known as Ternary Operator. It can
be represented with ? : . It is also called as conditional operator
Advantage of Ternary Operator
Using ?: reduce the number of line codes and improve the performance of application.
Syntax:

Expression 1? Expression 2: Expression 3;

In the above symbol expression-1 is condition and expression-2 and expression-3 will be
either value or variable or statement or any mathematical expression. If condition will be true
expression-2 will be execute otherwise expression-3 will be executed.

19

DOWNLOADED FROM STUCOR APP


DOWNLOADED FROM STUCOR APP

Programming in C PUBLISHED IN STUCOR

Conditional Operator flow diagram


Example:
find largest number among 3 numbers using ternary operator
#include<stdio.h>
void main()
{
int a,b,c,large;
printf(“Enter any three numbers:”);
scanf(“%d%d%d”,&a,&b,&c);
large=a>b?(a>c?a:c):(b>c?b:c);
printf(“The largest number is:%d”,large);
}
Output:

Enter any three numbers: 12 67 98


The largest number is 98
Special Operators
C supports some special operators

20

DOWNLOADED FROM STUCOR APP

You might also like