C the programming language "Character Counter" example won't run - c

I'm completely new to C, and I am attempting to run this example code on xcode, but it says build failed. I got the code exact like it is in the book, but it still won't run.
#include <stdio.h>
/* count characters in input; 2nd version */
int main()
{
double nc;
for (nc =0; getchar() != EOF; ++nc) {
;
}
printf("%.0f\n", nc);
}
Sorry if it's a noob question. All help is appreciated!

The question code lacks a 'return {int value}' statement. I added this missing line, and ended up with the following code:
#include <stdio.h>
/* count characters in input; 2nd version */
int main()
{
double nc;
for (nc =0; getchar() != EOF; ++nc) {
;
}
printf("%.0f\n", nc);
return(0);
}
The above compiled without errors or warnings using:
gcc -Wall -o test test.c
To run it requires that input to the program comes from a file.
I made a file named 'junk.txt' with the following content (not including the braces):
[ adddsadsd 33334343434243]
Then I executed the program using the following command:
./test < junk.txt
which generated the output:
25

Related

C: undefined reference to 'WinMain#16'

I'm very new to coding and I have been trying to write code to adds two integers. But whenever I try to run it using 'gcc addition.c' in the terminal I always reports an error. I tried reinstalling the compiler i.e Mingw several times but the problem does not gets fixed.
(I m currently doing C language on VS CODE software, when you answer to my issue please use layman language)
#include <stdio.h>
int main() {
int x=1;
int y=2;
int z=0;
z=x+y;
printf("%d", z);
return 0;
}
Windows PowerShell
PS D:\C tutorials> gcc addition.c
c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../libmingw32.a(main.o):(.text.startup+0xa0): undefined reference to "WinMain#16' collect.exe: error: ld returned 1 exit status
I added a \n to clean up the printf().
#include <stdio.h>
int main(void) {
int x=1;
int y=2;
int z=0;
z=x+y;
printf("%d\n", z);
return 0;
}
% gcc -o addition addition.c -lc ; ./addition
3
You needed to include the C library, represented by the -lc in the gcc line.

printf() Overflows on TI-83 Plus

I'm following the tutorial here. What I'm doing is to simply print out a string but it is giving me an overflow error:
#include <stdio.h>
int main(){
printf("Hello World\n");
return 0;
}
I'm compiling the C code with z88dk into a .83p file and loaded it into a TI-83p. I ran it with Doors CS7.2.

Cannot run program

I started learning C programming with C Programming Language by Denis M Ritchie I am trying to execute program from that book
#include <stdio.h>
/* count lines in input */
main()
{
int c, nl;
nl = 0;
while ((c = getchar()) != EOF)
if (c == '\n')
++nl;
printf("%d\n", nl);
getchar();
}
However all I get is blank console and when I type text and press enter,no value is displayed.
I am using Visual Studio 2013 IDE.
The program you posted here is for counting number of lines.
Q. However all I get is blank console and when I type text and press
enter,no value is displayed
A. Yes it shows nothing because while ((c = getchar()) != EOF) waits until you enter EOF (use ctrl + z then you will get number for lines).
getchar(c);
See declaration of getchar.
int getchar(void)
It doesn't take any parameter. Didn't it gave you error.
And main should be int main.
There are several issues. First, main has to have a type, usually int
- main()
+ int main()
and your program should return an exit status
Secondly getchar takes no arguments, and returns the input
http://www.tutorialspoint.com/c_standard_library/c_function_getchar.htm
- getchar(c)
+ c = getchar()
You can compile your program by calling
gcc -Wall test.c -o test
Where test.c is your code, and test is the binary. -Wall will show all the "warning" (errors that don't interfere with the program execution)
I believe that you use Windows, it's better to add:
system("pause");
Moreover to use system("pause"), only in Windows, you need to include another library:
#include <stdlib.h>
You can see your results but the problem here, is that you have an infinite loop. Yo need to check the \n, count and when you have a specific number, you leave from the loop.

GCC `scanf` segmentation fault

I was playing around with C and the scanf function and came across this weird error that I can't seem to figure out. Given the following code:
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int a;
} sample;
void fn(sample *s) {
char command;
scanf("%[abc]", &command);
printf("Read: %c\n", command);
printf("In the sample function:, %i\n", s->a);
}
int main() {
sample *s = malloc(sizeof(sample));
s->a = 4;
printf("Before sample function: %i\n", s->a);
fn(s);
printf("After sample function: %i\n", s->a);
return 0;
}
It seems to seg fault. With the output:
$ ./sample
Before sample function: 4
a
Read: a
In the sample function:, 4
Segmentation fault (core dumped)
I used gdb and attached a watch to the struct, it seems that inside the scanf function, it seems to 'modify' the struct? Which is weird, because even after the scanf inside the sample function 'fn', it is able to print out the struct fields fine. However, once returning from the fn and jumping back into main, it seg faults when it tries to print out the same information?
Interestingly, if you change the scanf to scanf("%c\n", &command); (without the character set) it seems to work fine. For the record, the version of gcc I am using is 4.7.2, and I am compiling the code with: gcc -O0 -o sample sample.c.
My only thought is that perhaps character sets aren't supported by gcc? I'm not sure. Just wondering if anyone else could clear this up?
scanf("%[abc]", &command);
writes a string not a single character. The trailing null character of the string is being written in &command + 1 in your program.
You should pass to scanf something like:
command with command being:
char command[2];

ncurses library compile error

I have looked at similar question on this site and googled why this is happening but have tried these solutionnot able to compile, difference between ncurses and curses, another compile error, but I am still having the problem with the error undefined reference to stdscr and wgetch the compiler is finding the library as far as I can figure out as I used find to locate the curses library and entered the full location in the #include line.
my code is as follows
#include <ncurses.h>
#include <stdio.h>
#include <string.h>
int first_line(char);
int main(){
char c = 0;
while((c = getch())!=EOF){
first_line(c);
}
return 0;
}
int first_line(char c){
if (c != '\n' && c != '\r'){
putchar(c);
do{
c = getch();
putchar(c);}
while( c !='\n');
}
else return 0;
return 0;
}
If you can point to what I've missed or am doing wrong I would much appreciate it.
ncurses.h is a header file (containing declarations), not a library (containing code that implements those declarations).
The error message you're seeing is from the linker, not from the compiler.
If you're using gcc, you need to add either -lcurses or -lncurses to the compiler command line (after your source file name).
For example, on my system I copied your source to c.c, and I can compile and link it using either
gcc c.c -o c -lcurses
or
gcc c.c -o c -lncurses

Resources