Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Program to read and write the array:

error with the output
No. of digits scanned != No. of digits printed

What I have tried:

#include <stdio.h>

int main()
{
    int x;
    int arr[x];
    int i;
    printf("Enter the value of 'x'\n");
    scanf(" %d", &x);
    printf("Enter %d elements\n",x+1);
    for(i=0;i<x;i++)
    {
        scanf(" %d\n", &arr[i]);
    }
    printf("the digits you entered are as follows: \n");
    for(i=0;i<x;i++)
    {
        printf("%d\t",arr[i]);
    }

    return 0;
}
Posted
Updated 10-Dec-17 6:16am
v2

You can't create an array with dynamic size before knowing that size.
C++
int x;
int arr[x]; // you create the array here
int i;
printf("Enter the value of 'x'\n");
scanf(" %d", &x); // you know the array size here


Advice: use the debugger to check that anything goes as expected.
There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
Comments
shreyas s 11-Dec-17 1:22am    
Thank you
Patrice T 11-Dec-17 1:28am    
You welcome.
As others already noted, you need to create the array after you know its size. With a C99 compliant compiler (like GCC) you may write:
Please note: in order to simplify the code, I have omitted checking the scanf return value. You should always perform such a check in the actual code.
C
#include <stdio.h>

int main()
{
    int x;
    int i;

    printf("Enter the value of 'x'\n");
    scanf("%d", &x);

    int arr[x]; // here you know the array size.

    printf("Enter %d elements\n",x);
    for(i=0;i<x;i++)
    {
        scanf("%d", &arr[i]);
    }
    printf("the digits you entered are as follows: \n");
    for(i=0;i<x;i++)
    {
        printf("%d\t",arr[i]);
    }
    printf("\n");
    return 0;
}

On the other hand, with a not-C99-compliant compiler you have to write something like:
C
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int x;
    int i;

    printf("Enter the value of 'x'\n");
    scanf("%d", &x);

    int * arr = (int *) malloc( sizeof(int) * x); // dynamically allocate memory on the heap
    if ( ! arr ) return -1;

    printf("Enter %d elements\n",x);
    for(i=0;i<x;i++)
    {
        scanf("%d", &arr[i]);
    }
    printf("the digits you entered are as follows: \n");
    for(i=0;i<x;i++)
    {
        printf("%d\t",arr[i]);
    }
    printf("\n");

    free (arr); // release the heap allocated memory

    return 0;
}
 
Share this answer
 
v2
Comments
shreyas s 11-Dec-17 1:32am    
As a beginner to the programming world, should I learn C99 or C11 version of C?
Most of the online tutorials are C99ish isn't it?
CPallini 11-Dec-17 2:59am    
If supported by the compiler I would use the latest statndard (unless you have to face compatibility issues with legacy applications).
Please note, with 'not-C99-compliant' compiler, I meant legacy (C89 orC90) compilers.
You really need to get hold of a good book on C programming, something like The C Programming Language, by Brian W. Kernighan & Dennis M. Ritchie[^]. You will learn much faster from a book or on-line tutorials. These forums are not designed to teach you programming.
 
Share this answer
 
Comments
shreyas s 10-Dec-17 10:31am    
Yeah, I tried reading that book but understanding the concepts wasn't easy for me:( So I opted to learn from online resources.
I apologize for bothering people here with my silly questions, and also do you know any place(website) on the internet where noobs like me can ask questions like these?
Richard MacCutchan 10-Dec-17 10:51am    
You misunderstand the point I am trying to make. You will learn much better from a proper book or online tutorial. Once you have followed a formal tutorial then simple question like this will not give you trouble.
shreyas s 11-Dec-17 1:24am    
Oh, I see what you mean.
Thanks for the advice:)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900