C While loop does not working [closed] - c

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
i'm beginner in C programming language, i have piece of code written in C language on linux Platform my code is :
#include <stdio.h>
int main(int argc, char* argv[]){
printf("Count Of Args %d \n",argc);
int i = 0;
while(i < argc){
printf("%s \n",argv[i]);
printf("loop N: %d \n",i);
i++;
}
return 0;
}
While loop does not working and i don't know why... please show me where is problem? thanks.

Works perfectly:
$ ./program abc def
Count Of Args 3
./program
loop N: 0
abc
loop N: 1
def
loop N: 2

Related

how can I covert int to char in C [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I'm trying to covert an int to char. Is there any way to do that?
For example:
{
int i;
char d;
i = 55;
d = i;
printf("%c\n", d);
}
How do I make d = 55?
If you want to put the number 55 into a string, use sprintf
Indeed your example can do what you want.
If you really want to place safe, you may:
d = (char) i;
Try this code segment:
printf("%d\n", d);
char are presented in the memory as binary format wich is equivalent to a number and this number is called a code ascii. when you print the code ascii with "%c" Then it will print the charchter equivalent to this code ascii

What is wrong with my code [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I have to write a program in c language and this is the code : The problem is that when I try to compile it it says : syntax error before return .Where is my error?
#include <stdio.h>
int main (void)
{
char i,c2,j;
int c=4;
i=j=3;
while (++i <=c)
{
int j=1;
printf("\n Nr1=%c Nr2=%d",64+i,c2);
} do;
return 1;
}
Remove the do from your code. Just while (++i <= c) { /* ... */ };
You are redeclaring j inside the while loop. Remove int j=1;
and remove the do; at the end of your while
There is no such thing as while ... do loop in C. There are while loops or do ... while loops.

Basic C programming exercise stuck [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I have been doing A LOT of exercises and examples to get the hang of C, its finally starting to sink in slowly, but surely. Though I'm having one small problem with this and can't amend it for the life of me:
#include "stdafx.h"
#include <stdio.h>
int _tmain(int argc, _TCHAR * argv[]) {
int i, grades[] = {98, 87, 92, 79, 85};
for(i = 0; i <= 4; ++i) {
printf("Element %d is %d\n"
i, grades[i]);
}
i = getchar();
return 0;
}
The problem is the i it is expecting a bracket, I don't know why and any amendment I have tried has failed, I know its a simple problem but it has me stuck.
Any help would be greatly appreciated.
The problem is that you're missing a comma that separates arguments in your call to printf:
printf("Element %d is %d\n", i, grades[i]);
// ^
// This one

don't know why the program is crashing [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I have written a code to find nth ugly number(a number which has at least one prime factor grater than 5) where n is a given input. my program runs well if the user inputs something less than 240. But if input gets bigger than that, program crashes!. My question is if it is a time consuming problem then it should take time but why the program crashes? I have used double everywhere so it might not be the matter of variable capacity!!
my code is below:
#include<stdio.h>
#include<math.h>
double primes[1000]={2,3,5};
int serial=3;
double next_prime()
{
double f=primes[serial-1]+2;
int count;
for(count=1;primes[count]<=(sqrt(f)+1) && count<serial;count++){
if(fmod(f,primes[count])==0){
f+=2;
count=1;
}
}
return primes[serial++]=f;
}
int main()
{
double ugly_serial=12,ugly_number=16,j;
int c,count,loop,input;
scanf("%d",&input);
while(ugly_serial<input)
{
loop=0;
for(c=3;primes[c-1]<=sqrt(ugly_number);c++){
j=next_prime();
}
for(count=3;count<c;count++){
if(fmod(ugly_number,primes[count])==0){
loop=1;
break;
}
}
if(loop==0){ugly_serial++;}
ugly_number++;
}
printf("%.0lf",ugly_number);
return 0;
}
I have compiled and run your code. The program works fine with all the input I have tried, including 56565.
Are you sure you are running the most recently compiled version of your program?

Can someone help me put my brain to rest "C Language" [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
Ok this is a simple C code but for some unknown reason the program refuse to compile and give segmentation fault 11 Please help me
#include <stdio.h>
typedef struct {
int P_answer[9];
int number;
} BOX;
int main()
{
BOX Matrix[8][8];
int i,j;
int k;
for(i=0;i<9;i++){
for(j=0;j<9;j++){
Matrix[i][j].number=0;
Matrix[i][j].P_answer[0]=1;
Matrix[i][j].P_answer[1]=2;
Matrix[i][j].P_answer[2]=3;
Matrix[i][j].P_answer[3]=4;
Matrix[i][j].P_answer[4]=5;
Matrix[i][j].P_answer[5]=6;
Matrix[i][j].P_answer[6]=7;
Matrix[i][j].P_answer[7]=8;
Matrix[i][j].P_answer[8]=9;
}
}
}
Matrix is an 8-by-8 array; each of your loops goes through 9 iterations.
The indexes of an array go from 0 to (size-1).
In your for-loops you go from 0 to size.
That's the reason of your segmentation fault.

Resources