עבור לתוכן
View in the app

A better way to browse. Learn more.

HWzone

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

c|מבקש הסבר על קוד

Featured Replies

פורסם

הקוד הזה לא כל כך

מובן לי.ומזה אומר הוראת ה-* Define

ו-const .

אם אפשר הסבר על השורות בקוד.

כדי שזה יחדד לי את ההבנה.

תודה.

[CODE /* Demonstartes variables and constants */
#include <stdio.h>
/* Define a Constant to convert from punds to grams */
<M>#define<M> GRAMS_PER_POUND 454
/* Define a constant for the start of the next century */
const int NEXT_CENTURY = 2000;
/* Define the needed variables */
int weight_in_grams, weight_in_pounds;
int year_of_birth, int age_in_2000;

main()
{
/* Input data from user */

printf("Enter your Weight in pounds: ");
scanf("%d", &weight_in_pounds);
printf("Enter your year of birth: ");
scanf("%d", &year_of_birth);

/* Perform conversions */

weight_in_grams = weight_in_pounds *
GRAMS_PER_POUNDS;
age_in_2000 = NEXT_CENTURY - year_of_birth;

/* Display results on the screen */

printf("\nYour weoght in grams = %d", weight_in_grams);
printf("\nIn 2000 you will be %d years old", age_in_2000);
}]

פורסם

תנסה לתקן אותו שיופיע משמאל לימין ..

- - - תגובה אוחדה: - - -

ותבדיל בין הערות לבין הקוד עצמו ..

פורסם

הפקודה Define אומרת לקומפיילר להחליף מילה אחת באחרת לדוגמה:

#define zero 0

הפקודה אומרת שכל פעם שהקומפיילר נתקל במילה zero, הוא יחליף אותה ב-0 (הרי zero זו מילה, אין לה ייצוג בשפת c, אבל ל-0 יש משמעות).

const זה פשוט קבוע, זו צורת כתיב (אם תנסה במהלך התוכנית לשנות את המשתנה הזה, תקבל שגיאת קומפילציה כי לא היית אמור לשנות אותו וכנראה עשית טעות), בעיקרון יכולת להגדיר אותה כמשתנה גלובאלי int רגיל, ההבדל הוא ש-const לא אמור להשתנות.

בקוד שלך 454, זה מספר שאין סיכוי שישתנה... מדובר בכמות הגרמים בפאונד.

הוראת * אומרת שזה מצביע למשתנה כלשהו.

נערך על-ידי Dimka

פורסם
  • מחבר
תנסה לתקן אותו שיופיע משמאל לימין ..

- - - תגובה אוחדה: - - -

ותבדיל בין הערות לבין הקוד עצמו ..

איך עושים משמאל לימין?

ומה זאת תבדיל בין הערות לקוד עצמו?

פורסם

תנסה להכניס את הקוד לתוך תיבה ..

בעורך המתקדם יש לך אפשרות כזאת ..

פורסם
  • מחבר

יש לי שגיאת קופילציה בקוד הזה.

מהי השגיאה?.

error C2014: preprocessor command must start as first nonwhite space

/* Demonstartes variables and constants */      #include <stdio.h>
/* Define a Constant to convert from punds to grams */
<M>#define<M> GRAMS_PER_POUND 454
/* Define a constant for the start of the next century */
const int NEXT_CENTURY = 2000;
/* Define the needed variables */
int weight_in_grams, weight_in_pounds;
int year_of_birth, int age_in_2000;


int main()
{
/* Input data from user */


printf("Enter your Weight in pounds: ");
scanf("%d", &weight_in_pounds);
printf("Enter your year of birth: ");
scanf("%d", &year_of_birth);


/* Perform conversions */


weight_in_grams = weight_in_pounds *
GRAMS_PER_POUNDS;
age_in_2000 = NEXT_CENTURY - year_of_birth;


/* Display results on the screen */


printf("\nYour weoght in grams = %d", weight_in_grams);
printf("\nIn 2000 you will be %d years old", age_in_2000);
}

- - - תגובה אוחדה: - - -

נערך על-ידי eliom

פורסם

מאיפה הגיע ה-<M> מסביב ל-Define?

