Vous êtes sur la page 1sur 9

http://www.technocratsofit.blogspot.

com

C
Notes
http://www.technocratsofit.blogspot.com

In early 1970’s Dennis Ritchie at the AT&T’s Bell Laboratories USA developed
‘C’. It was the best result of development process which started with older languages,
‘BCPL’ (Basic Combined Programming Language) developed by Martin Richards and
‘B’ developed by Ken Thompson. ‘BCPL’ and ‘B’ both turned out to be very specific.
Ritchie inherited the features of ‘B’ and ‘BCPL’, added some of his and developed ‘C’.

C stands in Middle Level Language. Since it was designed to have a good


programming efficiency and machine efficiency.

STRUCTURE OF A ‘C’ PROGRAM.


A Typical ‘C’ program can be divided into the following sections.

DOCUMENTATION SECTION
LINK SECTION
DEFINATION SECTION
GLOBAL DECLARATION SECTION
main() FUNCTION SECTION
{
DECLARATION PART
EXECUTION PART
}
SUB PROGRAM SECTION
FUNCTION 1
FUNCTION 2
.
.
.
FUNCTION N

1. Documentation Section – This section is used for comments. Comments begin


with/* and end with */. Comments are a must in every program. It helps in better
understanding of the program. Comments can appear any where within the
program also.

2. Link Section – In C there are some special function libraries that are required
while executing program because many function of the library can be used for
various tasks. A function written by a programmer can also be placed in the
library. A header file is the one in which declaration of standard functions are
made.

3. Definition Section – In this section of C program user defines the value of a


constant. This value can be used within the programs.
4. Global Declaration Section – In C variables can be defined globally. A variable
with a global scope is accessible everywhere inside the program.

5. main() Function Section- Every C program must have a special function named
main(). The program execution begins from here. The statements with function
are enclosed within a pair of braces {}. It has 2 parts:
• Declaration Part –
to declare all the variables used in executable part
• Executable Part –
is the actual program where processing is done.

6. Sub Program Section – Function is a self contained block of statements that


performs a specific task. This is a section for user defined functions that are called
by the main() function.

