0% found this document useful (0 votes)
16 views24 pages

Module-1 - Part - 2

The document provides an introduction to the C programming language, detailing its classification into low-level and high-level languages, with C being a high-level language developed in 1972. It outlines the structure of a C program, including sections for documentation, linking, definitions, and the main function, along with the process for writing, compiling, and running a C program. Additionally, it covers C tokens, data types, variables, constants, and input/output functions, emphasizing the importance of syntax and rules for identifiers and variable names.

Uploaded by

Sowmiya V
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)
16 views24 pages

Module-1 - Part - 2

The document provides an introduction to the C programming language, detailing its classification into low-level and high-level languages, with C being a high-level language developed in 1972. It outlines the structure of a C program, including sections for documentation, linking, definitions, and the main function, along with the process for writing, compiling, and running a C program. Additionally, it covers C tokens, data types, variables, constants, and input/output functions, emphasizing the importance of syntax and rules for identifiers and variable names.

Uploaded by

Sowmiya V
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

MODULE – 1 DEPT OF CSE

Introduction to C
COMPUTER LANGUAGES
In order to communicate with the computer user also needs to have a language that
should be understood by the computer. For this purpose, different languages are developed
for performing different types of work on the computer. Basically, languages are divided into
two categories according to their interpretation.

1. Low Level Languages.

2. High Level Languages.

Low Level Languages:

- Low level computer languages are machine codes or close to it.


- Computer cannot understand instructions given in high level languages or in English.
It can only understand and execute instructions given in the form of machine language
(i.e.) language of 0 and 1.
- There are two types of low level languages:
o Machine Language.
o Assembly Language

High Level Languages:


- High-level languages are basically symbolic languages that use English words and/or
mathematical symbols rather than mnemonic codes.
- Each instruction in the high level language is translated into many machine language
instructions thus showing one-to-many translation

Introduction to C:
C is a programming language developed at AT & T‟s Bell Laboratories of USA in
1972. It was designed and written by Dennis Ritche. Dennis Ritchie is known as the founder
of c [Link] is a very popular language, despite being old.

14
MODULE – 1 DEPT OF CSE

The features of C

1. Portability or machine independent


2. Sound and versatile language
3. Fast program execution.
4. An extendible language.
5. Tends to be a structured language.

General Structure of a C program

/* Documentation section */

/* Link section */

/* Definition section */

/* Global declaration section */

main()

Declaration part

Executable part (statements)

/* Sub-program section */

- The documentation section is used for displaying any information about the program
like the purpose of the program, name of the author, date and time written etc, and
this section should be enclosed within comment lines. The statements in the
documentation section are ignored by the compiler.
- The link section consists of the inclusion of header files.
- The definition section consists of macro definitions, defining constants etc,.
- Anything declared in the global declaration section is accessible throughout the
program, i.e. accessible to all the functions in the program.
- main() function is mandatory for any program and it includes two parts, the
declaration part and the executable part.

15
MODULE – 1 DEPT OF CSE

- The last section, i.e. sub-program section is optional and used when we require
including user defined functions in the program.

First C Program

Before getting started with C language, you need to learn how to write, compile and run the
first c program.

Creating and Running Programs:

There are four steps in this process.

1. Writing and editing the program using Text editor (source code).
2. Compile the program using any C compiler.(.bak file)
3. Linking the program with the required library modules(object file)
4. Executing the program. (.exe file)

#include <stdio.h>
#include<conio.h>
void main()
{
printf("Hello C Language");
getch();
}

16
MODULE – 1 DEPT OF CSE

- #include<stdio.h> includes the standard input output library [Link]


printf() function is defined in stdio.h .
- #include<conio.h> includes the console input output library functions. The
getch() function is defined in conio.h file.
- void main() The main() function is the entry point of every program in c
language. The void keyword specifies that it returns no value.
- printf() The printf() function is used to print data on the console.
- getch() The getch() function asks for a single [Link] you press any
key, it blocks the screen.

C TOKENS

The smallest individual units are known as tokens. C has six types of tokens.

1. Identifiers
2. Keywords
3. Constants
4. Strings
5. Special Symbols
6. Operators

Identifiers:

Identifiers refer to the names of variables, constants, functions and arrays. These are
user-defined names is called Identifiers. These identifier are defined against a set of rules.

Rules for an Identifier

1. An Identifier can only have alphanumeric characters ( a-z , A-Z , 0-9 ) and
underscore( _ ).
2. The first character of an identifier can only contain alphabet ( a-z , A-Z ) or
underscore ( _ ).
3. Identifiers are also case sensitive in C. For example name and Name are two
different identifier in C.
4. Keywords are not allowed to be used as Identifiers.
5. No special characters, such as semicolon, period, whitespaces, slash or comma
are permitted to be used in or as Identifier.
6. C‟ compiler recognizes only the first 31 characters of identifiers.

Ex :

17
MODULE – 1 DEPT OF CSE

Valid Invalid
STDNAME Return
SUB $stay
TOT_MARKS 1RECORD
_TEMP STDNAME.
Y2K
Keywords

A keyword is a reserved word. All keywords have fixed meaning that means we cannot
change. Keywords serve as basic building blocks for program statements. All keywords must
be written in lowercase. A list of 32 keywords in c language is given below:

Note: Keywords we cannot use it as a variable name, constant name etc.

Data Types

- To store data the program must reserve space which is done using datatype.
- A datatype is a keyword/predefined instruction used for allocating memory for
data.
- A data type specifies the type of data that a variable can store such as integer,
floating, character etc.
- It used for declaring/defining variables or functions of different types before to
use in a program.

18
MODULE – 1 DEPT OF CSE

There are 4 types of data types in C language.

Types Datatypes
Basic Data Type int, char, float, double
Derived Data Type array, pointer, structure, union
Enumeration Data Type Enum
Void Data Type void

The basic data types are integer-based and floating-point based. C language supports both
signed and unsigned literals. The memory size of basic data types may change according to
32 or 64 bit operating system. Let‟s see the basic data types. Its size is given according to 32
bit architecture

19
MODULE – 1 DEPT OF CSE

Size and Ranges of Data Types with Type Qualifiers

Type Size (bytes) Range Control String


char or signed char 1 -128 to 127 %c
unsigned char 1 0 to 255 %c
int or signed int 2 -32768 to 32767 %d or %i
unsigned int 2 0 to 65535 %u
short int or signed short int 1 -128 to 127 %d or %i
unsigned short int 1 0 to 255 %d or %i
long int or signed long int 4 -2147483648 to 2147483647 %ld
unsigned long int 4 0 to 4294967295 %lu
float 4 3.4E-38 to 3.4E+38 %f or %g
double 8 1.7E-308 to 1.7E+308 %lf
long double 10 3.4E-4932 to 1.1E+4932 %Lf

Variables

- A variable is a name of memory location. It is used to store data. Variables are


changeable, we can change value of a variable during execution of a program. . It can
be reused many times.
- Note: Variable are nothing but identifiers.

Rules to write variable names:

1. Variable name contains maximum of 30 characters/ Variable name must be upto 8


characters.
2. A variable name includes alphabets and numbers, but it must start with an alphabet.
3. It cannot accept any special characters, blank spaces except under score( _ ).
4. It should not be a reserved word (keyword)

Declaration of Variables :

A variable can be used to store a value of any data type. The declaration of variables must be
done before they are used in the program. The general format for declaring a variable.

Syntax :

data_type variable-1,variable-2,------, variable-n;

20
MODULE – 1 DEPT OF CSE

Variables are separated by commas and declaration statement ends with a semicolon

Ex : int x,y,z;

float a,b;

char m,n;

Assigning values to variables :

Values can be assigned to variables using the assignment operator (=). The general
format statement is :

Syntax : variable = constant;

Ex :

x=100;

a= 12.25;

m=‟f‟;

We can also assign a value to a variable at the time of the variable is declared.

The general format of declaring and assigning value to a variable is :

data_type variable = constant;

Ex ; int x=100;

float a=12.25;

char m=‟f‟;

Types of Variables in C :

There are many types of variables in c:

1. local variable 2. global variable 3. static variable

Constants

Constants refer to fixed values that do not change during the execution of a program.

Note: constants are also called literals.

21
MODULE – 1 DEPT OF CSE

