0% found this document useful (0 votes)
37 views33 pages

Unit-I C Language Fybsc It

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)
37 views33 pages

Unit-I C Language Fybsc It

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/ 33

Sahyog College of Management Studies, Thane

UNIT-1
 Remember your last visit to the Super Market. You might have purchased many
items.
 What did you do after picking all the items and putting them into the carriage?
Probably you met with the billing clerk to make the payment. Have you observed
his way of preparing the bill?
 He picks the items one by one and enters something into the computer repeating
the same task for all the items.
 Within a count of few minutes or even may be within few seconds he gives the
bill, you pay and come out carrying all the required things. So, what made him to
process the bill so fast? It is nothing but the “program” running in the
computer’s memory.
Program:
 A computer program is a collection of instructions that can be executed by
a computer to perform a specific task.
 Computer program is usually written by a computer programmer in
a programming language.
Programmer:
 One who writes the programs/instructions for the computer is known as
Programmer.

Programming:
 The act of writing computer programs using some programming language is
called computer programming.

Programming Language:
 Programming language is a language which is used to communicate with the
computers i.e. it is a language using which programmers create
programs/instructions for the computers so that computer can perform some
actions.
 Computers do not understand Hindi, Marathi, English, Gujarati etc. they only
understand programming language.
 Like, we human prefer our native language Hindi, Marathi etc. for communication
purpose, similarly computers prefer programming languages.

Sahyog College of Management Studies, Thane


Types of Programming Language:
1. Machine Language
2. Assembly Language
3. High Level Language

1. Machine Language:
 Machine language is the only language a computer is capable of understanding.
 Machine Language is represented in the sequence of 0’s and 1’s i.e. the
instructions are given to computer in the form of 1 and 0 only.
 It is also known as binary language.
 It also comes under low level programming Language and difficult to understand.
 E.g.: 01100110 10101010

2. Assembly Language:
 Assembly Language is again a low level language.
 It uses “Mnemonics code” for the instructions.
 Mnemonics codes are the symbolic constants used for instructions.
 E.g. Add, Sub, Mov etc. are the mnemonics code.
 Assembly language is used in micro-controller based embedded systems.
 Example of embedded systems are AC, Washing Machine and Elevator etc.
 But, Assembly language is not understandable to computers because they only
understand machine language.
 So the instructions/code written in assembly language are first converted into
machine language, so that computer can understand it and perform some
operation on it.
 The convertor/translator which is used for converting assembly language into
machine language are called “Assembler”.

Sahyog College of Management Studies, Thane


3. High Level Language:
 High level language is very much user friendly and easier for programmers to
understand.
 It uses English-Like structure and basic mathematical syntax.
 Eg: 2+6, 10/2, printf(“Good Morning”); etc.
 As computer doesn’t understands high level language so, the program written in
high level language need to converted into machine language so that computer
can understand it.
 The translator/converter which is used to convert high level language into
machine language are called “Compiler” or “Interpreter”.
 C, C++, JAVA, C#, PYTHON, JAVASCRIPT, PHP, RUBY etc. are the example of high
level language.
 C, C++ programming language uses compiler for translation.
 Python, Javascript ,Php uses interpreter for translation.

Sahyog College of Management Studies, Thane


Characteristics of Programming:

1. Accurate:
The programming of a program is based on a certain problem. The problem must be pre-
defined clearly with specification of requirements. It is expected from the programmer to
design a program that strictly follows these requirements. So, the designed program must
be accurate to perform the specified task. Such characteristic of programming is referred
‘accurate’.

2. Efficient:
Every program utilizes the resources of the computer system. It is expected from the
programming that the designed program utilizes these resources in an efficient manner. It
means the program must not spend much time or over use the processor in executing its
coded instructions. Such characteristic of the programming is referred ‘efficient’.

3. Maintainable:
When proper structuring method is used in programming, the designed program can be
made maintainable. Here maintainable means the ability to change as per the new needs.
With very little modification a program should work for the new needs. Such characteristic
of programming is referred ‘maintainable”.

4. Portable:
It is expected from the designed program that it can be carried to any platform to solve
the task. If the programming is done keeping many systems rather than one system in
mind the designed programs are portable. Once the program is portable, it can be easily

Sahyog College of Management Studies, Thane


transferred from one machine to another. Such characteristic of programming is referred
‘portable’.

