Tags down


I have a problem with scanf and giving input a char array with a for loop?

By : Sreya Sringa
Date : September 12 2020, 04:00 PM
fixed the issue. Will look into that further Your scanf function is catching a carriage return (see Jonathan Leffler comment), using getchar() can solve this, like so:
code :

int main(){ 
    int i; char a[100]; 
    for(i = 0; i < 5; i++){    
        printf("print char\n");  
    a[i] = '\0';
    for(int i = 0; i < 5; i++){
        printf("%c", a[i]);

Share : facebook icon twitter icon

How to use scanf for a char array input?

By : Malcolm Shore
Date : March 29 2020, 07:55 AM
this one helps. The scanf() with format specifier %s scans the input until a space is encountered. In your case, what you are seeing is undefined behavior. Your array can hold 10 chars, but you are writing out of its boundaries.
While you are getting an expected answer now, this is not always guarnateed and may instead cause a crash.

How to use scanf in a loop to get a char* array in c

By : Chris Patten
Date : March 29 2020, 07:55 AM
may help you . This is because a '\n' is left in the buffer when you do scanf() for f_l.
Use a trailing space, it should work
code :
for(i=0 ; i<=f_l ; i++) {
    scanf(" %c", &first[i]);

I can not input the names into the char array using scanf

By : InitialSpr
Date : March 29 2020, 07:55 AM
Hope this helps I can not use scanf to enter a name in stdnames array. when compiled it had no error , but as soon as i enter a name and then press enter to write the other name it gives an error and shuts the program. How should I go about it ? , By
code :
char *stdnames[100];
stdnames[count]=malloc(100*sizeof(char)); // You may replace 100 with desired size
stdnames[count]=malloc(100); // sizeof(char) is almost always 1

C : How to stop the loop when getting the end of input from keyboard or file while using scanf one char at a time

By : user721046
Date : March 29 2020, 07:55 AM
hope this fix your issue For a start it should be int main...
Also you need to check the return value from scanf - please read the manual page.
code :
#include <stdlib.h>
#include <stdio.h>
int main()
    char character;

    while (scanf("%c", &character) == 1) {
       if (character != '\n) {
         printf("%c", character)
    return EXIT_SUCCESS;

scanf replaces last elements in char array with the next scanf input

By : Christian Santiago
Date : March 29 2020, 07:55 AM
like below fixes the issue You are passing a char** to both printf and scanf. The %ss expect char*s, not char**s. So, replace the following:
Related Posts Related Posts :
  • How is malloc() than free() works
  • strcpy() in c gives me segmentation fault
  • What is the best practice when declaring a user defined array?
  • Efficient Assembly multiplication
  • while (getchar != '\n'); alternative to clear the buffer
  • How to fill a number with a variable amount of 0s?
  • Why C programming gives different output?
  • How to get float value from uint32_t?
  • What is the output of expression c=a+++b? If a=2,b=5 in c language...?
  • Why does it print 1 at the end?
  • Caesar cipher for numbers in C
  • Could someone explain the method of making rules for connect 4 game in C?
  • Is it a memory leak in C when code fails to free memory, but the OS will anyway?
  • Why can't I initialize a loop with i = -1?
  • How does the fork() function behave in if statements?
  • Why strcpy_s is safer than strcpy?
  • gcc flags to disable arithmetic optimisations
  • Recursion in c to print characters
  • Character array initialization using for loop
  • C Programming - Cannot Call Function More Than Once
  • Structures and pointer to pointer element
  • Error when initializing struct members in struct definition
  • Is that fixed-size array? a[]
  • It is not outputting what was expected and am unsure as to the issue
  • i want use C shell code to make buffer overflow and execute bin/sh
  • what the main function in this program returns to os?
  • Suffixes for integer types
  • I don't understand why a equals 1 and b equals 0 in this program
  • C prints other integer number than what it shows on Watches. Why is that?
  • Output of the for loop?
  • How to find 64-bit divisions in existing C code?
  • Finding a numbers with the largest/smallest number of bits set
  • how does " for (int j = 0; j < n || !putchar('\n'); j++) " work
  • handle ctrl+c in client-server socket in c programing
  • Restrict a process to read "/etc/resolv.conf" file in linux
  • Restrict scope of global variable inside one file in C
  • Arduino AES128 encryption - decryption problem
  • Why is not ampersand needed in the following code?
  • Is it possible to define a function name with #define?
  • Why is the size of the data type different when the value is directly passed to the sizeof operator?
  • Segmentation fault / Incorrect checksum triggered ocasionally when reading lines from a file
  • Adding bits at specific indexes for a uint8_t block
  • how can I use "shadow call stack" feature of clang-8 in x86_64?
  • malloc Assertion Failed
  • Array of structs inside of another struct in C
  • Why does this print the wrong part of the string?
  • Moving the cursor and printing characters in console doesn't work with sleep
  • Confusion in adding an element to a sorted list of integers
  • How I can separate integer number in 3 "houses"? Hundred, Ten and Unity
  • User-implemented memory management
  • What it entails to declare and initialize a pointer in this way: int (*p)[n]
  • Function to look though any struct in C
  • Microcontroller not recognized at USB port when connected via pins
  • Unexpected bitshift behavior in C
  • Model 3D spring from points
  • C control reaches end of non void function
  • Tax outputting 0
  • Gtk 3.0 Text View Resizable
  • Statically linking libc into my binary causes it to segfault
  • Child Process Executing Print Statement But Nothing Afterwards
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk