The induction argument, writing out on the screen, structure and base

Welcome.
I firmly beginners every one can see;) I wrote a few selected programs in ANSI C and would like to find out how I can improve these programs, they make mistakes, and how I should write them correctly.
Regards

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
 1. #include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main(void){
	FILE *data = NULL;
	char fname[16];
	char sname[16];
	int i = 0;

	data = fopen("data.txt", "r");
	if (in == NULL){
		printf("ERROR\n");
		system("pause");
	}

	fscanf(data, "%s", &fname);
	fscanf(data, "%s", &sname);
	


	for (i = 0; i < strlen(fname); i++){
		printf("%c\n", fname[i]);
	}

	printf("\n");

	for (i = 0; i < strlen(sname); i++){
		printf("%c\n", sname[i]);
	}

	system("pause");
	return 0;
}

2. #include <stdio.h>
#include <stdlib.h>
#include <math.h>

typedef struct person{
	char name[16];
	char lname[16];
	int age;

}person;

int main(void)//
{
	person tab[2];
	char buf[64];

	int i;
	for (i = 0; i < 2; i++){
		printf("enter the name: ");

		fscanf_s(stdin, " %s", tab[i].name);
		printf("etner the last name: ");
		fscanf_s(stdin, " %s", tab[i].lname);
		printf("enter the age of: ");
		scanf_s(" %d", tab[i].age);
		getchar();
	}

	for (i = 0; i < 2; i++){
		printf("%s %s is %d years old.\n", tab[i].name, tab[i].lastname, tab[i].age);
		
	}



	system("pause");
	return 0;
}

3. #include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define _PI 3.14
 
 
int main ( int argc, char* argv[] )
{
 float r;
 
 if(argc=!4){
         printf("ERROR\n");
         system("pause");
         exit(1);
 }
 
 
  a = atof( argv[1] );
  b = atof( argv[2] );
  c = atof( argv[3] );
 
  printf( "Circle of radius %d has an area equal to %f and the circumference equal to %f", r, r*r*_PI, 2*r*_PI);
  return 0;
}

4. #include<stdio.h>
#include<stdlib.h>
 
typedef struct
{
int a;
int h;
}triangle;
 
int main(void)
{
 
        triangle OurTriangle={0};
        printf("Specify the length of the base: ");
        scanf("%d", &OurTriangle.a);
        printf("enter the amount: ");
        scanf("%d", &OurTriangle.h);
        printf("Pole wynosi: %f \n", (float)OurTriangle.a*OurTriangle.h*0.5);
 
 
 
 
 
system("pause");
return 0;
}
> I wrote a few selected programs in ANSI C

Prefer adherence to a more current C standard. ISO C99 is now almost universally supported. (Microsoft being the one notorious exception.)

Here is a rewrite of the first program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int main(void) {

    /* prefer initialising a variable at (or close to) its point of definition */
    /* http://stackoverflow.com/questions/6429460/should-one-keenly-consciously-attempt-to-postpone-variable-definitions-as-long#6429591 */
	FILE *data = fopen( "data.txt", "r" ) ;
	if( data == NULL ) {
        puts( "error opening file" ) ;
        return EXIT_FAILURE ;
	}

    /* avoid magic numbers */
    /* https://en.wikipedia.org/wiki/Magic_number_(programming)#Unnamed_numerical_constants */
    enum { MAX_CHARS_IN_NAME = 15, BUFFER_SZ = MAX_CHARS_IN_NAME + 1 };

    /* prepare the format string */
    /* http://crasseux.com/books/ctutorial/String-overflows-with-scanf.html */
    char format[32] ;
    sprintf( format, "%%%us", MAX_CHARS_IN_NAME ) ;

    char fname[BUFFER_SZ] ;
    int nread = fscanf( data, format, fname ) ;
    if( nread == -1 ) {
        puts( "error reading first name" ) ;
        return EXIT_FAILURE ;
    }

    /* skip characters remaining in the first input field (if any)
       after the first MAX_CHARS_IN_NAME characters  */
    int c ;
    while( ( c = fgetc(data) ) != EOF && !isspace(c) ) ;

    char sname[BUFFER_SZ] ;
    nread = fscanf( data, format, sname ) ;
    if( nread == -1 ) {
        puts( "error reading second name" ) ;
        return EXIT_FAILURE ;
    }

    puts(fname) ;
    puts(sname) ;
}


Programming in C is hard.
Topic archived. No new replies allowed.