咳嗽消炎药吃什么好| 一垒二垒三垒全垒打是什么意思| 后续是什么意思| 特斯拉用的是什么电池| 慢性疾病都包括什么病| 多子多福是什么意思| 咽炎咳嗽吃什么药| 香片属于什么茶| 失眠吃什么食物效果最好| 睾丸炎吃什么药好得快| 儿童办护照需要什么证件| 儿童超敏c反应蛋白高说明什么| 倩字五行属什么| 高尿酸有什么症状| cro是什么意思| 狗吃什么药会立马就死| 口臭用什么药| 一切尽在不言中是什么意思| 夏天适合种什么菜| 捐精有什么要求| 心梗吃什么药好得快| 5点到7点是什么时辰| 夏天煲什么汤好| 抑菌是什么意思| 子宫前位后位有什么区别| 母鸡学公鸡叫什么征兆| 正太是什么| 不自觉摇头是什么病| 11月份是什么星座的| 牟作为姓氏时读什么| 35属什么| 骨折什么意思| 天空像什么的比喻句| 上天是什么意思| rh阳性是什么意思| 妈妈吃什么帮宝宝排气| 抚触是什么意思| 特朗普为什么叫川普| 十天干代表什么| 前凸后翘什么意思| 素毛肚是什么做的| 心跳不规律是什么原因| 三维b片主治什么病| 嗅觉失灵是什么原因| 梦见别人打我是什么意思| 火锅油碟是什么油| 妈祖属什么生肖| 胃窦溃疡a1期是什么意思| 吸尘器什么牌子好| 移车打什么电话| 消瘦挂什么科| 水可以做什么| 红沙日是什么意思| 蒲公英的种子像什么| 汆是什么意思| 八一是什么节| 益气固表是什么意思| 排卵期同房要注意什么| flair是什么意思| 什么药治便秘最好最快| 烧伤的疤痕怎么去除用什么法最好| 这是什么石头| 梦到亲人死了是什么征兆| 1998年五行属什么| 阳起石是什么东西| 出汗多吃什么| 依巴斯汀片是什么药| ut是什么| cj什么意思| 猫咪打呼噜代表什么| ercp是什么| 燊读什么| 体征是什么意思| 什么海没有鱼| 心电图逆钟向转位是什么意思| 出汗太多会对身体造成什么伤害| 泪囊炎用什么眼药水| 苍蝇馆子什么意思| aimer是什么意思| 国老是什么中药| 湿气是什么原因引起的| 胎儿右侧脉络丛囊肿是什么意思| 产酸克雷伯菌属于什么菌| 灵芝孢子粉治什么病| 糖尿病不能吃什么水果| 手小的男人代表什么| 气道高反应是什么意思| 甲状腺偏高是什么原因引起的| 例假什么意思| dickies是什么牌子| 这是什么鱼| 平片是什么| 七月七是什么日子| 血糖高是什么原因引起| 年兽叫什么| 红油是什么油| 脑梗三项是检查什么| 手机cpu是什么| 可否是什么意思| 聚少离多是什么意思| 眩晕是什么原因| 做梦梦见屎是什么意思| 舌头上火了吃什么降火| 卷腹是什么| 青瓜是什么瓜| 多发纳氏囊肿是什么意思| 寒咳吃什么药| 缺钙吃什么食物| 孕妇放屁多是什么原因| 为什么发烧| led灯是什么灯| 嘴苦是什么原因引起的| 升白细胞的针剂叫什么| 螺旋杆菌阳性是什么病| 什么是周围神经病| 一个黑一个俊的右边念什么| 职位是什么意思| 627是什么意思| 睡醒后口干口苦是什么原因| 食粉是什么粉| 5.8是什么星座| 胎儿右肾盂分离是什么意思| 艾灸是什么| 洗银水是什么成分| 吃什么能让奶水变多| 胃疼胃胀用什么药效果最好| 橘子什么时候成熟| 鱼露是什么东西| 舒张压是什么意思| 表面积是什么意思| 人为什么打哈欠| 白羊座是什么星象| 10月17日什么星座| 十二月十八号是什么星座| 无学历学什么技术好| prince是什么牌子| 河东狮吼什么意思| 腰疼是什么原因引起的| 血糖高挂什么科| 为什么家里不能放假花| 新生儿老打嗝什么原因| 迫切是什么意思| 荷花指什么生肖| 为什么做爱那么舒服| 空囊是什么原因造成的| 命途多舛是什么意思| 一岁宝宝能吃什么水果| 壁立千仞无欲则刚是什么意思| 憋尿有什么危害| 铁树开花是什么意思| 健谈是什么意思| 气滞血瘀吃什么食物好| 受体是什么| 喉咙痛吃什么好| 做梦车丢了有什么预兆| 股票五行属什么| 葡萄补什么| 什么是飞秒手术| vad是什么意思| 丝瓜络是什么东西| 你是什么动物| 怀孕吃什么水果| c2是什么车型| 动物奶油是什么做的| 气短气喘吃什么药| 早晨六点是什么时辰| 上市公司是什么意思| 12.6是什么星座| 梦见办酒席是什么意思| 颈动脉强回声斑块是什么意思| parker是什么牌子| 行动派是什么意思| 100mg是什么意思| 什么是干咳| 双子座女和什么星座最配| 被是什么偏旁怎么读| 精神小伙什么意思| 胎儿左心室点状强回声是什么意思| 无性婚姻会有什么好处| 小分子肽有什么作用| 男子精少吃什么药可以生精| aimer是什么意思| 广西属于什么方向| 滔滔不绝的绝是什么意思| 扁桃体疼吃什么药| 不自主的摇头是什么病| 孟姜女姓什么| 高胆固醇血症是什么病| 二胎什么时候放开的| 亲子鉴定去医院挂什么科| 酪蛋白是什么| 11月12日什么星座| 芦荟有什么功效| 宝宝什么时候可以吃盐| 巴西货币叫什么| 坐月子什么不可以吃| 中老年人补钙吃什么牌子的钙片好| 大便颗粒状是什么原因造成的| 男属龙和什么属相最配| 失信名单有什么影响| 免疫力低下吃什么| saa是什么意思| 左肩膀疼是什么原因| 什么是代理| 幻听是什么原因| ssg是什么意思| 脆皖鱼是什么鱼| 异想天开是什么意思| 肉桂属于什么茶| 送女生礼物送什么好| 枸杞与菊花一起泡水喝有什么功效| 肺部钙化灶是什么意思| 澳门为什么叫澳门| 桃胶有什么作用| 什么是杀猪菜| 滋生是什么意思| 福禄寿的禄是什么意思| 桂圆和红枣泡水喝有什么好处| 涤纶是什么材料| 肠炎能吃什么水果| 炒房是什么意思| 心悸心慌焦虑吃什么药能缓解| 小人难防前一句是什么| 肠胃痉挛吃什么药| 军长相当于地方什么官| 存在感是什么意思| 水痘能吃什么食物| 美的本质是什么| cnn是什么意思| 什么是再生纤维素纤维| 什么补钙| 什么路最窄打一生肖| 熹字五行属什么| 胆囊炎吃什么消炎药| 意有所指是什么意思| pass掉是什么意思| 过的第五笔是什么| 类风湿是什么意思| 40gp是什么意思| 屌丝是什么| 贪恋是什么意思| 文科生选什么专业| 突然好想你你会在哪里是什么歌| 保外就医是什么意思| 小学教师需要什么学历| 癣是什么| ip是什么意思| 水压低用什么花洒| 淀粉酶高有什么危害| 三伏的伏是什么意思| 522是什么意思| 什么水果补血效果最好| 疼和痛有什么区别| 不等闲是什么意思| 慢性咽炎有什么症状| 权志龙为什么这么火| 雄脱是什么意思| 女生爱出汗是什么原因| 直肠脱垂有什么症状| 乌龟肺炎用什么药| 感染性腹泻吃什么药| 朝鲜和韩国是什么关系| disease是什么意思| 一碗香是什么菜| 百度


