Programe in C
Programe in C
" Swapping"
" Find Negative and positive value"
#include <stdio.h>
#include <stdio.h> int main()
int main() {
{ int a,b,c;
int b; printf("Enter the value of A:\n");
printf("Enter a value:"); scanf("%d",&a);
scanf("%d",&b); printf("Enter the value of B:\n");
if (b<0) scanf("%d",&b);
printf("The value is negative\n"); printf("Value of A before swapping:%d\n",a);
else if(b==0) printf("Value of B before swapping:%d\n",b);
printf("The value is Zero\n"); c=a;
else a=b;
printf("The value is positive\n"); b=c;
printf("Value of A after swapping:%d\n",a);
return 0; printf("Value of B after swapping:%d\n",b);
} return 0;
}
Output: Enter a value: -55 Output: Enter the value of A:
The value is negative 6
Enter the value of B:
7
Value of A before swapping:6
Value of B before swapping:7
Value of A after swapping:7
Value of B after swapping:6
What is C
C is a high-level and general purpose programming language that is ideal for developing firmware or portable
applications. Originally intended for writing system software, C was developed at AT & Ts Bell Laboratories of
USA in 1972 by Dennis Ritchie.
C is what is called a compiled language. This means that once anybody write a C program, he must run it
through a C compiler to turn his program into an executable that the computer can run (execute).
Steps of learning c
Following rules must be observed while constructing real constants expressed in fractional form:
1. A real constant must have at least one digit.
2. It must have a decimal point.
3. It could be either positive or negative,
4. Default sign is positive.
5. No commas or blanks are allowed within real constant.
Ex.: +325.34
426.0
-32.76
-48.5792
Rules for constructing Character Constants:
1. A character constant is a single alphabet, a single digit or a single special symbol enclosed within single
inverted commas. Both the inverted commas should point to the left. For example, A is a valid character
constant whereas A is not.
2. The maximum length of a character constant can be 1 character.
Ex: A
I
5
=
Variable
An entity that may vary during program
execution is called a variable.
EX: si_int
m_hra
pop_e_89
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 cannot 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. The keywords are also called Reserved words.
There are several such IDEs available in the market targeted towards different operating systems. For example,
Turbo C, Turbo C++ and Microsoft C are some of the popular compilers that work under MS-DOS; Visual C++ and
Borland C++ are the compilers that work under Windows, whereas gcc compiler works under Linux. Note that Turbo
C++, Microsoft C++ and Borland C++ software also contain a C compiler bundled with them. If you are a beginner you
would be better off using a simple compiler like Turbo C or Turbo C++. Once you have mastered the language
elements you can then switch over to more sophisticated compilers like Visual C++ under Windows or gcc under
Linux. Most of the programs in this book would work with all the compilers. Wherever there is a deviation I would
point it out that time.
Assuming that you are using a Turbo C or Turbo C++ compiler here are the steps that you need to follow to compile
and execute your first C program
1. Start the compiler at C> prompt. The compiler (TC.EXE is usually present in C:\TC\BIN directory).
2. Select New from the File menu.
3. Type the program.
4. Save the program using F2 under a proper name (say Program1.c).
5. Use Ctrl + F9 to compile and execute the program.
6. Use Alt + F5 to view the output.
Compiler:
A compiler is a special program that processes statements written in a particular programming language
and turns them into machine language or "code" that a computer's processor uses.
Difference between Compiler and Interpreter
No Compiler Interpreter
Program need not be compiled every Every time higher level program is
5
time converted into lower level program
Errors are displayed after entire Errors are displayed for every
6
program is checked instruction interpreted (if any)
Decision Making in C
a. The if statement
b. The else statement
c. The else-if statement
d. The conditional operators