2. C program is made of basic elements, such as
expressions, statements, statement blocks, and function
blocks.
Constant is a value that never changes
Variables can be used to present different values
Expression: is a combination of constants, variables,
and operators that are used to denote computations.
6 An expression of a constant.
i An expression of a variable.
6 + i An expression of a constant plus a variable.
exit(0) An expression of a function call.
5. statement is a complete instruction, ending
with a semicolon.
i = 1;
i = (2 + 3) * 10;
i = 2 + 3 * 10;
j = 6 % 4;
k = i + j;
return 0;
exit(0);
printf ("Howdy, neighbor! This is my first C
program.n");
6. A group of statements can form a statement block that
starts with an opening brace ({) and ends with a closing
brace (}). A statement block is treated as a single
statement by the C compiler.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i;
for(i=1;i<=20;i++)
{
textcolor(i);
cprintf("Computer Bak Touk Centernr");
}
getch();
}
7. standard C library functions
Functions are the building blocks of C programs
printf() and exit()
scanf()
clrscr()
gotoxy(x,y);
textcolor();
colorbackground()
8. Illegal Name The Rule
2 (digit) A function name cannot start with a
digit.
* (Asterisk) A function name cannot start with an
asterisk.
+ (Addition) A function name cannot start with
one of the arithmetic signs that are reserved C
keywords.
. (dot) A function name cannot start with ..
total-number A function name cannot contain a
minus sign.
account'97 A function name cannot contain an
apostrophe.
13. #include<stdio.h>
int integeradd(int x, int y)
{
int result;
result=x+y;
return result;
}
int main()
{
int sum;
sum=integeradd(5,12);
printf("5+12 =%d",sum);
return 0;
}
14. #include<stdio.h>
int integer_add(int x, int y);
int main()
{
int sum;
sum=integer_add(5,12);
printf("5+12 =%d",sum);
return 0;
}
int integer_add(int x, int y)
{
int result;
result=x+y;
return result;
}
17. The sin() function
The cos() function
The tan() function
The pow() function
The sqrt() function
18. #include <stdio.h>
#include <math.h>
main()
{
double x;
x = 45.0; /* 45 degree */
x *= 3.141593 / 180.0; /* convert to radians */
printf("The sine of 45 is: %f.n", sin(x));
printf("The cosine of 45 is: %f.n", cos(x));
printf("The tangent of 45 is: %f.n", tan(x));
return 0;
}
#include <stdio.h>
#include <math.h>
main()
{
double x, y, z;
x = 64.0;
y = 3.0;
z = 0.5;
printf("pow(64.0, 3.0) returns: %7.0fn", pow(x, y));
printf("sqrt(64.0) returns: %2.0fn", sqrt(x));
printf("pow(64.0, 0.5) returns: %2.0fn", pow(x, z));
return 0;
}
19. The if statement
The if-else statement
The switch statement
The break statement
The continue statement
The goto statement
20. used to evaluate the conditions as well as to
make the decision whether the block of code
controlled by the statement is going to be
executed.
if (5>1) {
statement1;
statement2;
.
.
}
if (x > 0)
printf("The square root of x is: %fn", sqrt(x));
21. EX:
#include <stdio.h>
main()
{
char ch;
printf("Enter a charactern");
scanf("%c", &ch);
if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' || ch
=='o' || ch=='O' || ch == 'u' || ch == 'U')
printf("%c is a vowel.n", ch);
else
printf("%c is not a vowel.n", ch);
return 0;
}
29. use to make unlimited decisions or choices based on
the value of a conditional expression and specified
cases. Int, byte, short, char
switch (expression) {
case expression1:
statement1;
case expression2:
statement2;
.
.
.
default:
statement-default;
}
30. #include <stdio.h>
main()
{
int day;
printf("Please enter a single digit for a dayn");
printf("(within the range of 1 to 3):n");
day = getchar();
switch (day){
case `1':
printf("Day 1n");break;
case `2':
printf("Day 2n");break;
case `3':
printf("Day 3n");break;
default:
}
return 0;
}
32. You can add a break statement at the end of
the statement list following every case label,
if you want to exit the switch construct after
the statements within a selected case are
executed. BREAK LOOP ALSO
34. break;
case `2':
printf("Day 2 is Monday.n");
break;
case `3':
printf("Day 3 is Tuesday.n");
break;
case `4':
printf("Day 4 is Wednesday.n");
break;
case `5':
printf("Day 5 is Thursday.n");
break;
case `6':
printf("Day 6 is Friday.n");
break;
case `7':
printf("Day 7 is Saturday.n");
break;
default:
printf("The digit is not within the range of 1 to 7.n");
break;
}
return 0;
}
35.
36. Instead of breaking a loop, there are times
when you want to stay in a loop but skip over
some statements within the loop. To do this,
you can use the continue statement provided
by C. The continue statement causes
execution to jump to the top of the loop
immediately.
37. #include <stdio.h>
main()
{
int i, sum;
sum = 0;
for (i=1; i<8; i++){
if ((i==3) || (i==5))
continue;
sum += i;
}
printf("The sum of 1, 2, 4, 6, and 7 is: %dn",
sum);
return 0;
}
38. Looping, called iteration, is used in
programming to perform the same set of
statements over and over until certain
specified conditions are met.
The for statement
The while statement
The do-while statement
#include<stdio.h>
#include<conio.h>
main()
{
int year,day;
char ch;
for(;ch!=27;)
{
clrscr();
printf("Please Enter The Year:");
scanf("%d",&year);
day=(year+(year-1)/4-(year-1)/100+(year-1)/400)%7;
switch(day)
{
case 0:printf("Sunday 1st January %d",year);break;
case 1:printf("Monday 1st January %d",year);break;
case 2:printf("Tuesday 1st January %d",year);break;
case 3:printf("Wednesday 1st January %d",year);break;
case 4:printf("Thursday 1st January %d",year);break;
case 5:printf("Friday 1st January %d",year);break;
case 6:printf("Saturday 1st January %d",year);break;
}
ch=getch();
}
return 0;
}