Tutorial Map

Introduction Pointers, arrays, strings File IO Linked lists, trees, recursion

南岸区南坪星宇花园大门前的道路上,有违章...


By Alex Allain
This tutorial is designed to be a stand-alone introduction to C, even if you've never programmed before. However, because C++ is a more modern language, if you're not sure if you should learn C or C++, I recommend the C++ tutorial instead, which is also designed for people who have never programmed before. Nevertheless, if you do not desire some of C++'s advanced features or simply wish to learn C instead of C++, then this tutorial is for you!

Getting set up - finding a C compiler

The very first thing you need to do, before starting out in C, is to make sure that you have a compiler. What is a compiler, you ask? A compiler turns the program that you write into an executable that your computer can actually understand and run. If you're taking a course, you probably have one provided through your school.

If you haven't yet done so, go ahead and get a compiler set up--you'll need it for the rest of the tutorial. If you're on Windows, your best bet is to use Code::Blocks with MinGW. If you're on Linux, you can use gcc, and if you're on Mac OS X, you can use XCode.

Intro to C

Every full C program begins inside a function called "main". A function is simply a collection of commands that do "something". The main function is always called when the program first executes. From main, we can call other functions, whether they be written by us or by others or use built-in language features. To access the standard functions that comes with your compiler, you need to include a header with the #include directive. What this does is effectively take everything in the header and paste it into your program. Let's look at a working program: 百度 此外,西九龙站有完善的交通配套连接现有的道路和铁路网络,加上西九文化区的落成,日后整个围绕西九龙站的地区将成为香港的新地标。
#include <stdio.h>
int main()
{
    printf( "I am alive!  Beware.\n" );
    getchar();
    return 0;
}
Let's look at the elements of the program. The #include is a "preprocessor" directive that tells the compiler to put code from the header called stdio.h into our program before actually creating the executable. By including header files, you can gain access to many different functions--both the printf and getchar functions are included in stdio.h.

