Category:C: Difference between revisions

no edit summary
m (already in right panel)
No edit summary
Line 26:
* '''C18''' is the current standard, published in June 2018. It is the default for [[GCC]] as of version 8.1.
* '''C2x''' is the upcoming standard, planned for a 2021 publication. [[GCC]] 9 and [[Clang]] 9 have preliminary support for it.
==Overview==
 
===Curly Braces===
C uses curly braces as a separator for sections of code. All curly braces must be "balanced," i.e. every left curly brace must have a right curly brace after it. Most code writing programs will do this for you automatically.
<lang C>int main()
{
 
// your main program goes here
 
}</lang>
 
===Scope===
Unlike assembly which lets you jump anywhere or read any memory address, C imposes restrictions on labeled values. A variable defined inside a function can only be "seen" by that function, and not the ones outside it. Furthermore, you can re-use variable names inside a function and it refers to a different entity than the variable of the same name defined outside.
 
===Functions===
A function is made up of three parts: its return value, its name, and its arguments.
<lang C>int main(void){ //This is the function "main," which takes no arguments and returns a 32-bit signed integer value.
int sum(int a,int b){ //This is the function "sum," which takes two integer arguments and returns an integer.</lang>
Note that the variable names listed as arguments when declaring a function are just for convenience. They need not be declared nor defined, nor do they refer to any variables in your program that happen to have the same name. It's only when a function is actually <i>used</i> are the argument variables required to exist.
<lang C>int foo(int x){
return x;
} // the "x" here is just a placeholder for whatever actually goes in when you invoke foo.
 
int main()
{
 
int y;
int z = 2;
 
y = foo(z); //note that x was never involved. That's because the "x" earlier was just a placeholder name.
 
}</lang>
 
==Assignment==
C allows you to define a variable as equal to a value, in more ways than just simple numerals.
<lang C>int a = 3; //declare the variable a of type int, define it equal to decimal 3.
 
int b = -1; //declare the variable b of type int, define it equal to -1 (0xFFFFFFFF in hex)
 
char letter = "A";
//declare the variable "letter" of type char, it equals capital A.
//C allows you to treat an ascii value as a numeral whenever you feel like it. Other languages do not.
 
char myString = "Hello"; //define the array "myString" containing the letters "Hello" followed by a null terminator.
 
int myArray[5] = {10,20,30,40,50};
//declare the array variable "myArray" containing integer values, with a maximum size of 5 elements.
//Then assign 10 to the beginning, 20 after it, 30 after that, and so on.
 
int c = sum(a,b);
//declare the integer variable "c".
//Define it to equal the output of the function sum using the previously defined variables "a" and "b" as arguments.
//This is only valid if the return type of the function "sum" matches the type of the variable "c."</lang>
==Citation==
*[[wp:C_%28programming_language%29|Wikipedia:C (programming language)]]
1,489

edits