TYPES OF C CONSTANT:

1. Integer constants

2. Real or Floating point constants

3. Character constants

4. String constants

5. Backslash character constants

Integer constants:

An integer constant is a numeric constant (associated with number) without any


fractional or exponential part.

Floating point/Real constants:

A floating point constant is a numeric constant that has either a fractional form or an
exponent form. Eg : 2.0 , 0.0000234 , -0.22E-5

Character Constant:

Single Character Constant : A character constant is either a single alphabet, a single


digit, a single special symbol enclosed within single inverted commas. a)

it is value represent in „ „ (single quote).

22
MODULE – 1 DEPT OF CSE

String constant :

A string constant is a sequence of characters enclosed in double quote, the characters


may be letters, numbers, special characters and blank space etc.

EX : “rama” , “a” , “+123” , “1-/a”

Escape characters or backslash characters:

\n newline

\r carriage return

\t tab

\v vertical tab

\b backspace

\f form feed (page feed)

\a alert (beep)

\‟ single quote(„)

\” double quote(“)

\? Question mark (?)

\\ backslash (\)

Two ways to define constant in C

There are two ways to define constant in C programming.

1. const keyword

2. #define pre-processor

C const keyword

The const keyword is used to define constant in C programming.

const float PI=3.14;

Now, the value of PI variable can't be changed.

1. #include <stdio.h>

23
MODULE – 1 DEPT OF CSE

2. #include <conio.h>
3. void main() {
4. const float PI=3.14; .
5. clrscr();
6. . printf("The value of PI is: %f",PI);
7. getch();
8. }

C #define pre-processor

The #define preprocessor is also used to define constant. C#define 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.

1. #include<stdio.h>
2. #define PI 3.14
3. main()
4. {
5. printf("%f",PI);
6. }
Output: 3.140000

INPUT AND OUTPUT STATEMENTS

- Input and Output statement are used to read and write the data in C programming.
These are embedded in stdio.h (standard Input/Output header file).
- Input means to provide the program with some data to be used in the program
and Output means to display data on screen or write the data to a printer or a file.
- C programming language provides many built-in functions to read any given input
and to display data on screen when there is a need to output the result.

There are mainly two of Input/Output functions are used for this purpose. These are discussed
as:

24
MODULE – 1 DEPT OF CSE

- Unformatted I/O functions


- Formatted I/O functions

Formated I/O Functions: formatted I/O functions operates on various types of data.

1 : printf() :

Output data or result of an operation can be displayed from the computer to a standard
output device using the library function printf(). This function is used to print any
combination of data.

Syntax : printf(“control string “, variable1, variable2, -----------, variablen);


Ex : printf(“%d”,3977); // Output: 3977

printf() statement another syntax : Syntax : printf(“fomating string”); Formating string : it


prints all the character given in doublequotes (“ “) except formatting specifier.

Ex :
printf(“ hello “); hello
printf(“a”);  a
printf(“%d”, a); a value
printf(“%d”);  no display

25
MODULE – 1 DEPT OF CSE

scanf() :

Input data can be entered into the computer using the standard input „C‟ library function
called scanf().

This function is used to enter any combination of input.

Syntax :

scanf(“control string “,&var1, &var2,----, &varn);

The scanf() function is used to read information from the standard input device (keyboard).

Ex : scanf(“ %d “,&a);-> hello

Each variable name (argument) must be preceeded by an ampersand (&). The (&) symbol
gives the meaning “address of “ the variable

Unformatted I/O functions:

a) Character I/O

b) String I/O

a) Character I/O:

1. getchar(): Used to read a character from the standard input


2. putchar(): Used to display a character to standard output
3. getch() and getche(): these are used to take the any alpha numeric characters from
the standard input getche() read and display the character getch() only read the
single character but not display
4. putch(): Used to display any alpha numeric characters to standard output

1. getchar() - This function is an Input function. It is used for reading a single character from
the keyboard. It is a buffered function. Buffered functions get the input from the keyboard
and store it in the memory buffer temporally until you press the Enter [Link] general syntax
is as:

v = getchar();

eg :

char n;

26
MODULE – 1 DEPT OF CSE

n = getchar();

/*To read a single character from the keyboard using the getchar() function*/