The next important line is int main(). This line tells the compiler that there is a function named main, and that the function returns an integer, hence int. The "curly braces" ({ and }) signal the beginning and end of functions and other code blocks. If you have programmed in Pascal, you will know them as BEGIN and END. Even if you haven't programmed in Pascal, this is a good way to think about their meaning.

The printf function is the standard C way of displaying output on the screen. The quotes tell the compiler that you want to output the literal string as-is (almost). The '\n' sequence is actually treated as a single character that stands for a newline (we'll talk about this later in more detail); for the time being, just remember that there are a few sequences that, when they appear in a string literal, are actually not displayed literally by printf and that '\n' is one of them. The actual effect of '\n' is to move the cursor on your screen to the next line. Notice the semicolon: it tells the compiler that you're at the end of a command, such as a function call. You will see that the semicolon is used to end many lines in C.

The next command is getchar(). This is another function call: it reads in a single character and waits for the user to hit enter before reading the character. This line is included because many compiler environments will open a new console window, run the program, and then close the window before you can see the output. This command keeps that window from closing because the program is not done yet because it waits for you to hit enter. Including that line gives you time to see the program run.

Finally, at the end of the program, we return a value from main to the operating system by using the return statement. This return value is important as it can be used to tell the operating system whether our program succeeded or not. A return value of 0 means success.

The final brace closes off the function. You should try compiling this program and running it. You can cut and paste the code into a file, save it as a .c file, and then compile it. If you are using a command-line compiler, such as Borland C++ 5.5, you should read the compiler instructions for information on how to compile. Otherwise compiling and running should be as simple as clicking a button with your mouse (perhaps the "build" or "run" button).

You might start playing around with the printf function and get used to writing simple C programs.

Explaining your Code

Comments are critical for all but the most trivial programs and this tutorial will often use them to explain sections of code. When you tell the compiler a section of text is a comment, it will ignore it when running the code, allowing you to use any text you want to describe the real code. To create a comment in C, you surround the text with /* and then */ to block off everything between as a comment. Certain compiler environments or text editors will change the color of a commented area to make it easier to spot, but some will not. Be certain not to accidentally comment out code (that is, to tell the compiler part of your code is a comment) you need for the program.

When you are learning to program, it is also useful to comment out sections of code in order to see how the output is affected.

Using Variables

So far you should be able to write a simple program to display information typed in by you, the programmer and to describe your program with comments. That's great, but what about interacting with your user? Fortunately, it is also possible for your program to accept input.

But first, before you try to receive input, you must have a place to store that input. In programming, input and data are stored in variables. There are several different types of variables; when you tell the compiler you are declaring a variable, you must include the data type along with the name of the variable. Several basic types include char, int, and float. Each type can store different types of data.

A variable of type char stores a single character, variables of type int store integers (numbers without decimal places), and variables of type float store numbers with decimal places. Each of these variable types - char, int, and float - is each a keyword that you use when you declare a variable. Some variables also use more of the computer's memory to store their values.

It may seem strange to have multiple variable types when it seems like some variable types are redundant. But using the right variable size can be important for making your program efficient because some variables require more memory than others. For now, suffice it to say that the different variable types will almost all be used!

Before you can use a variable, you must tell the compiler about it by declaring it and telling the compiler about what its "type" is. To declare a variable you use the syntax <variable type> <name of variable>;. (The brackets here indicate that your replace the expression with text described within the brackets.) For instance, a basic variable declaration might look like this:
int myVariable;
Note once again the use of a semicolon at the end of the line. Even though we're not calling a function, a semicolon is still required at the end of the "expression". This code would create a variable called myVariable; now we are free to use myVariable later in the program.

It is permissible to declare multiple variables of the same type on the same line; each one should be separated by a comma. If you attempt to use an undefined variable, your program will not run, and you will receive an error message informing you that you have made a mistake.

Here are some variable declaration examples:
int x;
int a, b, c, d;
char letter;
float the_float;
While you can have multiple variables of the same type, you cannot have multiple variables with the same name. Moreover, you cannot have variables and functions with the same name.

Reading input

Using variables in C for input or output can be a bit of a hassle at first, but bear with it and it will make sense. We'll be using the scanf function to read in a value and then printf to read it back out. Let's look at the program and then pick apart exactly what's going on. You can even compile this and run it if it helps you follow along.
#include <stdio.h>

int main()
{
    int this_is_a_number;

    printf( "Please enter a number: " );
    scanf( "%d", &this_is_a_number );
    printf( "You entered %d", this_is_a_number );
    getchar();
    return 0;
}
So what does all of this mean? We've seen the #include and main function before; main must appear in every program you intend to run, and the #include gives us access to printf (as well as scanf). (As you might have guessed, the io in stdio.h stands for "input/output"; std just stands for "standard.") The keyword int declares this_is_a_number to be an integer.

This is where things start to get interesting: the scanf function works by taking a string and some variables modified with &. The string tells scanf what variables to look for: notice that we have a string containing only "%d" -- this tells the scanf function to read in an integer. The second argument of scanf is the variable, sort of. We'll learn more about what is going on later, but the gist of it is that scanf needs to know where the variable is stored in order to change its value. Using & in front of a variable allows you to get its location and give that to scanf instead of the value of the variable. Think of it like giving someone directions to the soda aisle and letting them go get a coca-cola instead of fetching the coke for that person. The & gives the scanf function directions to the variable.

When the program runs, each call to scanf checks its own input string to see what kinds of input to expect, and then stores the value input into the variable.

The second printf statement also contains the same '%d'--both scanf and printf use the same format for indicating values embedded in strings. In this case, printf takes the first argument after the string, the variable this_is_a_number, and treats it as though it were of the type specified by the "format specifier". In this case, printf treats this_is_a_number as an integer based on the format specifier.

So what does it mean to treat a number as an integer? If the user attempts to type in a decimal number, it will be truncated (that is, the decimal component of the number will be ignored) when stored in the variable. Try typing in a sequence of characters or a decimal number when you run the example program; the response will vary from input to input, but in no case is it particularly pretty.

Of course, no matter what type you use, variables are uninteresting without the ability to modify them. Several operators used with variables include the following: *, -, +, /, =, ==, >, <. The * multiplies, the / divides, the - subtracts, and the + adds. It is of course important to realize that to modify the value of a variable inside the program it is rather important to use the equal sign. In some languages, the equal sign compares the value of the left and right values, but in C == is used for that task. The equal sign is still extremely useful. It sets the value of the variable on the left side of the equals sign equal to the value on the right side of the equals sign. The operators that perform mathematical functions should be used on the right side of an equal sign in order to assign the result to a variable on the left side.

Here are a few examples:
a = 4 * 6; /* (Note use of comments and of semicolon) a is 24 */
a = a + 5; /* a equals the original value of a with five added to it */
a == 5     /* Does NOT assign five to a. Rather, it checks to see if a equals 5.*/ 
The other form of equal, ==, is not a way to assign a value to a variable. Rather, it checks to see if the variables are equal. It is extremely useful in many areas of C; for example, you will often use == in such constructions as conditional statements and loops. You can probably guess how < and > function. They are greater than and less than operators.

For example:
a < 5  /* Checks to see if a is less than five */
a > 5  /* Checks to see if a is greater than five */ 
a == 5 /* Checks to see if a equals five, for good measure */ 



Quiz yourself
Next: If Statements
Back to C Tutorial Index



眼睛吹风就流泪是什么原因 空调吹感冒吃什么药 海澜之家是什么档次 喉咙有烧灼感吃什么药 老年人腿肿是什么原因引起的
屁股上的骨头叫什么骨 喝什么降尿酸 臭鳜鱼是什么鱼 灰指甲有什么特效药可以治好 什么是网红
鳀鱼是什么鱼 常吃火龙果有什么好处 男生的隐私长什么样 晟这个字念什么 心包隐窝是什么意思
大便颗粒状是什么原因 o型血的父母是什么血型 长方形的纸可以折什么 什么是肝阳上亢 乳酸脱氢酶偏低是什么意思
晚上睡觉流口水是什么病wzqsfys.com 刚怀孕有什么特征hcv8jop3ns3r.cn 乙肝表面抗体阴性什么意思hcv8jop8ns3r.cn 把头是什么意思hcv9jop1ns4r.cn hpv什么时候检查最好hcv9jop1ns8r.cn
抖是什么生肖sanhestory.com 羊宝是什么hcv9jop1ns4r.cn 犯罪心理学属于什么专业hcv9jop6ns9r.cn 手汗脚汗多是什么原因hcv7jop6ns7r.cn 有什么办法可以怀孕hcv8jop5ns7r.cn
puma是什么意思hcv8jop2ns1r.cn 慰劳是什么意思kuyehao.com 田字出头是什么字hcv9jop2ns3r.cn 五指毛桃有什么用hcv9jop6ns5r.cn 吃生洋葱有什么好处hcv8jop0ns0r.cn
孕早期吃什么水果hcv7jop6ns4r.cn 春风十里不如你什么意思hcv8jop3ns7r.cn 做梦掉粪坑什么征兆hcv8jop2ns1r.cn 玛瑙五行属什么hcv9jop4ns2r.cn 吃什么补蛋白质最快hcv9jop4ns3r.cn
百度