Oisko jollain antaa joku tutoriaali miten saan C:n toimimaan Visual Studio 2010:ssä?
Tässä on suoraan kirjasta otettu (copypastella) esimerkki:
/* Program 4.2 Drawing a box */
#include <stdio.h>
int main(void)
{
printf("\n**************"); /* Draw the top of the box */
for(int count = 1 ; count <= 8 ; ++count)
printf("\n* *"); /* Draw the sides of the box */
printf("\n**************\n"); /* Draw the bottom of the box */
return 0;
}Visual Studion mukaan siinä on 8 virhettä ja yhden varoituksenkin antaa:
Error 1 error C2143: syntax error : missing ';' before 'type'
Error 2 error C2143: syntax error : missing ';' before 'type'
Error 3 error C2143: syntax error : missing ')' before 'type'
Error 4 error C2143: syntax error : missing ';' before 'type'
Error 5 error C2065: 'count' : undeclared identifier
Warning 6 warning C4552: '<=' : operator has no effect; expected operator with side-effect
Error 7 error C2059: syntax error : ')'
Error 8 error C2065: 'count' : undeclared identifier
Error 9 error C2146: syntax error : missing ';' before identifier 'printf'
Ja yhtään noista en osaa korjata.
Miten tästä eteenpäin?
Kai olet valinnut, että "Compile as C"? Oletuksenahan siellä on "Compile as C++"
Aha. Nyt toimii. Oikea vastaus on tässä:
/* Program 4.2 Drawing a box */
#include <stdio.h>
int main(void)
{
int count;
printf("\n**************"); /* Draw the top of the box */
for(count = 1; count <= 8; ++count)
{
printf("\n* *"); /* Draw the sides of the box */
}
printf("\n**************\n"); /* Draw the bottom of the box */
getchar();
return 0;
}Alkuperäinen virhe oli siis tuo count-muuttujan määrittely for-silmukassa; oikeaoppisessa ANSI C:ssä kaikki muuttujat täytyy määritellä heti funktion alussa ennen mitään muuta sisältöä.
Joo, niinhän se menee. Visual Studio 2010 on toteuttanut C99:n vain osittain.
Aihe on jo aika vanha, joten et voi enää vastata siihen.