Questions tagged [kr-c]

For questions about K&R C, the pre-standard language described in the first edition of "The C Programming Language" by Kernighan & Ritchie

This tag is for questions about the pre-standard C language as defined by the first version of "The C Programming Language" by Brian Kernighan and Dennis Ritchie, published in 1978.

The second edition of the book was published shortly before ratification of the first standard in 1990 and describes that version of the language (C90).

For questions about the book, use instead.

36 questions
5
votes
3 answers

I don't understand itoa() in K&R book

I am reading K&R; so far I'm doing well with it, but there is something in function itoa() which I don't understand. Here in itoa() they say they reverse the numbers themselves. For example 10 is 01 (they reverse the string): void itoa(int n, char…
user182502
  • 23
  • 3
4
votes
4 answers

Why the program compiles?

I am just trying to understand this C code ( not trying to achieve any functional goal by the program). This compiles using gcc. Is this main in main(int a, char *argv[] ) format? Is it permissible to declare anything beween argument and function…
qqqqq
  • 837
  • 12
  • 31
4
votes
3 answers

K and R exercise 1-24

I am doing programs in The C Programming Language by Kernighan and Ritchie. I am currently at exercise 1-24 that says: Write a program to check a C Program for rudimentary syntax errors like unbalanced parentheses, brackets and braces. Don't…
Sam
  • 1,842
  • 3
  • 19
  • 33
3
votes
4 answers

How to convert a K&R function declaration to an ANSI function declaration automatically?

// K&R syntax int foo(a, p) int a; char *p; { return 0; } // ANSI syntax int foo(int a, char *p) { return 0; } As you see, in K&R style, the types of variables are declared in new lines instead of in the braces. How to convert a…
user1038013
  • 83
  • 1
  • 5
3
votes
4 answers

K&R Exercise 2-3 "Hex to int converter" Problem

The program I wrote works in demographics consisting of only single Hexadecimal values. (Probably not the most elegant solution, but I'm a new programmer) My question is, how would I go about handling of multiple hexadecimal digits, such as 0xAF,…
F0D
3
votes
1 answer

Learning C (via K&R) using xcode

I'm learning C with The C Programming Language (K&R). Since I don't particularly want to bob back and forth between a text editor and running gcc, I've decided to use xcode as an IDE. So far, I've been able to follow the book's examples without a…
deeb
  • 1,332
  • 4
  • 15
  • 27
3
votes
1 answer

What does this weird C++ definition mean?

I have this strange function definition in my homework code and I don't really know what it's supposed to mean. char * sh_single_quote (string) char *string; {...} Especially the "char *string;" line, what with the semicolon at the end.
3
votes
5 answers

Writing into c-string

my code segfaults and I don't know why. 1 #include 2 3 void overwrite(char str[], char x) { 4 int i; 5 for (i = 0; str[i] != '\0'; i++) 6 str[i] = x; 7 } 8 9 int main(void) { 10 char *s = "abcde"; 11 char x =…
Martin
  • 1,473
  • 2
  • 12
  • 20
2
votes
2 answers

Exercise 1-24 from K&R - Rudimentary Syntax Checking

The exercise reads "Write a program to check a C program for rudimentary syntax errors like unbalanced parentheses, brackets, and braces. Don't forget about quotes, both single and double, escape sequences, and comments." I chose to go about solving…
2
votes
8 answers

Help with custom getline() function

Can anyone explain to me why this isn't working? #include #include char *getline(int lim) { char c; int i; char *line; line = malloc(sizeof(char) * lim); i = 0; while((c = getchar()) != '\n' && c !=…
Tyler
  • 4,679
  • 12
  • 41
  • 60
2
votes
3 answers

What has changed since “The C Programming Language”

My experience in C is mostly from second edition of The C Programming language which is a very old book. What has changed in C since it was released, what obsolete or deprecated functions should I avoid?
Iceland_jack
  • 6,848
  • 7
  • 37
  • 46
2
votes
3 answers

unix Read & Write function

/* Low Level I/O - Read and Write Chapter 8 - The C Programming Language - K&R Header file in the original code is "syscalls.h" Also BUFSIZ is supposed to be defined in the same header file */ #include #include #include…
user1547436
1
vote
0 answers

Decay rules in C

In C, a function automatically decays to a pointer to function, and an array to a pointer to its first element. However, structs and unions don't decay to pointers to themselves. My question is: Why decay rules were designed like this? I mainly (but…
nalzok
  • 14,965
  • 21
  • 72
  • 139
1
vote
1 answer

Discover if structure initialization doesn't modify all members

Consider the following code: typedef struct _sMYSTRUCT_BASE { int b_a; int b_b; int b_c; } sMYSTRUCT_BASE; typedef struct _sMYSTRUCT { sMYSTRUCT_BASE base; int a; int b; } sMYSTRUCT; Private const…
user2448122
  • 195
  • 12
1
vote
1 answer

Mixing data types results in heart output

I was fooling around with one of the sample programs in the K&R, and found that this #include main() { double nc; for (nc = 0; getchar() != EOF; ++nc) ; printf("%lf\n", nc ); putchar(nc); } produces output that…
Laura
  • 523
  • 4
  • 12