14 Replies - 2311 Views - Last Post: 20 August 2012 - 07:49 AM Rate Topic: -----

#1 karank6195  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 17-August 12

Something unsual happened while running 'C'.

Posted 17 August 2012 - 03:22 AM

I am new to programming and I was just practicing 'C' and I wrote a code and ran it,then while executing,it alarmed.Can you please tell me what was wrong?

My code:-


#include<stdio.h>
#include<stdlib.h>
int main()
{

 for (int a = 1 ; a <= 31 ; a += 1)

    printf("%i\n",a);

for (char b ; 'enum (Monday,Tuesday,Wednesday,Thursday,Friday,Satursay)';b+=1)
    printf("%c\n",B );
    return 0;

}


MOD EDIT: Added code tags. When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 17 August 2012 - 04:02 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Something unsual happened while running 'C'.

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3616
  • View blog
  • Posts: 11,263
  • Joined: 05-May 12

Re: Something unsual happened while running 'C'.

Posted 17 August 2012 - 03:42 AM

At some point b in your second for loop became equal to 7 which is the BELL character. This is probably what you heard as the alarm.

To learn more about the ASCII characters see: http://www.asciitable.com/
Was This Post Helpful? 1
  • +
  • -

#3 karank6195  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 17-August 12

Re: Something unsual happened while running 'C'.

Posted 17 August 2012 - 03:57 AM

thanks!
Is it normal?
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6075
  • View blog
  • Posts: 23,540
  • Joined: 23-August 08

Re: Something unsual happened while running 'C'.

Posted 17 August 2012 - 04:03 AM

Wen you post code...USE CODE TAGS!!!

:code:

Please explain what you are trying to do here.

for (char b ; 'enum (Monday,Tuesday,Wednesday,Thursday,Friday,Satursay)';b+=1)

Was This Post Helpful? 0
  • +
  • -

#5 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2128
  • View blog
  • Posts: 3,260
  • Joined: 21-June 11

Re: Something unsual happened while running 'C'.

Posted 17 August 2012 - 06:56 AM

View Postkarank6195, on 17 August 2012 - 12:57 PM, said:

Is it normal?


It is normal that printing the bell character will produce some sort of bell-sound, yes.

Is it normal that your loop will print the bell character? Well, you're using b uninitialized, so the behavior of your program is undefined. That said, it's not entirely unexpected that the value of b will eventually reach 7.

If you changed char b to char b=0, the behavior would be defined (assuming char is signed on your platform - otherwise it would be undefined on account of signed overflow) and it would definitely print the bell character (an infinite number of times, because the loop will repeat forever since your condition is always true).

Do note that the value of multi-character character-literals is unspecified and that your loop is equivalent to for(char b; true; b+=1).
Was This Post Helpful? 1
  • +
  • -

#6 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 810
  • View blog
  • Posts: 2,405
  • Joined: 20-March 10

Re: Something unsual happened while running 'C'.

Posted 17 August 2012 - 07:48 AM

Shit I pressed the wrong button
I meant .+1 that reply

Mods please fix
Was This Post Helpful? 0
  • +
  • -

#7 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Something unsual happened while running 'C'.

Posted 17 August 2012 - 08:21 AM

The question is: What was the expected behavior of your program?

The syntax for a for-loop is in general:

for(initialize; while-condition; increment)
{
body }


So for the initialize step you generally have something like:

char b = 'a'; or int i = 0; -- the variable that is initialized is generally going to be used in the condition (though some times indirectly) and is generally changed in the "increment" step or body.

you just have: char b; which does not "initialize" anything.

For the condition one generally sees: i < 10 or b != 0 -- you have:
'enum (Monday,Tuesday,Wednesday,Thursday,Friday,Satursay)' which I assume the compiler sees as a constant value... if it does not give you an error.

for the increment you have b+=1 which is expected...

EXCEPT -- nothing in the increment or body changes the value of the condition and therefore this would be an infinite loop.

