Program Termination error while printing the array - c

#include <stdio.h>
int main() {
int n, i;
char arr[20];
clrscr();
printf("Enter size of array(<=20)");
scanf("%d", &n);
printf("Enter array");
for (i = 0; i < n; i++) {
scanf("%s", &arr[i]);
}
for (i = 0; i < n; i++) {
printf("%s", arr[i]);
}
getch();
return 0;
}
The program does not prints the array
and instead shows
Program termination message
The image shows the program termiantion message

The problem is with the line
printf("%s", arr[i]);.
If you change this line to
printf("%c", arr[i]);
then it will work because %s is used with character arrays that contain strings
I am just giving the solution for your program termination. Still we can do some modification in your code.
Thanks

Related

I want to print all the input of this string in for loop but it only prints the last input

Help me correcting this code.... I don't know what extra details should I give so it lets me post.
#include<stdio.h>
int main(){
char s[34];
int a = 4;
for (int i = 0; i < a; i++)
{
printf("Student %d enter your name: ", i+1);
scanf("%s", s);
}
for (int i = 0; i < a; i++)
{
printf("Student %d name is %s\n", i+1, s);
}
return 0;
}
You are overwriting the same block of memory: scanf("%s", s);.
You should have s as an array of pointers to char i.e. char *s[4] in which case you should allocate space for each name using malloc and then use free to give back all allocated memory to the OS.
Or, you could declare s to be char s[4][34] and use s[i] in your scanf and printf statements.

More output than desired in an array of string

I am trying to solve a problem. You can ignore the problem in the code. My doubt is that if I am taking the value of t as 2, still the array outputs 3 strings although I am running the loop only t times for output.
#include<stdio.h>
#include<string.h>
int main(void){
int t;
int i;
int j;
int n;
int c;
int temp;
char result[30][3];
int flag;
scanf("%d", &t);
for(i = 0; i < t; i++){
flag = 0;
scanf("%d", &n);
scanf("%d", &c);
for(j = 0; j < n; j++){
scanf("%d", &temp);
if(c > temp){
c = c - temp;
} else{
flag = 1;
}
}
if(flag == 0){
strcpy(result[i], "Yes");
} else{
strcpy(result[i], "No");
}
}
for(i = 0; i < t; i++){
printf("%s", result[i]);
}
}
Add a \n when you printf a result[i], then you'll find that you actually output 2 strings. For example, if you first strcpy(result[0], "Yes") and then strcpy(result[0], "No"), you'll get the outputs like this:
YesNo
No
In fact, the storage of result is as follows:
result[0]: ['Y']['e']['s']
result[1]: ['N']['o']['\0']
You get "YesNo" when you output result[0], since a two-dimensional array is stored contiguously in the memory and a string ends with \0.
As another example, if you strcpy(result[0], "Hello"), then when you output result[0], you'll get
Hello
and when you output result[1], you'll get
lo
Since the storage in result is as follows:
result[0]: ['H']['e']['l']
result[1]: ['l']['o']['\0']

fgets() in c doesn't read the sentence at all? [duplicate]

This question already has answers here:
Program is skipping fgets without allowing input
(2 answers)
scanf() leaves the newline character in the buffer
(7 answers)
Closed 3 years ago.
I am very new to C. Trying to practice with input and output now. I am trying to use fgets to read in a sentence and convert it to all upper case letters, but for some reason it's not reading it. I HAVE ADDED *c in MY SCANF AND IT STILL DOES NOT WORK. Any suggestions and tips are greatly appreciated! Thanks in advance.
#include <stdio.h>
#include <ctype.h>
#define ARR_SIZE 100
void modify(int pInt[100], int input);
void modify2(char *pString[100]);
int main() {
printf("Enter a size for your array:\n");
int input;
scanf("%d", &input);
printf("Great! You now have an array of size %d.\n", input);
int array[ARR_SIZE];
printf("Now please enter %d numbers.\n", input);
int i;
for (i = 0; i < input; i++) {
scanf("%d*c", &array[i]);
}
printf("Awesome. All %d slots in the array are now filled.\n", input);
printf("Here is how your array looks:\n");
int j;
for (j = 0; j < input; j++) {
printf(" %d |", array[j]);
}
printf("\n");
modify(array, input);
printf("Now your array looks like this:\n");
for (j = 0; j < input; j++) {
printf(" %d |", array[j]);
}
printf("\n");
printf("Experiment #2:\n");
printf("Enter a sentence:\n");
char sentence[ARR_SIZE];
fgets(sentence, ARR_SIZE, stdin);
modify2(sentence);
printf("Now your sentence is upper case: %s\n", sentence);
return 0;
}
void modify2(char *pString[100]) {
int i = 0;
while (pString[i]) {
pString[i] = toupper(pString[i]);
i++;
}
}
void modify(int pInt[100], int input) {
int i;
for (i = 0; i < input; i++) {
pInt[i] *= 2;
}
}

Character Array User Input Scanf