פורסם
  • מחבר
מאיפה הגיע ה-<M> מסביב ל-Define?

ככה רשום בספר שאני קורא.

פורסם

הנה, "ספר" טוב, שלב אחר שלב - מבוסס על *הספר* של דניס ריצ'י (מי שיצר את השפה).

http://www.howstuffworks.com/c.htm

אה ותמחק את התגיות כי זה לא חלק מהשפה (<M> ו-<M/>).

פורסם
  • מחבר

מחקתי את התגית (<M> ועדיין יש לי שגיאה בקוד.

פורסם

...ואתה מצפה שנדע באורח קסם מהי השגיאה, או איפה היא?

פורסם

סדרתי קצת את הקוד:


#include <stdio.h>
#define GRAMS_PER_POUNDS 454
const int NEXT_CENTURY = 2000;
int weight_in_grams, weight_in_pounds;
int year_of_birth, age_in_2000;




int main()
{
/* Input data from user */




printf("Enter your Weight in pounds: ");
scanf_s("%d", &weight_in_pounds);
printf("Enter your year of birth: ");
scanf_s("%d", &year_of_birth);




/* Perform conversions */




weight_in_grams = weight_in_pounds *
GRAMS_PER_POUNDS;
age_in_2000 = NEXT_CENTURY - year_of_birth;




/* Display results on the screen */




printf("\nYour weoght in grams = %d", weight_in_grams);
printf("\nIn 2000 you will be %d years old", age_in_2000);
return 0;
}

*אם פונקציית main מחזירה משהו אז תרשום ביטוי החזרה בסוף

*הייתה אי תאימות בין שם הגדרת המקרו לבין שם המקרו שניכתב בקוד

*הגדרת את השורה הזאת לא נכון :


int year_of_birth, int age_in_2000;

תוקן ל:


int year_of_birth, age_in_2000;

במידה ואתה לא משתמש בVS תשאיר את scanf במקום scanf_s(משום מה לא נתן לי לקמפל ולהריץ בלי לשנות)

*תקנו אותי במידת הצורך,אני לא מתכנת בC*

נערך על-ידי MrAlex

פורסם
  • מחבר

בסדר תיקנתי את הקוד.

לפי מה שאני יודע התוכנית ממירה את המשקל שלי בקילוגרם לגרמים.

אני הכנסתי 100 ק"ג וגם בגרמים יצא לי 100 איך זה יכול להיות.?

ומזה אומר השורה הזאת.

#define
GRAMS_PER_POUND (454)

בקוד הזה.

/*Credits to EmuleDanielFXP */
/* Demonstartes variables and constants */
#include<stdio.h>
/* Define a Constant to convert from punds to grams */
#define GRAMS_PER_POUND (454)

/* Define a constant for the start of the next century */

const int NEXT_CENTURY = 2000;

/* Define the needed variables */
int weight_in_grams, weight_in_pounds;
int year_of_birth, age_in_2000;

int main()
{

/* Input data from user */
printf("Enter your Weight in pounds: ");
scanf("%d", &weight_in_pounds);
printf("Enter your year of birth: ");
scanf("%d", &year_of_birth);

/* Perform conversions */
weight_in_grams = weight_in_pounds;
age_in_2000 = NEXT_CENTURY - year_of_birth;

/* Display results on the screen */
printf("\nYour weoght in grams = %d", weight_in_grams);
printf("\nIn 2000 you will be %d years old", age_in_2000);

return 0;
}


נערך על-ידי eliom

פורסם

שוב, define# מגדיר לקומפיילר, לפני שהוא עושה קומפילציה לקוד, לעבור על כל הטקסט ולעשות replace לכל מופע של GRAMS_PER_POUND במקרה שלך למספר 454. תחשוב שאתה יכול לעשות את זה ידנית - עם שימוש ב-define הרבה יותר קריא לדעת שזה GRAMS_PER_POUND ולא איזה מספר "קסם" כמו 454 שמישהו אחר לא ידע מה המספר הזה בכלל מייצג.

ארכיון

דיון זה הועבר לארכיון ולא ניתן להוסיף בו תגובות חדשות.

דיונים חדשים

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.