#include <stdio.h>

main()

char n;

n = getchar();

putchar() - This function is an output function. It is used to display a single character on the
screen. The general syntax is as:

putchar(v);

/*Program illustrate the use of getchar() and putchar() functions*/

#include <stdio.h>

main()

char n;

n = getchar();

putchar(n);

getch()- This is also an input function. This is used to read a single character from the
keyboard like getchar() function. But getchar() function is a buffered is function, getchar()
function is a non-buffered function.

general syntax is as:

v = getch();

/*Program to explain the use of getch() function*/

27
MODULE – 1 DEPT OF CSE

#include <stdio.h>

main()

char n;

puts("Enter the Char");

n = getch();

puts("Char is :");

putchar(n);

getch();

getche()

All are same as getch(0 function execpt it is an echoed function.

/*Program to explain the use of getch() function*/

#include <stdio.h>

main()

char n;

puts("Enter the Char");

n = getche();

puts("Char is :");

putchar(n);

getche();

28
MODULE – 1 DEPT OF CSE

b) String I/O:

1. gets(): Used for accepting any string from the standard input(stdin) eg:gets()
2. puts(): Used to display a string or character array Eg:puts()
3. Cgets():read a string from the console eg; cgets(char *st)
4. Cputs():display the string to the console eg; cputs(char *st)

gets() - This function is an input function. It is used to read a string from the keyboard.

Syntax : gets(v);

/*Program to explain the use of gets() function*/

#include <stdio.h>

main()

char n[20];

gets(n);

puts() - This is an output function. It is used to display a string inputted by gets() function.

Syntax :

puts(v);

or

puts("text line");

/*Program to illustrate the concept of puts() with gets() functions*/

#include <stdio.h>

main()

char name[20];

29
MODULE – 1 DEPT OF CSE

puts("Enter the Name");

gets(name);

puts("Name is :");

puts(name);

OPERATORS AND EXPRESSIONS

Operators :

An operator is a Symbol that performs an operation. An operators acts some variables


are called operands to get the desired result.

Ex : a+b; Where a,b are operands and + is the operator.

Types of Operator :

1) Arithmetic Operators.

2) Relational Operators.

3) Logical Operators.

4) Assignment Operators.

5). Unary Operators.

6) Conditional Operators.

7) Special Operators.

8) Bitwise Operators.

Arithmetic Operators : An arithmetic operator performs mathematical operations such as


addition, subtraction and multiplication on numerical values (constants and variables). C
Program to demonstrate the working of arithmetic operators.

Example:

#include void main() {

int a = 9,b = 4, c;

30
MODULE – 1 DEPT OF CSE

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); }

Output

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

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. Operands may be variables, constants or
expressions.

- Relational operators are used in decision making and loops.

// C Program to demonstrate the working of relational operators

31
MODULE – 1 DEPT OF CSE

#include<stdio.h>

int main() {
int a = 5, b = 5, c = 10;
printf("%d == %d = %d \n", a, b, a == b); // true
printf("%d == %d = %d \n", a, c, a == c); // false
printf("%d > %d = %d \n", a, b, a > b); //false
printf("%d > %d = %d \n", a, c, a > c); //false
printf("%d < %d = %d \n", a, b, a < b); //false
printf("%d < %d = %d \n", a, c, a < c); //true
printf("%d != %d = %d \n", a, b, a != b); //false
printf("%d != %d = %d \n", a, c, a != c); //true
printf("%d >= %d = %d \n", a, b, a >= b); //true
printf("%d >= %d = %d \n", a, c, a >= c); //false
printf("%d <= %d = %d \n", a, b, a <= b); //true
printf("%d <= %d = %d \n", a, c, a <= c); //true
return 0;
}

Output :
5 == 5 = 1
5 == 10 = 0
5>5=0
5 > 10 = 0
5<5=0
5 < 10 = 1
5 != 5 = 0
5 != 10 = 1
5 >= 5 = 1
5 >= 10 = 0
5 <= 5 = 1
5 <= 10 = 1

32
MODULE – 1 DEPT OF CSE

Logical Operators.:

These operators are used to combine the results of two or more conditions. 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.

Logical AND : If any one condition false the complete condition becomes false.

