Académique Documents
Professionnel Documents
Culture Documents
WHAT IS C C is a programming language developed at AT & Ts Bell Laboratories of USA in 1972. It was designed and written by a man named Dennis Ritchie. In the late seventies C begane to replace the more familiar languages of that time like PL/I, ALGOL etc. No one pushed C. It was not made the official Bell Lab language. Thus, without any advertisement Cs reputation spread and its pool of users grew. Ritchie seems to have been rather surprised that so many programmers preferred C to older languasges like FORTRAN or PL/I, or the newer one ones like Pascal and APL. But, thats what happened. Possibly why C seems so popular is because it is reliable, simple and easy to use.
WHERE C STANDS All programming languages divided into two categories. C when compared with other programming languages.
These
languages have been designed to give a programming efficiency i.e. faster program developed. Example : FORTRAN, BASIC, PASCAL etc.
These
Due to these features C not placed in these categories. That is why it is often called Middle Level Language. Since it is designed to
-1-
have both a relatively good programming efficiency (as an problem oriented language) and relatively good machine efficiency.
DATA TYPES IN C C has several different types of data, each of which can be represented differently with in computers memory but memory may the different for one C compiler to other. The basic types of data are as follows:
-2-
VARIABLES A variable is an identifier that is used to represent some specified type of information with in a designated portion of the program or in other words variable is an identifier that is used to represent a single data item i.e. a numerical quantity or a character constant. The contents of a variable can change for e.g. in equation 3x+y=20 since 3 and 20 can not change, they are constants whereas x and y change or vary they are called variable.
CONSTANTS A constant refers to quantity that does not change its value. There are four basic types of constants in C. They are : a. Integer constant b. Real constant c. Character constant d. String constant C KEYWORDS Keywords are the words whose meaning has already been explained to the C compiler (or in a broad sense to the computer). The keywords can not be used as variable names because if we do so we are trying to assign a new meaning to the keyword, which is not allowed by the computer. So it would be good to not mix up the variable names and keywords. Keywords are just like Reserve Words. There are only 32 keywords available in C. e.g. auto, double, float, for, if, register, static, void, break, char, continue, do, else, extern, int, return, struct, while.
-3-
THE C CHARACTER SET C uses the uppercase letters A to Z, the lower case letters a to z, the digits 0 to 9 and certain special characters as building blocks to form basic program elements (e.g. constants, variables, operators, expressions). The special characters are listed below : ! % & * ) _ + ~ ] \ ; ` } . < / # ^ ( = [ | : { , > ?
[blank]
FORMAT SPECIFIERS Some of the different format specifiers that can be used with the printf() statement are listed below : Code %c %d, %I %f %s %u %x %p %% Format character signed decimal integers decimal floating point string of characters unsigned decimal integers unsigned hexadecimal displays a pointer prints a % sign
-4-
ESCAPE SEQUENCES In addition to the format specifiers, escape sequences can also be used with the printf(). These are specified in the first argument of the printf() and are used mainly for screen formatting of the output. They are always preceded with a backslash(\), and since this backslash is considered an escape character, these sequences are called escape sequences. They cause an escape from the normal interpretation of a string, so that the next character (after backslash) is recognized as having a special meaning. An example will make this more clear. The following table lists the different codes that can be used with the printf() function. Code meaning \b \r \\ Code \t meaning Code meaning new line \ alert \ Code meaning single quote \0 backspace \f carriage return null backslash \v vertical tab \a form feed \n
horizontal tab
double quote
OPERATORS Relational Operators Relational Operators are symbols that are used to test the relationship between two variables, or between a variable and a constant. The test for quality for example , is made by means of two adjacent equal signs with no space separating them. Six relational operators in C are :== equal to
-5-
greater than less than greater than or equal to less than or equal to not equal to.
-6-
WHAT IS A FUNCTION A function is a self contained block of statements that performs a coherent task of some kind. Every C program can be thought of as a collection of these functions. As we noted earlier, using a function is something like hiring a person to do a specific job for you. Sometimes the interaction with this person is very simple; sometimes it s compex. WHY USE FUNCTIONS
a.) Writing functions avoids rewriting the same code over and over. Suppose you
have a section of code in your program that calculates area of a triangle. If, later in the program, you want to calculate the area of a different triangle, you wont like it if you are required to write the same instructions all over again. Instead, you would prefer to jump a section of code that calculate area and then jump back to the place from where you left off. This section of code is nothing but a function.
b.) Using functions it becomes easier to write programs and keep track of what they
are doing. If the operation of a program can be divided into separate activities, and each activity placed in a different function, then each could be written and checked more or less independently. Separating the code into modular functions also makes the program easier to design and understand.
-7-
-8-
Console I/O Functions Console I/O functions that can be further classified into two categories : formatted and unformatted console I/O functions. The basic difference between them is that the formatted functions allow the input read from the keyboard or the output displayed on the VDU to be formatted as per our requirements.
Formatted functions
Unformatted functions
Type Output
Input
Type Output
Input
char printf( )
scanf( )
getch( ) getche( )
scanf( ) scanf( )
Int float
-9-
C++
language:
C++
is
a statically
paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell Labs as an enhancement to the C language. Originally named C with Classes, the language was later renamed C++ in 1983. C++ is one of the most popular programming languageswith application domains including systems software (such as Microsoft Windows), application software, device drivers, embedded software, high-performance server and client applications, and entertainment software such as video games. C++ is also used for hardware design, where the design is initially described in C++, then analyzed, architecturally constrained, and scheduled to create a register-transfer level hardware description language via high-level enhancements functions, operator and exception to C, synthesis. first The language began as adding classes, other features. After then virtual years of
overloading, multiple
inheritance, templates,
handling among
development, the C++ programming language standard was ratified in 1998 asISO/IEC 14882:1998.
History of C++:Bjarne Stroustrup began work on "C with Classes" in 1979. The idea of creating a new language originated from Stroustrup's experience in programming for his Ph.D. thesis. Stroustrup found that Simula had features that were very helpful for large software development, but the language was too slow for practical use, while BCPL was fast but too low-level to be suitable for large software development. When Stroustrup started working
- 10 -
in AT&T
Bell
Labs,
he
had
the
problem
of
analyzing
the UNIX kernel with respect to distributed computing. Remembering his Ph.D. experience, Stroustrup set out to enhance the C language with Simula-like features. C was chosen because it was generalpurpose, fast, portable and widely used. Besides C and Simula, some other languages that inspired him were ALGOL 68, Ada, CLUand ML. At first, the class, derived class, strong type checking, inlining, and default argument features were added to C via Stroustrup's C++ to C compiler, Cfront. The first commercial implementation of C++ was released on October 14, 1985. In 1983, the name of the language was changed from C with Classes to C++ (++ being the increment operator in C). New features were added including virtual functions, function name and operator overloading, references, constants, usercontrolled free-store memory control, improved type checking, and BCPL style single-line comments with two forward slashes (//). In 1985, the first edition of The C++ Programming Language was released, providing an important reference to the language, since there was not yet an official standard. Release 2.0 of C++ came in 1989 and the updated second edition of The C++ Programming Language was released in 1991. New features included multiple inheritance, abstract classes, static member functions, const member functions, and protected members. In 1990, As the C++ language evolved, the standard library evolved with it. The first addition to the C++ standard library was the stream I/O library which provided facilities to replace the traditional C functions such as printf and scanf. Later, among the most significant additions to the standard library, was large amounts of the Standard Template Library. C++ is sometimes called a hybrid language. C++ continues to be used and is
- 11 -
Philosophy: C++:]
In The
Design
and
Evolution
of
C++
Bjarne
C++ is designed to be a statically typed, general-purpose language that is as efficient and portable as C. C++ is designed to directly and comprehensively support multiple programming styles (procedural programming, data abstraction, object-oriented programming, and generic programming).
C++ is designed to give the programmer choice, even if this makes it possible for the programmer to choose incorrectly. C++ is designed to be as compatible with C as possible, therefore providing a smooth transition from C. C++ avoids features that are platform specific or not general purpose. C++ does not incur overhead for features that are not used (the "zero-overhead principle"). C++ is designed to function without a sophisticated programming environment.
Criticism of c++ : Due to its large feature set and oft-perceived "strict" syntax, the language is sometimes criticized as being overly complicated and thus difficult to fully master. Because it includes most of the C programming language as a subset, C++ also inherits many of the criticisms leveled at C. Other criticisms stem from what is missing from C++, such as a lack of "native" multithreading facilitiesa feature present in some other languages, including Ada, C#, and Java. The upcoming C++0x standard addresses this issue by adding direct support for concurrency to the language, but at present this is only possible by using operating system calls or third party libraries.
- 12 -
Richard Stallman criticizes C++ for having ambiguous grammar and "gratuitous, trivial, incompatibilities with C (...) that are of no great benefit" .Linus Torvalds has said, "C++ is a horrible language. It's made more horrible by the fact that a lot of substandard programmers use it".
- 13 -
C/C++ PROGRAMS
1. Wap to take a 3 digit number from user .Add one to every digit and print the new number.
INPUT: #include<iostream.h> #include<conio.h> void main() { clrscr(); int onum=156; int nnum; int p,q,r; p=onum%10; onum/=10; q=onum%10; onum/=10; r=onum%10; nnum=(r+1)*100+(q+1)*10+(p+1); cout<<"nnum is"<<nnum; getch(); } OUTPUT: 267
- 14 -
2.Wap to take a number from user and to swap them using third variable.
INPUT: #include<iostream.h> #include<conio.h> void main() { clrscr(); int a,b,c; cout<<"enter a,b"; cin>>a>>b; c=a; a=b; b=c; cout<<"a="<<a<<endl; cout<<"b="<<b; getch(); } OUTPUT: enter a,b 17 15 a=15 b=17
- 15 -
3. Wap to take a number from user and to swap them without using third variable.
INPUT: #include<iostream.h> #include<conio.h> void main() { clrscr(); int a,b; cout<<"enter a,b"; cin>>a>>b; a=a+b; b=a-b; a=a-b; cout<<"a="<<a<<endl; cout<<"b="<<b; getch(); } OUTPUT: enter a,b 17 15 a=15 b=17
- 16 -
- 17 -
- 18 -
6.Wap to check whether the number is divisible by 7 or not ,if yes then further check whether it is divisible by 4 or not.
INPUT: #include<iostream.h> #include<conio.h> void main() { clrscr(); int n; cout<<"enter number"; cin>>n; if(n%7==0) { if(n%4==0) cout<<"divisible by both"; else cout<<"divisible by 7 but not by 4"; } getch(); } OUTPUT: enter number 364 divisible by both
- 19 -
- 20 -
INPUT: #include<iostream.h> #include<conio.h> void main() { clrscr(); int n,i,fact=1; cout<<"enter number"; cin>>n; for(i=n;i>=1;i--) { fact*=i; } cout<<"factorial is"<<fact; getch(); } OUTPUT: enter number 5 factorial is 120
- 21 -
INPUT: #include<iostream.h> #include<conio.h> void main() { clrscr(); int i,j; for(i=0;i<=10;i+=2) { cout<<"\n"; for(j=1;j<=i;j+=2) { cout<<j; } getch(); } OUTPUT: 1 1 1 1 1 3 3 5 3 5 7 3 5 7 9
- 22 -
10. Wap to check whether a number is even or odd using ternary operator .
INPUT: #include<iostream.h> #include<conio.h> void main() { clrscr(); int a; cout<<"enter a"; cin>>a; { a%2==0?cout<<"even":cout<<"odd"; { getch(); }
- 23 -
INPUT: #include<iostream.h> #include<conio.h> void main() { clrscr(); int a; cout<<"enter a; cin>>a; cout<<(a<<1); getch(); }
OUTPUT: enter a 14 28
- 24 -
INPUT: #include<iostream.h> #include<conio.h> void main() { clrscr(); int n,sum=0,r; cout<<"\n enter no."; cin>>n; for(;num!=0;) { r=n%10; n/=10; sum+=r; } cout<<sum; getch(); }
- 25 -
INPUT: #include<iostream.h> #include<conio.h> void main() { clrscr(); int n,sum=0,i=1; cout<<"enter no."; cin>>n; while(i<n) { if(n%i==0) sum+=i; i++; } if(sum==n) cout<<"perfect"; else cout<<"not perfect"; getch(); }
- 26 -
INPUT: #include<iostream.h> #include<conio.h> void main() { clrscr(); int n,flag=0; cout<<"\n enter n"; cin>>n; for(int i=2;i<n;i++) { if(n%i==0) { flag=1; break; } } if(flag==0) cout<<"prime"; else cout<<"not prime"; getch(); }
- 27 -
15. Wap to create a function which accepts integer parameter and return the no. of digits in that number.
INPUT: #include<iostream.h> #include<conio.h> Int count(int); void main() { clrscr(); int n; cout<<enter n; cin>>n; int rec=count(n); count<<rec; } int count(int a) { int c=0; while(a!=0) { c++; a/=10; } return c; } OUTPUT: enter n 123456 6
- 28 -
- 29 -
INPUT: #include<iostream.h> #include<conio.h> void main() { clrscr(); int n,r,arm=0,org; cout<<enter num \n; cin>>n; org=n; while (n>0) { r=n%10; arm+=r*r*r; n/=10; } if(arm==org) { cout<<armstrome \n; } else { cout<<not armstrome; } getch(); }
- 30 -
INPUT: #include<iostream.h> #include<conio.h> void main() { clrscr(); int I,j,k=1; for(i=1;i<=4;i+=1) { for(j=1;j<=I;j+=1) { cout<<k++; } cout<<\n; } getch(); } OUTPUT: 1 2 4 7 3 5 8
6 9 10
- 31 -
19. Wap to take first 10 even numbers in array and print them.
INPUT: #include<iostream.h> #include<conio.h> void main() { clrscr(); int a[10],j=0; for(int i=1;i<=20;i++) { if(i%2==0) { a[j]=I; j++; } } for(i=0;i<=10;i++) { cout<<a[i]<<endl; } getch(); }
OUTPUT: 2 4 6 8 10 12 14 16 18 20
- 32 -
INPUT: #include<iostream.h> #include<conio.h> void main() { clrscr(); int i,j; for(i=5;i>=1;i--) { for(j=1;j<=I;j++) { cout<<j; } cout<<endl; } getch(); } OUTPUT: 1 1 1 1 1 2 2 2 2 3 3 3 4 4 5
- 33 -
INPUT: #include<iostream.h> #include<conio.h> void main() { clrscr(); int i,j,k,sum; for(i=5,sum=0;i>=1;i--,sum++) { for(j=0;j<sum;j++) cout<< ; for(int k=I;k>=1;k--) cout<<k; cout<<endl; } getch(); } OUTPUT: 5 4 4 3 3 3 2 2 2 2 1 1 1 1 1
- 34 -
INPUT: #include<iostream.h> #include<conio.h> void main() { clrscr(); int i,j,k,sum; for(i=9,sum=0;i>=1;i-=2,sum++) { for(int k=0;k>sum;k++) cout<< ; for(j=I;j>=1;j-=2) { cout<<j; } for(int l=3;l<=I;l+=2) { cout<<l; } cout<<endl; } getch(); } OUTPUT: 9 7 5 3 1 3 5 7 7 5 3 1 3 5 7 5 3 1 3 5 3 1 3 1 9
- 35 -
23. Wap to create a function called reverse case which reverse the case of a character passed to it as an argument.
INPUT: #include<iostream.h> #include<conio.h> char reversecase(char); void main() { clrscr(); char ch; cout<<enter ch; cin>>ch; char chx=reversecase(ch); cout<<after reversing<<chx; getch(); } char reversecase(char ch) { if(ch>=65&&ch<=95) ch+=32; else if(ch>=97&&ch<=122) ch-=32; } OUTPUT: enter ch r R
- 36 -
24. Wap to initialize an array of size 10 with first 10 prime number starting from 3 using for loop
INPUT: #include<iostream.h> #include<conio.h> void main() { clrscr(); int a[10],i.j,count=0; for(i=3;count<10;i++) { int flag=0; for(j=2;j<I;j++) { if(i%j==0) { flag=1; break; } } if(flag==0) { a[count]=i; count ++; } } for(i=0;i<10;i++) cout<<a[i]<<endl; getch(); } OUTPUT: 3 5 7 11 13 17 19 23 29 31
- 37 -
- 38 -
26.Wap to take 5 elements in an array and print the sum of elements of in the last index
INPUT: #include<iostream.h> #include<conio.h> void main() { clrscr(); int s=0; int a1[5]={2,,5,8,9,10}; for(int i=0;i<5;i++) { cin>>a1[i]; } for(i=0;i<5;i++) { s+=a1[i]; } a1[4]=s; for(i=0;i<5;i++) cout<<a1[i]; getch(); } OUTPUT: 2 5
34
- 39 -
INPUT: #include<iostream.h> #include<conio.h> void main() { clrscr(); char ch[20]; cout<<\n enter string:; cin>>ch; cout<<\n length of string: <<strlen(ch); getch(); }
- 40 -
28. Wap to count the no. of spaces in string by using c style character string.
INPUT: #include<iostream.h> #include<conio.h> #include<string.h> #include<stdio.h> void main() { clrscr(); int count=0; char ch[20]; gets(ch); for(int i=0;i<strlen(ch);i++) { if(ch[i]== ) count++; } cout<<count; getch(); } OUTPUT: abcd efgh 1
- 41 -
29. Wap to reverse the case entered by the user using c style character string.
INPUT: #include<iostream.h> #include<conio.h> #include<string.h> #include<stdio.h> void main() { clrscr(); char ch[20]; cout<<enter string:; gets(ch); for(int i=0;i<strlen;i++) { if(ch[i]>=65&&ch[i]<=95) ch[i]+=32; else if(ch[i]>=97&&ch[i]<=122) ch[i]-=32; } cout<<ch; getch(); } OUTPUT: enter string :Qwerty Asdfg qWERTY aSDFG
- 42 -
30. Using c style character string store a password in an character array and display the ***** while user is typing a password & then display the password entered by user
INPUT: #include<iostream.h> #include<conio.h> void main() { clrscr(); int i=0; char ch[10]; cout<<enter password; while(ch[i]!=\r) { i++; ch[i]=getch(); cout<<*; } cout<<endl<<ch; getch(); }
#include <iostream.h> #include <conio.h> classmyclass { public: int a; int function() { cout<</n m a class function; } }; Void main() { Cirscr(); myclassobj; Obj.function(); getch(); } Output: m a class function
- 44 -
- 45 -