Category:C: Difference between revisions
Puppydrum64 (talk | contribs) |
Puppydrum64 (talk | contribs) No edit summary |
||
Line 82: | Line 82: | ||
//Define it to equal the output of the function sum using the previously defined variables "a" and "b" as arguments. |
//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> |
//This is only valid if the return type of the function "sum" matches the type of the variable "c."</lang> |
||
===Declaring vs. Defining=== |
|||
This is a very unintuitive aspect of C that often confuses new users. Declaring a variable or function tells the compiler that a function may exist. Defining a variable or function assigns it a value or procedure, respectively. Compare the two examples below: |
|||
<lang C>int a; // The variable "a" has been declared, but not defined. |
|||
a = 2; // Now the variable has been defined.</lang> |
|||
<lang C>int a = 2; //The variable "a" has been both declared and defined.</lang> |
|||
* You cannot define a variable without declaring it first. |
|||
* Before a variable can be used, it must be defined. |
|||
Functions work the same way. You can declare a function without defining what it does. |
|||
<lang C> int foo(int bar); |
|||
// The function foo was declared. It takes an integer as an argument and returns an integer. |
|||
// What it actually does is currently unknown but can be defined later.</lang> |
|||
==Citation== |
==Citation== |
||
*[[wp:C_%28programming_language%29|Wikipedia:C (programming language)]] |
*[[wp:C_%28programming_language%29|Wikipedia:C (programming language)]] |
Revision as of 15:15, 10 September 2021
This programming language may be used to instruct a computer to perform a task.
Execution method: | Compiled (machine code) |
---|---|
Garbage collected: | No |
Parameter passing methods: | By value |
Type safety: | Unsafe |
Type strength: | Weak |
Type compatibility: | Nominative |
Type expression: | Explicit |
Type checking: | Static |
Lang tag(s): | c |
See Also: |
C is a general-purpose, procedural, imperative computer programming language developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories for use with the UNIX operating system. C evolved from its predecessor, B.
C has since spread to many other platforms, and is now one of the most widely used programming languages. C has also greatly influenced many other popular languages, such as C++ and Objective-C, which were originally designed as enhancements to C. People are so familiar with its syntax that many other languages such as AWK, PHP, Java, JavaScript, D, and C# deliberately used its "look and feel". C is the most commonly used programming language for writing system software, though it is also widely used for writing applications. C is the lingua franca of the open source community.
Versions
- K&R C was the first widely-used form of C. It was originally documented in The C Programming Language, published in 1978. It is named for the authors, Brian Kernighan and Dennis Ritchie (also the language's creator). Code in this style is virtually nonexistent today.
- C89 (often called ANSI C) is the version of C standardized by ANSI in 1989. It is the most commonly used and supported version of the language.
- C90 (often called ISO C) is identical to C89, republished by ISO in 1990.
- C99 is a significant improvement, adopting many features of C++ and standardizing common compiler extensions. It was standardized by ISO in 1999, and by ANSI in 2000. It is primarily supported by commercial C compilers, but most of its features are available in Clang GCC. [1]
- C11 is the previous standard, published in December 2011. It is the default for GCC as of version 5.1.
- 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>
Semicolons
Any "executable" statement must end in a semicolon, such as an assignment or function call. If you get an error message from your compiler, it won't explicitly tell you "Expected semicolon at end of line X." Go to the line number it says the error is at, and look a few lines above that. You might have forgotten a semicolon there.
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 used 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>
Declaring vs. Defining
This is a very unintuitive aspect of C that often confuses new users. Declaring a variable or function tells the compiler that a function may exist. Defining a variable or function assigns it a value or procedure, respectively. Compare the two examples below: <lang C>int a; // The variable "a" has been declared, but not defined. a = 2; // Now the variable has been defined.</lang>
<lang C>int a = 2; //The variable "a" has been both declared and defined.</lang>
- You cannot define a variable without declaring it first.
- Before a variable can be used, it must be defined.
Functions work the same way. You can declare a function without defining what it does. <lang C> int foo(int bar); // The function foo was declared. It takes an integer as an argument and returns an integer. // What it actually does is currently unknown but can be defined later.</lang>
Citation
Subcategories
This category has the following 3 subcategories, out of 3 total.
@
- C examples needing attention (5 P)
- C Implementations (5 P)
- C User (468 P)
Pages in category "C"
The following 200 pages are in this category, out of 1,297 total.
(previous page) (next page)F
- Find the last Sunday of each month
- Find the missing permutation
- Find words which contains all the vowels
- Find words which contains more than 3 e vowels
- Find words whose first and last three letters are equal
- Find words with alternating vowels and consonants
- Finite state machine
- First 9 prime Fibonacci number
- First class environments
- First perfect square in base n with n unique digits
- First power of 2 that has leading decimal digits of 12
- First-class functions
- Five weekends
- Fivenum
- FizzBuzz
- Flatten a list
- Flipping bits game
- Flow-control structures
- Floyd's triangle
- Floyd-Warshall algorithm
- Forbidden numbers
- Forest fire
- Fork
- Formal power series
- Formatted numeric output
- Fortunate numbers
- Forward difference
- Four bit adder
- Four is magic
- Four is the number of letters in the ...
- Four sides of square
- Fractal tree
- Fraction reduction
- Fractran
- Frobenius numbers
- FTP
- Function composition
- Function definition
- Function frequency
- Function prototype
- Fusc sequence
G
- Galton box animation
- Gamma function
- Gapful numbers
- Gauss-Jordan matrix inversion
- Gaussian elimination
- General FizzBuzz
- Generate Chess960 starting position
- Generate lower case ASCII alphabet
- Generate random chess position
- Generator/Exponential
- Generic swap
- Get system command output
- Getting the number of decimal places
- Globally replace text in several files
- Go Fish
- Golden ratio/Convergence
- Gotchas
- Gray code
- Grayscale image
- Greatest common divisor
- Greatest element of a list
- Greatest subsequential sum
- Greedy algorithm for Egyptian fractions
- Greyscale bars/Display
- Guess the number
- Guess the number/With feedback
- Guess the number/With feedback (player)
- GUI component interaction
- GUI enabling/disabling of controls
- GUI/Maximum window dimensions
H
- Hailstone sequence
- Halt and catch fire
- Hamming numbers
- Handle a signal
- Happy numbers
- Harshad or Niven series
- Hash from two arrays
- Haversine formula
- Hello world/Graphical
- Hello world/Line printer
- Hello world/Newbie
- Hello world/Newline omission
- Hello world/Standard error
- Hello world/Text
- Hello world/Web server
- Heronian triangles
- Hex dump
- Hickerson series of almost integers
- Higher-order functions
- Hilbert curve
- Hofstadter Figure-Figure sequences
- Hofstadter Q sequence
- Hofstadter-Conway $10,000 sequence
- Holidays related to Easter
- Honaker primes
- Honeycombs
- Horizontal sundial calculations
- Horner's rule for polynomial evaluation
- Host introspection
- Hostname
- Hough transform
- HTTP
- HTTPS
- HTTPS/Authenticated
- Huffman coding
- Humble numbers
- Hunt the Wumpus
I
- I before E except after C
- IBAN
- Iccanobif primes
- Identity matrix
- Idiomatically determine all the lowercase and uppercase letters
- Idoneal numbers
- Image convolution
- Image noise
- Imaginary base numbers
- Implicit type conversion
- Include a file
- Increasing gaps between consecutive Niven numbers
- Increment a numerical string
- Infinity
- Inheritance/Multiple
- Inheritance/Single
- Input loop
- Input/Output for lines of text
- Input/Output for pairs of numbers
- Integer comparison
- Integer overflow
- Integer roots
- Integer sequence
- Intersecting number wheels
- Introspection
- Inverted index
- Inverted syntax
- IPC via named pipe
- ISBN13 check digit
- Isqrt (integer square root) of X
- Iterated digits squaring
J
K
- K-d tree
- K-means++ clustering
- Kahan summation
- Kaprekar numbers
- Kernighans large earthquake problem
- Keyboard input/Flush the keyboard buffer
- Keyboard input/Keypress check
- Keyboard input/Obtain a Y or N response
- Keyboard macros
- Klarner-Rado sequence
- Knapsack problem/0-1
- Knapsack problem/Bounded
- Knapsack problem/Continuous
- Knapsack problem/Unbounded
- Knight's tour
- Knuth shuffle
- Knuth's algorithm S
- Koch curve
- Kolakoski sequence
- Kronecker product
- Kronecker product based fractals
L
- Lah numbers
- Langton's ant
- Largest difference between adjacent primes
- Largest five adjacent number
- Largest int from concatenated ints
- Largest number divisible by its digits
- Largest prime factor
- Largest proper divisor of n
- Last Friday of each month
- Last letter-first letter
- Last list item
- Law of cosines - triples
- Leap year
- Least common multiple
- Least m such that n! + m is prime
- Left factorials
- Legendre prime counting function
- Length of an arc between two angles
- Leonardo numbers
- Letter frequency
- Levenshtein distance
- Levenshtein distance/Alignment
- Line circle intersection
- Linear congruential generator
- Linux CPU utilization
- List comprehensions
- List rooted trees