Presentation is loading. Please wait.

Presentation is loading. Please wait.

Strings Input/Output scanf and printf sscanf and sprintf gets and puts.

Similar presentations


Presentation on theme: "Strings Input/Output scanf and printf sscanf and sprintf gets and puts."— Presentation transcript:

1 Strings Input/Output scanf and printf sscanf and sprintf gets and puts

2 scanf scanf(format, string_name); scanf(“%s”, mystring); –reads string from the standard input from first non-white space character to the first white space (including ‘\n’ character) to string_name[]. –it automatically appends the null character ‘\0’ to the end of the last character read into the array. –The array size should be at least “string length + 1” (+1 is for the null character) –If the string length is less than the array size, the string is stored and remaining array elements are left unknown –If the string length is greater than or equal to the array size, error condition occurs, because only the array size is filled, the null character and the remaining part of the string is not stored. Dr. Sadık EşmelioğluCENG 1142

3 printf printf(format, string_name); printf(“%s”, mystring); –prints the string to the standard output (without the null character) –You can use %ns, where n is an integer (Ex: %8s) The string is printed right justified If the string length is less than n, (n – length) spaces are added in front of the string If the string length is greater than n, then the string is printed as if %s is used –You can use %-ns, where n is an integer (Ex: %-8s) The string is printed left justified If the string length is less than n, (n – length) spaces are added to the end of the string If the string length is greater than n, then the string is printed as if %s is used Dr. Sadık EşmelioğluCENG 1143

4 sscanf sscanf(in_string_name, format, out_string_list); sscanf(MySentence, “%s%s%d”, str1, str2, &x); –Same as scanf, but the input is not the standard input, instead it is the first parameter of the sscanf (Ex. MySentence) char A[] = "The temperature is 50 degrees"; char a1[10], a2[15], a3[10]; int T; sscanf(A, "%s%s%s%d", a1, a2, a3, &T); printf("%s %s %s %d\n", a1, a2, a3, T); Dr. Sadık EşmelioğluCENG 1144

5 sprintf sprintf(in_string_name, format, out_string_list); sprintf(MySentence, “%s%s%d, str1, str2, x); –Same as printf, but the output is not the standard output, it is instead the first parameter of the sprintf (Ex. MySentence) char A[100]; char a1[10] = "You"; char a2[10] = "are"; char a3[10]= "years old"; int Age; Age=21; sprintf(A, "%s %s %d %s", a1, a2, Age, a3); printf("%s\n", A); Dr. Sadık EşmelioğluCENG 1145

6 gets gets(string_name); gets(mystring); –reads string from the standard input to an array string_name [] until the CTRL+Z (end of input) or CR (carriage return) character is encountered. –This function reads the spaces as well. –The null character is added to the end. –This function returns the NULL pointer, if it reads nothing char A[10]; char *pt; pt = gets(A); if(pt == NULL)printf("Nothing is read\n"); elseprintf("%s\n", A); Dr. Sadık EşmelioğluCENG 1146

7 puts puts(string_name); puts(mystring); –prints the string string_name to the standard output –This function adds a new line to the end of the string char A[20] = "How are you?"; char B[20] = "I hope you are fine"; puts(A); puts(B); Dr. Sadık EşmelioğluCENG 1147


Download ppt "Strings Input/Output scanf and printf sscanf and sprintf gets and puts."

Similar presentations


Ads by Google