This document discusses structures in C programming. It explains that structures can contain elements of different data types, accessed by name, unlike arrays where all elements must be of the same type and accessed by index. It provides examples of declaring a structure type with members, defining structure variables, accessing members using the dot operator, passing structures to functions, and initializing an array of structures.
The document discusses call-by-value in function invocation in C. When a function is called, only the values of the arguments are passed to the function, not the variables themselves. So any changes made to the parameters inside the function are not reflected in the calling function. This causes an issue when trying to swap variables by passing them to a Swap function.
5. 5
Displaying String and characters
printf()
String豢レ 伎 %s 襯
豢レ 炎概企 豢ル 蠍 覦, 蠏碁讌
朱 -1 覦
[Ex] int nchars;
char p[ ] = Hello! the world;
nchars = printf(%s, p);
printf(nnum of chars=%dn, nchars);
Hello! the world
num of chars = 16
6. 6
Displaying String and characters
puts()
printf()覲企 fast, simple
String 豢 , next line朱 企
int puts(char *str); /*function prototype */
return
- no. of chars written if successful
- EOF(-1) if not
[Ex] char p[ ] = Hi !!;
puts(p);
puts(Hello!!); Hi !!
Hello!!
7. 7
Reading Strings from the KB
scanf()
%s : next white space char 蟾讌 read
%ns : n螳 chars 襯 read, 蠏 white space螳 り
覃 white space蟾讌襯 read
int scanf(char *format, argument_list);
return
- no. of successfully matched and input items
- 0 if not
8. 8
Reading Strings from the KB
[Ex] char name[80];
scanf(%s, name); /* name <- SKKU */
SKKU Univ.
[Ex] char name[80];
scanf(%3s, name); /* name <= C-P */
scanf(%8s, name); /* name <= rogram */
C-Program is
White space 蟾讌 read
3螳 char襯 read
9. 9
Reading Strings from the KB
gets()
KB襦覿 n蟾讌, 讀 line襦 read
n 0襦 convert string ル.
char* gets(char *format);
return
- the address of the string
- NULL if EOF (end-of-file)
scanf()襯 牛 String 覦 蟆曙:
white space skip朱 white space read 覿螳.
Line 襦 string 覦 .
10. 10
Reading Strings from the KB
[Ex] char data[81], *P;
while( gets(data) != NULL) {
printf(%sn, data);
}
or while(gets(data) != 0)
^D襯 ロ 蟾讌 line襦
read 蠏碁襦 覃伎 豢
program
<blank line> 轟
<[ctrl] + D> 譬襭
11. 11
Reading Strings from the KB
char a[5], b[4]=1234;
scanf(%s, a);
printf( %s %sn, a, b ) ;
Array蠍磯慨 襷 覓語襯 ロ 蟆曙
abcde襯 ロ覃 豢 螳?