Were you trying to print the days of the week?
#include<stdio.h>
#include<stdlib.h>
const char* WeekDays[] = {
    "Monday",
    "Tuesday",
    "Wednesday",
    "Thursday",
    "Friday",
    "Saturday",
    "Sunday"
};

typedef enum {
    DAY_MONDAY,         //DAY_MONDAY = 0
    DAY_TUESDAY,        //DAY_TUESDAY = 1
    DAY_WEDNESDAY,
    DAY_THURSDAY,
    DAY_FRIDAY,
    DAY_SATURDAY,
    DAY_SUNDAY,         //DAY_SUNDAY = 6
    DAY_COUNT, //== Counts the number of items above...
} DaysOfWeek;

int main() {
    int a, day;
   
    for (a = 1 ; a <= 31 ; ++a) {
       printf("%i\n",a);
    }

    for (day =  DAY_MONDAY; day < DAY_COUNT; ++day) {
        printf("%s\n", WeekDays[day]);
    }
    return 0;
}

This post has been edited by NickDMax: 17 August 2012 - 08:22 AM

Was This Post Helpful? 1
  • +
  • -

#8 karank6195  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 17-August 12

Re: Something unsual happened while running 'C'.

Posted 19 August 2012 - 04:41 AM

<b>As I've said I'm new to programming,I was just trying to get the output like this:-</b>

1 Monday
2 Tuesday
3 Wednesday
.
.
.
.
31 (whatever the day comes)
Was This Post Helpful? 0
  • +
  • -

#9 Hezekiah  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 208
  • View blog
  • Posts: 552
  • Joined: 12-July 09

Re: Something unsual happened while running 'C'.

Posted 19 August 2012 - 06:12 AM

In that case you only want one loop going from 1 to 31. I would also recommend using an array to store the days:
const char* days[] = {
    "Monday",
    "Tuesday",
    "Wednesday",
    "Thursday",
    "Friday",
    "Saturday",
    "Sunday"
};


You can use the % operator to calculate the array index from the day number. It returns the remainder when the left operand is divided by the right operand:
8 % 3 == 2

Was This Post Helpful? 0
  • +
  • -

#10 karank6195  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 17-August 12

Re: Something unsual happened while running 'C'.

Posted 19 August 2012 - 10:21 AM

@NickDMax:- Thanks but your program displayed the numbers first, then right below it, it displayed the days.
I rather want the output like this:-

1 Monday
2 Tuesday
3 Wednesday
.
.
.
.
31 (whatever the day comes)
Was This Post Helpful? 0
  • +
  • -

#11 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 810
  • View blog
  • Posts: 2,405
  • Joined: 20-March 10

Re: Something unsual happened while running 'C'.

Posted 19 August 2012 - 02:08 PM

You really have to try harder..

One way of doing this (pretty ugly way too if you ask me)

adapting Nick's code to this..
#include<stdio.h>
#include<stdlib.h>
const char* WeekDays[] = {
    "Monday",
    "Tuesday",
    "Wednesday",
    "Thursday",
    "Friday",
    "Saturday",
    "Sunday"
};

typedef enum {
    DAY_MONDAY,         //DAY_MONDAY = 0
    DAY_TUESDAY,        //DAY_TUESDAY = 1
    DAY_WEDNESDAY,
    DAY_THURSDAY,
    DAY_FRIDAY,
    DAY_SATURDAY,
    DAY_SUNDAY,         //DAY_SUNDAY = 6

} DaysOfWeek;

int main() {
    int day,a;
    int start =1;
    int end =31;
    int flag = 1;
    int counter =0;
    for (day = start; day <= end; day++)
     {
       a = day-1;
       printf("%i ",day);

       if(a > 6)
       {
         a = day - (7*flag)-1;
       }
       if (day >=14 && counter ==0)
       {
         flag++;
         counter++;
       }
       if (day >=21 && counter ==1)
       {
         flag++;
         counter++;
       }
       if (day >=28 && counter ==2)
       {
         flag++;
         counter++;
       }
       printf("%s\n", WeekDays[a]);

     }
    return 0;
}



