Principles of C language
1. A C program consists of a set of functions.
2. A function in C is a code with the syntax:
type name(type var)
{
code.....
return value;
}
3. A function must be defined before it is used.
4. A function must return a value whose type must be declared (one
of int, float, double, char). The last line of a function must be a return xx; statement
where xx is a value to be returned upon exit.
5. A function must take arguments and must have a placeholder () even if there is no
argument.
6. The content of a function must be enclosed by "{" and "}".
7. A special function, int main(), is the one which is executed first (it is recommended
that this function returns an integer value of 0).
8. All the variables used within a function must be declared.
9. It is important to distinguish different types of numbers (examples shown).
Skeleton C programs
int main()
{
return 0;
}
#include <stdio.h>
int main()
{
printf("Hello world\n");
return 0;
}
• link (http://en.wikipedia.org/wiki/C_standard_library) to the list of
functions defined in the ANSI standard C library.
• The first line, #include, is called a preprocessor directive, not part of the C program.
Another example
#include <stdio.h>
#include <math.h>
int main()
{
float x, y;
x = 6.28;
y=sin(x);
printf("Sine of %f is %f.\n", x, y);
return 0;
}
$ gcc -lm MyProgram.c; a.out
C variables
Type of variables
You must use the proper format in the printf and scanf functions.
Type Content Format Range Example
int integers %d −2147483647 ∼ +2147483647 10
float floating numbers %f ±2.9387e−39 ∼ ±1.7014e+38 3.14
−63 +63
double double precision floating %lf 2 ∼2 3.14159265358979
char characters %c ASCII code 'a'
/*Print a character*/
#include <stdio.h>
int main()
{
char a='h';
printf("%c\n",a);
return 0;
}
/*Print an integer*/
#include <stdio.h>
int main()
{
int a=10;
printf("%d\n",a);
return 0;
}
/* Print a floating number */
#include <stdio.h>
int main()
{
float a=10.5;
printf("%f\n",a);
return 0;
}
/* Print floating numbers */
#include <stdio.h>
int main()
{
float a, b=9.0, c;
a=10.0; c=-2.3;
printf("a = %f\n",a);
printf("c = %f\n",c);
return 0;
}
Input/Output
printf("format",argument);
Examples:
printf("Hello world !\n");
printf("Two integers are %d and %d.\n",a,b);
printf("Two floating numbers are %f and %f.\n",a,b);
printf("Three floating numbers are %f, %f and %f.\n",a,b,c);
Compare the following two programs:
#include <stdio.h>
int main()
{
int a, b;
printf("Enter two integers separated by a comma = ");
scanf("%d, %d",&a, &b);
printf("a=%d b=%d\n", a, b);
return 0;
}
#include <stdio.h>
int main()
{
int a; float b;
printf("Enter an integer and a real number separated by a space = ");
scanf("%d %f",&a, &b);
printf("a=%d b=%f\n", a, b);
return 0;
}
Operators for variables
Symbol Meaning
< a < b ; a is less than b.
<= a < = b ; a is less than or equal to b.
> a > b ; a is greater than b.
>= a > = b; a is greater than or equal to b.
== a = = b ; a is equal to b.
!= a ! = b ; a is not equal to b.
if (a==b) printf("a and b are equal.\n"); else printf("a and b are not
equal.\n");
Logical operators:
Symbol Meaning
&& And
|| Or
! Not
if (a>0 && a<100) printf("a is between 0 and 100.\n");
if (a>0 || a<-5) printf("a is positive or less than -5.\n");
int a=20;
if (!(a==10)) printf("a is not equal to 10.");
if (a==10)
{ printf("You entered 10.\n");
return 0;
}
else ....
Increment/decrement operators
Symbol Meaning
++ b=++a a is incremented by 1 and assigned to b. Same as a=a+1;b=a;
b=a++ a is assigned to b first and incremented by 1. Same as b=a;a=a+1;
−− b=− −a a is decremented by 1 and assigned to b. Same as a=a−1; b=a;
b=a− − a is assigned to b first and decremented by 1. Same as b=a;a=a−1;
i=100;
i++;
is the same as
i=100;
i=i+1;
Substitution operators
Symbol Meaning
= a=b b is assigned to a.
+= a+=b a + b is assigned to a. Same as a=a+b;
−= a−=b a − b is assigned to a. Same as a=a−b;
*= a*=b a * b is assigned to a. Same as a=a*b;
/= a/=b a / b is assigned to a. Same as a=a/b;
%= a % = b Remainder of a / b is assigned to a. Same as a=a%b;
i=i+1
i+=1
i++
++i
The difference between ++i and i++ is that the former pre-increments i before any
operation while the latter post-increments i after the operation is done.
Control statements
The following are five statements that can control the flow of the program:
• if .... else
• for ( ; ; )
• while
• do while
• switch
if Statement
#include <stdio.h>
int main()
{
int i;
printf("Enter an integer ");
scanf("%d",&i);
if (i>1 && i<100)
printf("The number is between 1 and 100.\n");
else
printf("The number is not in that range.\n");
return 0;
}