C is Easy

C Programming made easy with just a few little steps

Wednesday, 1 February 2012

Intro to C

Beginning C programming 

Before starting programming in C, it is best to understand the components needed to make a program. First of all we need a compiler to create a C file. Now what is a compiler? A compiler is basically a computing program which turns your programming code into a language that the computer can understand i.e. binary language. Upon compiling we successfully create a file with .C extension and also creates an objective file with .obj extension. After compiling we have to link our source code. Here the question arises What is linking? Linking is the process of creating an executable file of our C source code so that we can see our program run on the user screen.It has a .exe extension.

Getting started with C

The C Programming Language was developed by Dennis Ritchie between 1969 and 1973.We will leave the history and theory part there as we are concerned with a more practical approach. The first thing that comes forward to discuss is a character set.

C Character Set- It is a set of valid characters that the computer can recognise. A character set represents any letter, digit, or any other sign.The C Programming Language has following chararcters

Letters A-Z,a-z
Digits 0-9
Special symbols + - * / ^ \ ( ) { } [ ] = != < > . ' " , : ; % & ? _ # and so on   
White Spaces  Blank Space( " ") Horizontal Tab ("\t") Vertical Tab ("\v") New Line("\n") Form feed("\f")

Keywords- Words that convey a special meaning to the language compiler. These are reserved for special purpose.

auto
break
case
char
const
continue
default
do
Double
else
enum
extern
float
for
goto
if 
int
long
register
return
short
signed
sizeof
static
struct
switch
typedef
union
unsigned
void
volatile
while

 

Identifiers- They are the user defined names for different parts of the program. Identifiers may contain letters (a-z), (A-Z) or digits(0-9).

Constants and Variables

A ‘constant’ is an entity that does not change, but a ‘variable’ as the name suggests may change.There are three different types of constants, integer, real and character. The integer constant consists of whole numbers and has a memory allocation of 2 bytes. There are three different types of integer constants 
1. Decimal -0 to 9
2. Octal- 0 to 7
3. Hexadecimal- 0 to 9 and A to F

The real or floating point constants are in two forms namely fractional form and exponential form. A real constant in fractional form 867.9 and in exponential form 3.2e4.

The character constants is a single alphabet, a single digit or a special symbol enclosed within inverted commas. Eg.'B'.

Variables are names given to locations in computer memory. 



Concept of Data Types

Data types are means to identify the type of data and associated operations of handling it. There are a lot of data types in C programming language. A C programmer has to use appropriate data type as per his requirements. The C Programming Language has a lot of data types. But the most commonly used are:-

1.Integer-int - They are whole numbers with a machine dependent range of values.
2.Character-char - May be a single character.
3. Floating Point- float - Real numbers with 6 digit precision.
4.Double precision floating point- double - same as float but with longer precision.
5.Void- void

The  declaration of a variable used in a C program is very important as it does two things

  • Tells the compiler the variables name
  • Specifies the data type of the variable
for eg:-

int sum;
float average;
char a;
etc.

A statement is a complete direction instructing the computer to carry out some task. For eg:-


x=2+3;

A C Expression is anything that evaluates to a numeric value.

percentage=(sum/total)*100;

Now, moving further we will now discuss about operators and expressions in the C Programming language. C offers different operators

Punctuators 

These are used in C for special reasons. I will explain there usage all along the C programming.

Standard I/O


getchar( ) - used to enter single characters.


putchar( ) - used to print single characters.


Syntax:-


c=getchar( );
putchar(c);


printf- It is the standard output function. 


Syntax:- printf("Arguments/Statements/Format specifier" , Function(if any));


scanf- It is the standard input function.


Syntax:- scanf("Arguments/Statements/Format specifier", Function(if any));



Note that for different inputs of different data types you should use different symbols.These are known as format specifiers and some of the common are:-

%d-integer
%f-float
%c-character
%s-string



gets- used to enter a string of characters.


puts - used to print a string of characters.  


For eg:-


char line[20];
gets(line);
puts(line);



Operators

They are tokens that trigger some computation when applied to variables and other objects in an expression.
&,*,+,-,++,--,!etc.



  1. The Assignment Operator - = Example:- x=y/variable=expression, here the expression is evaluated, and the resulting value is assigned to variable.
  2. The Mathematical Operators 
  • Unary Mathematical Operators
    • Negate- - Example: -x, negates the value 
    • Increment - ++ Example: ++x,x++, Increments the operand by one 
    • Decrement - -- Example: --x,x--, Decrements the operand by one Note:- That the basic difference between postfix(x--/x++) and prefix(--x/x--) is that it first uses the value and then changes it and the other changes the value and then uses it.
  • Binary Mathematical Operators 
    • Addition - + - Adds two operands Example: x+y
    • Subtraction - - Subtracts the second operand from the first operand Example: x-y
    • Multiplication - * Multiplies two operands Example: x*y
    • Division - / Divides the first operand by the second operand x/y
    • Modulus - % Gives the remainder x%y
   3. Relational Operators
  •  Equal to - == 
  •  Less Than - <
  •  Greater Than >
  •  Less than Equal to - <=
  •  Greater than Equal to - >=
  •  Not equal to - !=
   4. Logical Operators
  •  Logical AND - && Example: exp1 && exp2 , True(1), if both are true otherwise False(0). 
  •  Logical OR - || Example exp1 || exp2, False(0), if both are false otherwise True(1).
  •  Logical NOT - ! Example !exp1, False(0), if exp1 is True(1), True(1), if exp1 is False(0).
   5. The Conditional Operator - exp1 ?exp2:exp3;
    If exp1 evaluates to True, the entire expression evaluates to the value of exp2 otherwise the entire expression evaluates to the value of exp3.

   6. The Comma Operator - ,
It combines several expressions into a single statement and returns the value of the rightmost expression.
Example: a=0,b=3,a=a+b,b=0; sets a to3, b to 0 and returns 0.


Using C

There are many softwares that are available for programming in C. But I personally prefer Turbo C.First thing is to open the Turbo C.There are two ways one is manually which all can do but I am telling the DOS method.

Step 1:- Click on  the Start Button

Step 2:- type cmd on the search programs and files 

Step 3:- Change the directory to C:\TC\BIN\tc

step4:- Start your work



Our First C Program
Now as the basics are complete lets try our first program.

#include<stdio.h>               
#include<conio.h>
int main()
{            clrscr();
              printf("Welcome to C Programming");
              return 0;
}
Explanation:-
  • Statements that begin with # are directives for the preprocessor. That means that these statements are processed before compilation takes place. 
  • Generally a program in C includes a header file stdio.h for standard input/output facilities.
  • The declaration of printf(for output) and scanf(for input) are defined in stdio.h. If stdio.h is not included in a program the every reference to printf and scanf will produce a type error.
  • Every C program must contain a function named main( ). The program execution begins at main( ).
  • Every executable Statement must be terminated by a semicolon (;).


The output of the above program will be 

Welcome to C Programming

Lets try another program

1.1 Program that input's a students marks in three subjects( out of 100) and prints the percentage marks?


#include<stdio.h>
#include<conio.h>
int main()
{       float sub1,sub2,sub3,marks,perc;
clrscr();
printf("Enter marks obtained in three subjects\n");
scanf("%f %f %f", &sub1,&sub2,&sub3);
marks=sub1+sub2+sub3;
perc=(marks/300)*100;
printf("The percentage marks is : %f %",perc);
return 0;
}


Output




1.2The Temperature Conversion Program

#include<stdio.h>
#include<conio.h>
void main ()
{       int ch;
float temp_c, temp_f;
clrscr();
printf("Temperature Conversion Program\n");
printf("1.Celsius to Fahrenheit\n2.Fahrenheit to Celsius\nEnter your choice");
scanf("%d",&ch);
if(ch==1)
{ printf ("Enter the value of Temperature in Celsius: ");
scanf ("%f", &temp_c);
temp_f = (1.8 * temp_c) + 32;
printf ("The value of Temperature in Fahrenheit is: %f", temp_f);
}
else if(ch==2)
{       printf ("Enter the value of Temperature in Fahrenheit: ");
scanf ("%f", &temp_f);
temp_c = (temp_f-32)*0.55;
printf ("The value of Temperature in Celsius is: %f", temp_c);
}
else
printf("Wrong choice!!!!Abort!!!!");
getch();
}


Some Programs I recommend to practice before moving any further.......
  1. WAP to read values of w,x,y and z and display the value of P, where P=(w+x)/(y-z)
  2. WAP that accepts the radius of the circle and prints its area.
  3. WAP that accepts marks in five subjects and outputs their average marks.
  4. WAP that accepts two integers and prints their sum. 
If you encounter any problem in any of the above programs you can ask as and when required or can mail me your problems at hannan_khan93@yahoo.com.














  




No comments:

Post a Comment