Character Set
A character denotes any alphabet, digit or a special symbol used to represent
information. Valid character sets allowed in C are:
Alphabets A – Z and a – z
Digits 0–9
Special Symbols ~ ` ! @ # $ % ^ & * ( ) _ - + = | | { } [ ] : ; ” ’ < > , . ? /

Identifiers and Keywords


User defined objects like variables, function, labels are called identifiers. The first
character of every identifier must be an alphabet or an underscore (_). Identifiers in C
are case sensitive. Identifiers cannot be the same as a C keyword.

Keywords are the words whose meaning has already been explained to the C
compiler. Keywords cannot be used as a variable name because if we do so we are
trying to assign a new meaning to the keyword, which is not allowed. Keywords are
also called ‘reserved words’. There are 32 keywords available in C:
auto break case char const continue default do
double else enum extern float far for goto
if int long near register return short signed
static struct switch typedef union unsigned void while

http://www.technocratsofit.blogspot.com
http://www.technocratsofit.blogspot.com

DATA INPUT AND OUTPUT


printf()
this function is used to print or display data on the console or screen in a formatted
form. A control string is used to specify the type of data to be printed.
E.g.: printf(“This is an example of printf)
#include<stdio.h>
void main()
{
int a, b;
a=20;
b=10;
printf(“Value of a = %d\n”,a);
printf(“Value of b = %d\n”,b);
}
Output:-
Value of a = 20
Value of b = 10

scanf()
This function is used to read a formatted data from a standard input device normally
from the keyboard. The first argument is the control string containing formats and the
other arguments are addresses. E.g.: (“%d”,&a);
Program:
#include<stdio.h>
void main()
{
int a;
printf(“Enter a number: “);
scanf(“%d”,&a);
printf(“\nNumber entered was %d\n”,a);
}
Output:-
Enter a number: 13
Number entered was 13

http://www.technocratsofit.blogspot.com
http://www.technocratsofit.blogspot.com

getch()
This function allows you to accept a character and store the character accepted to the
variable if specified but does not display the character on the screen. In C, there is no
command to wait. This function is used as a wait command. E.g.: ch=getch();
Program:
#include<stdio.h>
void main()
{
char ch;
printf(“Enter a character: “);
ch=getch();
printf(“\nCharacter entered was 5c\n”, ch);
getch();
}
Output:-
Enter a character
Character entered was n

getche()
This function allows you to accept a character and stores the character accepted to the
variable if specified. It also displays the character on the screen. E.g.: ch=getche();
Program:-
#include<stdio.h>
void main()
{
char ch();
printf(“Enter a character ”);
ch=getche();
printf(“\nCharacter entered was %c\n”,ch);
getch();
}
Output:-
Enter a character n
Character entered was n

putch()
This function allows you to display the value of a variable on the screen. E.g.:
putch(ch);
Program:-
#include<stdio.h>
void main()
{
Char ch;
printf(“Enter a character: ”);
http://www.technocratsofit.blogspot.com
ch=getch();
putch(ch);
getch();
}
Output:-
Enter a Character: n

clrscr()
This function is used to clear the screen of previous program. E.g.: clrscr();
Program:-
#include<stdio.h>
void main()
{
char ch();
clrscr();
printf(“Enter a character: ”);
ch=getche();
clrscr();
printf(“\nCharacter entered was %c\n”,ch);
getch();
}
Output:-
Enter a character: n
Character enter was n

getchar()
getchar means get character. This function allows you to accept a character from the user.
It displays the character entered on the screen. E.g.: c=getchar();
Program:-
#include<stdio.h>
void main()
{
char ch();
printf(“Enter a character: ”);
ch=getchar();

printf(“\nCharacter entered was %c\n”,ch);


}
Output:-
Enter a character: n
Character entered was n

puts()
Puts means put string. This function allows you to print a string on the screen. E.g.:
puts(nm);
http://www.technocratsofit.blogspot.com
Program:-
#include<stdio.h>
void main()
{
char ch();
clrscr();
printf(“Enter a Name: ”);
gets(nm);
printf(“Name: ”);
puts(nm);
getch();
}
Output:-
Enter a Name: nirav
Name: nirav

putchar()
This function allows you to display the value of a variable on the screen. E.g.:
putchar(ch);
Program:-
#include<stdio.h>
void main()
{
char ch;
printf(“Enter a character: ”);
ch=getchar();
putchar(ch);
getch();
}
Output:-
Enter a character: n
Character entered was n

strcpy()
strcpy means string copy. This function allows you to copy a string. E.g.:
strcpy(nm1,nm);
Program:-
#include<stdio.h>
void main()
{
char nm[20], nm1[20];
clrscr();
printf(“Enter name: ”);
gets(nm);
strcpy(nm1,nm);
printf(“Name entered was %s”,nm1);
http://www.technocratsofit.blogspot.com
getch();
}

Output:-
Enter Name: nirav
Name entered was nirav

strcat()
strcat means string concatenation. This function is used to combine two strings. E.g.:
strcat(nm1,nm);
Program:-
#include<stdio.h>
void main()
{
char nm[20], nm1[20];
clrscr();
printf(“Enter Name: ”);
gets(nm);
printf(“Enter Surname: ”);
gets(nm1);
strcat(nm1,nm);
printf(“Full name entered was %s”, nm1);
getch();
}
Output:-
Enter Name: nirav
Enter Surname: rana
Full name entered was rananirav

strcmp()
strcmp means string comparison. It is used to compare two strings. It is case sensitive. It
returns a zero if the comparison is true. E.g.: strcmp(nm, “nirav”);
Program:-
#include<stdio.h>
#include<string.h>
void main()
{
char nm[10];
clrscr();
printf(“Enter name: ”);
gets(nm);
if (strcmp(nm, “nirav”)==0)
{
printf(“Correct Name Entered”);
}
else
http://www.technocratsofit.blogspot.com
{
printf(“Wrong Name Entered”);

}
getch();
}
Output:-
Enter name: nirav
Correct Name Entered

strcmpi()
strcmpi also means string comparison. But the only difference is that it is
case insensitive. Even it compares two strings. If the comparison is true it returns a value
zero. E.g.: strcmpi(nm, “nirav”)
Program:-
#include<stdio.h>
#include<string.h>
void main()
{
char nm[10];
clrscr();
printf(“Enter name: ”);
gets(nm);
if(strcmpi(nm, “nirav”)==0)
{
printf(“Correct Name Entered”);
}
else
{
printf(“Wrong Name Entered”);

}
getch();
}
Output:-
Enter name: NiRaV
Correct Name Entered

Vous aimerez peut-être aussi