Jump to content

Problems running C file

- - - - -

  • Please log in to reply
4 replies to this topic

#1
JanitorMoe

JanitorMoe

    Learning Programmer

  • Members
  • PipPipPip
  • 54 posts
Hey here is the program I am trying to run in C. I am using code::blocks ide to compile and run.



#include <stdio.h>

#include <stdlib.h>


int main (void)


{


    int a[5];

    int i;


    for ( i = 0; i < 5; i++ ) {

        a[ i ] = 0;

    }


    printf( "%s%13%s\n", "Element", "Value" );


    for ( i = 0; i < 5; i++ ) {

        printf ( "%s%13%s\n", i, a[ i ] );

    }


        return 0;

}


When I do it, it pulls up my cmd window and tells me the program has stopped working. Am I doing it wrong? I am hitting build and run.

#2
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
printf( "%s%13%s\n", "Element", "Value" );

You're specifying three printing fields in the string but are only passing two arguments. Same thing in your second loop.
sudo rm -rf /

#3
JanitorMoe

JanitorMoe

    Learning Programmer

  • Members
  • PipPipPip
  • 54 posts
So how do I fix this? My book shows this exact text and shows it working in the book?

#4
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
It's waay wrong. It won't even compile on my machine:

Quote

blah.c: In function ‘main’:
blah.c:15: error: conversion lacks type at end of format
blah.c:18: error: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘int’
blah.c:18: error: conversion lacks type at end of format
blah.c:18: error: format ‘%s’ expects type ‘char *’, but argument 3 has type ‘int’
The printf statement at the bottom in the loop should use "%d" not "%s".
sudo rm -rf /

#5
JanitorMoe

JanitorMoe

    Learning Programmer

  • Members
  • PipPipPip
  • 54 posts
Ahhh I fixed it, its supposed to be one less % in the first loop. In the second I used the wrong letters, supposed to be %d not %s




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users