5. Readable:
The program designed by the programmer must be self-readable as he is the first reader.
Generally the designed program is also read by co-programmers or others. So, the
designed program must contain proper comments to explain the coded instructions. This
commenting will help in understanding and reading the program. Such characteristic of
programming is referred ‘readable’.

6. Reliable:
The designed program must perform as per the need all the time. It should also produce
the intended results for any sort of inputs. In case of improper inputs, it should stop only
after displaying proper error messages. These will indicate the cause of termination of the
program. Such programs are created with the ‘reliable’ characteristic of the programming.

7. Robust:
The designed program is expected to continue with its functionalities even at the
unexpected errors. It is the art of programming that takes care of all the possible errors
before completing the design. Such programs keep on doing their work even at worst
situations. Such characteristic of programming is referred ‘robust’.

8. Usable:
The designed program must be easy to use. It must be designed with proper Interactive
messages so that the user can easily get accustomed to it. Proper thinking in interface
design will prove its worthiness. The documentation of the program must be prepared in
good format to train the users. Such characteristic of the programming is referred ‘usable’.

9. Documentation:
The usage of comments or remarks to explain the coded instructions and the modules of
the program is called documenting the program. Such documented program will also help
in providing other characteristic of the programming. The comments will help to find the
errors as well as to rectify them. When the program is lengthy one proper documentation
will properly connect the components of the program. It improves the readability as well

Sahyog College of Management Studies, Thane


as usability of the program. Such characteristic of programming is referred
‘documentation’.

IDE:
● IDE Stands for “Integrated Development Environment”.
● IDE provides a window where all the c programs are written, compiled and linked
to execute the program.
● As all these 3 processes are provided by a single environment i.e. IDE.
● Turbo C, Dev C++, Code Blocks etc. are the IDE’s and all these IDE’s are easily
available on internet.
● We can download any of them for writing the c program.
Introduction to C language:

● C is a procedural programming language that is extremely popular, simple and


flexible. It was initially developed by “Dennis Ritchie” in the year 1972 at AT&T Bell
Laboratories.
● It was mainly developed as a system programming language to write an operating
system “UNIX”.
● C is a base for the programming. If you know 'C,' you can easily grasp the
knowledge of the any other programming Language.
● Some examples of the use of C:
a) Operating Systems
b) Language Compilers
c) Assemblers
d) Text Editors and Data Bases

History of C Language/Standards of C Language:


● The base or father of programming languages is 'ALGOL' (Algorithmic Language). It
was first introduced in 1960.
● In 1967, a new computer programming language was announced called as 'BCPL'
which stands for Basic Combined Programming Language. BCPL was designed and
developed by Martin Richards.
● In 1970 a new programming language called 'B' was introduced by Ken Thompson
that contained multiple features of 'BCPL'.
Sahyog College of Management Studies, Thane
● In 1972, a great computer scientist Dennis Ritchie created a new programming
language called 'C' at the Bell Laboratories. It was created from 'ALGOL', 'BCPL' and
'B' programming languages. 'C' programming language contains all the features of
these languages and many more additional concepts that make it unique from
other languages.
American National Standards Institute (ANSI) defined a commercial standard for 'C'
language in 1989. Later, it was approved by the International Standards Organization
(ISO) in 1990. 'C' programming language is also called as 'ANSI C'.

C Library Functions/built-in /pre-defined:


● C Standard library functions or simply C Library functions are inbuilt functions in C
programming.
● The prototype i.e. function declaration of these functions are present in their
respective header files. To use these functions we need to include the header file in
our program.
● Function definitions of these functions are present in library file.
● Extension of header file is .h and library file is .lib
● Various built in function are printf( ),
scanf(),getch(),getche(),getchar(),puts(),gets(),sqrt(),pow(),abs(),
strcat(),strlwr(),strupr(),strrev(),strlen(),sleep(),system(),malloc(),exit(0) etc.
● And all the above built in functions are defined in some header files and in order to
use any function we need to include the respective header file in our program.
Sahyog College of Management Studies, Thane
Header File Functions

stdio.h printf() , scanf(),puts(),gets() etc.

string.h strcat(),strlwr(),strupr(),strrev(),strlen() etc.

math.h sqrt(),pow(),abs(),round() etc.

ctype.h isdigit(),islower(),isupper() etc.

How to Use Library Functions:


