0% found this document useful (0 votes)
8 views17 pages

Full UNIT 2

Uploaded by

Upasana Y
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)
8 views17 pages

Full UNIT 2

Uploaded by

Upasana Y
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/ 17

PROGRAMMING IN C

UNIT 2 ~ BY IGDTUW RESOURCE

BASIC STRUCTURE OF C PROGRAMMING:


To write a C program, we first create functions and put them together. A C program may contain one or more
sections as shown in fig.

The documentation section contains 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.
The link section provides instructions to the computer to link functions from the system Library.
The definition section defines all symbolic constants.
There are some variables that are used in more than one function such variables are called global variables
and our declared in the global declaration section that is outside of all the functions. This section also declares
all the user defined functions.
Every C program must have one main () function section. This section contains two parts declaration part and
executable part.
The declaration part declares all the variables used in the executable part. There is at least one statement in
the executable part. These two parts must appear between the opening and the closing braces. The program
execution begins at the opening brace and ends at the closing brace. the closing brace of the main function
section is the logical end of the program. All the statements in the declaration and executable part ends with
semicolon (;)
--------------------------------------------------------------------------------------------------------------
C TOKENS : In a passage of text, individual words and punctuation marks are called tokens. Similarly, in C
program, the smallest individual units are known as C TOEKNS.
C has 6 types of tokens as shown in fig. C programs are written using these tokens and the syntax of the
language.

--------------------------------------------------------------------------------------------------------------
KEYWORDS & IDENTIFIERS:

SR.
NO. KEYWORD IDENTIFIER

Keywords are predefined word that gets Identifiers are the values used to define different
reserved for working program that have programming items such as variables, integers,
special meaning and cannot get used structures, unions and others and mostly have an
1 anywhere else. alphabetic character.

2 Specify the type/kind of entity. Identify the name of a particular entity.

First character can be a uppercase, lowercase letter


3 It always starts with a lowercase letter. or underscore.
SR.
NO. KEYWORD IDENTIFIER

4 A keyword should be in lower case. An identifier can be in upper case or lower case.

A keyword contains only alphabetical An identifier can consist of alphabetical characters,


5 characters. digits and underscores.

They help to identify a specific property They help to locate the name of the entity that gets
6 that exists within a computer language. defined along with a keyword.

No punctuation or special symbol except ‘underscore’


7 No special symbol, punctuation is used. is used.

Examples of keywords are: int, char, if, Examples of identifiers are: Test, count1, high_speed,
8 while, do, class etc. etc.

--------------------------------------------------------------------------------------------------------------
C LIBRARY FUNCTIONS:

S Header
No. Files Description

It checks the value of an expression that we expect to be true under


normal circumstances.
1 <assert.h> If the expression is a nonzero value, the assert macro does nothing.

2 <complex.h> A set of functions for manipulating complex numbers.

Defines macro constants specifying the implementation-specific


properties of the
3 <float.h> floating-point library.
S Header
No. Files Description

These limits specify that a variable cannot store any value beyond
these limits, for example-
4 <limits.h> An unsigned character can store up to a maximum value of 255.

The math.h header defines various mathematical functions and one


macro. All the Functions
in this library take double as an argument and return double as the
5 <math.h> result.

The stdio.h header defines three variable types, several macros, and
various
6 <stdio.h> function for performing input and output.

7 <time.h> Defines date and time handling functions.

Strings are defined as an array of characters. The difference


between a character array
8 <string.h> and a string is that a string is terminated with a special character ‘\0’.

--------------------------------------------------------------------------------------------------------------
VARIABLES IN C :
A variable in C is a memory location associated with some name in order to store some form of data and
retrieve it when required. We can store different types of data in the variable and reuse the same variable for
storing some other data any number of times.
For using a variable in C, we have to first define it to tell the compiler about its existence so that compiler can
allocate the required memory to it.
There are 3 aspects of defining a variable:
Variable Declaration
Variable declaration in C tells the compiler about the existence of the variable with the given name and data
type. No memory is allocated to a variable in the declaration.