Nick probably knows a better way but hey it works and beggars can't be choosers..

Snoopy.
Was This Post Helpful? 2
  • +
  • -

#12 #define  Icon User is online

  • Duke of Err
  • member icon

Reputation: 1368
  • View blog
  • Posts: 4,732
  • Joined: 19-February 09

Re: Something unsual happened while running 'C'.

Posted 19 August 2012 - 03:22 PM

Prettiness is in the eye of the beholder.

Day can be separated into day of the week (dow) and day of the month (dom). There are four or five weeks in a month, so a for loop could be used for each week.


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

const char* WeekDays[] = {
    "Monday",
    "Tuesday",
    "Wednesday",
    "Thursday",
    "Friday",
    "Saturday",
    "Sunday"
};

typedef enum {
    DAY_MONDAY,         //DAY_MONDAY = 0
    DAY_TUESDAY,        //DAY_TUESDAY = 1
    DAY_WEDNESDAY,
    DAY_THURSDAY,
    DAY_FRIDAY,
    DAY_SATURDAY,
    DAY_SUNDAY,         //DAY_SUNDAY = 6
    DAY_COUNT
} DaysOfWeek;


int main()
{
  int month_start = 1;
  int month_end   = 31;

  int dow;
  int dom;

  dom = month_start;
  dow = DAY_MONDAY;
  for (   ; dow < DAY_COUNT; dow++) {
    printf("%2i %s\n", dom++, WeekDays[dow]);
  }

  for (dow = DAY_MONDAY; dow < DAY_COUNT; dow++) {
    printf("%2i %s\n", dom++, WeekDays[dow]);
  }

  for (dow = DAY_MONDAY; dow < DAY_COUNT; dow++) {
    printf("%2i %s\n", dom++, WeekDays[dow]);
  }

  for (dow = DAY_MONDAY; dow < DAY_COUNT && dom <= month_end; dow++) {
    printf("%2i %s\n", dom++, WeekDays[dow]);
  }

  for (dow = DAY_MONDAY; dow < DAY_COUNT && dom <= month_end; dow++) {
    printf("%2i %s\n", dom++, WeekDays[dow]);
  }

  /* getchar(); */

  return (0);
}


Was This Post Helpful? 1
  • +
  • -

#13 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 810
  • View blog
  • Posts: 2,405
  • Joined: 20-March 10

Re: Something unsual happened while running 'C'.

Posted 19 August 2012 - 03:52 PM

Well there you go another way..

Proves my point though...

Snoopy.
Was This Post Helpful? 0
  • +
  • -

#14 karank6195  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 17-August 12

Re: Something unsual happened while running 'C'.

Posted 19 August 2012 - 10:54 PM

:^:
-.-
Yeah I've got it or I got it .I don't know which one is correct.
Anyway thanks a lot :tup:
You all are very great programmers.


:rolleyes2:
Was This Post Helpful? 1
  • +
  • -

#15 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Something unsual happened while running 'C'.

Posted 20 August 2012 - 07:49 AM

I am glad you got the answer. I hope your solution was a little more elegant than the ones proffered above.

The key to the way I would do it was give to you by Hezekiah and #define.

Use a printf like #define but use the mod operator as described by Hezekiah.
0 % 7 = 0
1 % 7 = 1
..
6 % 7 = 6
7 % 7 = 0
8 % 7 = 8 - n7 where int(8/7) = 1 so 8%7 = 1
14% 7 = 0
N % 7 = N - int(N/7)*7 = the remainder of N/7

so dayOfMonth % 7 is a number 0-6. If we use a little math and logic to map these values to array indexes we can do the whole thing in 1 little loop!

This post has been edited by NickDMax: 20 August 2012 - 07:50 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1