Académique Documents
Professionnel Documents
Culture Documents
C Language Keywords
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
Variables
1.
2.
3.
4.
5.
6.
/* pi to 6 decimal places */
number_of_students /* number students in this class */
/* Contains a "$" */
/* Contains a space */
/* Reserved word */
int main(void)
{
int salary;
salary = 10000;
printf("My salary is %d.", salary);
return 0;
}
Initialize int value in declaration
#include <stdio.h>
int main(void)
{
int number0 = 10, number1 = 40, number2 = 50, number3 = 80, number4 = 10;
int number5 = 20, number6 = 30, number7 = 60, number8 = 70, number9 = 110;
int sum = number0 + number1+ number2 + number3 + number4+
number5 + number6 + number7 + number8 + number9;
float average = (float)sum/10.0f;
printf("\nAverage of the ten numbers entered is: %f\n", average);
return 0;
}
Average of the ten numbers entered is: 48.000000
int balance_owed;
int term_3;
term = 3 * 5;
term_2 = 2 * term;
term_3 = 3 * term;
return (0);
}
Use printf to output variable
#include <stdio.h>
int main()
{
int term;
term = 3 * 5;
printf("Twice %d is %d\n", term, 2*term);
printf("Three times %d is %d\n", term, 3*term);
return (0);
}
Twice 15 is 30
Three times 15 is 45
The special character tells the program how to convert the data.
Conversion Specifier
%d
%f
%c
Description
Displays integer value
Displays floating-point numbers
Displays character
size
size
size
size
size
size
of
of
of
of
of
of
the
the
the
the
the
the
of
of
of
of
of
of
of
type
type
type
type
type
type
type
of
of
of
of
of
of
of
type
type
type
type
type
type
type
Scope of variables
1.
2.
3.
4.
5.
#include <stdio.h>
main() {
int i = 10;
{
int i = 0;
for( i=0;i<2;i++)
{
printf("value of i is %d\n",i);
}
}
printf("the value of i is %d\n",i);
}
value of i is 0
value of i is 1
the value of i is 10
=
=
=
=
=
=
=
=
=
=
1
1
1
1
1
1
1
1
1
9
#include <stdio.h>
int count = 0;
*/
void test1(void){
printf("\ntest1 count = %d ", ++count);
}
void test2(void){
static int count;
/* This hides the global count */
printf("\ntest2 count = %d ", ++count);
}
int main(void){
int count = 0;
Static variable
#include <stdio.h>
int g = 10;
main(){
int i =0;
void f1();
f1();
printf(" after first call \n");
f1();
printf("after second call \n");
f1();
printf("after third call \n");
}
void f1()
{
static int k=0;
int j = 10;
printf("value of k %d j %d",k,j);
k=k+10;
}
value of k 0 j 10 after first call
value of k 10 j 10after second call
value of k 20 j 10after third call
count
count
count
count
count
count
count
count
count
count
=
=
=
=
=
=
=
=
=
=
1
1
1
2
1
3
1
4
1
5
int main(void)
{
int number = 0;
int *pointer = NULL;
number = 10;
printf("\nnumber's address: %p", &number);
printf("\nnumber's value: %d\n\n", number);
return 0;
}
number's address: 9a378
number's value: 10
#include <stdio.h>
main(){
int a[5];
int *b;
int *c;
int i;
for(i = 0;i<5;i++){
a[i]=i;
}
for(i = 0;i<5;i++) {
printf("value in array %d\n",a[i]);
}
b=a;
b++;
*b=4;
b++;
*b=6;
b++;
*b=8;
b++;
*b=10;
b++;
*b=12;
printf("after\n\n\n");
for(i = 0;i<5;i++) {
printf("value in array %d\n",a[i]);
}
}
value
value
value
value
value
after
in
in
in
in
in
array
array
array
array
array
0
1
2
3
4
value
value
value
value
value
in
in
in
in
in
array
array
array
array
array
0
4
6
8
10
/* Function prototype */
int main(void)
{
double Val1 = 10.5, Val2 = 2.5;
int num1 = 6, num2 = 5;
long num3 = 12, num4 = 20;
printf("\n Average = %lf", average(Val1, 3.5, Val2, 4.5, 0.0));
printf("\n Average = %lf", average(1.0, 2.0, 0.0));
printf("\n Average = %lf\n", average( (double)num2, Val2,(double)num1,
(double)num4,(double)num3, 0.0));
return 0;
}
/* Function to calculate the average of a variable number of arguments */
double average( double v1, double v2,...)
{
va_list parg;
/* Pointer for variable argument list */
double sum = v1+v2;
/* Accumulate sum of the arguments */
double value = 0;
/* Argument value
*/
int count = 2;
/* Count of number of arguments
*/
va_start(parg,v2);
Adding Comments
Comments in a C program have a starting point and an ending point.
Everything between those two points is ignored by the compiler.
/* This is how a comment looks in the C language */
The beginning of the comment is marked by the slash and the asterisk: /*.
The end of the comment is marked by the asterisk and the slash: */.
#include <stdio.h>
int main(){
/* This is the comment.*/
printf("%15s","right\n");
printf("%-15s","left\n");
*/
*/
return(0);
}
right
left
1.15.Comments
Indentation and Code Format
while (! done) {
printf("Processing\n");
next_entry();
}
if (total <= 0) {
printf("You owe nothing\n");
total = 0;
} else {
printf("You owe %d dollars\n", total);
all_totals = all_totals + total;
}
In this case, curly braces ({}) are put on the same line as the statements.
The other style puts the {} on lines by themselves:
while (! done)
{
printf("Processing\n");
next_entry();
}
if (total <= 0)
{
printf("You owe nothing\n");
total = 0;
}
else
{
printf("You owe %d dollars\n", total);
all_totals = all_totals + total;
}
Clarity
/* poor programming practice */
temp = box_x1;
box_x1 = box_x2;
box_x2 = temp;
temp = box_y1;
box_y1 = box_y2;
box_y2 = temp;
Headers
1. Each function in the C standard library has its associated header.
2. The headers are included using #include.
3. Header Including obtains the Declarations for the standard library functions.
Common Headers
Header
assert.h
ctype.h
errno.h
float.h
limits.h
locale.h
math.h
setjmp.h
signal.h
stdarg.h
stddef.h
stdio.h
stdlib.h
string.h
time.h
Purpose
Defines the assert() macro
Character handling
Error reporting
Defines implementation-dependent floating-point limits
dependent limits
Localization
math library
Nonlocal jumps
Signal handling
Variable argument lists
Constants
I/O system
Miscellaneous declarations
string functions
system time functions