- %c : Character
- %s : String
- %d : Sign decimal Integer
- %f : Decimal floating point
- %u : unsigned decimal integer
- %x : unsigned hexa integer
2. Handle input/output
a. function getc(FILE *stream)
- in stdio.h library
- get the next character from the specified stream
- Declaration : getc(FILE *stream);
+ parameters: stream -- this is pointer to a FILE object identify the stream on which operation is to be performed.
- Example :
#include <stdio.h>
int main() {
int ch;
printf("Please type one charecter \n");
ch = getc(stdin);
printf("This is charecter type from key-broad: %c", ch);
return 0;
}
b. fuction getchar()
- Return numeric value of character read. If an end-of-file or error occurs, function return EOF.
NOTE :
getc(stdin) = getchar()
- Example :
int main() {
printf("=====================================================\n");
printf("function getchar()\n");
int ch1,ch2;
printf("Please type two character : \n");
ch1 = getc(stdin);
ch2 = getchar();
printf("this is character type ch1 : %c \n",ch1);
printf("this is character type ch2 : %c",ch2);
return 0;
}
c. function putc()
- Write a character to the specified file stream.
- Declaration : int putc(int c, FILE *stream);
+ int c : the output is a character saved in Integer variable c.
+ FILE *stream : specifies a file stream.
3. Pointer
- pointer variable contain the memory address of another variable.
e.x
#include <stdio.h>
#include <stdlib.h>
/*
*
*/
int main() {
char c;
int x;
float y;
printf("c : address = %p, content = %c\n", &c,c);
printf("x : address = %p, content = %d\n", &x,x);
printf("y : address = %p, content = %f\n",&y,y);
c = 'A';
x = 10;
y = 20;
printf("c : address = %p, content = %c\n", &c,c);
printf("x : address = %p, content = %d\n", &x,x);
printf("y : address = %p, content = %f\n",&y,y);
return 0;
}
- declaring pointer
data-type *pointer_name;
+ data-type : type of data to which pointer point.
ex.
char c, *prt_c;
int x, *prt_x;
float y, *prt_y;
c = 'A';
x = 29;
y = 69.96;
printf("c : address = %p, content = %d\n", &c,c);
printf("x : address = %p,content = %d\n", &x,x);
printf("y : address = %p, content = %5.2f\n",&y,y);
prt_c = &c;
printf("prt_c : address = %p, content = %p\n", &prt_c, prt_c);
printf("prt_c => %c\n",*prt_c);
prt_x = &x;
printf("prt_x : address = %p, content = %p\n", &prt_x, prt_x);
printf("prt_x => %d\n", *prt_x);
prt_y = &y;
printf("prt_y : address = %p, content = %p\n", &prt_y, prt_y);
printf("prt_y => %
Note : * value of memory address pointer point
& address memory .
4. Array
- An Array is a collection of variables that are same data type.
- Decraling Array :
data type arrary_name[array size];
ex: int array_name[9];
- Index : index in array start from zero ( 0 < index < array_size)
- Initializing array :
ex : int array_name[5] = {0,1,2,3,4};
example to initializing arrays:
int array_int[10];
int i;
for (i = 0; i < 10; i++) {
array_int[i] = random();
printf("array_int[%d] = %d\n" ,i,array_int[i]);
}
- Array and pointer : pointer point to first element of array
ex :
int array_int[10];
int *ptr_array;
int i;
for (i = 0; i < 10; i++) {
array_int[i] = random();
}
ptr_array = array_int;
printf("the start address of array_int is %p\n", ptr_array);
printf("the value start element of array_int is : %d \n", *ptr_array);
ptr_array = &array_int[0];
printf("the start address of array_int is %p\n", ptr_array);
printf("the value start element of array_int is : %d \n", *ptr_array);
- Multidimensional Arrays:
data-type name_array[array-size1][array-size2]......[array-sizen];
ex;
for (int i = 0; i < 3; i++) {
printf("\n");
for (int j = 0; j < 5; j++) {
array_int[i][j] = i + j * 5;
printf("%6d",array_int[i][j]);
}
}
Không có nhận xét nào:
Đăng nhận xét