● #include is a way of including files in the program and is mostly written at the
beginning of any C
● The #include is a preprocessor directive and it is used to paste code of given file
into current file.
● The C Preprocessor is not a part of the compiler, but is a separate step in the
compilation process. In simple terms, a C Preprocessor is just a text substitution
tool and it instructs the compiler to do required pre-processing before the actual
compilation.
● All preprocessor commands begin with a hash symbol (#).
Syntax to use header file:
#include <file_name.h>
Example:
#include <stdio.h>

Structure of c program:
#include<header file>
int main()
{
Statements-1;
Statements-2;
return 0;
}

Sahyog College of Management Studies, Thane


 #include<header file>
● As we know that before using any variable we declare it first in our program and
then only we use that variable.
● Similarly, before using any predefined function, we will have to declare that
function first in our program and as we know declaration i.e. prototype of the
functions are defined in the header file only.so when we write the line
#include<header file> then all the function which is declared in respective
header file are copied into our program and then we use those functions.
● For eg. stdio.h contains declaration of printf(),scanf() etc. so when we write
#include<stdio.h> then declaration of printf(),scanf() etc. are copied into the
program at the beginning. Then we use these function efficiently.

 int main()
● main() function is mandatory function to write in any program.
● Execution of any program start from the main function only.
● It is the entry point for a program. Compiler starts the execution from main
function only.
● int is the return type of main function that means main function will return an
integer value at the end of the program.

 { }
● { } curly brackets defines scope of a block.
● All the program should be written inside the curly brackets “{ }” only.

 return 0;
● return 0; statement causes the termination of main function.
● 0 value indicates successful execution of the program.
● statement written after return 0; never get executed.
Program to display a message on output screen using c language:
#include<stdio.h>
int main()
{
printf(“ Good Morning”);
return 0;
}

Sahyog College of Management Studies, Thane


Output:
Good Morning

Flow of Execution of a program

1. C program (source code) is sent to preprocessor first. The preprocessor is


responsible to convert preprocessor directives into their respective values. The
preprocessor generates an expanded source code.
2. Expanded source code is sent to compiler which compiles the code and converts
it into object code i.e. machine code.
3. The object code is sent to linker which links it to the library such as header files.
Then it is converted into executable code. A simple.exe file is generated. For Eg. if
we have use printf() function,then definition of printf() is present in library file, so
linker will link the calling function with its definition.
4. The executable code is sent to loader which loads it into memory and then it is
executed. After execution, output is sent to console.

Sahyog College of Management Studies, Thane


Algorithm:
 An algorithm is a procedure or step-by-step instruction for solving a problem.
 They form the foundation of writing a program.

For writing any programs, the following has to be known:


1. Input
2. Tasks to be performed
3. Output expected

Examples of Algorithms in Programming:

1. Write an algorithm to add two numbers.


Step 1: Start
Step 2: Declare variables num1, num2 and sum.
Step 3: Read: num1 and num2.
Step 4: Add num1 and num2 and assign the result to sum.
Sum=num1+num2
Step 5: Display: sum
Step 6: Stop

2. Write an algorithm to calculate square of a number.


Step 1: Start
Step 2: Declare 2 variables num, result
Step 3: Read: num
Step 4: calculate square and assign the result to result variable
result=num * num
Step 5: Display: result
Step 6: Stop

3. Write an algorithm to display smallest no. between 2 nos.


Step 1: Start
Step 2: Declare 2 variables first, second
Step 3: Read: first and second
Step 4: check which one is the smallest
if (first < second)
Sahyog College of Management Studies, Thane
Display: first
else
Display: second
Step 6: Stop

4. Write an algorithm to check eligibility for voting.


Step 1: Start
Step 2: Declare a variable: age
Step 3: Read: age
Step 4: check the age
if (age>=18)
Display “you are eligible”
else
Display “you are not eligible”
Step 6: Stop

5. Write an algorithm to check whether student is failed or passed.


Step 1: Start
Step 2: Declare a variable: marks
Step 3: Read: marks
Step 4: check the marks
if(marks>=35)
Display: “you are passed”
else
Display: “you are failed”
Step 6: Stop

Flowchart:
 Flowchart is the graphical/diagrammatic representation of an algorithm.
 Programmers often use it as a program-planning tool to solve a problem. It makes
use of symbols which are connected among them to indicate the flow of
information and processing.

Sahyog College of Management Studies, Thane


Symbols Used In Flowchart:

1. Terminal:
 The oval symbol indicates Start, Stop and Halt in a program’s logic flow.
 Terminal is the first and last symbols in the flowchart.

2. Input/Output:
 A parallelogram denotes any function of input/output type.
 Program instructions that take input from input devices and display output on
output devices are indicated with parallelogram in a flowchart.

3. Processing:
 A box represents arithmetic instructions.
 All arithmetic processes such as addition, subtraction, multiplication and division
are indicated by action or process symbol.

4. Decision :
 Diamond symbol represents a decision point.
 Decision based operations such as yes/no question or true/false are indicated by
diamond in flowchart.

5. Connectors:
A. On-Page Connector:
 Connects 2 or more parts of flowcharts which are on the same page.
 On-page connectors are referenced using numbers.
Sahyog College of Management Studies, Thane
B. Off-Page Connector:
 Whenever flowchart becomes complex or it spreads over more than one page.
 Off-page connectors are referenced using alphabets
 It is useful to use connectors to avoid any confusions.

6. Flow lines:
 Flow lines indicate the exact sequence in which instructions are executed.
 It indicates the flow of logic by connecting symbols.

Sahyog College of Management Studies, Thane


On-page Connector Connects 2 or more parts of
flowcharts which are on the
same page.

Off-page Connector Connects two parts of a


flowchart which are spread over
different pages.

Examples of flowcharts in programming:

1. Design a flowchart to find the addition of two numbers.

Sahyog College of Management Studies, Thane


2. Flowchart to find the average of two numbers.

3. Flowchart to find the smallest number between two numbers.

Sahyog College of Management Studies, Thane


4. Prepare a flowchart to check biggest number.(Use on page connector)

5. Prepare a flowchart to calculate square and cube of a number.(Using off page


connector).

Sahyog College of Management Studies, Thane


Continuation of above flowchart

Character Set In C
Characters are used in forming either words or numbers or even expressions in C
programming. Characters in C are classified into 4 groups:

 Letters
In C programming, we can use both uppercase and lowercase letters of English
language
 Uppercase Letters: A to Z
 Lowercase Letters: a to z

 Digits
We can use decimal digits from 0 to 9.

 Special Characters
C Programming allows programmer to use following special characters:

comma , slash /

period . backslash

semicolon ; percentage %

colon : left and right brackets [ ]

question mark ? left and right parenthesis ( )

Sahyog College of Management Studies, Thane


 White Spaces
In C Programming, white spaces contains:
 Blank Spaces
 Tab
 New Line

Tokens:
● Tokens in C is the most important element to be used in creating a program in C.
● We can define the token as the smallest individual element in C. For `example, we
cannot create a sentence without using words; similarly, we cannot create a
program in C without using tokens in C.
● Therefore, we can say that tokens in C is the building block or the basic component
for creating a program in C language.

The tokens in C language are broadly classified as shown below:

Sahyog College of Management Studies, Thane


1. Keywords:
● The words that have pre-defined meaning for C compiler are called keywords.
Since they are reserved for specific purpose in C language, they are also called
reserved words.
● The keywords have some specific purpose in C language and hence cannot be used
as user defined names like variable names or function names. The different
keywords of C language are shown below.
Keywords

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

2. 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.
● Identifier is a synonym for name.

Sahyog College of Management Studies, Thane


Rules for constructing C identifiers
● The first character of an identifier should be either an alphabet or an underscore “
_“, and then it can be followed by any of the character, digit, or underscore.
● It should not begin with any numerical digit.
● In identifiers, both uppercase and lowercase letters are distinct. Therefore, we can
say that identifiers are case sensitive.
● Commas or blank spaces cannot be used within an identifier.
● Keywords cannot be represented as an identifier.
● The length of the identifiers is not fixed.
● Identifiers should be written in such a way that it is meaningful, short, and easy to
read.

Identifiers valid/Invalid Invalidity Reason

abc valid

12a invalid Cannot start with digit

a12 valid

_12 valid

3. Constants:
A constant is a data item which will not be changed during the execution of a
program.

 Integer constant:
An integer constant is a whole number without any decimal point.
E.g.: 10, 20,-10, 1000 etc.
Sahyog College of Management Studies, Thane
 Floating point constant:
A Floating point constant is a number with decimal point.
e.g.: 10.2, 123.34, 1.3 etc.

 Character constants:
A character constant is a single alphabet, a single digit or a single special symbol
enclosed within single quotes.
E.g.: ‘a’,’Z’,’B’,’+’,’@’ etc.

Escape sequence characters:


● An escape sequence character begins with a ‘\’ and is followed by one character.
● A backslash along with one character gives rise to special print effect by changing
(escaping) the meaning of the character. Since an escape sequence character starts
with backslash, they are also called backslash constants.

The commonly used escape sequences characters are shown below:


Character Escape sequence Use
character

New Line \n Cursor moves to the next line.

Horizontal tab \t Cursor moves towards right by 8


positions.

Bell \a Beep sound.

Slash \\ To display slash

Double \” To display double quote


quotes

Note: The backslash characters such as ‘\a’, ‘\n’, ‘\t’, etc. are non-printable characters.

4. String constant:
● A sequence of characters (i.e. collection of characters) enclosed within a pair of
double quotes is called a string constant.
Sahyog College of Management Studies, Thane
e.g.: “sahyog”,”hello”,”morning” etc.

5. Variables:
● When we want to store any information (data) on our computer/laptop, we store it
in the computer's memory space. Instead of remembering the complex address of
that memory space where we have stored our data, our operating system provides
us with an option to create folders, name them, so that it becomes easier for us to
find it and access it.
● Similarly, in C language, when we want to use some data value in our program, we
can store it in a memory space.
● A variable is a name of the memory location. It is used to store data. Its value can
be changed.
● A variable in C language must be given a type, which defines what type of data the
variable will hold.

Rules for defining variables:


● A variable can have alphabets, digits, and underscore.
● A variable name can start with the alphabet, and underscore only. It can't start with
a digit.
● No whitespace is allowed within the variable name.
● A variable name must not be any reserved word or keyword, e.g. int, float, etc.

Syntax to declare a variable:


Data_type Variable_name;
Example:
int n1;
float num;
char ch;

Sahyog College of Management Studies, Thane


Data type:
● Data types specify the type of data. Data type determines the type of data a
variable will hold.
● C language has some predefined set of data types to handle various kinds of data
that we can use in our program. These data types have different storage capacities.

Type of data types:


1. Primitive data type (Built-in)
2. Non-primitive data type(user defined data type)
3. Derived data type

Primitive data type:


1. int
● It is a keyword which is used to define integer numbers. Normally they are
associated with the variables to store signed integer values in memory locations.
That is, using this data type both positive and negative numbers can be stored in
the memory.
● 10,-20, 501, 1003 etc. are integer values.

Sahyog College of Management Studies, Thane


2. char
● Character data type allows a variable to store only one character.
● “char” keyword is used to refer character data type.
● For example, ‘A’ can be stored using char data type. You can’t store more than one
character using char data type.
● We can also store numerical value too in char type but if will store numerical value
then “ASCII” value will be printed.

● Note:
● Every character has a corresponding ASCII value to it ranging from -128 to 127.
● Numbers as a character has their corresponding ASCII values too. For example, ‘1’ as
char has ASCII value 49, ‘A’ has ASCII value 65.

3. float
● It is a keyword which is used to define floating point numbers. The floating point
numbers are also called real numbers. Normally they are associated with the
variables (also called identifiers) to store floating point numbers in memory
locations.
● That is, using this data type both positive and negative floating point numbers can
be stored in the memory.
● Eg: 1.23, 2234.4532 etc.

4. Double:
● Double is also used to define floating point numbers.
● But double data type accepts 15 digit after decimal whereas float accepts 6 digits.
● Eg: 523.45353

Modifiers in C Language:
1. Size Modifier
2. Sign Modifier

1. Size Modifier:
● Size Modifiers are prefixed with basic data types to modify (either increase or
decrease) the amount of storage space allocated to a variable.
Sahyog College of Management Studies, Thane
● Short, long, long long are the size modifiers.
● For example, storage space for int data type is 4 byte. We can increase the range
by using long int which is 8 byte. We can decrease the range by using short int
which is 2 byte.
● short is only used with int type.
● long can be used with int and double.
● Any size modifier cannot be used with float and char data type.
● long long can not be used with float and double.
● Long long can be used with int type only.

2. Sign Modifier:
● Sign modifiers decides whether the variable will hold positive or negative number.
● signed & unsigned are the sign modifiers.
● In signed type we can store both positive as well as negative value but in unsigned
type we can store only positive value.
● By default the data type is signed only.
● Sign modifiers are used with int and char only.
● Sign modifiers cannot be used with float and double.

Size & Range of data type:


Data type Space Range Format
specifier

int 4 byte -2147483648 to 2147483647 %d

char 1 byte -128 to 127 %c

float 4 byte 6 digits %f

double 8 byte 15 digit %lf

short int 2 byte -32768 to 32767 %hi

long int 4 byte -2147483648 to 2147483647 %ld

long long int 8 byte -9223372036854775808 to 9223372036854775807 %lld

signed short int 2 byte -32768 to 32767 %hi

unsigned short int 2 byte 0 to 65535 %hu

Signed int 4 byte -2147483648 to 2147483647 %d

Sahyog College of Management Studies, Thane


unsigned int 4 byte 0 to 4294967295 %u

unsigned long long 8 byte 0 to 18446744073709551615 %llu


int

signed long long int 8 byte -9223372036854775808 to 9223372036854775807 %lld

Signed char 1 byte -128 to 127 %c

unsigned char 1 byte 0 to 255 %hc

Note:
● By default int and char data type is of signed type that means we can store positive
as well as negative value in it.
● int and signed int are same data type.
● short int and signed short int are same.
● long long int and signed long long int are same.
● char and signed char are same.

Format specifier:
The format specifier is used during input and output. It is a way to tell the compiler
what type of data is in a variable during taking input using scanf() or printing using
printf().
Some examples are %c, %d, %f, etc.
● Format specifier for hexadecimal -%x or %X
● Format specifier octal - %o
● Format specifier decimal/integer - %d
Note: Hexadecimal, Octal and Decimal Values comes under integer constant so we can
store value of these types into int datatype.

Eg: int a=0XA is valid (Hexadecimal value)


int b=010 is valid(Octal value)
int c=102 is valid(Decimal value)

Sahyog College of Management Studies, Thane


Hexadecimal Octal Decimal
Hexadecimal values Octal values comes Decimal values comes
comes under integer under integer constant. under integer constant.
constant.

An hexadecimal integer An octal integer constant A decimal integer


constant can be any can be any combination constant can be any
combination of digits of digits from ‘0’ to ‘7’ combination of digits
from ‘0’ to ‘9' along with from ‘0’ to ‘9’.
With a prefix 0 (digit
the letters ‘A’ to ‘F’ or ‘a’
zero).
to ‘f’. This constant has
to be preceded by 0X
OR 0x.

EG: 0x18A, 0XB, 0XFFF EG: 01612, 034567, -0765 EG: 100, –7, 989

%x or %X %o %d

1. Program for Hexadecimal Value

Sahyog College of Management Studies, Thane


2. Program for Octal Value

3. Program for decimal Value

Sahyog College of Management Studies, Thane


Constants:
The constants in C are the read-only variables whose values cannot be modified once they are
declared in the C program. The type of constant can be an integer constant, a floating pointer
constant, a string constant, or a character constant. In C language, the const keyword is used to
define the constants.

Syntax to Define Constant


const data_type var_name = value;

Declaring Symbolic Constants:


Identifiers are used to represent fixed values in programs using symbolic constants in the C
programming language. These parameters are frequently used to increase
the readability, maintainability, and modifiability of code, which may be numerical or not. The
C language's "#define" command is used to declare symbolic constants.

Syntax for defining a symbolic constant in C:


#define MAX_V 100

In this example, the symbolic constant "MAX_V" is specified to have a value of 100. It indicates
that the number 100 will be substituted for "MAX_V" wherever it appears in the code at
compilation time.
We may specify non-numerical values like strings using symbolic constants.

For example:
#define error "INVALID"

In this example, the text "INVALID" is declared as the symbolic constant "error". It can be helpful
for defining error messages or other strings that are frequently used throughout a program.

typedef
The typedef is a keyword used in C programming to provide some meaningful names to the
already existing variable in the C program. It behaves similarly as we define the alias for the
commands. In short, we can say that this keyword is used to redefine the name of an already
existing variable.

Sahyog College of Management Studies, Thane


Syntax of typedef
typedef <existing_name> <alias_name>

Example
typedef int i;
Below is the C program to illustrate how to use typedef.
#include <stdio.h>
typedef int i;

int main()
{
i var= 20;
printf("%d", var);
return 0;
}

Output
20

Use of typedef in C
Following are some common uses of the typedef in C programming:
 The typedef keyword gives a meaningful name to the existing data type which helps
other users to understand the program more easily.
 It can be used with structures to increase code readability and we don’t have to type
struct repeatedly.

************

Sahyog College of Management Studies, Thane

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy