פורסם 2014 באפריל 1911 שנים הקוד הזה לא כל כךמובן לי.ומזה אומר הוראת ה-* 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);}]
פורסם 2014 באפריל 1911 שנים תנסה לתקן אותו שיופיע משמאל לימין ..- - - תגובה אוחדה: - - -ותבדיל בין הערות לבין הקוד עצמו ..
פורסם 2014 באפריל 1911 שנים הפקודה Define אומרת לקומפיילר להחליף מילה אחת באחרת לדוגמה:#define zero 0הפקודה אומרת שכל פעם שהקומפיילר נתקל במילה zero, הוא יחליף אותה ב-0 (הרי zero זו מילה, אין לה ייצוג בשפת c, אבל ל-0 יש משמעות).const זה פשוט קבוע, זו צורת כתיב (אם תנסה במהלך התוכנית לשנות את המשתנה הזה, תקבל שגיאת קומפילציה כי לא היית אמור לשנות אותו וכנראה עשית טעות), בעיקרון יכולת להגדיר אותה כמשתנה גלובאלי int רגיל, ההבדל הוא ש-const לא אמור להשתנות.בקוד שלך 454, זה מספר שאין סיכוי שישתנה... מדובר בכמות הגרמים בפאונד.הוראת * אומרת שזה מצביע למשתנה כלשהו. נערך 2014 באפריל 1911 שנים על-ידי Dimka
פורסם 2014 באפריל 1911 שנים מחבר תנסה לתקן אותו שיופיע משמאל לימין ..- - - תגובה אוחדה: - - -ותבדיל בין הערות לבין הקוד עצמו ..איך עושים משמאל לימין?ומה זאת תבדיל בין הערות לקוד עצמו?
פורסם 2014 באפריל 1911 שנים מחבר יש לי שגיאת קופילציה בקוד הזה.מהי השגיאה?.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);} - - - תגובה אוחדה: - - - נערך 2014 באפריל 1911 שנים על-ידי eliom
פורסם 2014 באפריל 2011 שנים הנה, "ספר" טוב, שלב אחר שלב - מבוסס על *הספר* של דניס ריצ'י (מי שיצר את השפה).http://www.howstuffworks.com/c.htmאה ותמחק את התגיות כי זה לא חלק מהשפה (<M> ו-<M/>).
פורסם 2014 באפריל 2011 שנים סדרתי קצת את הקוד:#include <stdio.h>#define GRAMS_PER_POUNDS 454const 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* נערך 2014 באפריל 2011 שנים על-ידי MrAlex
פורסם 2014 באפריל 2011 שנים מחבר בסדר תיקנתי את הקוד.לפי מה שאני יודע התוכנית ממירה את המשקל שלי בקילוגרם לגרמים.אני הכנסתי 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;} נערך 2014 באפריל 2011 שנים על-ידי eliom
פורסם 2014 באפריל 2011 שנים שוב, define# מגדיר לקומפיילר, לפני שהוא עושה קומפילציה לקוד, לעבור על כל הטקסט ולעשות replace לכל מופע של GRAMS_PER_POUND במקרה שלך למספר 454. תחשוב שאתה יכול לעשות את זה ידנית - עם שימוש ב-define הרבה יותר קריא לדעת שזה GRAMS_PER_POUND ולא איזה מספר "קסם" כמו 454 שמישהו אחר לא ידע מה המספר הזה בכלל מייצג.
ארכיון
דיון זה הועבר לארכיון ולא ניתן להוסיף בו תגובות חדשות.