Escolar Documentos
Profissional Documentos
Cultura Documentos
Strings in C
CIT 593
String Declaration
Variable amessage
Answer:
Variable pmessage
m e s s a g e \0
m e s s a g e \0
CIT 593
CIT 593
The
string can contain additional characters after those that form the
integral number, which are ignored and have no effect on the
behavior of this function.
If
CIT 593
16
CIT 593
Strings
CIT 593
CIT 593
Usage of strlen
Output:
H
e
l
l
o
#include <string.h>
#include <stdio
<stdio.h>
h>
int main(){
char array[] = Hello;
for(i = 0; i < strlen(array);i++){
printf(%c\n,array[i]);
}
}
CIT 593
10
CIT 593
CONFUSING!!!
CIT 593
CIT 593
12
13
CIT 593
C String Library
Functions include
int strlen(const char* str)
1.
2.
14
CIT 593
3.
Bad things
15
CIT 593
16
Extremely important:
Array size must be max string length + 1
why??
17
CIT 593
CIT 593
18
CIT 593
Return Value
3
2
1
//Example in scanf2.c
#include <stdio.h>
int main(){
int check = 0;
int i = 0;
while(i != 1){
printf("Enter number\n");
if((check = scanf("%d",&i)) != 1){
printf(Error in input, must be a number");
}
}
}
What happens when you enter letter or float response to the prompt for an
integer?
Stuck in a while loop, why??
19
CIT 593
20
Why?
1
\n
The p
picture shows the stream of input
p characters after the first call to
scanf was complete. Here is what the first call did:
Read the 1', saw that it was a digit and can be used as part of an
int
Read the 2', saw that it was a digit and can be used as part of an
int
Read the '.', saw that it was not a digit and could be not be used
as part of an int. The '.'
. was put back on the input stream (in our
e.g. stdin) so that it could be read by the next input operation
So how do we take care of this ?
CIT 593
21
CIT 593
22