Thread: Simple lines of code

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    1

    Simple lines of code

    Hello.

    If anyone could explain to me in depth these short lines of code, so that I can understand other ones similar to this one when they show up. I know the rest, I just need the bit in the printf brackets explained. Thanks.

    Code:
    #define NUL '\0'
    void main(){
    char c=NULL
    printf("%d",(!c) ? x : c);}

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I'll let my compiler do the explaining:
    Code:
    test.c:2:6: warning: return type of 'main' is not 'int' [-Wmain]
    test.c: In function 'main':
    test.c:3:8: error: 'NULL' undeclared (first use in this function)
    test.c:3:8: note: each undeclared identifier is reported only once for each function it appears in
    test.c:4:1: error: expected ',' or ';' before 'printf'
    test.c:3:6: warning: unused variable 'c' [-Wunused-variable]
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    You want (!c) ? x : c explained.
    That's conditional expression. The part in the parenthesis is evaluated as to TRUE or FALSE. If it's TRUE then the first statement after '?' is executed. If FALSE then the statement after ':'.
    If c was zero then !c is TRUE so therefore 'x' is executed (stacked, printed).
    If c was non-zero then !c if FALSE so therefore 'c' is executed (stacked, printed).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Meaning of Code Lines
    By shwetha_siddu in forum C Programming
    Replies: 4
    Last Post: 10-14-2008, 04:54 PM
  2. Simple answer for intersecting lines?
    By LLINE in forum C++ Programming
    Replies: 6
    Last Post: 06-01-2008, 02:10 AM
  3. Checking lines of code
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 04-10-2008, 02:05 AM
  4. Lines of code per day.
    By abachler in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 10-26-2007, 09:20 AM
  5. What do these tow lines of code mean?
    By Gamma in forum C++ Programming
    Replies: 1
    Last Post: 04-25-2002, 04:30 PM