I thought it would be really practical to use the int voter_count from the main function also in my print_winners function. But since the int voter_count is not introduced before the main function as the int candidates_count is, for example, I am not able to use the voter_count int.
When I introduce the int before main and remove the int before voters_count in the main function when it is getting called, then my code works and all scenarios I tried worked correctly.
But I know that we were not supposed to change code in the main function and even with the changed main function my code still does not pass the check50.
Does anyone know why my code is not passing the check50?
void print_winner(void)
{
for (int c = voter_count; (c > 0); c--)
{
int u = 0;
for (int j = 0; (j < candidate_count); j++)
{
if (candidates[j].votes == c)
{
u++;
printf("%s \n", candidates[j].name);
}
}
if (u != 0)
{
return;
}
}
}
Response for check:
voter_count = get_int("Number of voters: ");
Here I changed the main function by removing the int before the voter_count because
//Numver of votes
int voter_count;
I introduce the program to the int above the header file.
Functions in C can use either global variables (but please don't), local variables, or their arguments. A local variable in one function cannot be accessed in another.
E.g.
void foo(void);
int main(void) {
int bar = 42;
foo();
return 0;
}
void foo(void) {
printf("%d\n", bar);
}
That will not work. However, we can pass bar as an argument to foo.
void foo(int bar);
int main(void) {
int bar = 42;
foo(bar);
return 0;
}
void foo(int bar) {
printf("%d\n", bar);
}
The following will work, but you shouldn't use it.
int bar;
void foo(void);
int main(void) {
bar = 42;
foo();
return 0;
}
void foo(void) {
printf("%d\n", bar);
}
Related
Something like
int main(void){
doTenTimes({
printf("Hello World");
});
}
And then the function doTenTimes could execute that block of code 10 times.
You probably don't want to "pass a block of code", but rather to implement the function to suit whatever you need to do:
void doManyTimes(int n)
{
for(int i=0; i<n; i++)
printf("Hello World\n");
}
Alternatively, you could pass a function pointer pointing at a different function, to provide the behavior separately:
typedef void dofunc_t (void);
void print (void)
{
printf("Hello World\n");
}
void doManyTimes(dofunc_t* func, int n)
{
for(int i=0; i<n; i++)
func();
}
// call:
doManyTimes(print,10);
You really can't treat "naked" blocks of code like data in C. Lundin's approach is the best for what you want to do.
However, for completeness' sake, here's an example using a macro (FOR THE LOVE OF GOD NEVER DO THIS):
#include <stdio.h>
/**
* Wrapping in a do...while(0) keeps the compiler from bitching at
* me over the trailing semicolon when I invoke the macro in main.
*/
#define doTenTimes(STMT) do { for (size_t i = 0; i < 10; i++ ) STMT } while( 0 )
int main( void )
{
doTenTimes( { printf( "hello, world\n" ); } );
return 0;
}
After preprocessing we get this:
int main( void )
{
do { for (size_t i = 0; i < 10; i++ ) {printf( "hello, world\n" ); } } while (0);
return 0;
}
It "works", but it's super ugly and made me itch as I wrote it. Don't ever do anything like this.
#include <stdio.h>
void hello(void) {
puts("Hello, World!");
}
void byebye(void) {
puts("Good bye, World!");
}
int main(void) {
void (*work)(void) = hello;
for (int i = 0; i < 10; i++) work();
work = byebye;
for (int i = 0; i < 10; i++) work();
return 0;
}
See code running at https://ideone.com/ivsxxR
#include <stdio.h>
int main(void) {
int list_size;
printf("size of the array:");
scanf("%d", &list_size);
int list[5];
for (int i = 0; i < list_size; i++) {
scanf("%d", &list[i]);
}
printArray(list, 5);
return 0;
}
void printArray(int list[], int list_size){
for (int j = 0; j < list_size; j++) {
printf("%d ", list[j]);
}
}
Error C2371 : 'printArray' : Override, the default format is different. How can I change the code?
Is the array declaration wrong?
I have to make function 'printArray', so I can't put code into main function.
For starters you are not using the variable list_size in the array declaration
scanf("%d", &list_size);
int list[5];
It seems you mean
int list[list_size];
Correspondingly the function printArray should be called like
printArray(list, list_size);
You have to declare the function printArray before its call for example before main
void printArray(int list[], int list_size);
int main( void )
{
//...
}
As the function does not change the passed array then the first parameter should be declared with the qualifier const.
void printArray(const int list[], int list_size);
int main( void )
{
//...
}
And the function can be defined like
void printArray( const int list[], int list_size ) {
for (int j = 0; j < list_size; j++) {
printf("%d ", list[j]);
}
putchar( '\n' );
}
Pay attention to that this declaration
int list[list_size];
declares a variable length array. You need to set a corresponding option of the compiler to compile your program according to the C99 Standard.
Otherwise you could define some large enough constant for the size of the array. For example
int main( void )
{
enum { N = 100 };
int list[N];
int list_size;
printf( "size of the array (not greater than %d): ", N );
if ( scanf( "%d", &list_size ) != 1 || N < list_size ) list_size = N;
//...
Like this
/* function declaration or prototype */
void printArray(int list[], int list_size);
int main(void) {
...
}
void printArray(int list[], int list_size) {
...
}
You have to declare functions before you use them. Function declarations are also called prototypes.
You should declarate the function after the headers files if you make the function after the principal program
#include <stdio.h>
void printArray(int list[], int list_size);
int main(void)
{
int list_size;
do
{
printf("size of the array:");
scanf("%d", &list_size);
}
int list[list_size];
for (int i = 0; i < list_size; i++)
{
scanf("%d", &list[i]);
}
printf("\nDisplay of array :\n");
for (int i = 0; i < list_size; i++)
{
printf("%d", list[i]);
}
printf("\n\n");
printArray(list, list_size);
return 0;
}
void printArray(int list[], int list_size)
{
printf("\nDisplay of array :\n");
for (int j = 0; j < list_size; j++)
{
printf("%d ", list[j]);
}
}
This code print 2 time the output of array,if you want to display the array 1 time you should remove this foor loop :
printf("\nDisplay of array :\n");
for (int i = 0; i < list_size; i++)
{
printf("%d", list[i]);
}
C is parsed without looking ahead. You could write a single-pass C compiler (not the most useful thing, but the language was designed with this in mind). When the compiler sees the call to printArray, i.e. the line printArray(list, 5);, it doesn't know yet what printArray's real declaration is - it hasn't read it yet. But it has to do something with that call, and instead it makes a guess: the compiler implicitly declares, at that moment, the function as-if you had the following declaration:
int printArray();
In C, this declaration really means
int printArray(...);
And thus, the call to printArray is generated with such an implicit declaration - the function is called as if it was a variadic function. But it isn't a variadic function. You later attempt to define it in an incompatible way, as:
void printArray(int [], int);
This is not allowed and triggers an error: in C, all the declarations must agree with the definition. C allows multiple declarations - they are not an error, as long as they all agree. In your case they didn't: the implicit one didn't match the explicit one that came later.
There are two ways of fixing this error:
Declare void printArray(int list[], int size); before the point of use, e.g. before main. You can still define it later - after main.
Change the signature of printArray to match what the compiler implied, i.e. printArray(...) { ... }. The only problem is: such a signature would be useless. You can pass whatever arguments you want into that function, but there's no way to actually use them from inside the function. That variadic argument mechanism of C requires at least one named argument. So no luck there. A minimum viable printArray function with variadic arguments could look as follows - and of course then it's not defined as printArray(...) anymore, so the compiler would raise the error you see due to the mismatch of implicit declaration and definition, and you're back to square one.
void printArray(int list[], ...)
{
va_list args;
va_start(args, list);
int count = va_arg(args, int);
for (int i = 0; i < count; ++i) printf("%d ", list[i]);
printf("\n");
va_end(args);
}
The problem is in va_start: you have to pass it the first named argument to the function. With a signature like printArray(...), there are no named arguments, and thus you can't use va_start, and there's no way of accessing any arguments passed to such function from the inside of it - not while depending on Standard (portable) C. On various architectures there are non-portable "hacks" to bypass this issue, but they are not recommended at all.
Note: If you want to declare a function with no arguments in C, it should be declared as:
int myFunction(void); /* <- like this, not like -> */ int myFunction();
To add to the potential confusion, C++ sees things differently: there, the int foo(void) is redundant, since it interprets int foo() as a function taking no parameters, and not as int foo(...) like C would.
You could've declared your function before the main() one. As for the rest, I don't see the problem with your function.
#include <stdio.h>
void printArray(int list[], int list_size)
{
for (int j = 0; j < list_size; j++)
{
printf("%d ", list[j]);
}
}
int main(void)
{
int list_size;
printf("size of the array:");
scanf("%d", &list_size);
int list[list_size];
for (int i = 0; i < list_size; i++)
{
scanf("%d", &list[i]);
}
printArray(list, list_size);
return 0;
}
In my code I often have a for loop for doing a single operation n times. E.g:
// Wait for settle
int delayLoop = 0;
int count = 0;
for(delayLoop = 0; delayLoop < count; delayLoop++) {
__NOP(); // operation to do
}
At first I wanted to make this as an function....but then I realized I do not know how to pass in the operations as a function argument.
In the above example the __NOP() is itself a macro that expands to:
__ASM volatile ("nop")
So how can I come up with a macro that I can call like this:
DO_LOOP(10, __NOP)
and what if I need to do more operations? e.g.
DO_LOOP(8, __NOP, myFunction(arg1))
that would expand to:
for(int i = 0; i < 8; i++) {
__NOP;
myFunction(arg1);
}
#define DO_LOOP(x, ...) for (int i = 0; i < x; ++i) { __VA_ARGS__; }
void f1() { printf("test\n"); }
void f2() { printf("a\n"); }
int main()
{
DO_LOOP(10, f1(), f2());
return 0;
}
gcc -E test.c:
void f1() { printf("test\n"); }
void f2() { printf("a\n"); }
int main()
{
for (int i = 0; i < 10; ++i) { f1(), f2(); };
return 0;
}
This doesn't work with inline assembly though. You can do something like:
#define DO2(x, a, b) for (int i = 0; i < x; ++i) { a; b;}
and use:
DO2(10, __NOP(), f1());
So I'm getting this message when I try to load my array using pointers.
I don't know why this keep appearing since the last program had no problem
#include<stdio.h>
#define T 10
void FLoad(int *);
void main () {
int a[T];
void FLoad(a);
}
void FLoad(int *a) {
int x;
for (x = 0; x < T; x++)
scanf("%d", a+x);
}
And here is a little program that works perfectly
#include <stdio.h>
void FImp(int *, int );
main () {
int a[] = {-10,-5,3,4}, tam;
tam = sizeof(a) / sizeof(int);
FImp(a, tam);
}
void FImp(int *a, int t) {
int x;
for (x = 0; x < t; x++)
printf("%d ",*(a + x));
putchar('\n');
}
You are using incorrect syntax when calling your function
void main()
{
int a[T];
void FLoad(a);
}
should be
void main()
{
int a[T];
FLoad(a);
}
or even better
int main(void)
{
int a[T];
FLoad(a);
}
You don't specify the function return value when you call it.
void FLoad(a);
This won't call the function. The compiler will consider this as a function declaration. So call the function without void it will work fine.
I want to call a particular function according to the value i passed to the macro. But it is giving me compilation error
#include <stdio.h>
#define calling(m, j) execcall ## m(j);
void execcall0 (int x) {
printf("called 0 with arg %d\n", x);
}
void execcall1 (int x) {
printf("called 1 with arg %d\n", x);
}
void execcall2 (int x) {
printf("called 2 with arg %d\n", x);
}
int main () {
int i = 0;
for (i = 0; i < 3; i++) {
calling(i, 1);
}
}
Compilation error:
In function `main':
new.c:(.text+0x7a): undefined reference to `execcalli'
collect2: ld returned 1 exit status
Is it even possible whatever i am trying?
If you want to call a function based on the value of an integer you're better off writing an array of pointers to your functions, and indexing into the array using your integer.
void execcall0(int x);
void execcall1(int x);
void execcall2(int x);
/* Array of pointers to void functions taking an int parameter. */
void (*apfn[])(int) =
{
execcall0,
execcall1,
execcall2,
};
int main()
{
int i;
for (i = 0; i < 3; ++i) {
(apfn[i])(1);
}
}
Remember to check the boundary conditions before you index!
You can use an array to store functions, and modify the macro:
#include <stdio.h>
#define calling(m, j) exec_funcs[m](j)
void execcall0 (int x) {
printf("called 0 with arg %d\n", x);
}
void execcall1 (int x) {
printf("called 1 with arg %d\n", x);
}
void execcall2 (int x) {
printf("called 2 with arg %d\n", x);
}
void (*exec_funcs[3])(int) = { execcall0, execcall1, execcall2 };
int main () {
int i = 0;
for (i = 0; i < 3; i++) {
calling(i, 1);
}
}
But then you don't really need a macro.