Just started learning programming on my own and whilst trying to create an array of characters from user input, using scanf, have hit the wall; the code is as below:
#include <stdio.h>
#define I 5
int main (void) {
char a[I+1] = {a[I+1] = '\0'}, q;
int i;
for(i = 0; i < I; i++) {
printf("Enter an alphabet:\t");
//scanf("%c", &q);
//scanf("%c*\n", &q);
//scanf("%[^\n]", &q);
//scanf("%[a-z, A-Z]", &q);
scanf("%127[^\n]", &q);
a[i] = q;
}
printf("\n");
for(i = 0; i < I; i++) {
printf("Element a[%d] of a[I]:\t%c\n", i, a[i]);
}
printf("And the a[I] string:\t%s\n", a);
return 0;
}
None of the scanf combinations in the above code works: the program either skips input prompt after the first one or does not store response.
How can this be resolved with scanf?
char a[I+1] = {a[I+1] = '\0'} is not valid. Even if it compiles, it is going out of bounds when assigning the '\0' character. The commonly used convention looks more like this instead:
char a[I+1] = {0};
Or simply:
char a[I+1] = {};
That said, q is only 1 char in size, but your scanf() is trying to read a string up to 127 chars into q. So you are going to trash memory. To read a single char at a time, use %c instead:
#include <stdio.h>
#include <string.h>
#define MAX_INPUT 5
int main (void) {
char a[MAX_INPUT+1];
int i, len;
for(i = 0; i < MAX_INPUT; i++) {
printf("Enter an alphabet:\t");
scanf("%c", &a[i]);
}
a[MAX_INPUT] = '\0';
printf("\n");
len = strlen(a);
for(i = 0; i < len; i++) {
printf("Element a[%d]:\t%c\n", i, a[i]);
}
printf("And the a string:\t%s\n", a);
return 0;
}
Or, you can remove the loop and just use a single call to scanf() using "%5[^\n]" as the format string:
#include <stdio.h>
#include <string.h>
#define MAX_INPUT 5
int main (void) {
char a[MAX_INPUT+1];
char fmt[20];
int i, len;
sprintf(fmt, "%%%d[^\n]", MAX_INPUT);
printf("Enter an alphabet:\t");
scanf(fmt, a);
printf("\n");
len = strlen(a);
for(i = 0; i < len; i++) {
printf("Element a[%d]:\t%c\n", i, a[i]);
}
printf("And the a string:\t%s\n", a);
return 0;
}
This works without any warning or error on Cygwin gcc v7.3 with Wall flag:
#include <stdio.h>
#define I 5
int main (void) {
char a[I+1] = {a[I+1] = '\0'},q;
int i;
for(i = 0; i < I; i++) {
printf("Enter an alphabet:\t");
scanf("%c%*c", &q);
a[i] = q;
}
printf("\n");
for(i = 0; i < I; i++) {
printf("Element a[%d] of a[I]:\t%c\n", i, a[i]);
}
printf("And the string a[I]:\t%s\n", a);
return 0;
}

Reading and printing strings in C

I want to scan and print two strings one after another in a loop.But I cannot do it.Only one string gets scanned and printed if i use the loop.If i try to print without the loop then the two "gets()" work properly.
#include <stdio.h>
int main()
{
int T,i,j;
char name1[100];
char name2[100];
scanf("%d",&T);
for(i=0; i<T; i++)
{
printf("Case %d: ",i+1);
//scanf("%[^\n]s",name1);
gets(name1);
/*for(j=0; j<strlen(name1); j++)
{
printf("%c",name1[j]);
}*/
puts(name1);
//scanf("%[^\n]s",name2);
gets(name2);
/*for(j=0; j<strlen(name2); j++)
{
printf("%c",name2[j]);
}*/
puts(name2);
}
}
Here you go. Use fflush(stdin). It will take two inputs and print them one after the another.
#include<stdio.h>
int main()
{
int T,i,j;
char name1[100];
char name2[100];
scanf("%d",&T);
for(i=0; i<T; i++)
{
printf("Case %d: ",i+1);
fflush(stdin);
gets(name1);
gets(name2);
puts(name1);
puts(name2);
}
return 0;
}
Edit: As suggested in the comment below, using gets() is not advisable if you do not know the number of characters you wish to read.
After taking testcase from the user, next line gets() function will take the '\n' you have to ignore the scenario.
Here's a tricky solution of this problem. Just use '\n' after %d in scanf function. scanf("%d\n",&T);
#include <stdio.h>
int main(void) {
char s1[100],s2[100];
int i,T;
scanf("%d\n",&T);
for(i = 0; i < T; i++){
printf("Case %d: ",i+1);
gets(s1);
puts(s1);
gets(s2);
puts(s2);
}
return 0;
}
You do not terminate your prints.
stdout is buffered.
Print is only performed after a "\n" or explicit flush.
try something around the lines:
#include <stdio.h>
int main()
{
int T,i,j;
char name1[100];
char name2[100];
scanf("%d",&T);
for(i=0; i<T; i++)
{
#ifdef BAD_CODE
printf("Case %d: ",i+1);
gets(name1);
puts(name1);
gets(name2);
puts(name2);
putchar("\n");
#else //better code
fgets(name1, sizeof(name1)-1, stdin);
fgets(name2, sizeof(name2)-1, stdin);
printf("Case %d: '%s' '%s'\n",i+1, name1, name2);
#endif
}
}

Resources