0% found this document useful (0 votes)
17 views

c notes 1.1

C is a general-purpose programming language developed by Dennis Ritchie at AT&T's Bell Laboratories in 1972, characterized by its structured programming capabilities and middle-level language features. It is widely used for system and application software development due to its efficiency and portability. The document outlines the basic structure of a C program, including syntax, data types, variables, constants, and input-output operations.

Uploaded by

manjudhakad305
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

c notes 1.1

C is a general-purpose programming language developed by Dennis Ritchie at AT&T's Bell Laboratories in 1972, characterized by its structured programming capabilities and middle-level language features. It is widely used for system and application software development due to its efficiency and portability. The document outlines the basic structure of a C program, including syntax, data types, variables, constants, and input-output operations.

Uploaded by

manjudhakad305
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

INTRODUCTION TO C LANGUAGE

C is a programming language developed at AT & T‟s Bell laboratories of USA in 1972. It


was developed by Dennis Ritchie.
Characteristics of C:
 C is a general purpose programming language.
 It is a structured programming language (It allows modularization process of dividing
problems into sub-problems and C has structured statements like sequence, selection
and iteration)

 C is a middle level language (C is a high level language but also incorporates features
of low-level languages like manipulation of bits, bytes, words and addresses.)

 Helps in development of system software ( C is used for writing application programs


and since it has low level languages features it can be used in development of system
software)
 Has rich set of operators and data types and built-in functions.
 Provides compact representation for expressions. This makes the language simple,
orderly and easy to learn.
 No rigid format. Any number of statements can be typed in a single line.
 Portability: any C program can be run on different machines with little or no
modifications.
 Less number of reserved words
 Pointer arithmetic and pointer manipulation
 Ability to extend itself by adding functions to its library.
History of C:
year Language Developed by Remarks
1960 ALGOL International committee Too general, to abstract
1963 CPL Cambridge Difficult to learn and implement
University(CU)
1967 BCPL Martin Richards at CU Could deal with only specific problems
1970 B Ken Thomson at AT & T Could deal with only specific problems
1972 C Dennis Ritchie at Restored the lost generality of BCPL
AT & T and B

1|Page
Applications of C: Because of its portability and efficiency, C is used to develop the
system as well as application software. Some important system programs include operating
systems, compilers, interpreters, assemblers, editors, linkers and loaders.
Being a structured language, it is useful in developing application programs like database
systems, word processors, spreadsheets, graphics packages, CAD/CAM applications, office
automation tools, scientific and engineering applications, animation and games.

Basic structure of a C program or Syntax:


Every C program includes the following components
 Pre-processor directives
 Main()

 A pair of flower brackets { }

 Declaration and statements

 User created sub-programs or functions.

Pre-processor directives

Global declarations;

Main ()

Declarations;

Statements;

User created sub-programs or functions

Pre-processor directives: these statements begin with # symbol. They direct the C pre-
processor to include header files and symbolic constants into a C program.

E.g.: #include<stdio.h> : For the standard input, output function

#include<conio.h> : For the Console input, output function

# define NULL 0 : For defining symbolic constant, NULL=0

2|Page
Global declarations: Variables or functions whose existence is known in the main ()
function and other user defined functions, are called the global variables or functions. Their
declarations should be made before main ().

Main (): This is the main function of every C program. Execution of a C program starts with
Main (). No C program is executed without the main () function. It should be written in
lowercase letters and should not be terminated with semicolon. It calls other library functions
and user defined functions. There must be one and only one main () function in any C
program.

A pair of flower brackets: The flower brackets indicate body of the program, which
includes instructions to perform the required task. { indicates beginning of the main ()
function and } indicates end of the main() function.

Declarations: It is the part where all the variables, arrays, functions etc., used in the C
program are declared and may be initialized with their basic data types.

Statements: These are instructions to perform certain operations like input, output,
assignment, arithmetic statements, control statements and other statements. They also include
comments. Comments are explanatory notes on some instructions. The statements to be
commented must be enclosed within /* and */. The comment lines are not compiled and
executed.

User created sub-programs or functions: These are sub-programs created by the user to
perform certain common functions. They may be written before or after main ().

Example:

/* C program to display hello world */

#include<stdio.h>

#include<conio.h>

void main()

printf(“\n HELLO
WORLD”); getch();

Output:

HELLO WORLD

3|Page
C PROGRAMMING PRELIMINARIES

CHARACTER SET:

Alphabets Uppercase letters A to Z

Lowercase letters a to z

Digits 0 to 9

