kernighan-and-ritchie

What does char * argv[] means?

冷暖自知 提交于 2019-12-02 20:53:12
I'm new to C programming, I encountered a problem. In case of complicated declarations i found this int *daytab[13]; // daytab is an array of 13 pointers to int which means daytab is the name of the array and the name of the array points to the first element of the array. The array name is not compatible with pointer manipulation like daytab++ etc (correct me if I'm wrong). But I found this code written in Dennis Ritchie main(int argc, char * argv[]) { while( --argc > 0 ) printf("%s%s",*++argv,(argc>1) > " " : ""); printf("\n"); return 0; } How can they manipulate argv ? Is it not the array

K&R C Programming Language 1.5.1 (File Copying) [duplicate]

旧巷老猫 提交于 2019-12-02 16:02:41
问题 This question already has answers here : Difference between int and char in getchar/fgetc and putchar/fputc? (2 answers) Closed 8 months ago . Well, i've read some months ago another "well know" C book(in my language), and i never learn't nothing about this. The way that K&R writes 3 chapters in 20 pages it's simply amazing, and of course that i can't expect huge explanations, but that also rises questions. I have a question about this point 1.5.1 The book says(pag 16): main(){ int c;// <--

I'm trying to understand the K&R's exercise 1-21 [duplicate]

流过昼夜 提交于 2019-12-02 14:23:01
This question already has an answer here: K&R answer book exercise 1.21 2 answers This is the question I am trying to understand: Write a program 'entab' that replaces strings of blanks by the minimum number of tabs and blanks to achieve the same spacing. When either a tab or a single blank would suffice to reach a tab stop, which should be given preference? Decoding the question: a . It's a program that injects 'tabs' in the input. b . If a string consists of continuous blank spaces, then these blank spaces has to be replaced with minimum number of tabs and spaces. How should the program

K&R C Programming Language 1.5.1 (File Copying) [duplicate]

帅比萌擦擦* 提交于 2019-12-02 09:32:24
This question already has an answer here: Difference between int and char in getchar/fgetc and putchar/fputc? 2 answers Well, i've read some months ago another "well know" C book(in my language), and i never learn't nothing about this. The way that K&R writes 3 chapters in 20 pages it's simply amazing, and of course that i can't expect huge explanations, but that also rises questions. I have a question about this point 1.5.1 The book says(pag 16): main(){ int c;// <-- Here is the question c=getchar(); while (c != EOF){ putchar(c); c = getchar(); } } [...] The type char is specifically meant

What is the purpose of this line? (Function declaration)

人走茶凉 提交于 2019-12-01 23:28:00
问题 I'm working through K & R to learn programming. Going well so far, but I'm unclear about the role of a line of code from section 1.8 (functions). In section 1.8, the authors show you how to create a function to raise one integer to the power of another integer. I've pasted the code below, as it was written in the book. Everything outputs fine. But I don't know why they've included this line at the top: int power(int m, int n); The book doesn't mention it, apart from saying that the program

How to not invoke warning: type specifier missing?

假装没事ソ 提交于 2019-12-01 19:56:29
I am reading " The C programming Language " by Brian W. Kernighan and Dennis M. Ritchie. In chapter 1.2 "Variables and Arithmetic Expressions" they demonstrate a simple Fahrenheit to Celsius converter program. When I compile the program (Terminal.app, macOS Sierra), I get this warning: $ cc FtoC.c -o FtoC.o FtoC.c:5:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int] main() ^ 1 warning generated. This is the C program: FtoC.c: 1 #include <stdio.h> 2 3 /* print Fahrenheit-Celsius table 4 for fahr = 0, 20, ..., 300 */ 5 main() 6 { 7 int fahr, celsius; 8 int lower, upper, step;

Error while writing strcat() using pointers

。_饼干妹妹 提交于 2019-12-01 13:01:21
I am trying to learn C with The C programming Language by K&R . I am trying to write a the strcat() program using pointers. char *strcat (char *s, char *t){ char *d; d = s; while(*s++); s--; while(*s++ = *t++); return d; } int main () { char *name1; char *name2; name1 = "stack" ; name2 = "overflow"; printf("%s %s\n", name1, name2); printf("strcat out : %s", strcat(name1, name2)); return 0; } But I am getting the ouput as stack overflow Segmentation fault Why is it not working ? Can anybody clarify the mistake here.. Because the pointers are pointers to literals, and not only are those read

EOF exercise 1-6 K&R The C programming language

不羁岁月 提交于 2019-11-30 23:48:22
This is taken directly from the K&R book: The precedence of != is higher than that of = , which means that in the absence of parentheses the relational test != would be done before the assignment = . So the statement c = getchar() != EOF is equivalent to c = (getchar() != EOF) This has the undesired effect of setting c to 0 or 1, depending on whether or not the call of getchar returned end of file. (More on this in Chapter 2.) Exercise 1-6. Verify that the expression getchar() != EOF is 0 or 1. I am having trouble understanding how to do this exercise as well as understanding what is going on

C Array Count (Beginner) [duplicate]

故事扮演 提交于 2019-11-30 20:06:18
问题 This question already has answers here : What's the real use of using n[c-'0']? (12 answers) Closed 6 years ago . I'm currently reading 'The C Programming Language' by Kernighan & Richie and I'm struggling to work out what a line does. I think I'm just being a little stupid, and not quite understanding their explanation. ++ndigit[c-'0']; I've had to change the program ever so slightly as ndigit was previously giving me garbage values, so I just instantiate the array to 0, instead of

Hex to Decimal conversion [K&R exercise]

时光怂恿深爱的人放手 提交于 2019-11-30 14:21:26
I'm learning C and I can't figure out one of the K&R exercises, the listing: Exercise 2-3, Write the function htoi(s) , which converts a string of hexadecimal digits (including an optional 0x or 0X ) into its equivalent integer value. The allowable digits are 0 through 9 , a through f and A through F . I suppose I need to do some recursion here, I just guess that I don't know a lot about the numbering types, and their various conversion methodologies, etc. Could someone give me some pointers on how best to understand it, I'm not looking for someone to hold my hand, but instead direct me to a