Variable Definition
In the definition of a C variable, the compiler allocates some memory and some value to it. A defined variable
will contain some random garbage value till it is not initialized.

Variable Initialization
Initialization of a variable is the process where the user assigns some meaningful value to the variable.
--------------------------------------------------------------------------------------------------------------
DATA TYPES IN C : Each variable in C has an associated data type. Each data type requires different
amounts of memory and has some specific operations which can be performed over it.
It specifies the type of data that the variable can store like integer, character, floating, double, etc.
Refer this website too: https://www.geeksforgeeks.org/data-types-in-c/

The data type is a collection of data with values having fixed values, meaning as well as its characteristics.
--------------------------------------------------------------------------------------------------------------
OPERATORS IN C : An operator is a symbol that tells the computer to perform certain mathematical or
logical manipulations. Operators are used in programs to manipulate data and variables.
C operators can be classified into various operators:

1. Arithmetic operators: An arithmetic operator performs mathematical operations such as addition,


subtraction, multiplication, division etc on numerical values (constants and variables).

Operator Meaning of Operator

+ addition or unary plus

- subtraction or unary minus

* multiplication

/ division

% remainder after division (modulo division)

EXAMPLE:
#include <stdio.h>

int main()
{
int a = 9,b = 4, c;

c = a+b;
printf("a+b = %d \n",c);
c = a-b;
printf("a-b = %d \n",c);
c = a*b;
printf("a*b = %d \n",c);
c = a/b;
printf("a/b = %d \n",c);
c = a%b;
printf("Remainder when a divided by b = %d \n",c);

return 0;
}
Output

a+b = 13
a-b = 5
a*b = 36
a/b = 2
Remainder when a divided by b=1

--------------------------------------------------------------------------------------------------------------

2. Increment & Decrement operators:


C programming has two operators increment ++ and decrement -- to change the value of an operand
(constant or variable) by 1.

Increment ++ increases the value by 1 whereas decrement -- decreases the value by 1. These two operators
are unary operators, meaning they only operate on a single operand.

EXAMPLE:
#include <stdio.h>

int main()
{
int a = 10, b = 100;
float c = 10.5, d = 100.5;

printf("++a = %d \n", ++a);


printf("--b = %d \n", --b);
printf("++c = %f \n", ++c);
printf("--d = %f \n", --d);

return 0;
}

Output

++a = 11
--b = 99
++c = 11.500000
--d = 99.500000
3. ASSIGNMENT OPERATORS : An assignment operator is used for assigning a value to a variable.
The most common assignment operator is =

Operator Example Same as

= a=b a=b

+= a += b a = a+b

-= a -= b a = a-b

*= a *= b a = a*b

/= a /= b a = a/b

%= a %= b a = a%b

#include <stdio.h>
int main()
{
int a = 5, c;

c = a; // c is 5
printf("c = %d\n", c);
c += a; // c is 10
printf("c = %d\n", c);
return 0;
}

Output

c=5
c = 10

--------------------------------------------------------------------------------------------------------------
4. RELATIONAL OPERATORS : A relational operator checks the relationship between two operands. If
the relation is true, it returns 1; if the relation is false, it returns value 0.
Relational operators are used in decision making and loops.

Operator Meaning of Operator Example

== Equal to 5 == 3 is evaluated to 0

> Greater than 5 > 3 is evaluated to 1

< Less than 5 < 3 is evaluated to 0

!= Not equal to 5 != 3 is evaluated to 1

>= Greater than or equal to 5 >= 3 is evaluated to 1

<= Less than or equal to 5 <= 3 is evaluated to 0

EXAMPLE :
#include <stdio.h>

int main()
{
int a = 5, b = 5, c = 10;

printf("%d == %d is %d \n", a, b, a == b);


printf("%d == %d is %d \n", a, c, a == c);
printf("%d > %d is %d \n", a, b, a > b);
printf("%d > %d is %d \n", a, c, a > c);
return 0;
}

Output

5 == 5 is 1
5 == 10 is 0
5 > 5 is 0
5 > 10 is 0
5. LOGICAL OPERATORS :
An expression containing logical operator returns either 0 or 1 depending upon whether
expression results true or false. Logical operators are commonly used in decision making
in C programming.

Operator Meaning Example

If c = 5 and d = 2 then,
Logical AND. True only if
&& expression ((c==5) && (d>5))
all operands are true
equals to 0.

Logical OR. True only if If c = 5 and d = 2 then,


|| either one operand is expression ((c==5) || (d>5))
true equals to 1.

Logical NOT. True only if If c = 5 then, expression


!
the operand is 0 !(c==5) equals to 0.

EXAMPLE:

#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10, result;

result = (a == b) && (c > b);


printf("(a == b) && (c > b) is %d \n", result);

result = (a == b) && (c < b);


printf("(a == b) && (c < b) is %d \n", result);
return 0;
}

Output

(a == b) && (c > b) is 1
(a == b) && (c < b) is 0
6. BITWISE OPERATORS: These operators are used for testing the bits, or shifting them right or left.
Bitwise operators might not be applied to float or double. During computation, mathematical operations like:
addition, subtraction, multiplication, division, etc are converted to bit-level which makes processing faster and
saves power.

Operators Meaning of operators

& Bitwise AND

| Bitwise OR

^ Bitwise exclusive OR

~ Bitwise complement

<< Shift left

>> Shift right

WANNA READ MORE ABOUT BITWISE OPERATORS ?


EXPLORE THIS SITE: https://www.programiz.com/c-programming/bitwise-operators

------------------------------------------------------------------------------------------------------------
7. sizeof OPERATOR : The sizeof is a unary operator that returns the size of data (constants, variables,
array, structure, etc). It is a compile time operator and when used with operand, it returns the number of
bytes the operand occupies.
It is normally used to determine the lenth of ARRAYS and structures when their sizes is unknown to the
programmer.

--------------------------------------------------------------------------------------------------------------
8. CONDITIONAL OPERATORS: The conditional operator is kind of similar to the if-
else statement as it does follow the same algorithm as of if-else statement but the
conditional operator takes less space and helps to write the if-else statements in the
shortest way possible.

Syntax: The conditional operator is of the form


variable = Expression1 ? Expression2 : Expression3
CONCEPT OF getchar()

The simplest of all input/ output operations is reading a character from the “standard input” unit (usually
the keyboard) and writing it to the “standard output” unit.
Reading a single character can be done by using the function getchar().
The getchar takes the following form
variable_name = getchar();

When this statement is encountered, the computer waits until a key is pressed and then assigns the
character as a value to getchar function.

Since getchar is used on the right-hand side of an assignment statement, the character value of getchar is in
turn assigned to the variable name on the left.

#include <stdio.h>

// Driver code
int main()
{
char character;
character = getchar();

printf("The entered character is : %c",


character );
return 0;
}
Output:

------------------------------------------------------------------------------------------------------------
CONCEPT OF PUTCHAR
putchar is used for writing the characters one at a time to the terminal.
It takes the following form:

putchar(variable_name);
where variable_name is a type char variable containing a character.

#include <stdio.h>

// Driver code
int main()
{
char character;
printf("Enter any random character between a-z: ");
character = getchar();

printf("The entered character is : ");


putchar(character);
return 0;
}
Output:

-----------------------------------------------------------------------------------------------
FOR DECISION MAKING STATEMENTS, KINDLY FOLLOW THIS LINK

https://www.geeksforgeeks.org/decision-making-c-cpp/

This link contains flowchart, algo, input, output and concept.

THANKS FOR READING THE NOTES

LEARN & GROW WITH IGDTUW RESOURCE

You might also like