Special Characters blank , ; : “ + - * / { } ? ! # _= | ^ ~ < > \ () []@...etc.

IDENTIFIERS: These are names given to program elements like variables, arrays, functions
and structures. Basically identifiers are the sequence of alphabets and digits.

Rules for forming identifier names:

1. The first character must be an alphabet (upper or lowercase) or an underscore.


2. All succeeding characters must be either letters or digits but no other special
character or punctuation symbols allowed.
3. Uppercase and lowercase identifiers are different in C. (E.g.: SUM, sum and
sum are different identifiers.)
4. No two successive underscores are allowed.
5. Keywords should not be used as identifiers.
6. Identifiers should be single words. That is, blank spaces cannot be included in
identifiers.
E.g.: valid identifiers: - sum, emp_name, stu_address, number, age

Invalid identifiers: - father‟s-name, while, @total, do-it, 3by2.

VARIABLES:

An identifier/ quantity that change its value during program execution are called
„variable‟. A variable is an object or element that may take on any value of a specified type.
Variables represent named storage locations, whose value can be manipulated during
program execution.

E.g.:- sum, area, m1, length, sname, age, city etc.

Rules for naming a variable – same as rules for forming an identifier name

Valid variables Invalid variables


Marks 8ab
TOTAL_MARK TOTAL-MARK
Gross_salary_2004 Gross-salary-2004
Area_of_circle Area-of-circle
Num[20] Num(20)

4|Page
CONSTANTS: It refers to fixed values that do not change during the execution of the
program. C supports several types of constants.

1. Numeric Constants
a. Integer constant
b. Float (real) constant
2. Character constants
a. Single character constants
b. String constants
3. Symbolic constants
4. Backslash constants
1. Numeric Constants
a. Integer constants: All whole numbers are integer constants. They are
sequence of digits without decimal point
E.g.: Decimal constants : 134, 9000, 3567, -987
Octal constant : 06, 034, 0677, -07867
Hexadecimal constant : 0x20, 0xA, 0x1AB
b. Float (Real) constant: Numbers having decimal point are floating point
constants. They can contain both an integer part and a fractional part in the
number. They are stored differently from integers. They may be represented
either in decimal form or in the exponent form.
E.g.: Valid decimal constants: - 18.5, .18, 5. , -25.67
Valid exponent constants: - 5.3E-5, -6.79E3, 34e-8
Here the number before „E‟ / „e‟ is called mantissa and
mantissa can be either real or integer, the number after „E‟/‟e‟ is called
exponent and exponent should be an integer.
2. Character constants
a. Single character: It is a letter or any symbol enclosed in single quotes.
Character constants can be used in assignment statements or in symbolic
constant definitions.
E.g.: ch=‟h‟ ;
#define PERIOD „.‟
b. String constant: A sequence of characters enclosed between double
quotes is called string constant.
E.g.: “C programming is fun”
“Enter two numbers”
3. Symbolic constants
It is a name that substitutes a numeric, character or a string constant.
E.g.: #define PI 3.1415 (Here we substitute PI for numeric constant)
#define PLACE “MYSORE”
#define FOUND 1
#define NUM 023 /* indicates octal number */
#define VALUE 0x20 /*0x indicates hexadecimal */

5|Page
4. Backslash constant
They are also called „escape sequences‟ and are used in the output statements. A
backslash constant is a combination of two characters in which the first character is
always the backslash (\) and the second character can be any one of a, b, f, n, r, t, v.
Backslash constant Meaning
„\a‟ System alarm
„\b‟ Back space
„\f‟ Form feed
„\n‟ New line
„\r‟ Carriage return
„\t‟ Horizontal tab
„\v‟ Vertical tab
„\”‟ Double quote
„\‟‟ Single quote
„\0‟ Null character
„\\‟ Back slash character

KEYWORDS (Reserved words): These are predefined words that have special meaning
and these meanings cannot be changed. They can be used only for the purpose for which they
have been defined. All keywords must be written in lowercase letters.

Example:

auto double int struct

break else long static

case enum register switch

char extern return typedef

const float short union

continue for signed unsigned

default goto sizeof void

do if while volatile

DATA TYPES:

Data type indicates the type of data a variable can have. Data type defines set of
values, which can be stored in the variable along with certain operations that may be
performed on those values.

6|Page
There are three classes of data types

 Primary (or fundamental) data types:- int, float, char, double


 Derived data types:- arrays, functions, structures, pointers
 User-defined data types:- typedef, enum
Primary Data types:

int: Means that the variable is an integer. An integer needs two bytes of space. Range of
values that can be stored in two bytes is -32768 to +32767. The operations that can be
performed on an integer value are addition, subtraction, multiplication, division and
remainder.

char: Means that the variable is a character. A character needs one byte of storage space. If
unsigned, the range of values is 0 to 255. The possible operations are input and output.

float: Means that the variable is a real number. Four bytes or 32 bits are allocated in memory,
this includes 7 significant digits after the decimal point. The range of values is 3.4E-38 to
3.4E+38.

double: Means that that variable is a double precision float type. Eight bytes or 64 bits of
storage space and range of values is 1.7E-308 to 1.7E+308.

Void: the void type has no values. This is usually used to specify the type of functions. The
type of a function is said to be void when it does not return any value to the calling function.

Size and Range of data types

Type Size (bits) Range


Char or signed char 8 (1 byte) -128 to 127
Unsigned char 8 (1 byte) 0 to 255
int or signed int 16 (2 bytes) -32,768 to 32,767
Unsigned int 16 (2 bytes) 0 to 65535
Short int or signed short int 8 (1 byte) -128 to 127
Unsigned short int 8 (1 byte) 0 to 255
Long int or signed long int 32 (4 bytes) -2,147,483,648 to
2,147,483,647
Unsigned long int 32 (4 bytes) 0 to 4,294,967,295
float 32 (4 bytes) 3.4E-38 to 3.4E+38
double 64 (8 bytes) 1.7E-308 to 1.7E+308
Long double 80 (10 bytes) 3.4E-4932 to 1.1E+4932

Declaration (definition) of variables:


Syntax: data_type variable list;
Variables must be separated by comma and declaration statement must end with a semicolon.
Example: int a, b, c; float sum, average;

7|Page
Assigning values to variables:
Method 1: values can be assigned to variables at the time of
declaration E.g.: int sum=0;
float val=3.14159;
char ans=‟y‟;
Method 2: values can be assigned using an assignment statement
E.g.: sum=0;
Val=3.14159;
Ans=‟y‟;
Example: C program to compute area and circumference of circle
#include<stdio.h>
#include<conio.h>
#define PI 3.1415
void main()
{
int r;
float a, c;
clrscr();

printf(“enter the radius of the circle\n”);


scanf(“%d”,&r);
a=PI*r*r;
c=2*PI*r;
printf(“area of the circle=%f\n”,a);
printf(“circumference=%f\n”,c);
getch();
}
Output:

8|Page
INPUT-OUTPUT STATEMENTS

C is a functional programming language. To perform input-output operations C


provides a library of functions. This library is called a standard input-output library. It is
denoted by stdio. The header file containing such library functions is called stdio.h
Some of the standard I-O functions in C are

1. scanf() 5. gets()
2. printf() 6. puts()
3. getchar() 7. getch()
4. putchar() 8. getche()

There are two types of input-output statements

1. Formatted I/O statements


2. Unformatted I/O statements

Input statements Output statements


Formatted scanf() printf()
Unformatted getchar() getche() getch() putchar() puts()
gets()

FORMATTED I/O STATEMENTS:

They allow the input read from the keyboard or the output displayed on VDU to be
formatted as per our requirements i.e., where this output will appear on screen, how many
spaces between two values, number of decimal places etc. Can be controlled using formatted
I/O statements.

FORMATTED INPUT:
scanf() statement is used to input the numeric, character and string type of data.

Syntax:- scanf(“control string”, address_list);

Example:- scanf(“%d%d”,&a,&b);
scanf(“%c%d%f”,&ch,&a,&r);

% indicates that a conversion specification.

9|Page
Control String Meaning
%c Read a single character
%d Read a decimal integer
%e or %f or %g Read a floating-point number
%h Read a short int
%I Read a decimal or hexadecimal or octal number
%o Read on octal number
%p Read a pointer
%s Read a string
%u Read an unsigned integer
%x Read a hexadecimal number

FORMATTED OUTPUT:
C provides the printf() function to display the data on the monitor.

Syntax: printf(“control string”, varlist);


E.g.: printf(“%d”,num);
printf(“ result=%f\n”,res);
printf(“ area=%6.2f, circumference=%6.2f\n “,ar,ci);

Format specifiers for integers:


printf(“%d”,6789);
6 7 8 9
printf(“%8d”,1239);
1 2 3 9
printf(“%-8d”,1239);
1 2 3 9
printf(“%08d”,1239);
0 0 0 0 1 2 3 9
Format specifiers for floating point numbers:
printf(“%f”,num);
1 2 3 4 . 6 7 8 0 0 0
printf(“%10.2f”,num);
1 2 3 4 . 6 8
printf(“%-10.2f”,num);
1 2 3 4 . 6 8
printf(“%10.2e”,num);
1 . 2 3 E + 0 3
Format specifiers for characters:
printf(“%c”,”A”);
A
printf(“%4c”,”A”);
A
printf(“%-4c”,”A”);
A

10 | P a g e
11 | P a g e
Example program 1:
#include<stdio.h>
#include<conio.h>
void main()
{
int val=63;
clrscr();
printf(“\n val is %d”,val);
printf(“\n val is %2d”,val);
printf(“\n val is %4d”,val);
printf(“\n val is %6d”,val);
printf(“\n val is %-6d”,val);
getch();
}
Output:

Example Program 2:
#include<stdio.h>
#include<conio.h>
void main()
{
int num=126;
clrscr();
printf(“ number is=%d\n”,num);
printf(“ octal equivalent of %d=%o\n”,num,num);
printf(“ hexadecimal equivalent of %d=%x\n”,num,num);
printf(“hexadecimal equivalent of %d=%X\n”,num,num);
getch();
}
Output:

UNFORMATTED INPUT:
These statements are primarily concerned with reading the character type data from
keyboard.
getchar() function : This function reads a single character from the standard input
device.
Syntax: ch_var=getchar();

12 | P a g e
gets() function: This function reads in everything you enter from the keyboard until the
ENTER key is pressed i.e., it reads a string of all printable ASCII characters.

Syntax: gets(string);

UNFORMATTED OUTPUT:
These statements are mainly concerned with displaying or printing the character type
data on the monitor.

putchar() function:- This function prints a single character on the screen.

Syntax: putchar(ch_var);

puts() function:- This function prints a string of characters on screen.

Syntax: puts(string);

Example 1:
#include<stdio.h>
#include<conio.h>
void main()
{
char letter;
clrscr();
letter=getchar();
putchar(letter);
getch();
}
Output:

Example 2:
#include<stdio.h>
#include<conio.h>
void main()
{
char mesg[20];
clrscr();
printf(“enter the message to be displayed\n”);
gets(mesg);
puts(mesg);
getch();
}

13 | P a g e
Output:

All data transfer operations in C are carried out using functions available in the
standard library. These functions are known as the standard I/O library. These operations can
be classified into two types.
i. Buffered Input/output
ii. Unbuffered Input/output
Buffered Input:

 Buffer is a temporary storage in the memory. The characters entered at the keyboard
are stored in the buffer until the ENTER key is pressed and then made available to the
program as a block of characters.
 Input-output operations take less time on a buffered system.
 If any mistake is done while entering the data before ENTER key is pressed, can be corrected.
E.g.: scanf() and getchar() are buffered input function.

Unbuffered Input:
Here, the character which is entered at the keyboard is made available to the program
immediately. It is useful in some interactive programs and to read and write binary data.

E.g.: getch() and getche() are unbuffered input function.

STANDARD MATHEMATICAL FUNCTIONS


Mathematical functions such as cos, sqrt, log, etc, are frequently used in analysis of
real life problems. Most of the C compilers support these basic math
functions[#include<math.h>].
Note : x and y should be declared as double.

Function Meaning
acos(x) Arc cosine of x
asin(x) Arc sine of x
atan(x) Arc tangent of x
atan2(x,y) Arc tangent of x/y
cos(x) Cosine of x
sin(x) Sine of x
tan(x) Tangent of x
cosh(x) Hyperbolic cosine of x
sinh(x) Hyperbolic sine of x
tanh(x) Hyperbolic tangent of x
ceil(x) X rounded up to the nearest integer
x
exp(x) E to the x power (e )
fabs() Absolute value of x

14 | P a g e
floor(x) X rounded down to the nearest integer
fmod() Remainder of x/y
log(x) Natural log of x, x>0
log 10(x) Base 10 log of x, x>0
y
pow(x,y) X to the power y (x )
sqrt(x) Square root of x, x>0

Example:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int x,y,p,z;
clrscr();
printf("enter x value\n");
scanf("%d",&x);
printf(“enter power\n”);
scanf(“%d”,&p)
y=sqrt(x);
z=pow(x,p);
printf("\n %d square root is=%d",x,y);
printf("\n %d power of %d=%d",x,p,z);
getch();
}
Output:

15 | P a g e

You might also like