Logical OR : If any one condition true the complete condition becomes true.

Logical Not : This operator reverses the value of the expression it operates on i.e, it makes a
true expression false and false expression true.

// C Program to demonstrate the working of logical operators

#include<stdio.h>

int main() {

int a = 5, b = 5, c = 10, result;

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

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

33
MODULE – 1 DEPT OF CSE

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

printf("(a = b) && (c < b) equals to %d \n", result);

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

printf("(a = b) || (c < b) equals to %d \n", result);

result = (a != b) || (c < b);

printf("(a != b) || (c < b) equals to %d \n", result);

result = !(a != b);

printf("!(a == b) equals to %d \n", result);

result = !(a == b);

printf("!(a == b) equals to %d \n", result);

return 0; }

Output:
(a = b) && (c > b) equals to 1

(a = b) && (c < b) equals to 0

(a = b) || (c < b) equals to 1

(a != b) || (c < b) equals to 0

!(a != b) equals to 1

!(a == b) equals to 0

Assignment Operators.

Assignment operators are used to assign a value (or) an expression (or) a value of a variable
to another variable.

Syntax : variable name=expression (or) value (or) variable

Compound assignment operator: C provides compound assignment operators to assign a


value to variable in order to assign a new value to a variable after performing a specified
operation

// C Program to demonstrate the working of assignment operators

34
MODULE – 1 DEPT OF CSE

#include<stdio.h>

int main() {

int a = 5, c;

c = a;

printf("c = %d \n", c);

c += a; // c = c+a

printf("c = %d \n", c);

c -= a; // c = c-a

printf("c = %d \n", c);

c *= a; // c = c*a

printf("c = %d \n", c);

c /= a; // c = c/a

printf("c = %d \n", c);

c %= a; // c = c%a

printf("c = %d \n", c);

return 0; }

Output :

c=5

c = 10

c=5

c = 25

c=5

c=0

Increment and Decrement Operators /Unary Operators:


- Unary operators are having higher priority than the other operators. Unary
operators, meaning they only operate on a single operand.
- Increment operator is used to increment the current value of variable by adding
integer 1. Decrement operator is used to decrement the current value of variable by
subtracting integer 1.

The operator ++ adds 1 to the operand and – subtracts 1 from the operand. These
operators in two forms : prefix (++x) and postfix(x++).

35
MODULE – 1 DEPT OF CSE

// C Program to demonstrate the working of increment and decrement


operators
#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

Bitwise Operators:
Bitwise operators are used to manipulate the data at bit level. It operates on integers only. It
may not be applied to float.

Expressions:
An expression is a combination of operators and operands which reduces to a single
value. An operator indicates an operation to be performed on data that yields a value. An
operand is a data item on which an operation is performed.
A simple expression contains only one operator

36
MODULE – 1 DEPT OF CSE

Type Conversion/Type casting:


- Type conversion is used to convert variable from one data type to another data type,
and after type casting complier treats the variable as of new data type.
- For example, if you want to store a 'long' value into a simple integer then you can
type cast 'long' to 'int'.

You can convert the values from one type to another explicitly using the cast operator.
Type conversions can be implicit which is performed by the compiler automatically, or it can
be specified explicitly through the use of the cast operator.
Syntax: (type_name) expression;

Without Type Casting:


1. int f= 9/4;
2. printf("f : %d\n", f );//Output: 2

With Type Casting:


1. float f=(float) 9/4;
2. printf("f : %f\n", f );//Output: 2.250000
(or)

Type Casting - C Programming


Type casting refers to changing an variable of one data type into another. The
compiler will automatically change one type of data into another if it makes sense. For
instance, if you assign an integer value to a floating-point variable, the compiler will convert
the int to a float. Casting allows you to make this type conversion explicit, or to force it when
it wouldn‟t normally happen.
Type conversion in c can be classified into the following two types:

1. Implicit Type Conversion When the type conversion is performed automatically


by the compiler without programmers intervention, such type of conversion is
known as implicit type conversion or type promotion.
2. Explicit Type Conversion The type conversion performed by the programmer by
posing the data type of the expression of specific type is known as explicit type
conversion. The explicit type conversion is also known as type casting.

37

You might also like